winlin

support JSONP DELTE/POST/PUT

... ... @@ -106,29 +106,25 @@ int srs_api_response_json_code(ISrsHttpResponseWriter* w, int code)
int srs_api_response(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string json)
{
string callback = r->query_get("callback");
bool jsonp = !callback.empty();
// no jsonp, directly response.
if (!jsonp) {
if (!r->is_jsonp()) {
return srs_api_response_json(w, json);
}
// jsonp, get function name from query("callback")
string callback = r->query_get("callback");
return srs_api_response_jsonp(w, callback, json);
}
int srs_api_response_code(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, int code)
{
string callback = r->query_get("callback");
bool jsonp = !callback.empty();
// no jsonp, directly response.
if (!jsonp) {
if (!r->is_jsonp()) {
return srs_api_response_json_code(w, code);
}
// jsonp, get function name from query("callback")
string callback = r->query_get("callback");
return srs_api_response_jsonp_code(w, callback, code);
}
... ...
... ... @@ -494,6 +494,7 @@ SrsHttpMessage::SrsHttpMessage(SrsStSocket* io, SrsConnection* c) : ISrsHttpMess
_uri = new SrsHttpUri();
_body = new SrsHttpResponseReader(this, io);
_http_ts_send_buffer = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE];
jsonp = false;
}
SrsHttpMessage::~SrsHttpMessage()
... ... @@ -570,6 +571,14 @@ int SrsHttpMessage::update(string url, http_parser* header, SrsFastBuffer* body,
_ext = "";
}
// parse jsonp request message.
if (!query_get("callback").empty()) {
jsonp = true;
}
if (jsonp) {
jsonp_method = query_get("method");
}
return ret;
}
... ... @@ -580,6 +589,18 @@ SrsConnection* SrsHttpMessage::connection()
u_int8_t SrsHttpMessage::method()
{
if (jsonp && !jsonp_method.empty()) {
if (jsonp_method == "GET") {
return SRS_CONSTS_HTTP_GET;
} else if (jsonp_method == "PUT") {
return SRS_CONSTS_HTTP_PUT;
} else if (jsonp_method == "POST") {
return SRS_CONSTS_HTTP_POST;
} else if (jsonp_method == "DELETE") {
return SRS_CONSTS_HTTP_DELETE;
}
}
return (u_int8_t)_header.method;
}
... ... @@ -590,6 +611,10 @@ u_int16_t SrsHttpMessage::status_code()
string SrsHttpMessage::method_str()
{
if (jsonp && !jsonp_method.empty()) {
return jsonp_method;
}
if (is_http_get()) {
return "GET";
}
... ... @@ -611,22 +636,22 @@ string SrsHttpMessage::method_str()
bool SrsHttpMessage::is_http_get()
{
return _header.method == SRS_CONSTS_HTTP_GET;
return method() == SRS_CONSTS_HTTP_GET;
}
bool SrsHttpMessage::is_http_put()
{
return _header.method == SRS_CONSTS_HTTP_PUT;
return method() == SRS_CONSTS_HTTP_PUT;
}
bool SrsHttpMessage::is_http_post()
{
return _header.method == SRS_CONSTS_HTTP_POST;
return method() == SRS_CONSTS_HTTP_POST;
}
bool SrsHttpMessage::is_http_delete()
{
return _header.method == SRS_CONSTS_HTTP_DELETE;
return method() == SRS_CONSTS_HTTP_DELETE;
}
bool SrsHttpMessage::is_http_options()
... ... @@ -803,6 +828,11 @@ SrsRequest* SrsHttpMessage::to_request(string vhost)
return req;
}
bool SrsHttpMessage::is_jsonp()
{
return jsonp;
}
SrsHttpParser::SrsHttpParser()
{
buffer = new SrsFastBuffer();
... ...
... ... @@ -203,6 +203,10 @@ private:
std::map<std::string, std::string> _query;
// the transport connection, can be NULL.
SrsConnection* conn;
// whether request is jsonp.
bool jsonp;
// the method in QueryString will override the HTTP method.
std::string jsonp_method;
public:
SrsHttpMessage(SrsStSocket* io, SrsConnection* c);
virtual ~SrsHttpMessage();
... ... @@ -285,6 +289,8 @@ public:
* @remark user must free the return request.
*/
virtual SrsRequest* to_request(std::string vhost);
public:
virtual bool is_jsonp();
};
/**
... ...
... ... @@ -527,6 +527,12 @@ public:
virtual int request_header_count() = 0;
virtual std::string request_header_key_at(int index) = 0;
virtual std::string request_header_value_at(int index) = 0;
public:
/**
* whether the current request is JSONP,
* which has a "callback=xxx" in QueryString.
*/
virtual bool is_jsonp() = 0;
};
#endif
... ...