Blame view

trunk/src/app/srs_app_http.hpp 5.4 KB
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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.
*/
24 25
#ifndef SRS_APP_HTTP_HPP
#define SRS_APP_HTTP_HPP
26 27

/*
28
#include <srs_app_http.hpp>
29 30 31
*/
#include <srs_core.hpp>
32
#include <srs_app_st.hpp>
winlin authored
33
34
#ifdef SRS_HTTP_CALLBACK
35
winlin authored
36
class SrsRequest;
37
class SrsSocket;
winlin authored
38 39 40 41 42

#include <string>

#include <http_parser.h>
43 44
#define SRS_HTTP_HEADER_BUFFER        1024
#define SRS_HTTP_BODY_BUFFER        32 * 1024
45
winlin authored
46 47 48 49 50 51 52 53 54 55 56 57
/**
* used to resolve the http uri.
*/
class SrsHttpUri
{
private:
    std::string url;
    std::string schema;
    std::string host;
    int port;
    std::string path;
public:
58 59
    SrsHttpUri();
    virtual ~SrsHttpUri();
winlin authored
60
public:
61 62 63 64
    /**
    * initialize the http uri.
    */
    virtual int initialize(std::string _url);
winlin authored
65 66 67 68 69
public:
    virtual const char* get_url();
    virtual const char* get_schema();
    virtual const char* get_host();
    virtual int get_port();
70
    virtual const char* get_path();
winlin authored
71 72 73 74 75 76 77 78 79 80 81 82 83
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
{
84
private:
85 86
    bool connected;
    st_netfd_t stfd;
87 88
private:
    http_parser http_header;
winlin authored
89
public:
90 91
    SrsHttpClient();
    virtual ~SrsHttpClient();
winlin authored
92
public:
93 94 95 96 97 98
    /**
    * 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);
99
private:
100 101
    virtual void disconnect();
    virtual int connect(SrsHttpUri* uri);
102 103 104 105 106 107 108 109
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
110 111 112 113 114 115 116 117 118 119
};

/**
* the http hooks, http callback api,
* for some event, such as on_connect, call
* a http api(hooks).
*/
class SrsHttpHooks
{
public:
120 121
    SrsHttpHooks();
    virtual ~SrsHttpHooks();
winlin authored
122
public:
123 124 125 126 127 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 166 167
    /**
    * 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);
winlin authored
168 169
};
170 171 172
#endif

#endif