From 4984631cd6a4f82c096d8c5b5abac69035cfbc99 Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 3 Apr 2014 16:39:55 +0800 Subject: [PATCH] refine the http crossdomain, send it only required --- trunk/src/app/srs_app_http.cpp | 47 +++++++++++++++++++++++------- trunk/src/app/srs_app_http.hpp | 12 ++++++-- trunk/src/app/srs_app_http_api.cpp | 16 ++++++---- trunk/src/app/srs_app_http_api.hpp | 1 + 4 files changed, 58 insertions(+), 18 deletions(-) diff --git a/trunk/src/app/srs_app_http.cpp b/trunk/src/app/srs_app_http.cpp index 5869da789..ab40e70c4 100644 --- a/trunk/src/app/srs_app_http.cpp +++ b/trunk/src/app/srs_app_http.cpp @@ -84,6 +84,7 @@ bool SrsHttpHandler::can_handle(const char* /*path*/, int /*length*/, const char int SrsHttpHandler::process_request(SrsSocket* skt, SrsHttpMessage* req) { if (req->method() == HTTP_OPTIONS) { + req->set_requires_crossdomain(true); return res_options(skt); } @@ -101,7 +102,7 @@ int SrsHttpHandler::process_request(SrsSocket* skt, SrsHttpMessage* req) << JOBJECT_END << JOBJECT_END; - return res_error(skt, status_code, reason_phrase, ss.str()); + return res_error(skt, req, status_code, reason_phrase, ss.str()); } return do_process_request(skt, req); @@ -266,37 +267,52 @@ int SrsHttpHandler::res_options(SrsSocket* skt) return res_flush(skt, ss); } -int SrsHttpHandler::res_text(SrsSocket* skt, std::string body) +int SrsHttpHandler::res_text(SrsSocket* skt, SrsHttpMessage* req, std::string body) { std::stringstream ss; res_status_line(ss)->res_content_type(ss) - ->res_content_length(ss, (int)body.length())->res_enable_crossdomain(ss) - ->res_header_eof(ss) + ->res_content_length(ss, (int)body.length()); + + if (req->requires_crossdomain()) { + res_enable_crossdomain(ss); + } + + res_header_eof(ss) ->res_body(ss, body); return res_flush(skt, ss); } -int SrsHttpHandler::res_json(SrsSocket* skt, std::string json) +int SrsHttpHandler::res_json(SrsSocket* skt, SrsHttpMessage* req, std::string json) { std::stringstream ss; res_status_line(ss)->res_content_type_json(ss) - ->res_content_length(ss, (int)json.length())->res_enable_crossdomain(ss) - ->res_header_eof(ss) + ->res_content_length(ss, (int)json.length()); + + if (req->requires_crossdomain()) { + res_enable_crossdomain(ss); + } + + res_header_eof(ss) ->res_body(ss, json); return res_flush(skt, ss); } -int SrsHttpHandler::res_error(SrsSocket* skt, int code, std::string reason_phrase, std::string body) +int SrsHttpHandler::res_error(SrsSocket* skt, SrsHttpMessage* req, int code, std::string reason_phrase, std::string body) { std::stringstream ss; res_status_line_error(ss, code, reason_phrase)->res_content_type_json(ss) - ->res_content_length(ss, (int)body.length())->res_enable_crossdomain(ss) - ->res_header_eof(ss) + ->res_content_length(ss, (int)body.length()); + + if (req->requires_crossdomain()) { + res_enable_crossdomain(ss); + } + + res_header_eof(ss) ->res_body(ss, body); return res_flush(skt, ss); @@ -319,6 +335,7 @@ SrsHttpMessage::SrsHttpMessage() _state = SrsHttpParseStateInit; _uri = new SrsHttpUri(); _match = NULL; + _requires_crossdomain = false; } SrsHttpMessage::~SrsHttpMessage() @@ -404,6 +421,11 @@ SrsHttpHandlerMatch* SrsHttpMessage::match() return _match; } +bool SrsHttpMessage::requires_crossdomain() +{ + return _requires_crossdomain; +} + void SrsHttpMessage::set_url(std::string url) { _url = url; @@ -425,6 +447,11 @@ void SrsHttpMessage::set_match(SrsHttpHandlerMatch* match) _match = match; } +void SrsHttpMessage::set_requires_crossdomain(bool requires_crossdomain) +{ + _requires_crossdomain = requires_crossdomain; +} + void SrsHttpMessage::append_body(const char* body, int length) { _body->append(body, length); diff --git a/trunk/src/app/srs_app_http.hpp b/trunk/src/app/srs_app_http.hpp index fdb1969fb..621e7cd71 100644 --- a/trunk/src/app/srs_app_http.hpp +++ b/trunk/src/app/srs_app_http.hpp @@ -236,9 +236,9 @@ public: virtual int res_flush(SrsSocket* skt, std::stringstream& ss); public: virtual int res_options(SrsSocket* skt); - virtual int res_text(SrsSocket* skt, std::string body); - virtual int res_json(SrsSocket* skt, std::string json); - virtual int res_error(SrsSocket* skt, int code, std::string reason_phrase, std::string body); + virtual int res_text(SrsSocket* skt, SrsHttpMessage* req, std::string body); + virtual int res_json(SrsSocket* skt, SrsHttpMessage* req, std::string json); + virtual int res_error(SrsSocket* skt, SrsHttpMessage* req, int code, std::string reason_phrase, std::string body); // object creator public: /** @@ -283,6 +283,10 @@ private: * best matched handler. */ SrsHttpHandlerMatch* _match; + /** + * whether the message requires crossdomain. + */ + bool _requires_crossdomain; public: SrsHttpMessage(); virtual ~SrsHttpMessage(); @@ -299,10 +303,12 @@ public: virtual int64_t body_size(); virtual int64_t content_length(); virtual SrsHttpHandlerMatch* match(); + virtual bool requires_crossdomain(); virtual void set_url(std::string url); virtual void set_state(SrsHttpParseState state); virtual void set_header(http_parser* header); virtual void set_match(SrsHttpHandlerMatch* match); + virtual void set_requires_crossdomain(bool requires_crossdomain); virtual void append_body(const char* body, int length); }; diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index bc97b25d2..b659e4984 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -80,7 +80,7 @@ int SrsApiRoot::do_process_request(SrsSocket* skt, SrsHttpMessage* req) << JOBJECT_END << JOBJECT_END; - return res_json(skt, ss.str()); + return res_json(skt, req, ss.str()); } SrsApiApi::SrsApiApi() @@ -108,7 +108,7 @@ int SrsApiApi::do_process_request(SrsSocket* skt, SrsHttpMessage* req) << JOBJECT_END << JOBJECT_END; - return res_json(skt, ss.str()); + return res_json(skt, req, ss.str()); } SrsApiV1::SrsApiV1() @@ -138,7 +138,7 @@ int SrsApiV1::do_process_request(SrsSocket* skt, SrsHttpMessage* req) << JOBJECT_END << JOBJECT_END; - return res_json(skt, ss.str()); + return res_json(skt, req, ss.str()); } SrsApiVersion::SrsApiVersion() @@ -168,7 +168,7 @@ int SrsApiVersion::do_process_request(SrsSocket* skt, SrsHttpMessage* req) << JOBJECT_END << JOBJECT_END; - return res_json(skt, ss.str()); + return res_json(skt, req, ss.str()); } SrsApiAuthors::SrsApiAuthors() @@ -197,7 +197,7 @@ int SrsApiAuthors::do_process_request(SrsSocket* skt, SrsHttpMessage* req) << JOBJECT_END << JOBJECT_END; - return res_json(skt, ss.str()); + return res_json(skt, req, ss.str()); } SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler) @@ -205,6 +205,7 @@ SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHan { parser = new SrsHttpParser(); handler = _handler; + requires_crossdomain = false; } SrsHttpApi::~SrsHttpApi() @@ -284,6 +285,7 @@ int SrsHttpApi::process_request(SrsSocket* skt, SrsHttpMessage* req) srs_info("best match handler, matched_url=%s", p->matched_url.c_str()); req->set_match(p); + req->set_requires_crossdomain(requires_crossdomain); // use handler to process request. if ((ret = p->handler->process_request(skt, req)) != ERROR_SUCCESS) { @@ -291,6 +293,10 @@ int SrsHttpApi::process_request(SrsSocket* skt, SrsHttpMessage* req) return ret; } + if (req->requires_crossdomain()) { + requires_crossdomain = true; + } + return ret; } diff --git a/trunk/src/app/srs_app_http_api.hpp b/trunk/src/app/srs_app_http_api.hpp index bb4f50a93..23a5f86d6 100644 --- a/trunk/src/app/srs_app_http_api.hpp +++ b/trunk/src/app/srs_app_http_api.hpp @@ -98,6 +98,7 @@ class SrsHttpApi : public SrsConnection private: SrsHttpParser* parser; SrsHttpHandler* handler; + bool requires_crossdomain; public: SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler); virtual ~SrsHttpApi();