mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
refine the http request code.
This commit is contained in:
parent
025b707330
commit
6fccfa061d
2 changed files with 62 additions and 60 deletions
|
@ -236,14 +236,6 @@ ISrsHttpResponseReader::~ISrsHttpResponseReader()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
ISrsHttpResponseAppender::ISrsHttpResponseAppender()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
ISrsHttpResponseAppender::~ISrsHttpResponseAppender()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
ISrsHttpHandler::ISrsHttpHandler()
|
ISrsHttpHandler::ISrsHttpHandler()
|
||||||
{
|
{
|
||||||
entry = NULL;
|
entry = NULL;
|
||||||
|
@ -862,10 +854,26 @@ SrsHttpResponseReader::~SrsHttpResponseReader()
|
||||||
srs_freep(cache);
|
srs_freep(cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SrsHttpResponseReader::empty()
|
||||||
|
{
|
||||||
|
return cache->length() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsHttpResponseReader::append(char* data, int size)
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
cache->append(data, size);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
int SrsHttpResponseReader::read(int max, std::string& data)
|
int SrsHttpResponseReader::read(int max, std::string& data)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
// TODO: FIXME: decode the chunked bytes.
|
||||||
|
|
||||||
// read from cache first.
|
// read from cache first.
|
||||||
if (cache->length() > 0) {
|
if (cache->length() > 0) {
|
||||||
int nb_bytes = srs_min(cache->length(), max);
|
int nb_bytes = srs_min(cache->length(), max);
|
||||||
|
@ -894,15 +902,6 @@ int SrsHttpResponseReader::read(int max, std::string& data)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsHttpResponseReader::append(char* data, int size)
|
|
||||||
{
|
|
||||||
int ret = ERROR_SUCCESS;
|
|
||||||
|
|
||||||
cache->append(data, size);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
SrsHttpMessage::SrsHttpMessage(SrsStSocket* io)
|
SrsHttpMessage::SrsHttpMessage(SrsStSocket* io)
|
||||||
{
|
{
|
||||||
_uri = new SrsHttpUri();
|
_uri = new SrsHttpUri();
|
||||||
|
@ -917,10 +916,19 @@ SrsHttpMessage::~SrsHttpMessage()
|
||||||
srs_freep(_http_ts_send_buffer);
|
srs_freep(_http_ts_send_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsHttpMessage::initialize()
|
int SrsHttpMessage::initialize(string url, http_parser* header, string body, vector<SrsHttpHeaderField>& headers)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
_url = url;
|
||||||
|
_header = *header;
|
||||||
|
_headers = headers;
|
||||||
|
|
||||||
|
if (!body.empty()) {
|
||||||
|
_body->append((char*)body.data(), (int)body.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse uri from url.
|
||||||
std::string host = get_request_header("Host");
|
std::string host = get_request_header("Host");
|
||||||
|
|
||||||
// donot parse the empty host for uri,
|
// donot parse the empty host for uri,
|
||||||
|
@ -1050,24 +1058,25 @@ string SrsHttpMessage::path()
|
||||||
return _uri->get_path();
|
return _uri->get_path();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SrsHttpMessage::set(string url, http_parser* header, string body, vector<SrsHttpHeaderField>& headers)
|
|
||||||
{
|
|
||||||
_url = url;
|
|
||||||
_header = *header;
|
|
||||||
_headers = headers;
|
|
||||||
|
|
||||||
if (!body.empty()) {
|
|
||||||
_body->append((char*)body.data(), (int)body.length());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int SrsHttpMessage::body_read_all(string body)
|
int SrsHttpMessage::body_read_all(string body)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
int64_t content_length = (int64_t)_header.content_length;
|
||||||
|
|
||||||
|
// ignore if not set, should be zero length body.
|
||||||
|
if (content_length < 0) {
|
||||||
|
if (!_body->empty()) {
|
||||||
|
srs_warn("unspecified content-length with body cached.");
|
||||||
|
} else {
|
||||||
|
srs_info("unspecified content-length with body empty.");
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
// when content length specified, read specified length.
|
// when content length specified, read specified length.
|
||||||
if ((int64_t)_header.content_length > 0) {
|
if (content_length > 0) {
|
||||||
int left = (int)(int64_t)_header.content_length;
|
int left = (int)content_length;
|
||||||
while (left > 0) {
|
while (left > 0) {
|
||||||
int nb_read = (int)body.length();
|
int nb_read = (int)body.length();
|
||||||
if ((ret = _body->read(left, body)) != ERROR_SUCCESS) {
|
if ((ret = _body->read(left, body)) != ERROR_SUCCESS) {
|
||||||
|
@ -1201,11 +1210,8 @@ int SrsHttpParser::parse_message(SrsStSocket* skt, SrsHttpMessage** ppmsg)
|
||||||
// create msg
|
// create msg
|
||||||
SrsHttpMessage* msg = new SrsHttpMessage(skt);
|
SrsHttpMessage* msg = new SrsHttpMessage(skt);
|
||||||
|
|
||||||
// dumps the header and body read.
|
|
||||||
msg->set(url, &header, body, headers);
|
|
||||||
|
|
||||||
// initalize http msg, parse url.
|
// initalize http msg, parse url.
|
||||||
if ((ret = msg->initialize()) != ERROR_SUCCESS) {
|
if ((ret = msg->initialize(url, &header, body, headers)) != ERROR_SUCCESS) {
|
||||||
srs_error("initialize http msg failed. ret=%d", ret);
|
srs_error("initialize http msg failed. ret=%d", ret);
|
||||||
srs_freep(msg);
|
srs_freep(msg);
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -1325,7 +1331,7 @@ int SrsHttpParser::on_header_field(http_parser* parser, const char* at, size_t l
|
||||||
if (!obj->expect_filed_name) {
|
if (!obj->expect_filed_name) {
|
||||||
obj->headers.push_back(std::make_pair(obj->filed_name, obj->field_value));
|
obj->headers.push_back(std::make_pair(obj->filed_name, obj->field_value));
|
||||||
|
|
||||||
// reset the field name when value parsed.
|
// reset the field name when parsed.
|
||||||
obj->filed_name = "";
|
obj->filed_name = "";
|
||||||
obj->field_value = "";
|
obj->field_value = "";
|
||||||
}
|
}
|
||||||
|
@ -1335,7 +1341,7 @@ int SrsHttpParser::on_header_field(http_parser* parser, const char* at, size_t l
|
||||||
obj->filed_name.append(at, (int)length);
|
obj->filed_name.append(at, (int)length);
|
||||||
}
|
}
|
||||||
|
|
||||||
srs_trace("Header field: %.*s", (int)length, at);
|
srs_trace("Header field(%d bytes): %.*s", (int)length, (int)length, at);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1349,7 +1355,7 @@ int SrsHttpParser::on_header_value(http_parser* parser, const char* at, size_t l
|
||||||
}
|
}
|
||||||
obj->expect_filed_name = false;
|
obj->expect_filed_name = false;
|
||||||
|
|
||||||
srs_trace("Header value: %.*s", (int)length, at);
|
srs_trace("Header value(%d bytes): %.*s", (int)length, (int)length, at);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -197,23 +197,6 @@ public:
|
||||||
virtual int read(int max, std::string& data) = 0;
|
virtual int read(int max, std::string& data) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* for connection response only.
|
|
||||||
*/
|
|
||||||
class ISrsHttpResponseAppender
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
ISrsHttpResponseAppender();
|
|
||||||
virtual ~ISrsHttpResponseAppender();
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* append specified size of bytes data to reader.
|
|
||||||
* when we read http message from socket, we maybe read header+body,
|
|
||||||
* so the reader should provides stream cache feature.
|
|
||||||
*/
|
|
||||||
virtual int append(char* data, int size) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Objects implementing the Handler interface can be
|
// Objects implementing the Handler interface can be
|
||||||
// registered to serve a particular path or subtree
|
// registered to serve a particular path or subtree
|
||||||
// in the HTTP server.
|
// in the HTTP server.
|
||||||
|
@ -406,7 +389,6 @@ public:
|
||||||
* response reader use st socket.
|
* response reader use st socket.
|
||||||
*/
|
*/
|
||||||
class SrsHttpResponseReader : virtual public ISrsHttpResponseReader
|
class SrsHttpResponseReader : virtual public ISrsHttpResponseReader
|
||||||
, virtual public ISrsHttpResponseAppender
|
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
SrsStSocket* skt;
|
SrsStSocket* skt;
|
||||||
|
@ -416,8 +398,19 @@ public:
|
||||||
SrsHttpResponseReader(SrsHttpMessage* msg, SrsStSocket* io);
|
SrsHttpResponseReader(SrsHttpMessage* msg, SrsStSocket* io);
|
||||||
virtual ~SrsHttpResponseReader();
|
virtual ~SrsHttpResponseReader();
|
||||||
public:
|
public:
|
||||||
virtual int read(int max, std::string& data);
|
/**
|
||||||
|
* whether the cache is empty.
|
||||||
|
*/
|
||||||
|
virtual bool empty();
|
||||||
|
/**
|
||||||
|
* append specified size of bytes data to reader.
|
||||||
|
* when we read http message from socket, we maybe read header+body,
|
||||||
|
* so the reader should provides stream cache feature.
|
||||||
|
*/
|
||||||
virtual int append(char* data, int size);
|
virtual int append(char* data, int size);
|
||||||
|
// interface ISrsHttpResponseReader
|
||||||
|
public:
|
||||||
|
virtual int read(int max, std::string& data);
|
||||||
};
|
};
|
||||||
|
|
||||||
// for http header.
|
// for http header.
|
||||||
|
@ -464,7 +457,12 @@ public:
|
||||||
SrsHttpMessage(SrsStSocket* io);
|
SrsHttpMessage(SrsStSocket* io);
|
||||||
virtual ~SrsHttpMessage();
|
virtual ~SrsHttpMessage();
|
||||||
public:
|
public:
|
||||||
virtual int initialize();
|
/**
|
||||||
|
* set the original messages, then initialize the message.
|
||||||
|
*/
|
||||||
|
virtual int initialize(std::string url, http_parser* header,
|
||||||
|
std::string body, std::vector<SrsHttpHeaderField>& headers
|
||||||
|
);
|
||||||
public:
|
public:
|
||||||
virtual char* http_ts_send_buffer();
|
virtual char* http_ts_send_buffer();
|
||||||
public:
|
public:
|
||||||
|
@ -480,8 +478,6 @@ public:
|
||||||
virtual std::string url();
|
virtual std::string url();
|
||||||
virtual std::string host();
|
virtual std::string host();
|
||||||
virtual std::string path();
|
virtual std::string path();
|
||||||
public:
|
|
||||||
virtual void set(std::string url, http_parser* header, std::string body, std::vector<SrsHttpHeaderField>& headers);
|
|
||||||
public:
|
public:
|
||||||
virtual int body_read_all(std::string body);
|
virtual int body_read_all(std::string body);
|
||||||
virtual int64_t content_length();
|
virtual int64_t content_length();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue