1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-13 20:01:56 +00:00

HTTP: Only enable infinite chunked for HTTP_REQUEST

This commit is contained in:
winlin 2020-07-03 17:05:01 +08:00
parent 2008aa1a99
commit 671af4369b
4 changed files with 27 additions and 19 deletions

View file

@ -99,7 +99,7 @@ srs_error_t SrsHttpParser::parse_message(ISrsReader* reader, ISrsHttpMessage** p
SrsHttpMessage* msg = new SrsHttpMessage(reader, buffer); SrsHttpMessage* msg = new SrsHttpMessage(reader, buffer);
// Initialize the basic information. // Initialize the basic information.
msg->set_basic(hp_header.method, hp_header.status_code, hp_header.content_length); msg->set_basic(hp_header.type, hp_header.method, hp_header.status_code, hp_header.content_length);
msg->set_header(header, http_should_keep_alive(&hp_header)); msg->set_header(header, http_should_keep_alive(&hp_header));
if ((err = msg->set_url(url, jsonp)) != srs_success) { if ((err = msg->set_url(url, jsonp)) != srs_success) {
srs_freep(msg); srs_freep(msg);
@ -312,8 +312,9 @@ SrsHttpMessage::~SrsHttpMessage()
srs_freep(_uri); srs_freep(_uri);
} }
void SrsHttpMessage::set_basic(uint8_t method, uint16_t status, int64_t content_length) void SrsHttpMessage::set_basic(uint8_t type, uint8_t method, uint16_t status, int64_t content_length)
{ {
type_ = type;
_method = method; _method = method;
_status = status; _status = status;
if (_content_length == -1) { if (_content_length == -1) {
@ -335,10 +336,13 @@ void SrsHttpMessage::set_header(SrsHttpHeader* header, bool keep_alive)
_content_length = ::atoll(clv.c_str()); _content_length = ::atoll(clv.c_str());
} }
// If method is OPTIONS, and no size(content-length or chunked), it's not infinite chunked, // If no size(content-length or chunked), it's infinite chunked,
// it means there is no body, so we must close the body reader. // it means there is no body, so we must close the body reader.
if (_method == SRS_CONSTS_HTTP_OPTIONS && !chunked && _content_length == -1) { if (!chunked && _content_length == -1) {
_body->close(); // The infinite chunked is only enabled for HTTP_RESPONSE, so we close the body for request.
if (type_ == HTTP_REQUEST) {
_body->close();
}
} }
} }

View file

@ -103,6 +103,10 @@ private:
// The transport connection, can be NULL. // The transport connection, can be NULL.
ISrsConnection* owner_conn; ISrsConnection* owner_conn;
private: private:
// The request type defined as
// enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
uint8_t type_;
// The HTTP method defined by HTTP_METHOD_MAP
uint8_t _method; uint8_t _method;
uint16_t _status; uint16_t _status;
int64_t _content_length; int64_t _content_length;
@ -133,7 +137,7 @@ public:
public: public:
// Set the basic information for HTTP request. // Set the basic information for HTTP request.
// @remark User must call set_basic before set_header, because the content_length will be overwrite by header. // @remark User must call set_basic before set_header, because the content_length will be overwrite by header.
virtual void set_basic(uint8_t method, uint16_t status, int64_t content_length); virtual void set_basic(uint8_t type, uint8_t method, uint16_t status, int64_t content_length);
// Set HTTP header and whether the request require keep alive. // Set HTTP header and whether the request require keep alive.
// @remark User must call set_header before set_url, because the Host in header is used for url. // @remark User must call set_header before set_url, because the Host in header is used for url.
virtual void set_header(SrsHttpHeader* header, bool keep_alive); virtual void set_header(SrsHttpHeader* header, bool keep_alive);

View file

@ -991,7 +991,7 @@ VOID TEST(ProtocolHTTPTest, HTTPServerMuxerCORS)
MockResponseWriter w; MockResponseWriter w;
SrsHttpMessage r(NULL, NULL); SrsHttpMessage r(NULL, NULL);
r.set_basic(HTTP_POST, 200, -1); r.set_basic(HTTP_REQUEST, HTTP_POST, 200, -1);
HELPER_ASSERT_SUCCESS(r.set_url("/index.html", false)); HELPER_ASSERT_SUCCESS(r.set_url("/index.html", false));
SrsHttpCorsMux cs; SrsHttpCorsMux cs;
@ -1011,7 +1011,7 @@ VOID TEST(ProtocolHTTPTest, HTTPServerMuxerCORS)
MockResponseWriter w; MockResponseWriter w;
SrsHttpMessage r(NULL, NULL); SrsHttpMessage r(NULL, NULL);
r.set_basic(HTTP_OPTIONS, 200, -1); r.set_basic(HTTP_REQUEST, HTTP_OPTIONS, 200, -1);
HELPER_ASSERT_SUCCESS(r.set_url("/index.html", false)); HELPER_ASSERT_SUCCESS(r.set_url("/index.html", false));
SrsHttpCorsMux cs; SrsHttpCorsMux cs;
@ -1031,7 +1031,7 @@ VOID TEST(ProtocolHTTPTest, HTTPServerMuxerCORS)
MockResponseWriter w; MockResponseWriter w;
SrsHttpMessage r(NULL, NULL); SrsHttpMessage r(NULL, NULL);
r.set_basic(HTTP_POST, 200, -1); r.set_basic(HTTP_REQUEST, HTTP_POST, 200, -1);
HELPER_ASSERT_SUCCESS(r.set_url("/index.html", false)); HELPER_ASSERT_SUCCESS(r.set_url("/index.html", false));
SrsHttpCorsMux cs; SrsHttpCorsMux cs;
@ -1051,7 +1051,7 @@ VOID TEST(ProtocolHTTPTest, HTTPServerMuxerCORS)
MockResponseWriter w; MockResponseWriter w;
SrsHttpMessage r(NULL, NULL); SrsHttpMessage r(NULL, NULL);
r.set_basic(HTTP_OPTIONS, 200, -1); r.set_basic(HTTP_REQUEST, HTTP_OPTIONS, 200, -1);
HELPER_ASSERT_SUCCESS(r.set_url("/index.html", false)); HELPER_ASSERT_SUCCESS(r.set_url("/index.html", false));
SrsHttpCorsMux cs; SrsHttpCorsMux cs;
@ -1662,7 +1662,7 @@ VOID TEST(ProtocolHTTPTest, HTTPMessageUpdate)
SrsHttpMessage m; SrsHttpMessage m;
m.set_header(&h, false); m.set_header(&h, false);
m.set_basic(HTTP_POST, 200, 2); m.set_basic(HTTP_REQUEST, HTTP_POST, 200, 2);
EXPECT_EQ(10, m.content_length()); EXPECT_EQ(10, m.content_length());
} }
@ -1672,7 +1672,7 @@ VOID TEST(ProtocolHTTPTest, HTTPMessageUpdate)
h.set("Content-Length", "10"); h.set("Content-Length", "10");
SrsHttpMessage m; SrsHttpMessage m;
m.set_basic(HTTP_POST, 200, 2); m.set_basic(HTTP_REQUEST, HTTP_POST, 200, 2);
m.set_header(&h, false); m.set_header(&h, false);
EXPECT_EQ(10, m.content_length()); EXPECT_EQ(10, m.content_length());
} }
@ -1706,7 +1706,7 @@ VOID TEST(ProtocolHTTPTest, HTTPMessageUpdate)
if (true) { if (true) {
SrsHttpMessage m; SrsHttpMessage m;
m.set_basic(HTTP_POST, 200, 0); m.set_basic(HTTP_REQUEST, HTTP_POST, 200, 0);
EXPECT_EQ(0, m.content_length()); EXPECT_EQ(0, m.content_length());
} }

View file

@ -523,12 +523,12 @@ VOID TEST(HTTPServerTest, MessageConnection)
if (true) { if (true) {
SrsHttpMessage m; SrsHttpMessage m;
m.set_basic(100, 0, 0); EXPECT_STREQ("OTHER", m.method_str().c_str()); m.set_basic(HTTP_REQUEST, 100, 0, 0); EXPECT_STREQ("OTHER", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_GET, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_GET, m.method()); EXPECT_STREQ("GET", m.method_str().c_str()); m.set_basic(HTTP_REQUEST, SRS_CONSTS_HTTP_GET, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_GET, m.method()); EXPECT_STREQ("GET", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_PUT, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_PUT, m.method()); EXPECT_STREQ("PUT", m.method_str().c_str()); m.set_basic(HTTP_REQUEST, SRS_CONSTS_HTTP_PUT, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_PUT, m.method()); EXPECT_STREQ("PUT", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_POST, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_POST, m.method()); EXPECT_STREQ("POST", m.method_str().c_str()); m.set_basic(HTTP_REQUEST, SRS_CONSTS_HTTP_POST, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_POST, m.method()); EXPECT_STREQ("POST", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_DELETE, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_DELETE, m.method()); EXPECT_STREQ("DELETE", m.method_str().c_str()); m.set_basic(HTTP_REQUEST, SRS_CONSTS_HTTP_DELETE, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_DELETE, m.method()); EXPECT_STREQ("DELETE", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_OPTIONS, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_OPTIONS, m.method()); EXPECT_STREQ("OPTIONS", m.method_str().c_str()); m.set_basic(HTTP_REQUEST, SRS_CONSTS_HTTP_OPTIONS, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_OPTIONS, m.method()); EXPECT_STREQ("OPTIONS", m.method_str().c_str());
} }
if (true) { if (true) {