winlin

refine the http request code.

@@ -236,14 +236,6 @@ ISrsHttpResponseReader::~ISrsHttpResponseReader() @@ -236,14 +236,6 @@ ISrsHttpResponseReader::~ISrsHttpResponseReader()
236 { 236 {
237 } 237 }
238 238
239 -ISrsHttpResponseAppender::ISrsHttpResponseAppender()  
240 -{  
241 -}  
242 -  
243 -ISrsHttpResponseAppender::~ISrsHttpResponseAppender()  
244 -{  
245 -}  
246 -  
247 ISrsHttpHandler::ISrsHttpHandler() 239 ISrsHttpHandler::ISrsHttpHandler()
248 { 240 {
249 entry = NULL; 241 entry = NULL;
@@ -862,10 +854,26 @@ SrsHttpResponseReader::~SrsHttpResponseReader() @@ -862,10 +854,26 @@ SrsHttpResponseReader::~SrsHttpResponseReader()
862 srs_freep(cache); 854 srs_freep(cache);
863 } 855 }
864 856
  857 +bool SrsHttpResponseReader::empty()
  858 +{
  859 + return cache->length() == 0;
  860 +}
  861 +
  862 +int SrsHttpResponseReader::append(char* data, int size)
  863 +{
  864 + int ret = ERROR_SUCCESS;
  865 +
  866 + cache->append(data, size);
  867 +
  868 + return ret;
  869 +}
  870 +
865 int SrsHttpResponseReader::read(int max, std::string& data) 871 int SrsHttpResponseReader::read(int max, std::string& data)
866 { 872 {
867 int ret = ERROR_SUCCESS; 873 int ret = ERROR_SUCCESS;
868 874
  875 + // TODO: FIXME: decode the chunked bytes.
  876 +
869 // read from cache first. 877 // read from cache first.
870 if (cache->length() > 0) { 878 if (cache->length() > 0) {
871 int nb_bytes = srs_min(cache->length(), max); 879 int nb_bytes = srs_min(cache->length(), max);
@@ -894,15 +902,6 @@ int SrsHttpResponseReader::read(int max, std::string& data) @@ -894,15 +902,6 @@ int SrsHttpResponseReader::read(int max, std::string& data)
894 return ret; 902 return ret;
895 } 903 }
896 904
897 -int SrsHttpResponseReader::append(char* data, int size)  
898 -{  
899 - int ret = ERROR_SUCCESS;  
900 -  
901 - cache->append(data, size);  
902 -  
903 - return ret;  
904 -}  
905 -  
906 SrsHttpMessage::SrsHttpMessage(SrsStSocket* io) 905 SrsHttpMessage::SrsHttpMessage(SrsStSocket* io)
907 { 906 {
908 _uri = new SrsHttpUri(); 907 _uri = new SrsHttpUri();
@@ -917,10 +916,19 @@ SrsHttpMessage::~SrsHttpMessage() @@ -917,10 +916,19 @@ SrsHttpMessage::~SrsHttpMessage()
917 srs_freep(_http_ts_send_buffer); 916 srs_freep(_http_ts_send_buffer);
918 } 917 }
919 918
920 -int SrsHttpMessage::initialize() 919 +int SrsHttpMessage::initialize(string url, http_parser* header, string body, vector<SrsHttpHeaderField>& headers)
921 { 920 {
922 int ret = ERROR_SUCCESS; 921 int ret = ERROR_SUCCESS;
923 922
  923 + _url = url;
  924 + _header = *header;
  925 + _headers = headers;
  926 +
  927 + if (!body.empty()) {
  928 + _body->append((char*)body.data(), (int)body.length());
  929 + }
  930 +
  931 + // parse uri from url.
924 std::string host = get_request_header("Host"); 932 std::string host = get_request_header("Host");
925 933
926 // donot parse the empty host for uri, 934 // donot parse the empty host for uri,
@@ -1050,24 +1058,25 @@ string SrsHttpMessage::path() @@ -1050,24 +1058,25 @@ string SrsHttpMessage::path()
1050 return _uri->get_path(); 1058 return _uri->get_path();
1051 } 1059 }
1052 1060
1053 -void SrsHttpMessage::set(string url, http_parser* header, string body, vector<SrsHttpHeaderField>& headers)  
1054 -{  
1055 - _url = url;  
1056 - _header = *header;  
1057 - _headers = headers;  
1058 -  
1059 - if (!body.empty()) {  
1060 - _body->append((char*)body.data(), (int)body.length());  
1061 - }  
1062 -}  
1063 -  
1064 int SrsHttpMessage::body_read_all(string body) 1061 int SrsHttpMessage::body_read_all(string body)
1065 { 1062 {
1066 int ret = ERROR_SUCCESS; 1063 int ret = ERROR_SUCCESS;
1067 1064
  1065 + int64_t content_length = (int64_t)_header.content_length;
  1066 +
  1067 + // ignore if not set, should be zero length body.
  1068 + if (content_length < 0) {
  1069 + if (!_body->empty()) {
  1070 + srs_warn("unspecified content-length with body cached.");
  1071 + } else {
  1072 + srs_info("unspecified content-length with body empty.");
  1073 + }
  1074 + return ret;
  1075 + }
  1076 +
1068 // when content length specified, read specified length. 1077 // when content length specified, read specified length.
1069 - if ((int64_t)_header.content_length > 0) {  
1070 - int left = (int)(int64_t)_header.content_length; 1078 + if (content_length > 0) {
  1079 + int left = (int)content_length;
1071 while (left > 0) { 1080 while (left > 0) {
1072 int nb_read = (int)body.length(); 1081 int nb_read = (int)body.length();
1073 if ((ret = _body->read(left, body)) != ERROR_SUCCESS) { 1082 if ((ret = _body->read(left, body)) != ERROR_SUCCESS) {
@@ -1201,11 +1210,8 @@ int SrsHttpParser::parse_message(SrsStSocket* skt, SrsHttpMessage** ppmsg) @@ -1201,11 +1210,8 @@ int SrsHttpParser::parse_message(SrsStSocket* skt, SrsHttpMessage** ppmsg)
1201 // create msg 1210 // create msg
1202 SrsHttpMessage* msg = new SrsHttpMessage(skt); 1211 SrsHttpMessage* msg = new SrsHttpMessage(skt);
1203 1212
1204 - // dumps the header and body read.  
1205 - msg->set(url, &header, body, headers);  
1206 -  
1207 // initalize http msg, parse url. 1213 // initalize http msg, parse url.
1208 - if ((ret = msg->initialize()) != ERROR_SUCCESS) { 1214 + if ((ret = msg->initialize(url, &header, body, headers)) != ERROR_SUCCESS) {
1209 srs_error("initialize http msg failed. ret=%d", ret); 1215 srs_error("initialize http msg failed. ret=%d", ret);
1210 srs_freep(msg); 1216 srs_freep(msg);
1211 return ret; 1217 return ret;
@@ -1325,7 +1331,7 @@ int SrsHttpParser::on_header_field(http_parser* parser, const char* at, size_t l @@ -1325,7 +1331,7 @@ int SrsHttpParser::on_header_field(http_parser* parser, const char* at, size_t l
1325 if (!obj->expect_filed_name) { 1331 if (!obj->expect_filed_name) {
1326 obj->headers.push_back(std::make_pair(obj->filed_name, obj->field_value)); 1332 obj->headers.push_back(std::make_pair(obj->filed_name, obj->field_value));
1327 1333
1328 - // reset the field name when value parsed. 1334 + // reset the field name when parsed.
1329 obj->filed_name = ""; 1335 obj->filed_name = "";
1330 obj->field_value = ""; 1336 obj->field_value = "";
1331 } 1337 }
@@ -1335,7 +1341,7 @@ int SrsHttpParser::on_header_field(http_parser* parser, const char* at, size_t l @@ -1335,7 +1341,7 @@ int SrsHttpParser::on_header_field(http_parser* parser, const char* at, size_t l
1335 obj->filed_name.append(at, (int)length); 1341 obj->filed_name.append(at, (int)length);
1336 } 1342 }
1337 1343
1338 - srs_trace("Header field: %.*s", (int)length, at); 1344 + srs_trace("Header field(%d bytes): %.*s", (int)length, (int)length, at);
1339 return 0; 1345 return 0;
1340 } 1346 }
1341 1347
@@ -1349,7 +1355,7 @@ int SrsHttpParser::on_header_value(http_parser* parser, const char* at, size_t l @@ -1349,7 +1355,7 @@ int SrsHttpParser::on_header_value(http_parser* parser, const char* at, size_t l
1349 } 1355 }
1350 obj->expect_filed_name = false; 1356 obj->expect_filed_name = false;
1351 1357
1352 - srs_trace("Header value: %.*s", (int)length, at); 1358 + srs_trace("Header value(%d bytes): %.*s", (int)length, (int)length, at);
1353 return 0; 1359 return 0;
1354 } 1360 }
1355 1361
@@ -197,23 +197,6 @@ public: @@ -197,23 +197,6 @@ public:
197 virtual int read(int max, std::string& data) = 0; 197 virtual int read(int max, std::string& data) = 0;
198 }; 198 };
199 199
200 -/**  
201 -* for connection response only.  
202 -*/  
203 -class ISrsHttpResponseAppender  
204 -{  
205 -public:  
206 - ISrsHttpResponseAppender();  
207 - virtual ~ISrsHttpResponseAppender();  
208 -public:  
209 - /**  
210 - * append specified size of bytes data to reader.  
211 - * when we read http message from socket, we maybe read header+body,  
212 - * so the reader should provides stream cache feature.  
213 - */  
214 - virtual int append(char* data, int size) = 0;  
215 -};  
216 -  
217 // Objects implementing the Handler interface can be 200 // Objects implementing the Handler interface can be
218 // registered to serve a particular path or subtree 201 // registered to serve a particular path or subtree
219 // in the HTTP server. 202 // in the HTTP server.
@@ -406,7 +389,6 @@ public: @@ -406,7 +389,6 @@ public:
406 * response reader use st socket. 389 * response reader use st socket.
407 */ 390 */
408 class SrsHttpResponseReader : virtual public ISrsHttpResponseReader 391 class SrsHttpResponseReader : virtual public ISrsHttpResponseReader
409 - , virtual public ISrsHttpResponseAppender  
410 { 392 {
411 private: 393 private:
412 SrsStSocket* skt; 394 SrsStSocket* skt;
@@ -416,8 +398,19 @@ public: @@ -416,8 +398,19 @@ public:
416 SrsHttpResponseReader(SrsHttpMessage* msg, SrsStSocket* io); 398 SrsHttpResponseReader(SrsHttpMessage* msg, SrsStSocket* io);
417 virtual ~SrsHttpResponseReader(); 399 virtual ~SrsHttpResponseReader();
418 public: 400 public:
419 - virtual int read(int max, std::string& data); 401 + /**
  402 + * whether the cache is empty.
  403 + */
  404 + virtual bool empty();
  405 + /**
  406 + * append specified size of bytes data to reader.
  407 + * when we read http message from socket, we maybe read header+body,
  408 + * so the reader should provides stream cache feature.
  409 + */
420 virtual int append(char* data, int size); 410 virtual int append(char* data, int size);
  411 +// interface ISrsHttpResponseReader
  412 +public:
  413 + virtual int read(int max, std::string& data);
421 }; 414 };
422 415
423 // for http header. 416 // for http header.
@@ -464,7 +457,12 @@ public: @@ -464,7 +457,12 @@ public:
464 SrsHttpMessage(SrsStSocket* io); 457 SrsHttpMessage(SrsStSocket* io);
465 virtual ~SrsHttpMessage(); 458 virtual ~SrsHttpMessage();
466 public: 459 public:
467 - virtual int initialize(); 460 + /**
  461 + * set the original messages, then initialize the message.
  462 + */
  463 + virtual int initialize(std::string url, http_parser* header,
  464 + std::string body, std::vector<SrsHttpHeaderField>& headers
  465 + );
468 public: 466 public:
469 virtual char* http_ts_send_buffer(); 467 virtual char* http_ts_send_buffer();
470 public: 468 public:
@@ -481,8 +479,6 @@ public: @@ -481,8 +479,6 @@ public:
481 virtual std::string host(); 479 virtual std::string host();
482 virtual std::string path(); 480 virtual std::string path();
483 public: 481 public:
484 - virtual void set(std::string url, http_parser* header, std::string body, std::vector<SrsHttpHeaderField>& headers);  
485 -public:  
486 virtual int body_read_all(std::string body); 482 virtual int body_read_all(std::string body);
487 virtual int64_t content_length(); 483 virtual int64_t content_length();
488 /** 484 /**