2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-12-30 02:10:35 +00:00
|
|
|
* Copyright (c) 2013-2020 Winlin
|
2017-03-25 09:21:39 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2014-03-27 04:14:04 +00:00
|
|
|
|
|
|
|
#ifndef SRS_APP_HTTP_CONN_HPP
|
|
|
|
#define SRS_APP_HTTP_CONN_HPP
|
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
2015-05-22 14:34:03 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2014-04-01 07:42:27 +00:00
|
|
|
|
2017-03-26 05:40:39 +00:00
|
|
|
#include <srs_service_http_conn.hpp>
|
2015-01-18 09:17:07 +00:00
|
|
|
#include <srs_app_reload.hpp>
|
2015-01-18 11:49:03 +00:00
|
|
|
#include <srs_kernel_file.hpp>
|
2020-09-11 08:44:40 +00:00
|
|
|
#include <srs_app_st.hpp>
|
2015-05-22 14:34:03 +00:00
|
|
|
#include <srs_app_conn.hpp>
|
2015-06-10 07:38:13 +00:00
|
|
|
#include <srs_app_source.hpp>
|
2014-04-01 04:36:56 +00:00
|
|
|
|
2015-05-03 15:34:59 +00:00
|
|
|
class SrsServer;
|
2015-01-18 10:39:53 +00:00
|
|
|
class SrsSource;
|
|
|
|
class SrsRequest;
|
2015-01-19 04:24:18 +00:00
|
|
|
class SrsConsumer;
|
2014-07-26 12:08:37 +00:00
|
|
|
class SrsStSocket;
|
2014-04-02 04:03:49 +00:00
|
|
|
class SrsHttpParser;
|
2015-05-22 14:24:05 +00:00
|
|
|
class ISrsHttpMessage;
|
2014-04-02 10:07:34 +00:00
|
|
|
class SrsHttpHandler;
|
2015-01-19 04:56:05 +00:00
|
|
|
class SrsMessageQueue;
|
2015-01-18 11:49:03 +00:00
|
|
|
class SrsSharedPtrMessage;
|
2015-05-22 14:34:03 +00:00
|
|
|
class SrsRequest;
|
2015-09-22 01:01:47 +00:00
|
|
|
class SrsFastStream;
|
2015-05-22 14:34:03 +00:00
|
|
|
class SrsHttpUri;
|
|
|
|
class SrsHttpMessage;
|
2015-06-14 06:06:39 +00:00
|
|
|
class SrsHttpStreamServer;
|
|
|
|
class SrsHttpStaticServer;
|
2015-05-22 14:34:03 +00:00
|
|
|
|
2020-11-05 07:18:13 +00:00
|
|
|
// The owner of HTTP connection.
|
|
|
|
class ISrsHttpConnOwner
|
2020-11-05 07:08:36 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-11-05 07:18:13 +00:00
|
|
|
ISrsHttpConnOwner();
|
|
|
|
virtual ~ISrsHttpConnOwner();
|
2020-11-05 07:08:36 +00:00
|
|
|
public:
|
2020-11-05 09:16:59 +00:00
|
|
|
// When start the coroutine to process connection.
|
|
|
|
virtual srs_error_t on_start() = 0;
|
2020-11-05 10:08:13 +00:00
|
|
|
// Handle the HTTP message r, which may be parsed partially.
|
2020-11-05 07:08:36 +00:00
|
|
|
// For the static service or api, discard any body.
|
|
|
|
// For the stream caster, for instance, http flv streaming, may discard the flv header or not.
|
2020-11-05 10:08:13 +00:00
|
|
|
virtual srs_error_t on_http_message(ISrsHttpMessage* r, SrsHttpResponseWriter* w) = 0;
|
2020-11-05 07:08:36 +00:00
|
|
|
// When connection is destroy, should use manager to dispose it.
|
2020-11-05 10:19:43 +00:00
|
|
|
// The r0 is the original error, we will use the returned new error.
|
|
|
|
virtual srs_error_t on_conn_done(srs_error_t r0) = 0;
|
2020-11-05 07:08:36 +00:00
|
|
|
};
|
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// The http connection which request the static or stream content.
|
2020-11-05 08:46:16 +00:00
|
|
|
class SrsHttpConn : virtual public ISrsStartableConneciton, virtual public ISrsCoroutineHandler
|
|
|
|
, virtual public ISrsExpire
|
2015-01-19 04:24:18 +00:00
|
|
|
{
|
2017-04-30 04:03:31 +00:00
|
|
|
protected:
|
2015-06-14 06:06:39 +00:00
|
|
|
SrsHttpParser* parser;
|
|
|
|
ISrsHttpServeMux* http_mux;
|
2016-12-15 08:22:04 +00:00
|
|
|
SrsHttpCorsMux* cors;
|
2020-11-05 07:18:13 +00:00
|
|
|
ISrsHttpConnOwner* handler_;
|
2020-11-05 03:47:24 +00:00
|
|
|
protected:
|
2020-11-05 04:25:54 +00:00
|
|
|
SrsTcpConnection* skt;
|
2020-11-05 03:47:24 +00:00
|
|
|
// Each connection start a green thread,
|
|
|
|
// when thread stop, the connection will be delete by server.
|
|
|
|
SrsCoroutine* trd;
|
|
|
|
// The ip and port of client.
|
|
|
|
std::string ip;
|
|
|
|
int port;
|
|
|
|
private:
|
|
|
|
// The connection total kbps.
|
|
|
|
// not only the rtmp or http connection, all type of connection are
|
|
|
|
// need to statistic the kbps of io.
|
|
|
|
// The SrsStatistic will use it indirectly to statistic the bytes delta of current connection.
|
|
|
|
SrsKbps* kbps;
|
|
|
|
SrsWallClock* clk;
|
|
|
|
// The create time in milliseconds.
|
|
|
|
// for current connection to log self create time and calculate the living time.
|
|
|
|
int64_t create_time;
|
2015-01-18 14:51:07 +00:00
|
|
|
public:
|
2020-11-05 07:18:13 +00:00
|
|
|
SrsHttpConn(ISrsHttpConnOwner* handler, srs_netfd_t fd, ISrsHttpServeMux* m, std::string cip, int port);
|
2015-06-14 06:06:39 +00:00
|
|
|
virtual ~SrsHttpConn();
|
2020-09-19 02:30:05 +00:00
|
|
|
// Interface ISrsResource.
|
|
|
|
public:
|
|
|
|
virtual std::string desc();
|
2019-04-30 00:30:13 +00:00
|
|
|
// Interface ISrsKbpsDelta
|
2015-01-19 04:24:18 +00:00
|
|
|
public:
|
2019-01-01 09:36:27 +00:00
|
|
|
virtual void remark(int64_t* in, int64_t* out);
|
2020-11-05 08:52:33 +00:00
|
|
|
// Interface ISrsStartable
|
|
|
|
public:
|
|
|
|
virtual srs_error_t start();
|
|
|
|
// Interface ISrsOneCycleThreadHandler
|
|
|
|
public:
|
|
|
|
virtual srs_error_t cycle();
|
2020-11-05 07:08:36 +00:00
|
|
|
private:
|
2017-07-29 13:39:57 +00:00
|
|
|
virtual srs_error_t do_cycle();
|
2020-11-05 08:52:33 +00:00
|
|
|
private:
|
|
|
|
virtual srs_error_t process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
|
|
|
|
// When the connection disconnect, call this method.
|
|
|
|
// e.g. log msg of connection and report to other system.
|
|
|
|
// @param request: request which is converted by the last http message.
|
|
|
|
virtual srs_error_t on_disconnect(SrsRequest* req);
|
2020-11-05 07:08:36 +00:00
|
|
|
public:
|
|
|
|
// Get the HTTP message handler.
|
2020-11-05 07:18:13 +00:00
|
|
|
virtual ISrsHttpConnOwner* handler();
|
2020-11-05 07:08:36 +00:00
|
|
|
// Whether the connection coroutine is error or terminated.
|
|
|
|
virtual srs_error_t pull();
|
2020-11-05 08:46:16 +00:00
|
|
|
// Whether enable the CORS(cross-domain).
|
|
|
|
virtual srs_error_t set_crossdomain_enabled(bool v);
|
|
|
|
// Whether enable the JSONP.
|
|
|
|
virtual srs_error_t set_jsonp(bool v);
|
2020-11-05 03:47:24 +00:00
|
|
|
public:
|
|
|
|
// Set socket option TCP_NODELAY.
|
|
|
|
virtual srs_error_t set_tcp_nodelay(bool v);
|
|
|
|
// Set socket option SO_SNDBUF in srs_utime_t.
|
|
|
|
virtual srs_error_t set_socket_buffer(srs_utime_t buffer_v);
|
|
|
|
// Interface ISrsConnection.
|
|
|
|
public:
|
|
|
|
virtual std::string remote_ip();
|
|
|
|
virtual const SrsContextId& get_id();
|
|
|
|
// Interface ISrsExpire.
|
|
|
|
public:
|
|
|
|
virtual void expire();
|
2015-02-03 08:01:07 +00:00
|
|
|
};
|
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// Drop body of request, only process the response.
|
2020-11-05 07:18:13 +00:00
|
|
|
class SrsResponseOnlyHttpConn : virtual public ISrsStartableConneciton, virtual public ISrsHttpConnOwner
|
2020-11-05 08:46:16 +00:00
|
|
|
, virtual public ISrsReloadHandler
|
2015-02-03 08:01:07 +00:00
|
|
|
{
|
2020-11-05 07:08:36 +00:00
|
|
|
private:
|
|
|
|
// The manager object to manage the connection.
|
|
|
|
ISrsResourceManager* manager;
|
|
|
|
SrsHttpConn* conn;
|
|
|
|
srs_netfd_t stfd;
|
2015-02-03 08:01:07 +00:00
|
|
|
public:
|
2020-09-19 02:30:05 +00:00
|
|
|
SrsResponseOnlyHttpConn(ISrsResourceManager* cm, srs_netfd_t fd, ISrsHttpServeMux* m, std::string cip, int port);
|
2015-06-14 11:42:43 +00:00
|
|
|
virtual ~SrsResponseOnlyHttpConn();
|
2017-04-30 04:03:31 +00:00
|
|
|
public:
|
|
|
|
// Directly read a HTTP request message.
|
|
|
|
// It's exported for HTTP stream, such as HTTP FLV, only need to write to client when
|
|
|
|
// serving it, but we need to start a thread to read message to detect whether FD is closed.
|
|
|
|
// @see https://github.com/ossrs/srs/issues/636#issuecomment-298208427
|
|
|
|
// @remark Should only used in HTTP-FLV streaming connection.
|
2017-07-29 13:39:57 +00:00
|
|
|
virtual srs_error_t pop_message(ISrsHttpMessage** preq);
|
2020-11-05 08:46:16 +00:00
|
|
|
// Interface ISrsReloadHandler
|
|
|
|
public:
|
|
|
|
virtual srs_error_t on_reload_http_stream_crossdomain();
|
2020-11-05 07:18:13 +00:00
|
|
|
// Interface ISrsHttpConnOwner.
|
2015-02-03 08:01:07 +00:00
|
|
|
public:
|
2020-11-05 09:16:59 +00:00
|
|
|
virtual srs_error_t on_start();
|
2020-11-05 10:08:13 +00:00
|
|
|
virtual srs_error_t on_http_message(ISrsHttpMessage* r, SrsHttpResponseWriter* w);
|
2020-11-05 10:19:43 +00:00
|
|
|
virtual srs_error_t on_conn_done(srs_error_t r0);
|
2020-11-05 07:08:36 +00:00
|
|
|
// Extract APIs from SrsTcpConnection.
|
2020-03-21 14:41:25 +00:00
|
|
|
public:
|
2020-11-05 07:08:36 +00:00
|
|
|
// Set socket option TCP_NODELAY.
|
|
|
|
virtual srs_error_t set_tcp_nodelay(bool v);
|
|
|
|
// Set socket option SO_SNDBUF in srs_utime_t.
|
|
|
|
virtual srs_error_t set_socket_buffer(srs_utime_t buffer_v);
|
|
|
|
// Interface ISrsResource.
|
|
|
|
public:
|
|
|
|
virtual std::string desc();
|
|
|
|
// Interface ISrsConnection.
|
|
|
|
public:
|
|
|
|
virtual std::string remote_ip();
|
|
|
|
virtual const SrsContextId& get_id();
|
|
|
|
// Interface ISrsStartable
|
|
|
|
public:
|
|
|
|
virtual srs_error_t start();
|
|
|
|
// Interface ISrsKbpsDelta
|
|
|
|
public:
|
|
|
|
virtual void remark(int64_t* in, int64_t* out);
|
2015-02-03 08:01:07 +00:00
|
|
|
};
|
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// The http server, use http stream or static server to serve requests.
|
2015-06-14 06:06:39 +00:00
|
|
|
class SrsHttpServer : public ISrsHttpServeMux
|
2014-04-04 10:55:21 +00:00
|
|
|
{
|
2015-03-14 11:45:13 +00:00
|
|
|
private:
|
|
|
|
SrsServer* server;
|
2015-06-14 06:06:39 +00:00
|
|
|
SrsHttpStaticServer* http_static;
|
|
|
|
SrsHttpStreamServer* http_stream;
|
2014-04-04 10:55:21 +00:00
|
|
|
public:
|
2015-03-14 11:45:13 +00:00
|
|
|
SrsHttpServer(SrsServer* svr);
|
2015-01-17 15:00:40 +00:00
|
|
|
virtual ~SrsHttpServer();
|
2014-04-04 10:55:21 +00:00
|
|
|
public:
|
2017-06-09 05:29:23 +00:00
|
|
|
virtual srs_error_t initialize();
|
2019-04-30 00:30:13 +00:00
|
|
|
// Interface ISrsHttpServeMux
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
2017-07-29 13:39:57 +00:00
|
|
|
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
|
2015-01-18 10:39:53 +00:00
|
|
|
public:
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t http_mount(SrsSource* s, SrsRequest* r);
|
2015-03-14 01:52:47 +00:00
|
|
|
virtual void http_unmount(SrsSource* s, SrsRequest* r);
|
2015-05-04 10:11:52 +00:00
|
|
|
};
|
|
|
|
|
2014-03-27 04:14:04 +00:00
|
|
|
#endif
|
2014-04-01 07:42:27 +00:00
|
|
|
|