Repository navigation
Expand file tree
/
Copy pathsrs_app_http_conn.cpp
More file actions
307 lines (249 loc) · 8.25 KB
/
Copy pathsrs_app_http_conn.cpp
File metadata and controls
307 lines (249 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* The MIT License (MIT)
*
* Copyright (c) 2013-2017 OSSRS(winlin)
*
* 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.
*/
#include <srs_app_http_conn.hpp>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
#include <srs_protocol_stream.hpp>
#include <srs_protocol_utility.hpp>
#include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
#include <srs_app_st.hpp>
#include <srs_core_autofree.hpp>
#include <srs_app_config.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_kernel_file.hpp>
#include <srs_kernel_flv.hpp>
#include <srs_rtmp_stack.hpp>
#include <srs_app_source.hpp>
#include <srs_rtmp_msg_array.hpp>
#include <srs_kernel_aac.hpp>
#include <srs_kernel_mp3.hpp>
#include <srs_kernel_ts.hpp>
#include <srs_app_pithy_print.hpp>
#include <srs_app_source.hpp>
#include <srs_app_server.hpp>
#include <srs_app_http_static.hpp>
#include <srs_app_http_stream.hpp>
#include <srs_app_http_api.hpp>
#include <srs_protocol_json.hpp>
#include <srs_app_http_hooks.hpp>
#include <srs_protocol_amf0.hpp>
#include <srs_app_utility.hpp>
#include <srs_app_st.hpp>
SrsHttpConn::SrsHttpConn(IConnectionManager* cm, srs_netfd_t fd, ISrsHttpServeMux* m, string cip)
: SrsConnection(cm, fd, cip)
{
parser = new SrsHttpParser();
cors = new SrsHttpCorsMux();
http_mux = m;
}
SrsHttpConn::~SrsHttpConn()
{
srs_freep(parser);
srs_freep(cors);
}
void SrsHttpConn::resample()
{
// TODO: FIXME: implements it
}
int64_t SrsHttpConn::get_send_bytes_delta()
{
// TODO: FIXME: implements it
return 0;
}
int64_t SrsHttpConn::get_recv_bytes_delta()
{
// TODO: FIXME: implements it
return 0;
}
void SrsHttpConn::cleanup()
{
// TODO: FIXME: implements it
}
int SrsHttpConn::do_cycle()
{
int ret = ERROR_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;
}
// set the recv timeout, for some clients never disconnect the connection.
// @see https://github.com/ossrs/srs/issues/398
skt->set_recv_timeout(SRS_HTTP_RECV_TMMS);
SrsRequest* last_req = NULL;
SrsAutoFree(SrsRequest, last_req);
// 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;
}
// process http messages.
while (!trd->pull()) {
ISrsHttpMessage* req = NULL;
// get a http message
if ((ret = parser->parse_message(skt, this, &req)) != ERROR_SUCCESS) {
break;
}
// if SUCCESS, always NOT-NULL.
srs_assert(req);
// 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;
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) {
break;
}
// ok, handle http request.
SrsHttpResponseWriter writer(skt);
if ((ret = process_request(&writer, req)) != ERROR_SUCCESS) {
break;
}
// donot keep alive, disconnect it.
// @see https://github.com/ossrs/srs/issues/399
if (!req->is_keep_alive()) {
break;
}
}
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);
}
return ret;
}
int SrsHttpConn::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
int ret = ERROR_SUCCESS;
srs_trace("HTTP %s %s, content-length=%" PRId64 "",
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;
}
return ret;
}
int SrsHttpConn::on_disconnect(SrsRequest* req)
{
int ret = ERROR_SUCCESS;
// TODO: implements it.s
return ret;
}
int SrsHttpConn::on_reload_http_stream_crossdomain()
{
int ret = ERROR_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;
}
return ret;
}
SrsResponseOnlyHttpConn::SrsResponseOnlyHttpConn(IConnectionManager* cm, srs_netfd_t fd, ISrsHttpServeMux* m, string cip)
: SrsHttpConn(cm, fd, m, cip)
{
}
SrsResponseOnlyHttpConn::~SrsResponseOnlyHttpConn()
{
}
int SrsResponseOnlyHttpConn::pop_message(ISrsHttpMessage** preq)
{
int ret = ERROR_SUCCESS;
SrsStSocket skt;
if ((ret = skt.initialize(stfd)) != ERROR_SUCCESS) {
return ret;
}
if ((ret = parser->parse_message(&skt, this, preq)) != ERROR_SUCCESS) {
return ret;
}
return ret;
}
int SrsResponseOnlyHttpConn::on_got_http_message(ISrsHttpMessage* msg)
{
int ret = ERROR_SUCCESS;
ISrsHttpResponseReader* br = msg->body_reader();
// when not specified the content length, ignore.
if (msg->content_length() == -1) {
return ret;
}
// drop all request body.
while (!br->eof()) {
char body[4096];
if ((ret = br->read(body, 4096, NULL)) != ERROR_SUCCESS) {
return ret;
}
}
return ret;
}
SrsHttpServer::SrsHttpServer(SrsServer* svr)
{
server = svr;
http_stream = new SrsHttpStreamServer(svr);
http_static = new SrsHttpStaticServer(svr);
}
SrsHttpServer::~SrsHttpServer()
{
srs_freep(http_stream);
srs_freep(http_static);
}
srs_error_t SrsHttpServer::initialize()
{
srs_error_t err = srs_success;
// for SRS go-sharp to detect the status of HTTP server of SRS HTTP FLV Cluster.
if ((err = http_static->mux.handle("/api/v1/versions", new SrsGoApiVersion())) != srs_success) {
return srs_error_wrap(err, "handle versin");
}
if ((err = http_stream->initialize()) != srs_success) {
return srs_error_wrap(err, "http stream");
}
if ((err = http_static->initialize()) != srs_success) {
return srs_error_wrap(err, "http static");
}
return err;
}
int SrsHttpServer::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
// try http stream first.
if (http_stream->mux.can_serve(r)) {
return http_stream->mux.serve_http(w, r);
}
return http_static->mux.serve_http(w, r);
}
int SrsHttpServer::http_mount(SrsSource* s, SrsRequest* r)
{
return http_stream->http_mount(s, r);
}
void SrsHttpServer::http_unmount(SrsSource* s, SrsRequest* r)
{
http_stream->http_unmount(s, r);
}