winlin

add api framework code

@@ -25,16 +25,24 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -25,16 +25,24 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 25
26 #ifdef SRS_HTTP_API 26 #ifdef SRS_HTTP_API
27 27
  28 +#include <sstream>
  29 +using namespace std;
  30 +
28 #include <srs_kernel_log.hpp> 31 #include <srs_kernel_log.hpp>
29 #include <srs_kernel_error.hpp> 32 #include <srs_kernel_error.hpp>
  33 +#include <srs_app_http.hpp>
  34 +#include <srs_app_socket.hpp>
  35 +#include <srs_core_autofree.hpp>
30 36
31 SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd) 37 SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd)
32 : SrsConnection(srs_server, client_stfd) 38 : SrsConnection(srs_server, client_stfd)
33 { 39 {
  40 + parser = new SrsHttpParser();
34 } 41 }
35 42
36 SrsHttpApi::~SrsHttpApi() 43 SrsHttpApi::~SrsHttpApi()
37 { 44 {
  45 + srs_freep(parser);
38 } 46 }
39 47
40 int SrsHttpApi::do_cycle() 48 int SrsHttpApi::do_cycle()
@@ -47,6 +55,73 @@ int SrsHttpApi::do_cycle() @@ -47,6 +55,73 @@ int SrsHttpApi::do_cycle()
47 } 55 }
48 srs_trace("api get peer ip success. ip=%s", ip); 56 srs_trace("api get peer ip success. ip=%s", ip);
49 57
  58 + // initialize parser
  59 + if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) {
  60 + srs_error("api initialize http parser failed. ret=%d", ret);
  61 + return ret;
  62 + }
  63 +
  64 + // underlayer socket
  65 + SrsSocket skt(stfd);
  66 +
  67 + // process http messages.
  68 + for (;;) {
  69 + SrsHttpMessage* req = NULL;
  70 +
  71 + // get a http message
  72 + if ((ret = parser->parse_message(&skt, &req)) != ERROR_SUCCESS) {
  73 + return ret;
  74 + }
  75 +
  76 + // if SUCCESS, always NOT-NULL and completed message.
  77 + srs_assert(req);
  78 + srs_assert(req->is_complete());
  79 +
  80 + // always free it in this scope.
  81 + SrsAutoFree(SrsHttpMessage, req, false);
  82 +
  83 + // ok, handle http request.
  84 + if ((ret = process_request(&skt, req)) != ERROR_SUCCESS) {
  85 + return ret;
  86 + }
  87 + }
  88 +
  89 + return ret;
  90 +}
  91 +
  92 +int SrsHttpApi::process_request(SrsSocket* skt, SrsHttpMessage* req)
  93 +{
  94 + int ret = ERROR_SUCCESS;
  95 +
  96 + if (req->method() == HTTP_OPTIONS) {
  97 + char data[] = "HTTP/1.1 200 OK" __CRLF
  98 + "Content-Length: 0"__CRLF
  99 + "Server: SRS/"RTMP_SIG_SRS_VERSION""__CRLF
  100 + "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT"__CRLF
  101 + "Access-Control-Allow-Origin: *"__CRLF
  102 + "Access-Control-Allow-Methods: GET, POST, HEAD, PUT, DELETE"__CRLF
  103 + "Access-Control-Allow-Headers: Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type"__CRLF
  104 + "Content-Type: text/html;charset=utf-8"__CRLFCRLF
  105 + "";
  106 + return skt->write(data, sizeof(data), NULL);
  107 + } else {
  108 + std::string tilte = "SRS/"RTMP_SIG_SRS_VERSION;
  109 + tilte += " hello http/1.1 api~\n";
  110 +
  111 + std::stringstream ss;
  112 + ss << "HTTP/1.1 200 OK " << __CRLF
  113 + << "Content-Length: "<< tilte.length() + req->body_size() << __CRLF
  114 + << "Server: SRS/"RTMP_SIG_SRS_VERSION"" << __CRLF
  115 + << "Allow: DELETE, GET, HEAD, OPTIONS, POST, PUT" << __CRLF
  116 + << "Access-Control-Allow-Origin: *" << __CRLF
  117 + << "Access-Control-Allow-Methods: GET, POST, HEAD, PUT, DELETE" << __CRLF
  118 + << "Access-Control-Allow-Headers: Cache-Control,X-Proxy-Authorization,X-Requested-With,Content-Type" << __CRLF
  119 + << "Content-Type: text/html;charset=utf-8" << __CRLFCRLF
  120 + << tilte << req->body().c_str()
  121 + << "";
  122 + return skt->write(ss.str().c_str(), ss.str().length(), NULL);
  123 + }
  124 +
50 return ret; 125 return ret;
51 } 126 }
52 127
@@ -32,16 +32,24 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -32,16 +32,24 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 32
33 #ifdef SRS_HTTP_API 33 #ifdef SRS_HTTP_API
34 34
  35 +class SrsSocket;
  36 +class SrsHttpMessage;
  37 +class SrsHttpParser;
  38 +
35 #include <srs_app_st.hpp> 39 #include <srs_app_st.hpp>
36 #include <srs_app_conn.hpp> 40 #include <srs_app_conn.hpp>
37 41
38 class SrsHttpApi : public SrsConnection 42 class SrsHttpApi : public SrsConnection
39 { 43 {
  44 +private:
  45 + SrsHttpParser* parser;
40 public: 46 public:
41 SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd); 47 SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd);
42 virtual ~SrsHttpApi(); 48 virtual ~SrsHttpApi();
43 protected: 49 protected:
44 virtual int do_cycle(); 50 virtual int do_cycle();
  51 +private:
  52 + virtual int process_request(SrsSocket* skt, SrsHttpMessage* req);
45 }; 53 };
46 54
47 #endif 55 #endif
@@ -60,7 +60,7 @@ int SrsHttpConn::do_cycle() @@ -60,7 +60,7 @@ int SrsHttpConn::do_cycle()
60 60
61 // initialize parser 61 // initialize parser
62 if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) { 62 if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) {
63 - srs_error("initialize http parser failed. ret=%d", ret); 63 + srs_error("http initialize http parser failed. ret=%d", ret);
64 return ret; 64 return ret;
65 } 65 }
66 66
@@ -109,7 +109,7 @@ int SrsHttpConn::process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -109,7 +109,7 @@ int SrsHttpConn::process_request(SrsSocket* skt, SrsHttpMessage* req)
109 return skt->write(data, sizeof(data), NULL); 109 return skt->write(data, sizeof(data), NULL);
110 } else { 110 } else {
111 std::string tilte = "SRS/"RTMP_SIG_SRS_VERSION; 111 std::string tilte = "SRS/"RTMP_SIG_SRS_VERSION;
112 - tilte += " hello http/1.1~\n"; 112 + tilte += " hello http/1.1 server~\n";
113 113
114 std::stringstream ss; 114 std::stringstream ss;
115 ss << "HTTP/1.1 200 OK " << __CRLF 115 ss << "HTTP/1.1 200 OK " << __CRLF