winlin

support JSONP DELTE/POST/PUT

@@ -106,29 +106,25 @@ int srs_api_response_json_code(ISrsHttpResponseWriter* w, int code) @@ -106,29 +106,25 @@ int srs_api_response_json_code(ISrsHttpResponseWriter* w, int code)
106 106
107 int srs_api_response(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string json) 107 int srs_api_response(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string json)
108 { 108 {
109 - string callback = r->query_get("callback");  
110 - bool jsonp = !callback.empty();  
111 -  
112 // no jsonp, directly response. 109 // no jsonp, directly response.
113 - if (!jsonp) { 110 + if (!r->is_jsonp()) {
114 return srs_api_response_json(w, json); 111 return srs_api_response_json(w, json);
115 } 112 }
116 113
117 // jsonp, get function name from query("callback") 114 // jsonp, get function name from query("callback")
  115 + string callback = r->query_get("callback");
118 return srs_api_response_jsonp(w, callback, json); 116 return srs_api_response_jsonp(w, callback, json);
119 } 117 }
120 118
121 int srs_api_response_code(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, int code) 119 int srs_api_response_code(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, int code)
122 { 120 {
123 - string callback = r->query_get("callback");  
124 - bool jsonp = !callback.empty();  
125 -  
126 // no jsonp, directly response. 121 // no jsonp, directly response.
127 - if (!jsonp) { 122 + if (!r->is_jsonp()) {
128 return srs_api_response_json_code(w, code); 123 return srs_api_response_json_code(w, code);
129 } 124 }
130 125
131 // jsonp, get function name from query("callback") 126 // jsonp, get function name from query("callback")
  127 + string callback = r->query_get("callback");
132 return srs_api_response_jsonp_code(w, callback, code); 128 return srs_api_response_jsonp_code(w, callback, code);
133 } 129 }
134 130
@@ -494,6 +494,7 @@ SrsHttpMessage::SrsHttpMessage(SrsStSocket* io, SrsConnection* c) : ISrsHttpMess @@ -494,6 +494,7 @@ SrsHttpMessage::SrsHttpMessage(SrsStSocket* io, SrsConnection* c) : ISrsHttpMess
494 _uri = new SrsHttpUri(); 494 _uri = new SrsHttpUri();
495 _body = new SrsHttpResponseReader(this, io); 495 _body = new SrsHttpResponseReader(this, io);
496 _http_ts_send_buffer = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE]; 496 _http_ts_send_buffer = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE];
  497 + jsonp = false;
497 } 498 }
498 499
499 SrsHttpMessage::~SrsHttpMessage() 500 SrsHttpMessage::~SrsHttpMessage()
@@ -570,6 +571,14 @@ int SrsHttpMessage::update(string url, http_parser* header, SrsFastBuffer* body, @@ -570,6 +571,14 @@ int SrsHttpMessage::update(string url, http_parser* header, SrsFastBuffer* body,
570 _ext = ""; 571 _ext = "";
571 } 572 }
572 573
  574 + // parse jsonp request message.
  575 + if (!query_get("callback").empty()) {
  576 + jsonp = true;
  577 + }
  578 + if (jsonp) {
  579 + jsonp_method = query_get("method");
  580 + }
  581 +
573 return ret; 582 return ret;
574 } 583 }
575 584
@@ -580,6 +589,18 @@ SrsConnection* SrsHttpMessage::connection() @@ -580,6 +589,18 @@ SrsConnection* SrsHttpMessage::connection()
580 589
581 u_int8_t SrsHttpMessage::method() 590 u_int8_t SrsHttpMessage::method()
582 { 591 {
  592 + if (jsonp && !jsonp_method.empty()) {
  593 + if (jsonp_method == "GET") {
  594 + return SRS_CONSTS_HTTP_GET;
  595 + } else if (jsonp_method == "PUT") {
  596 + return SRS_CONSTS_HTTP_PUT;
  597 + } else if (jsonp_method == "POST") {
  598 + return SRS_CONSTS_HTTP_POST;
  599 + } else if (jsonp_method == "DELETE") {
  600 + return SRS_CONSTS_HTTP_DELETE;
  601 + }
  602 + }
  603 +
583 return (u_int8_t)_header.method; 604 return (u_int8_t)_header.method;
584 } 605 }
585 606
@@ -590,6 +611,10 @@ u_int16_t SrsHttpMessage::status_code() @@ -590,6 +611,10 @@ u_int16_t SrsHttpMessage::status_code()
590 611
591 string SrsHttpMessage::method_str() 612 string SrsHttpMessage::method_str()
592 { 613 {
  614 + if (jsonp && !jsonp_method.empty()) {
  615 + return jsonp_method;
  616 + }
  617 +
593 if (is_http_get()) { 618 if (is_http_get()) {
594 return "GET"; 619 return "GET";
595 } 620 }
@@ -611,22 +636,22 @@ string SrsHttpMessage::method_str() @@ -611,22 +636,22 @@ string SrsHttpMessage::method_str()
611 636
612 bool SrsHttpMessage::is_http_get() 637 bool SrsHttpMessage::is_http_get()
613 { 638 {
614 - return _header.method == SRS_CONSTS_HTTP_GET; 639 + return method() == SRS_CONSTS_HTTP_GET;
615 } 640 }
616 641
617 bool SrsHttpMessage::is_http_put() 642 bool SrsHttpMessage::is_http_put()
618 { 643 {
619 - return _header.method == SRS_CONSTS_HTTP_PUT; 644 + return method() == SRS_CONSTS_HTTP_PUT;
620 } 645 }
621 646
622 bool SrsHttpMessage::is_http_post() 647 bool SrsHttpMessage::is_http_post()
623 { 648 {
624 - return _header.method == SRS_CONSTS_HTTP_POST; 649 + return method() == SRS_CONSTS_HTTP_POST;
625 } 650 }
626 651
627 bool SrsHttpMessage::is_http_delete() 652 bool SrsHttpMessage::is_http_delete()
628 { 653 {
629 - return _header.method == SRS_CONSTS_HTTP_DELETE; 654 + return method() == SRS_CONSTS_HTTP_DELETE;
630 } 655 }
631 656
632 bool SrsHttpMessage::is_http_options() 657 bool SrsHttpMessage::is_http_options()
@@ -803,6 +828,11 @@ SrsRequest* SrsHttpMessage::to_request(string vhost) @@ -803,6 +828,11 @@ SrsRequest* SrsHttpMessage::to_request(string vhost)
803 return req; 828 return req;
804 } 829 }
805 830
  831 +bool SrsHttpMessage::is_jsonp()
  832 +{
  833 + return jsonp;
  834 +}
  835 +
806 SrsHttpParser::SrsHttpParser() 836 SrsHttpParser::SrsHttpParser()
807 { 837 {
808 buffer = new SrsFastBuffer(); 838 buffer = new SrsFastBuffer();
@@ -203,6 +203,10 @@ private: @@ -203,6 +203,10 @@ private:
203 std::map<std::string, std::string> _query; 203 std::map<std::string, std::string> _query;
204 // the transport connection, can be NULL. 204 // the transport connection, can be NULL.
205 SrsConnection* conn; 205 SrsConnection* conn;
  206 + // whether request is jsonp.
  207 + bool jsonp;
  208 + // the method in QueryString will override the HTTP method.
  209 + std::string jsonp_method;
206 public: 210 public:
207 SrsHttpMessage(SrsStSocket* io, SrsConnection* c); 211 SrsHttpMessage(SrsStSocket* io, SrsConnection* c);
208 virtual ~SrsHttpMessage(); 212 virtual ~SrsHttpMessage();
@@ -285,6 +289,8 @@ public: @@ -285,6 +289,8 @@ public:
285 * @remark user must free the return request. 289 * @remark user must free the return request.
286 */ 290 */
287 virtual SrsRequest* to_request(std::string vhost); 291 virtual SrsRequest* to_request(std::string vhost);
  292 +public:
  293 + virtual bool is_jsonp();
288 }; 294 };
289 295
290 /** 296 /**
@@ -527,6 +527,12 @@ public: @@ -527,6 +527,12 @@ public:
527 virtual int request_header_count() = 0; 527 virtual int request_header_count() = 0;
528 virtual std::string request_header_key_at(int index) = 0; 528 virtual std::string request_header_key_at(int index) = 0;
529 virtual std::string request_header_value_at(int index) = 0; 529 virtual std::string request_header_value_at(int index) = 0;
  530 +public:
  531 + /**
  532 + * whether the current request is JSONP,
  533 + * which has a "callback=xxx" in QueryString.
  534 + */
  535 + virtual bool is_jsonp() = 0;
530 }; 536 };
531 537
532 #endif 538 #endif