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

For #913, use complex error for http

This commit is contained in:
winlin 2017-07-29 21:39:57 +08:00
parent 661eb8b37c
commit 9f5224c34a
24 changed files with 388 additions and 453 deletions

View file

@ -95,16 +95,16 @@ void SrsHttpConn::cleanup()
// TODO: FIXME: implements it
}
int SrsHttpConn::do_cycle()
srs_error_t SrsHttpConn::do_cycle()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
srs_trace("HTTP client ip=%s", ip.c_str());
// initialize parser
if ((ret = parser->initialize(HTTP_REQUEST, false)) != ERROR_SUCCESS) {
srs_error("http initialize http parser failed. ret=%d", ret);
return ret;
return srs_error_new(ret, "init parser");
}
// set the recv timeout, for some clients never disconnect the connection.
@ -116,20 +116,12 @@ int SrsHttpConn::do_cycle()
// initialize the cors, which will proxy to mux.
bool crossdomain_enabled = _srs_config->get_http_stream_crossdomain();
if ((ret = cors->initialize(http_mux, crossdomain_enabled)) != ERROR_SUCCESS) {
return ret;
if ((err = cors->initialize(http_mux, crossdomain_enabled)) != srs_success) {
return srs_error_wrap(err, "init cors");
}
// process http messages.
while (true) {
srs_error_t err = srs_success;
if ((err = trd->pull()) != srs_success) {
// TODO: FIXME: Use error
ret = srs_error_code(err);
srs_freep(err);
return ret;
}
while ((err = trd->pull()) == srs_success) {
ISrsHttpMessage* req = NULL;
// get a http message
@ -143,19 +135,19 @@ int SrsHttpConn::do_cycle()
// always free it in this scope.
SrsAutoFree(ISrsHttpMessage, req);
// get the last request, for report the info of request on connection disconnect.
delete last_req;
// 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.
if ((ret = on_got_http_message(req)) != ERROR_SUCCESS) {
if ((err = on_got_http_message(req)) != srs_success) {
break;
}
// ok, handle http request.
SrsHttpResponseWriter writer(skt);
if ((ret = process_request(&writer, req)) != ERROR_SUCCESS) {
if ((err = process_request(&writer, req)) != srs_success) {
break;
}
@ -166,50 +158,47 @@ int SrsHttpConn::do_cycle()
}
}
int disc_ret = ERROR_SUCCESS;
if ((disc_ret = on_disconnect(last_req)) != ERROR_SUCCESS) {
srs_warn("connection on disconnect peer failed, but ignore this error. disc_ret=%d, ret=%d", disc_ret, ret);
srs_error_t r0 = srs_success;
if ((r0 = on_disconnect(last_req)) != srs_success) {
err = srs_error_wrap(err, "on disconnect %s", srs_error_desc(r0).c_str());
srs_freep(r0);
}
return ret;
return err;
}
int SrsHttpConn::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
srs_error_t SrsHttpConn::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
srs_trace("HTTP %s %s, content-length=%" PRId64 "",
r->method_str().c_str(), r->url().c_str(), r->content_length());
r->method_str().c_str(), r->url().c_str(), r->content_length());
// use cors server mux to serve http request, which will proxy to http_remux.
if ((ret = cors->serve_http(w, r)) != ERROR_SUCCESS) {
if (!srs_is_client_gracefully_close(ret)) {
srs_error("serve http msg failed. ret=%d", ret);
}
return ret;
if ((err = cors->serve_http(w, r)) != srs_success) {
return srs_error_wrap(err, "mux serve");
}
return ret;
return err;
}
int SrsHttpConn::on_disconnect(SrsRequest* req)
srs_error_t SrsHttpConn::on_disconnect(SrsRequest* req)
{
int ret = ERROR_SUCCESS;
// TODO: implements it.s
return ret;
// TODO: FIXME: Implements it.
return srs_success;
}
int SrsHttpConn::on_reload_http_stream_crossdomain()
srs_error_t SrsHttpConn::on_reload_http_stream_crossdomain()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
// initialize the cors, which will proxy to mux.
bool crossdomain_enabled = _srs_config->get_http_stream_crossdomain();
if ((ret = cors->initialize(http_mux, crossdomain_enabled)) != ERROR_SUCCESS) {
return ret;
if ((err = cors->initialize(http_mux, crossdomain_enabled)) != srs_success) {
return srs_error_wrap(err, "init mux");
}
return ret;
return err;
}
SrsResponseOnlyHttpConn::SrsResponseOnlyHttpConn(IConnectionManager* cm, srs_netfd_t fd, ISrsHttpServeMux* m, string cip)
@ -221,43 +210,45 @@ SrsResponseOnlyHttpConn::~SrsResponseOnlyHttpConn()
{
}
int SrsResponseOnlyHttpConn::pop_message(ISrsHttpMessage** preq)
srs_error_t SrsResponseOnlyHttpConn::pop_message(ISrsHttpMessage** preq)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
SrsStSocket skt;
if ((ret = skt.initialize(stfd)) != ERROR_SUCCESS) {
return ret;
return srs_error_new(ret, "init socket");
}
if ((ret = parser->parse_message(&skt, this, preq)) != ERROR_SUCCESS) {
return ret;
return srs_error_new(ret, "parse message");
}
return ret;
return err;
}
int SrsResponseOnlyHttpConn::on_got_http_message(ISrsHttpMessage* msg)
srs_error_t SrsResponseOnlyHttpConn::on_got_http_message(ISrsHttpMessage* msg)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
ISrsHttpResponseReader* br = msg->body_reader();
// when not specified the content length, ignore.
if (msg->content_length() == -1) {
return ret;
return err;
}
// drop all request body.
while (!br->eof()) {
char body[4096];
if ((ret = br->read(body, 4096, NULL)) != ERROR_SUCCESS) {
return ret;
return srs_error_new(ret, "read response");
}
}
return ret;
return err;
}
SrsHttpServer::SrsHttpServer(SrsServer* svr)
@ -293,10 +284,16 @@ srs_error_t SrsHttpServer::initialize()
return err;
}
int SrsHttpServer::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
srs_error_t SrsHttpServer::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
srs_error_t err = srs_success;
// try http stream first.
if (http_stream->mux.can_serve(r)) {
ISrsHttpHandler* h = NULL;
if ((err = http_stream->mux.find_handler(r, &h)) != srs_success) {
return srs_error_wrap(err, "find handler");
}
if (!h->is_not_found()) {
return http_stream->mux.serve_http(w, r);
}