winlin

refine the http crossdomain, send it only required

@@ -84,6 +84,7 @@ bool SrsHttpHandler::can_handle(const char* /*path*/, int /*length*/, const char @@ -84,6 +84,7 @@ bool SrsHttpHandler::can_handle(const char* /*path*/, int /*length*/, const char
84 int SrsHttpHandler::process_request(SrsSocket* skt, SrsHttpMessage* req) 84 int SrsHttpHandler::process_request(SrsSocket* skt, SrsHttpMessage* req)
85 { 85 {
86 if (req->method() == HTTP_OPTIONS) { 86 if (req->method() == HTTP_OPTIONS) {
  87 + req->set_requires_crossdomain(true);
87 return res_options(skt); 88 return res_options(skt);
88 } 89 }
89 90
@@ -101,7 +102,7 @@ int SrsHttpHandler::process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -101,7 +102,7 @@ int SrsHttpHandler::process_request(SrsSocket* skt, SrsHttpMessage* req)
101 << JOBJECT_END 102 << JOBJECT_END
102 << JOBJECT_END; 103 << JOBJECT_END;
103 104
104 - return res_error(skt, status_code, reason_phrase, ss.str()); 105 + return res_error(skt, req, status_code, reason_phrase, ss.str());
105 } 106 }
106 107
107 return do_process_request(skt, req); 108 return do_process_request(skt, req);
@@ -266,37 +267,52 @@ int SrsHttpHandler::res_options(SrsSocket* skt) @@ -266,37 +267,52 @@ int SrsHttpHandler::res_options(SrsSocket* skt)
266 return res_flush(skt, ss); 267 return res_flush(skt, ss);
267 } 268 }
268 269
269 -int SrsHttpHandler::res_text(SrsSocket* skt, std::string body) 270 +int SrsHttpHandler::res_text(SrsSocket* skt, SrsHttpMessage* req, std::string body)
270 { 271 {
271 std::stringstream ss; 272 std::stringstream ss;
272 273
273 res_status_line(ss)->res_content_type(ss) 274 res_status_line(ss)->res_content_type(ss)
274 - ->res_content_length(ss, (int)body.length())->res_enable_crossdomain(ss)  
275 - ->res_header_eof(ss) 275 + ->res_content_length(ss, (int)body.length());
  276 +
  277 + if (req->requires_crossdomain()) {
  278 + res_enable_crossdomain(ss);
  279 + }
  280 +
  281 + res_header_eof(ss)
276 ->res_body(ss, body); 282 ->res_body(ss, body);
277 283
278 return res_flush(skt, ss); 284 return res_flush(skt, ss);
279 } 285 }
280 286
281 -int SrsHttpHandler::res_json(SrsSocket* skt, std::string json) 287 +int SrsHttpHandler::res_json(SrsSocket* skt, SrsHttpMessage* req, std::string json)
282 { 288 {
283 std::stringstream ss; 289 std::stringstream ss;
284 290
285 res_status_line(ss)->res_content_type_json(ss) 291 res_status_line(ss)->res_content_type_json(ss)
286 - ->res_content_length(ss, (int)json.length())->res_enable_crossdomain(ss)  
287 - ->res_header_eof(ss) 292 + ->res_content_length(ss, (int)json.length());
  293 +
  294 + if (req->requires_crossdomain()) {
  295 + res_enable_crossdomain(ss);
  296 + }
  297 +
  298 + res_header_eof(ss)
288 ->res_body(ss, json); 299 ->res_body(ss, json);
289 300
290 return res_flush(skt, ss); 301 return res_flush(skt, ss);
291 } 302 }
292 303
293 -int SrsHttpHandler::res_error(SrsSocket* skt, int code, std::string reason_phrase, std::string body) 304 +int SrsHttpHandler::res_error(SrsSocket* skt, SrsHttpMessage* req, int code, std::string reason_phrase, std::string body)
294 { 305 {
295 std::stringstream ss; 306 std::stringstream ss;
296 307
297 res_status_line_error(ss, code, reason_phrase)->res_content_type_json(ss) 308 res_status_line_error(ss, code, reason_phrase)->res_content_type_json(ss)
298 - ->res_content_length(ss, (int)body.length())->res_enable_crossdomain(ss)  
299 - ->res_header_eof(ss) 309 + ->res_content_length(ss, (int)body.length());
  310 +
  311 + if (req->requires_crossdomain()) {
  312 + res_enable_crossdomain(ss);
  313 + }
  314 +
  315 + res_header_eof(ss)
300 ->res_body(ss, body); 316 ->res_body(ss, body);
301 317
302 return res_flush(skt, ss); 318 return res_flush(skt, ss);
@@ -319,6 +335,7 @@ SrsHttpMessage::SrsHttpMessage() @@ -319,6 +335,7 @@ SrsHttpMessage::SrsHttpMessage()
319 _state = SrsHttpParseStateInit; 335 _state = SrsHttpParseStateInit;
320 _uri = new SrsHttpUri(); 336 _uri = new SrsHttpUri();
321 _match = NULL; 337 _match = NULL;
  338 + _requires_crossdomain = false;
322 } 339 }
323 340
324 SrsHttpMessage::~SrsHttpMessage() 341 SrsHttpMessage::~SrsHttpMessage()
@@ -404,6 +421,11 @@ SrsHttpHandlerMatch* SrsHttpMessage::match() @@ -404,6 +421,11 @@ SrsHttpHandlerMatch* SrsHttpMessage::match()
404 return _match; 421 return _match;
405 } 422 }
406 423
  424 +bool SrsHttpMessage::requires_crossdomain()
  425 +{
  426 + return _requires_crossdomain;
  427 +}
  428 +
407 void SrsHttpMessage::set_url(std::string url) 429 void SrsHttpMessage::set_url(std::string url)
408 { 430 {
409 _url = url; 431 _url = url;
@@ -425,6 +447,11 @@ void SrsHttpMessage::set_match(SrsHttpHandlerMatch* match) @@ -425,6 +447,11 @@ void SrsHttpMessage::set_match(SrsHttpHandlerMatch* match)
425 _match = match; 447 _match = match;
426 } 448 }
427 449
  450 +void SrsHttpMessage::set_requires_crossdomain(bool requires_crossdomain)
  451 +{
  452 + _requires_crossdomain = requires_crossdomain;
  453 +}
  454 +
428 void SrsHttpMessage::append_body(const char* body, int length) 455 void SrsHttpMessage::append_body(const char* body, int length)
429 { 456 {
430 _body->append(body, length); 457 _body->append(body, length);
@@ -236,9 +236,9 @@ public: @@ -236,9 +236,9 @@ public:
236 virtual int res_flush(SrsSocket* skt, std::stringstream& ss); 236 virtual int res_flush(SrsSocket* skt, std::stringstream& ss);
237 public: 237 public:
238 virtual int res_options(SrsSocket* skt); 238 virtual int res_options(SrsSocket* skt);
239 - virtual int res_text(SrsSocket* skt, std::string body);  
240 - virtual int res_json(SrsSocket* skt, std::string json);  
241 - virtual int res_error(SrsSocket* skt, int code, std::string reason_phrase, std::string body); 239 + virtual int res_text(SrsSocket* skt, SrsHttpMessage* req, std::string body);
  240 + virtual int res_json(SrsSocket* skt, SrsHttpMessage* req, std::string json);
  241 + virtual int res_error(SrsSocket* skt, SrsHttpMessage* req, int code, std::string reason_phrase, std::string body);
242 // object creator 242 // object creator
243 public: 243 public:
244 /** 244 /**
@@ -283,6 +283,10 @@ private: @@ -283,6 +283,10 @@ private:
283 * best matched handler. 283 * best matched handler.
284 */ 284 */
285 SrsHttpHandlerMatch* _match; 285 SrsHttpHandlerMatch* _match;
  286 + /**
  287 + * whether the message requires crossdomain.
  288 + */
  289 + bool _requires_crossdomain;
286 public: 290 public:
287 SrsHttpMessage(); 291 SrsHttpMessage();
288 virtual ~SrsHttpMessage(); 292 virtual ~SrsHttpMessage();
@@ -299,10 +303,12 @@ public: @@ -299,10 +303,12 @@ public:
299 virtual int64_t body_size(); 303 virtual int64_t body_size();
300 virtual int64_t content_length(); 304 virtual int64_t content_length();
301 virtual SrsHttpHandlerMatch* match(); 305 virtual SrsHttpHandlerMatch* match();
  306 + virtual bool requires_crossdomain();
302 virtual void set_url(std::string url); 307 virtual void set_url(std::string url);
303 virtual void set_state(SrsHttpParseState state); 308 virtual void set_state(SrsHttpParseState state);
304 virtual void set_header(http_parser* header); 309 virtual void set_header(http_parser* header);
305 virtual void set_match(SrsHttpHandlerMatch* match); 310 virtual void set_match(SrsHttpHandlerMatch* match);
  311 + virtual void set_requires_crossdomain(bool requires_crossdomain);
306 virtual void append_body(const char* body, int length); 312 virtual void append_body(const char* body, int length);
307 }; 313 };
308 314
@@ -80,7 +80,7 @@ int SrsApiRoot::do_process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -80,7 +80,7 @@ int SrsApiRoot::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
80 << JOBJECT_END 80 << JOBJECT_END
81 << JOBJECT_END; 81 << JOBJECT_END;
82 82
83 - return res_json(skt, ss.str()); 83 + return res_json(skt, req, ss.str());
84 } 84 }
85 85
86 SrsApiApi::SrsApiApi() 86 SrsApiApi::SrsApiApi()
@@ -108,7 +108,7 @@ int SrsApiApi::do_process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -108,7 +108,7 @@ int SrsApiApi::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
108 << JOBJECT_END 108 << JOBJECT_END
109 << JOBJECT_END; 109 << JOBJECT_END;
110 110
111 - return res_json(skt, ss.str()); 111 + return res_json(skt, req, ss.str());
112 } 112 }
113 113
114 SrsApiV1::SrsApiV1() 114 SrsApiV1::SrsApiV1()
@@ -138,7 +138,7 @@ int SrsApiV1::do_process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -138,7 +138,7 @@ int SrsApiV1::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
138 << JOBJECT_END 138 << JOBJECT_END
139 << JOBJECT_END; 139 << JOBJECT_END;
140 140
141 - return res_json(skt, ss.str()); 141 + return res_json(skt, req, ss.str());
142 } 142 }
143 143
144 SrsApiVersion::SrsApiVersion() 144 SrsApiVersion::SrsApiVersion()
@@ -168,7 +168,7 @@ int SrsApiVersion::do_process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -168,7 +168,7 @@ int SrsApiVersion::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
168 << JOBJECT_END 168 << JOBJECT_END
169 << JOBJECT_END; 169 << JOBJECT_END;
170 170
171 - return res_json(skt, ss.str()); 171 + return res_json(skt, req, ss.str());
172 } 172 }
173 173
174 SrsApiAuthors::SrsApiAuthors() 174 SrsApiAuthors::SrsApiAuthors()
@@ -197,7 +197,7 @@ int SrsApiAuthors::do_process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -197,7 +197,7 @@ int SrsApiAuthors::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
197 << JOBJECT_END 197 << JOBJECT_END
198 << JOBJECT_END; 198 << JOBJECT_END;
199 199
200 - return res_json(skt, ss.str()); 200 + return res_json(skt, req, ss.str());
201 } 201 }
202 202
203 SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler) 203 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 @@ -205,6 +205,7 @@ SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHan
205 { 205 {
206 parser = new SrsHttpParser(); 206 parser = new SrsHttpParser();
207 handler = _handler; 207 handler = _handler;
  208 + requires_crossdomain = false;
208 } 209 }
209 210
210 SrsHttpApi::~SrsHttpApi() 211 SrsHttpApi::~SrsHttpApi()
@@ -284,6 +285,7 @@ int SrsHttpApi::process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -284,6 +285,7 @@ int SrsHttpApi::process_request(SrsSocket* skt, SrsHttpMessage* req)
284 srs_info("best match handler, matched_url=%s", p->matched_url.c_str()); 285 srs_info("best match handler, matched_url=%s", p->matched_url.c_str());
285 286
286 req->set_match(p); 287 req->set_match(p);
  288 + req->set_requires_crossdomain(requires_crossdomain);
287 289
288 // use handler to process request. 290 // use handler to process request.
289 if ((ret = p->handler->process_request(skt, req)) != ERROR_SUCCESS) { 291 if ((ret = p->handler->process_request(skt, req)) != ERROR_SUCCESS) {
@@ -291,6 +293,10 @@ int SrsHttpApi::process_request(SrsSocket* skt, SrsHttpMessage* req) @@ -291,6 +293,10 @@ int SrsHttpApi::process_request(SrsSocket* skt, SrsHttpMessage* req)
291 return ret; 293 return ret;
292 } 294 }
293 295
  296 + if (req->requires_crossdomain()) {
  297 + requires_crossdomain = true;
  298 + }
  299 +
294 return ret; 300 return ret;
295 } 301 }
296 302
@@ -98,6 +98,7 @@ class SrsHttpApi : public SrsConnection @@ -98,6 +98,7 @@ class SrsHttpApi : public SrsConnection
98 private: 98 private:
99 SrsHttpParser* parser; 99 SrsHttpParser* parser;
100 SrsHttpHandler* handler; 100 SrsHttpHandler* handler;
  101 + bool requires_crossdomain;
101 public: 102 public:
102 SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler); 103 SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler);
103 virtual ~SrsHttpApi(); 104 virtual ~SrsHttpApi();