Blame view

trunk/src/core/srs_core_http.hpp 5.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
The MIT License (MIT)

Copyright (c) 2013 winlin

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef SRS_CORE_HTTP_HPP
#define SRS_CORE_HTTP_HPP

/*
#include <srs_core_http.hpp>
*/
#include <srs_core.hpp>
32 33
#ifdef SRS_HTTP
winlin authored
34
class SrsRequest;
35
class SrsSocket;
winlin authored
36 37 38 39 40

#include <string>

#include <http_parser.h>
41 42 43
#define SRS_HTTP_HEADER_BUFFER		1024
#define SRS_HTTP_BODY_BUFFER		32 * 1024
winlin authored
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/**
* used to resolve the http uri.
*/
class SrsHttpUri
{
private:
    std::string url;
    std::string schema;
    std::string host;
    int port;
    std::string path;
public:
	SrsHttpUri();
	virtual ~SrsHttpUri();
public:
	/**
	* initialize the http uri.
	*/
	virtual int initialize(std::string _url);
public:
    virtual const char* get_url();
    virtual const char* get_schema();
    virtual const char* get_host();
    virtual int get_port();
68
    virtual const char* get_path();
winlin authored
69 70 71 72 73 74 75 76 77 78 79 80 81
private:
    /**
    * get the parsed url field.
    * @return return empty string if not set.
    */
    virtual std::string get_uri_field(std::string uri, http_parser_url* hp_u, http_parser_url_fields field);
};

/**
* http client to GET/POST/PUT/DELETE uri
*/
class SrsHttpClient
{
82 83 84 85 86
private:
	bool connected;
	st_netfd_t stfd;
private:
    http_parser http_header;
winlin authored
87 88 89 90 91 92 93 94 95 96
public:
	SrsHttpClient();
	virtual ~SrsHttpClient();
public:
	/**
	* to post data to the uri.
	* @param req the data post to uri.
	* @param res the response data from server.
	*/
	virtual int post(SrsHttpUri* uri, std::string req, std::string& res);
97 98 99 100 101 102 103 104 105 106 107
private:
	virtual void disconnect();
	virtual int connect(SrsHttpUri* uri);
private:
    virtual int parse_response(SrsHttpUri* uri, SrsSocket* skt, std::string* response);
    virtual int parse_response_header(SrsSocket* skt, std::string* response, int& body_received);
    virtual int parse_response_body(SrsHttpUri* uri, SrsSocket* skt, std::string* response, int body_received);
    virtual int parse_response_body_data(SrsHttpUri* uri, SrsSocket* skt, std::string* response, size_t body_left, const void* buf, size_t size);
private:
    static int on_headers_complete(http_parser* parser);
    virtual void comple_header(http_parser* parser);
winlin authored
108 109 110 111 112 113 114 115 116 117 118 119 120 121
};

/**
* the http hooks, http callback api,
* for some event, such as on_connect, call
* a http api(hooks).
*/
class SrsHttpHooks
{
public:
	SrsHttpHooks();
	virtual ~SrsHttpHooks();
public:
	/**
122 123
	* on_connect hook, when client connect to srs.
	* @param client_id the id of client on server.
winlin authored
124 125 126 127
	* @param url the api server url, to valid the client. 
	* 		ignore if empty.
	* @return valid failed or connect to the url failed.
	*/
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	virtual int on_connect(std::string url, int client_id, std::string ip, SrsRequest* req);
	/**
	* on_close hook, when client disconnect to srs, where client is valid by on_connect.
	* @param client_id the id of client on server.
	* @param url the api server url, to process the event. 
	* 		ignore if empty.
	*/
	virtual void on_close(std::string url, int client_id, std::string ip, SrsRequest* req);
	/**
	* on_publish hook, when client(encoder) start to publish stream
	* @param client_id the id of client on server.
	* @param url the api server url, to valid the client. 
	* 		ignore if empty.
	* @return valid failed or connect to the url failed.
	*/
	virtual int on_publish(std::string url, int client_id, std::string ip, SrsRequest* req);
	/**
	* on_unpublish hook, when client(encoder) stop publish stream.
	* @param client_id the id of client on server.
	* @param url the api server url, to process the event. 
	* 		ignore if empty.
	*/
	virtual void on_unpublish(std::string url, int client_id, std::string ip, SrsRequest* req);
	/**
	* on_play hook, when client start to play stream.
	* @param client_id the id of client on server.
	* @param url the api server url, to valid the client. 
	* 		ignore if empty.
	* @return valid failed or connect to the url failed.
	*/
	virtual int on_play(std::string url, int client_id, std::string ip, SrsRequest* req);
	/**
	* on_stop hook, when client stop to play the stream.
	* @param client_id the id of client on server.
	* @param url the api server url, to process the event. 
	* 		ignore if empty.
	*/
	virtual void on_stop(std::string url, int client_id, std::string ip, SrsRequest* req);
winlin authored
166 167
};
168 169 170
#endif

#endif