1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

refine code, extract http hooks.

This commit is contained in:
winlin 2014-04-01 18:40:24 +08:00
parent 5bbb76a59d
commit e70609cea3
9 changed files with 948 additions and 847 deletions

View file

@ -29,9 +29,17 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <srs_core.hpp>
#ifdef SRS_HTTP_PARSER
#include <string>
#include <http_parser.h>
#include <srs_app_st.hpp>
#ifdef SRS_HTTP_PARSER
class SrsBuffer;
class SrsRequest;
class SrsSocket;
// http specification
// CR = <US-ASCII CR, carriage return (13)>
@ -49,16 +57,42 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define __CRLF "\r\n" // 0x0D0A
#define __CRLFCRLF "\r\n\r\n" // 0x0D0A0D0A
#endif
enum SrsHttpParseState {
SrsHttpParseStateInit = 0,
SrsHttpParseStateStart,
SrsHttpParseStateComplete
};
#ifdef SRS_HTTP_CALLBACK
/**
* the http message, request or response.
*/
class SrsHttpMessage
{
public:
std::string url;
http_parser header;
SrsBuffer* body;
SrsHttpParseState state;
SrsHttpMessage();
virtual ~SrsHttpMessage();
virtual void reset();
virtual bool is_complete();
};
class SrsRequest;
class SrsSocket;
#include <string>
#include <http_parser.h>
/**
* wrapper for http-parser,
* provides HTTP message originted service.
*/
class SrsHttpParser
{
public:
SrsHttpParser();
virtual ~SrsHttpParser();
public:
//virtual int parse_requst(SrsHttpMessage** ppreq);
};
/**
* used to resolve the http uri.
@ -93,97 +127,6 @@ private:
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
{
private:
bool connected;
st_netfd_t stfd;
private:
http_parser http_header;
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);
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 complete_header(http_parser* parser);
};
/**
* 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:
/**
* on_connect hook, when client connect to srs.
* @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_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);
};
#endif
#endif