1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #1181, Refine code to write utest

This commit is contained in:
winlin 2018-08-11 12:33:03 +08:00
parent c582590bb0
commit 01d8bba455
5 changed files with 44 additions and 28 deletions

View file

@ -1404,16 +1404,19 @@ srs_error_t SrsHttpApi::do_cycle()
ISrsHttpMessage* req = NULL; ISrsHttpMessage* req = NULL;
// get a http message // get a http message
if ((err = parser->parse_message(skt, this, &req)) != srs_success) { if ((err = parser->parse_message(skt, &req)) != srs_success) {
return srs_error_wrap(err, "parse message"); return srs_error_wrap(err, "parse message");
} }
// if SUCCESS, always NOT-NULL. // if SUCCESS, always NOT-NULL.
srs_assert(req);
// always free it in this scope. // always free it in this scope.
srs_assert(req);
SrsAutoFree(ISrsHttpMessage, req); SrsAutoFree(ISrsHttpMessage, req);
// Attach owner connection to message.
SrsHttpMessage* hreq = (SrsHttpMessage*)req;
hreq->set_connection(this);
// ok, handle http request. // ok, handle http request.
SrsHttpResponseWriter writer(skt); SrsHttpResponseWriter writer(skt);
if ((err = process_request(&writer, req)) != srs_success) { if ((err = process_request(&writer, req)) != srs_success) {

View file

@ -123,19 +123,21 @@ srs_error_t SrsHttpConn::do_cycle()
ISrsHttpMessage* req = NULL; ISrsHttpMessage* req = NULL;
// get a http message // get a http message
if ((err = parser->parse_message(skt, this, &req)) != srs_success) { if ((err = parser->parse_message(skt, &req)) != srs_success) {
break; break;
} }
// if SUCCESS, always NOT-NULL. // if SUCCESS, always NOT-NULL.
srs_assert(req);
// always free it in this scope. // always free it in this scope.
srs_assert(req);
SrsAutoFree(ISrsHttpMessage, req); SrsAutoFree(ISrsHttpMessage, req);
// Attach owner connection to message.
SrsHttpMessage* hreq = (SrsHttpMessage*)req;
hreq->set_connection(this);
// copy request to last request object. // copy request to last request object.
srs_freep(last_req); srs_freep(last_req);
SrsHttpMessage* hreq = dynamic_cast<SrsHttpMessage*>(req);
last_req = hreq->to_request(hreq->host()); last_req = hreq->to_request(hreq->host());
// may should discard the body. // may should discard the body.
@ -218,10 +220,14 @@ srs_error_t SrsResponseOnlyHttpConn::pop_message(ISrsHttpMessage** preq)
return srs_error_wrap(err, "init socket"); return srs_error_wrap(err, "init socket");
} }
if ((err = parser->parse_message(&skt, this, preq)) != srs_success) { if ((err = parser->parse_message(&skt, preq)) != srs_success) {
return srs_error_wrap(err, "parse message"); return srs_error_wrap(err, "parse message");
} }
// Attach owner connection to message.
SrsHttpMessage* hreq = (SrsHttpMessage*)(*preq);
hreq->set_connection(this);
return err; return err;
} }

View file

@ -124,7 +124,7 @@ srs_error_t SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg
} }
ISrsHttpMessage* msg = NULL; ISrsHttpMessage* msg = NULL;
if ((err = parser->parse_message(transport, NULL, &msg)) != srs_success) { if ((err = parser->parse_message(transport, &msg)) != srs_success) {
return srs_error_wrap(err, "http: parse response"); return srs_error_wrap(err, "http: parse response");
} }
srs_assert(msg); srs_assert(msg);
@ -170,7 +170,7 @@ srs_error_t SrsHttpClient::get(string path, string req, ISrsHttpMessage** ppmsg)
} }
ISrsHttpMessage* msg = NULL; ISrsHttpMessage* msg = NULL;
if ((err = parser->parse_message(transport, NULL, &msg)) != srs_success) { if ((err = parser->parse_message(transport, &msg)) != srs_success) {
return srs_error_wrap(err, "http: parse response"); return srs_error_wrap(err, "http: parse response");
} }
srs_assert(msg); srs_assert(msg);

View file

@ -68,7 +68,7 @@ srs_error_t SrsHttpParser::initialize(enum http_parser_type type, bool allow_jso
return err; return err;
} }
srs_error_t SrsHttpParser::parse_message(ISrsProtocolReaderWriter* io, SrsConnection* conn, ISrsHttpMessage** ppmsg) srs_error_t SrsHttpParser::parse_message(ISrsReader* reader, ISrsHttpMessage** ppmsg)
{ {
*ppmsg = NULL; *ppmsg = NULL;
@ -85,12 +85,12 @@ srs_error_t SrsHttpParser::parse_message(ISrsProtocolReaderWriter* io, SrsConnec
header_parsed = 0; header_parsed = 0;
// do parse // do parse
if ((err = parse_message_imp(io)) != srs_success) { if ((err = parse_message_imp(reader)) != srs_success) {
return srs_error_wrap(err, "parse message"); return srs_error_wrap(err, "parse message");
} }
// create msg // create msg
SrsHttpMessage* msg = new SrsHttpMessage(io, conn); SrsHttpMessage* msg = new SrsHttpMessage(reader);
// initalize http msg, parse url. // initalize http msg, parse url.
if ((err = msg->update(url, jsonp, &header, buffer, headers)) != srs_success) { if ((err = msg->update(url, jsonp, &header, buffer, headers)) != srs_success) {
@ -104,7 +104,7 @@ srs_error_t SrsHttpParser::parse_message(ISrsProtocolReaderWriter* io, SrsConnec
return err; return err;
} }
srs_error_t SrsHttpParser::parse_message_imp(ISrsProtocolReaderWriter* io) srs_error_t SrsHttpParser::parse_message_imp(ISrsReader* reader)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
@ -138,7 +138,7 @@ srs_error_t SrsHttpParser::parse_message_imp(ISrsProtocolReaderWriter* io)
// when nothing parsed, read more to parse. // when nothing parsed, read more to parse.
if (nparsed == 0) { if (nparsed == 0) {
// when requires more, only grow 1bytes, but the buffer will cache more. // when requires more, only grow 1bytes, but the buffer will cache more.
if ((err = buffer->grow(io, buffer->size() + 1)) != srs_success) { if ((err = buffer->grow(reader, buffer->size() + 1)) != srs_success) {
return srs_error_wrap(err, "grow buffer"); return srs_error_wrap(err, "grow buffer");
} }
} }
@ -254,14 +254,14 @@ int SrsHttpParser::on_body(http_parser* parser, const char* at, size_t length)
return 0; return 0;
} }
SrsHttpMessage::SrsHttpMessage(ISrsProtocolReaderWriter* io, SrsConnection* c) : ISrsHttpMessage() SrsHttpMessage::SrsHttpMessage(ISrsReader* reader) : ISrsHttpMessage()
{ {
conn = c; owner_conn = NULL;
chunked = false; chunked = false;
infinite_chunked = false; infinite_chunked = false;
keep_alive = true; keep_alive = true;
_uri = new SrsHttpUri(); _uri = new SrsHttpUri();
_body = new SrsHttpResponseReader(this, io); _body = new SrsHttpResponseReader(this, reader);
_http_ts_send_buffer = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE]; _http_ts_send_buffer = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE];
jsonp = false; jsonp = false;
} }
@ -329,7 +329,12 @@ srs_error_t SrsHttpMessage::update(string url, bool allow_jsonp, http_parser* he
SrsConnection* SrsHttpMessage::connection() SrsConnection* SrsHttpMessage::connection()
{ {
return conn; return owner_conn;
}
void SrsHttpMessage::set_connection(SrsConnection* conn)
{
owner_conn = conn;
} }
uint8_t SrsHttpMessage::method() uint8_t SrsHttpMessage::method()
@ -842,9 +847,9 @@ srs_error_t SrsHttpResponseWriter::send_header(char* data, int size)
return skt->write((void*)buf.c_str(), buf.length(), NULL); return skt->write((void*)buf.c_str(), buf.length(), NULL);
} }
SrsHttpResponseReader::SrsHttpResponseReader(SrsHttpMessage* msg, ISrsProtocolReaderWriter* io) SrsHttpResponseReader::SrsHttpResponseReader(SrsHttpMessage* msg, ISrsReader* reader)
{ {
skt = io; skt = reader;
owner = msg; owner = msg;
is_eof = false; is_eof = false;
nb_total_read = 0; nb_total_read = 0;

View file

@ -30,10 +30,10 @@
#include <srs_http_stack.hpp> #include <srs_http_stack.hpp>
class ISrsProtocolReaderWriter;
class SrsConnection; class SrsConnection;
class SrsFastStream; class SrsFastStream;
class SrsRequest; class SrsRequest;
class ISrsReader;
class SrsHttpResponseReader; class SrsHttpResponseReader;
class SrsStSocket; class SrsStSocket;
@ -77,12 +77,12 @@ public:
* @remark, if success, *ppmsg always NOT-NULL, *ppmsg always is_complete(). * @remark, if success, *ppmsg always NOT-NULL, *ppmsg always is_complete().
* @remark user must free the ppmsg if not NULL. * @remark user must free the ppmsg if not NULL.
*/ */
virtual srs_error_t parse_message(ISrsProtocolReaderWriter* io, SrsConnection* conn, ISrsHttpMessage** ppmsg); virtual srs_error_t parse_message(ISrsReader* reader, ISrsHttpMessage** ppmsg);
private: private:
/** /**
* parse the HTTP message to member field: msg. * parse the HTTP message to member field: msg.
*/ */
virtual srs_error_t parse_message_imp(ISrsProtocolReaderWriter* io); virtual srs_error_t parse_message_imp(ISrsReader* reader);
private: private:
static int on_message_begin(http_parser* parser); static int on_message_begin(http_parser* parser);
static int on_headers_complete(http_parser* parser); static int on_headers_complete(http_parser* parser);
@ -149,13 +149,13 @@ private:
// the query map // the query map
std::map<std::string, std::string> _query; std::map<std::string, std::string> _query;
// the transport connection, can be NULL. // the transport connection, can be NULL.
SrsConnection* conn; SrsConnection* owner_conn;
// whether request is jsonp. // whether request is jsonp.
bool jsonp; bool jsonp;
// the method in QueryString will override the HTTP method. // the method in QueryString will override the HTTP method.
std::string jsonp_method; std::string jsonp_method;
public: public:
SrsHttpMessage(ISrsProtocolReaderWriter* io, SrsConnection* c); SrsHttpMessage(ISrsReader* io);
virtual ~SrsHttpMessage(); virtual ~SrsHttpMessage();
public: public:
/** /**
@ -163,7 +163,9 @@ public:
*/ */
virtual srs_error_t update(std::string url, bool allow_jsonp, http_parser* header, SrsFastStream* body, std::vector<SrsHttpHeaderField>& headers); virtual srs_error_t update(std::string url, bool allow_jsonp, http_parser* header, SrsFastStream* body, std::vector<SrsHttpHeaderField>& headers);
public: public:
// Get the owner connection, maybe NULL.
virtual SrsConnection* connection(); virtual SrsConnection* connection();
virtual void set_connection(SrsConnection* conn);
public: public:
virtual uint8_t method(); virtual uint8_t method();
virtual uint16_t status_code(); virtual uint16_t status_code();
@ -295,7 +297,7 @@ public:
class SrsHttpResponseReader : virtual public ISrsHttpResponseReader class SrsHttpResponseReader : virtual public ISrsHttpResponseReader
{ {
private: private:
ISrsProtocolReaderWriter* skt; ISrsReader* skt;
SrsHttpMessage* owner; SrsHttpMessage* owner;
SrsFastStream* buffer; SrsFastStream* buffer;
bool is_eof; bool is_eof;
@ -306,7 +308,7 @@ private:
// already read total bytes. // already read total bytes.
int64_t nb_total_read; int64_t nb_total_read;
public: public:
SrsHttpResponseReader(SrsHttpMessage* msg, ISrsProtocolReaderWriter* io); SrsHttpResponseReader(SrsHttpMessage* msg, ISrsReader* reader);
virtual ~SrsHttpResponseReader(); virtual ~SrsHttpResponseReader();
public: public:
/** /**