1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +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;
// 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");
}
// if SUCCESS, always NOT-NULL.
srs_assert(req);
// always free it in this scope.
srs_assert(req);
SrsAutoFree(ISrsHttpMessage, req);
// Attach owner connection to message.
SrsHttpMessage* hreq = (SrsHttpMessage*)req;
hreq->set_connection(this);
// ok, handle http request.
SrsHttpResponseWriter writer(skt);
if ((err = process_request(&writer, req)) != srs_success) {

View file

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

View file

@ -124,7 +124,7 @@ srs_error_t SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg
}
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");
}
srs_assert(msg);
@ -170,7 +170,7 @@ srs_error_t SrsHttpClient::get(string path, string req, ISrsHttpMessage** ppmsg)
}
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");
}
srs_assert(msg);

View file

@ -68,7 +68,7 @@ srs_error_t SrsHttpParser::initialize(enum http_parser_type type, bool allow_jso
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;
@ -85,12 +85,12 @@ srs_error_t SrsHttpParser::parse_message(ISrsProtocolReaderWriter* io, SrsConnec
header_parsed = 0;
// 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");
}
// create msg
SrsHttpMessage* msg = new SrsHttpMessage(io, conn);
SrsHttpMessage* msg = new SrsHttpMessage(reader);
// initalize http msg, parse url.
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;
}
srs_error_t SrsHttpParser::parse_message_imp(ISrsProtocolReaderWriter* io)
srs_error_t SrsHttpParser::parse_message_imp(ISrsReader* reader)
{
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.
if (nparsed == 0) {
// 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");
}
}
@ -254,14 +254,14 @@ int SrsHttpParser::on_body(http_parser* parser, const char* at, size_t length)
return 0;
}
SrsHttpMessage::SrsHttpMessage(ISrsProtocolReaderWriter* io, SrsConnection* c) : ISrsHttpMessage()
SrsHttpMessage::SrsHttpMessage(ISrsReader* reader) : ISrsHttpMessage()
{
conn = c;
owner_conn = NULL;
chunked = false;
infinite_chunked = false;
keep_alive = true;
_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];
jsonp = false;
}
@ -329,7 +329,12 @@ srs_error_t SrsHttpMessage::update(string url, bool allow_jsonp, http_parser* he
SrsConnection* SrsHttpMessage::connection()
{
return conn;
return owner_conn;
}
void SrsHttpMessage::set_connection(SrsConnection* conn)
{
owner_conn = conn;
}
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);
}
SrsHttpResponseReader::SrsHttpResponseReader(SrsHttpMessage* msg, ISrsProtocolReaderWriter* io)
SrsHttpResponseReader::SrsHttpResponseReader(SrsHttpMessage* msg, ISrsReader* reader)
{
skt = io;
skt = reader;
owner = msg;
is_eof = false;
nb_total_read = 0;

View file

@ -30,10 +30,10 @@
#include <srs_http_stack.hpp>
class ISrsProtocolReaderWriter;
class SrsConnection;
class SrsFastStream;
class SrsRequest;
class ISrsReader;
class SrsHttpResponseReader;
class SrsStSocket;
@ -77,12 +77,12 @@ public:
* @remark, if success, *ppmsg always NOT-NULL, *ppmsg always is_complete().
* @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:
/**
* 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:
static int on_message_begin(http_parser* parser);
static int on_headers_complete(http_parser* parser);
@ -149,13 +149,13 @@ private:
// the query map
std::map<std::string, std::string> _query;
// the transport connection, can be NULL.
SrsConnection* conn;
SrsConnection* owner_conn;
// whether request is jsonp.
bool jsonp;
// the method in QueryString will override the HTTP method.
std::string jsonp_method;
public:
SrsHttpMessage(ISrsProtocolReaderWriter* io, SrsConnection* c);
SrsHttpMessage(ISrsReader* io);
virtual ~SrsHttpMessage();
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);
public:
// Get the owner connection, maybe NULL.
virtual SrsConnection* connection();
virtual void set_connection(SrsConnection* conn);
public:
virtual uint8_t method();
virtual uint16_t status_code();
@ -295,7 +297,7 @@ public:
class SrsHttpResponseReader : virtual public ISrsHttpResponseReader
{
private:
ISrsProtocolReaderWriter* skt;
ISrsReader* skt;
SrsHttpMessage* owner;
SrsFastStream* buffer;
bool is_eof;
@ -306,7 +308,7 @@ private:
// already read total bytes.
int64_t nb_total_read;
public:
SrsHttpResponseReader(SrsHttpMessage* msg, ISrsProtocolReaderWriter* io);
SrsHttpResponseReader(SrsHttpMessage* msg, ISrsReader* reader);
virtual ~SrsHttpResponseReader();
public:
/**