1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_http_conn.hpp

210 lines
7 KiB
C++
Raw Normal View History

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>
#include <srs_app_reload.hpp>
#include <srs_kernel_file.hpp>
#include <srs_app_st.hpp>
2015-05-22 14:34:03 +00:00
#include <srs_app_conn.hpp>
#include <srs_app_source.hpp>
class SrsServer;
class SrsSource;
class SrsRequest;
class SrsConsumer;
class SrsStSocket;
class SrsHttpParser;
class ISrsHttpMessage;
2014-04-02 10:07:34 +00:00
class SrsHttpHandler;
class SrsMessageQueue;
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;
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
{
public:
2020-11-05 07:18:13 +00:00
ISrsHttpConnOwner();
virtual ~ISrsHttpConnOwner();
public:
// Handle the HTTP message msg, which may be parsed partially.
// 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.
virtual srs_error_t on_http_message(ISrsHttpMessage* msg) = 0;
// When connection is destroy, should use manager to dispose it.
virtual void on_conn_done() = 0;
};
2019-04-30 00:24:52 +00:00
// The http connection which request the static or stream content.
2020-11-05 03:47:24 +00:00
class SrsHttpConn : virtual public ISrsStartableConneciton, virtual public ISrsReloadHandler
, virtual public ISrsCoroutineHandler, virtual public ISrsExpire
{
protected:
SrsHttpParser* parser;
ISrsHttpServeMux* http_mux;
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;
public:
2020-11-05 07:18:13 +00:00
SrsHttpConn(ISrsHttpConnOwner* handler, srs_netfd_t fd, ISrsHttpServeMux* m, std::string cip, int port);
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
public:
2019-01-01 09:36:27 +00:00
virtual void remark(int64_t* in, int64_t* out);
private:
2017-07-29 13:39:57 +00:00
virtual srs_error_t do_cycle();
public:
// Get the HTTP message handler.
2020-11-05 07:18:13 +00:00
virtual ISrsHttpConnOwner* handler();
// Whether the connection coroutine is error or terminated.
virtual srs_error_t pull();
private:
2017-07-29 13:39:57 +00:00
virtual srs_error_t process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
2019-04-30 00:24:52 +00:00
// 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.
2017-07-29 13:39:57 +00:00
virtual srs_error_t on_disconnect(SrsRequest* req);
2019-04-30 00:30:13 +00:00
// Interface ISrsReloadHandler
public:
2017-07-29 13:39:57 +00:00
virtual srs_error_t on_reload_http_stream_crossdomain();
2020-11-05 03:47:24 +00:00
// Extract APIs from SrsTcpConnection.
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 ISrsStartable
public:
virtual srs_error_t start();
// Interface ISrsOneCycleThreadHandler
public:
virtual srs_error_t cycle();
// Interface ISrsConnection.
public:
virtual std::string remote_ip();
virtual const SrsContextId& get_id();
// Interface ISrsExpire.
public:
virtual void expire();
};
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
{
private:
// The manager object to manage the connection.
ISrsResourceManager* manager;
SrsHttpConn* conn;
srs_netfd_t stfd;
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();
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 07:18:13 +00:00
// Interface ISrsHttpConnOwner.
public:
virtual srs_error_t on_http_message(ISrsHttpMessage* msg);
virtual void on_conn_done();
// Extract APIs from SrsTcpConnection.
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 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);
};
2019-04-30 00:24:52 +00:00
// The http server, use http stream or static server to serve requests.
class SrsHttpServer : public ISrsHttpServeMux
2014-04-04 10:55:21 +00:00
{
private:
SrsServer* server;
SrsHttpStaticServer* http_static;
SrsHttpStreamServer* http_stream;
2014-04-04 10:55:21 +00:00
public:
SrsHttpServer(SrsServer* svr);
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
public:
2017-07-29 13:39:57 +00:00
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
public:
2018-01-01 11:39:57 +00:00
virtual srs_error_t http_mount(SrsSource* s, SrsRequest* r);
virtual void http_unmount(SrsSource* s, SrsRequest* r);
};
2014-03-27 04:14:04 +00:00
#endif
2014-04-01 07:42:27 +00:00