refine the http crossdomain, send it only required

pull/133/head
winlin 11 years ago
parent 133a6f0dbf
commit 4984631cd6

@ -84,6 +84,7 @@ bool SrsHttpHandler::can_handle(const char* /*path*/, int /*length*/, const char
int SrsHttpHandler::process_request(SrsSocket* skt, SrsHttpMessage* req) int SrsHttpHandler::process_request(SrsSocket* skt, SrsHttpMessage* req)
{ {
if (req->method() == HTTP_OPTIONS) { if (req->method() == HTTP_OPTIONS) {
req->set_requires_crossdomain(true);
return res_options(skt); return res_options(skt);
} }
@ -101,7 +102,7 @@ int SrsHttpHandler::process_request(SrsSocket* skt, SrsHttpMessage* req)
<< JOBJECT_END << JOBJECT_END
<< 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); return do_process_request(skt, req);
@ -266,37 +267,52 @@ int SrsHttpHandler::res_options(SrsSocket* skt)
return res_flush(skt, ss); 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; std::stringstream ss;
res_status_line(ss)->res_content_type(ss) res_status_line(ss)->res_content_type(ss)
->res_content_length(ss, (int)body.length())->res_enable_crossdomain(ss) ->res_content_length(ss, (int)body.length());
->res_header_eof(ss)
if (req->requires_crossdomain()) {
res_enable_crossdomain(ss);
}
res_header_eof(ss)
->res_body(ss, body); ->res_body(ss, body);
return res_flush(skt, ss); 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; std::stringstream ss;
res_status_line(ss)->res_content_type_json(ss) res_status_line(ss)->res_content_type_json(ss)
->res_content_length(ss, (int)json.length())->res_enable_crossdomain(ss) ->res_content_length(ss, (int)json.length());
->res_header_eof(ss)
if (req->requires_crossdomain()) {
res_enable_crossdomain(ss);
}
res_header_eof(ss)
->res_body(ss, json); ->res_body(ss, json);
return res_flush(skt, ss); 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; std::stringstream ss;
res_status_line_error(ss, code, reason_phrase)->res_content_type_json(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_content_length(ss, (int)body.length());
->res_header_eof(ss)
if (req->requires_crossdomain()) {
res_enable_crossdomain(ss);
}
res_header_eof(ss)
->res_body(ss, body); ->res_body(ss, body);
return res_flush(skt, ss); return res_flush(skt, ss);
@ -319,6 +335,7 @@ SrsHttpMessage::SrsHttpMessage()
_state = SrsHttpParseStateInit; _state = SrsHttpParseStateInit;
_uri = new SrsHttpUri(); _uri = new SrsHttpUri();
_match = NULL; _match = NULL;
_requires_crossdomain = false;
} }
SrsHttpMessage::~SrsHttpMessage() SrsHttpMessage::~SrsHttpMessage()
@ -404,6 +421,11 @@ SrsHttpHandlerMatch* SrsHttpMessage::match()
return _match; return _match;
} }
bool SrsHttpMessage::requires_crossdomain()
{
return _requires_crossdomain;
}
void SrsHttpMessage::set_url(std::string url) void SrsHttpMessage::set_url(std::string url)
{ {
_url = url; _url = url;
@ -425,6 +447,11 @@ void SrsHttpMessage::set_match(SrsHttpHandlerMatch* match)
_match = match; _match = match;
} }
void SrsHttpMessage::set_requires_crossdomain(bool requires_crossdomain)
{
_requires_crossdomain = requires_crossdomain;
}
void SrsHttpMessage::append_body(const char* body, int length) void SrsHttpMessage::append_body(const char* body, int length)
{ {
_body->append(body, length); _body->append(body, length);

@ -236,9 +236,9 @@ public:
virtual int res_flush(SrsSocket* skt, std::stringstream& ss); virtual int res_flush(SrsSocket* skt, std::stringstream& ss);
public: public:
virtual int res_options(SrsSocket* skt); virtual int res_options(SrsSocket* skt);
virtual int res_text(SrsSocket* skt, std::string body); virtual int res_text(SrsSocket* skt, SrsHttpMessage* req, std::string body);
virtual int res_json(SrsSocket* skt, std::string json); virtual int res_json(SrsSocket* skt, SrsHttpMessage* req, std::string json);
virtual int res_error(SrsSocket* skt, int code, std::string reason_phrase, std::string body); virtual int res_error(SrsSocket* skt, SrsHttpMessage* req, int code, std::string reason_phrase, std::string body);
// object creator // object creator
public: public:
/** /**
@ -283,6 +283,10 @@ private:
* best matched handler. * best matched handler.
*/ */
SrsHttpHandlerMatch* _match; SrsHttpHandlerMatch* _match;
/**
* whether the message requires crossdomain.
*/
bool _requires_crossdomain;
public: public:
SrsHttpMessage(); SrsHttpMessage();
virtual ~SrsHttpMessage(); virtual ~SrsHttpMessage();
@ -299,10 +303,12 @@ public:
virtual int64_t body_size(); virtual int64_t body_size();
virtual int64_t content_length(); virtual int64_t content_length();
virtual SrsHttpHandlerMatch* match(); virtual SrsHttpHandlerMatch* match();
virtual bool requires_crossdomain();
virtual void set_url(std::string url); virtual void set_url(std::string url);
virtual void set_state(SrsHttpParseState state); virtual void set_state(SrsHttpParseState state);
virtual void set_header(http_parser* header); virtual void set_header(http_parser* header);
virtual void set_match(SrsHttpHandlerMatch* match); virtual void set_match(SrsHttpHandlerMatch* match);
virtual void set_requires_crossdomain(bool requires_crossdomain);
virtual void append_body(const char* body, int length); virtual void append_body(const char* body, int length);
}; };

@ -80,7 +80,7 @@ int SrsApiRoot::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
<< JOBJECT_END << JOBJECT_END
<< JOBJECT_END; << JOBJECT_END;
return res_json(skt, ss.str()); return res_json(skt, req, ss.str());
} }
SrsApiApi::SrsApiApi() SrsApiApi::SrsApiApi()
@ -108,7 +108,7 @@ int SrsApiApi::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
<< JOBJECT_END << JOBJECT_END
<< JOBJECT_END; << JOBJECT_END;
return res_json(skt, ss.str()); return res_json(skt, req, ss.str());
} }
SrsApiV1::SrsApiV1() SrsApiV1::SrsApiV1()
@ -138,7 +138,7 @@ int SrsApiV1::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
<< JOBJECT_END << JOBJECT_END
<< JOBJECT_END; << JOBJECT_END;
return res_json(skt, ss.str()); return res_json(skt, req, ss.str());
} }
SrsApiVersion::SrsApiVersion() SrsApiVersion::SrsApiVersion()
@ -168,7 +168,7 @@ int SrsApiVersion::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
<< JOBJECT_END << JOBJECT_END
<< JOBJECT_END; << JOBJECT_END;
return res_json(skt, ss.str()); return res_json(skt, req, ss.str());
} }
SrsApiAuthors::SrsApiAuthors() SrsApiAuthors::SrsApiAuthors()
@ -197,7 +197,7 @@ int SrsApiAuthors::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
<< JOBJECT_END << JOBJECT_END
<< 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) 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(); parser = new SrsHttpParser();
handler = _handler; handler = _handler;
requires_crossdomain = false;
} }
SrsHttpApi::~SrsHttpApi() 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()); srs_info("best match handler, matched_url=%s", p->matched_url.c_str());
req->set_match(p); req->set_match(p);
req->set_requires_crossdomain(requires_crossdomain);
// use handler to process request. // use handler to process request.
if ((ret = p->handler->process_request(skt, req)) != ERROR_SUCCESS) { if ((ret = p->handler->process_request(skt, req)) != ERROR_SUCCESS) {
@ -291,6 +293,10 @@ int SrsHttpApi::process_request(SrsSocket* skt, SrsHttpMessage* req)
return ret; return ret;
} }
if (req->requires_crossdomain()) {
requires_crossdomain = true;
}
return ret; return ret;
} }

@ -98,6 +98,7 @@ class SrsHttpApi : public SrsConnection
private: private:
SrsHttpParser* parser; SrsHttpParser* parser;
SrsHttpHandler* handler; SrsHttpHandler* handler;
bool requires_crossdomain;
public: public:
SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler); SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler);
virtual ~SrsHttpApi(); virtual ~SrsHttpApi();

Loading…
Cancel
Save