-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathngx_http_flv_module.c
1491 lines (1124 loc) · 40.6 KB
/
ngx_http_flv_module.c
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2017 Gnolizuh
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_rtmp.h>
#include "ngx_rtmp_live_module.h"
#include "ngx_http_flv_module.h"
#include "ngx_rtmp_codec_module.h"
typedef struct ngx_rtmp_header_val_s ngx_rtmp_header_val_t;
typedef ngx_int_t (*ngx_rtmp_set_header_pt)(ngx_http_request_t *r,
ngx_rtmp_header_val_t *hv, ngx_str_t *value);
typedef enum {
NGX_RTMP_EXPIRES_OFF,
NGX_RTMP_EXPIRES_EPOCH,
NGX_RTMP_EXPIRES_MAX,
NGX_RTMP_EXPIRES_ACCESS,
NGX_RTMP_EXPIRES_MODIFIED,
NGX_RTMP_EXPIRES_DAILY,
NGX_RTMP_EXPIRES_UNSET
} ngx_rtmp_expires_t;
struct ngx_rtmp_header_val_s {
ngx_http_complex_value_t value;
ngx_str_t key;
ngx_rtmp_set_header_pt handler;
ngx_uint_t offset;
ngx_uint_t always; /* unsigned always:1 */
};
typedef struct {
ngx_rtmp_expires_t expires;
time_t expires_time;
ngx_http_complex_value_t *expires_value;
ngx_array_t *headers;
} ngx_rtmp_headers_conf_t;
static ngx_rtmp_play_pt next_play;
static ngx_rtmp_close_stream_pt next_close_stream;
extern ngx_module_t ngx_http_headers_filter_module;
extern ngx_uint_t ngx_rtmp_playing;
ngx_uint_t ngx_http_flv_naccepted;
typedef struct {
} ngx_http_flv_httploc_conf_t;
/* http handler registered */
static ngx_int_t ngx_http_flv_http_init(ngx_conf_t *cf);
static void * ngx_http_flv_http_create_conf(ngx_conf_t *cf);
static char * ngx_http_flv_http_merge_conf(ngx_conf_t *cf, void *parent, void *child);
/* rtmp handler registered */
static ngx_int_t ngx_http_flv_rtmp_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_flv_send_message(ngx_rtmp_session_t *s, ngx_chain_t *out, ngx_uint_t priority);
static ngx_int_t ngx_http_flv_connect_local(ngx_rtmp_session_t *s);
static ngx_chain_t * ngx_http_flv_http_append_meta(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *meta);
static void ngx_http_flv_http_send_header(ngx_rtmp_session_t *s, ngx_rtmp_session_t *ps);
static ngx_int_t ngx_http_flv_http_send_message(ngx_rtmp_session_t *s, ngx_chain_t *in, ngx_uint_t priority);
static ngx_chain_t * ngx_http_flv_http_append_shared_bufs(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_rtmp_header_t *lh, ngx_chain_t *in);
static void ngx_http_flv_http_free_shared_chain(ngx_rtmp_session_t *s, ngx_chain_t *in);
static char ngx_http_server_string[] = "Server: nginx" CRLF;
static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static ngx_str_t ngx_http_status_lines[] = {
ngx_string("200 OK"),
ngx_string("201 Created"),
ngx_string("202 Accepted"),
ngx_null_string, /* "203 Non-Authoritative Information" */
ngx_string("204 No Content"),
ngx_null_string, /* "205 Reset Content" */
ngx_string("206 Partial Content"),
/* ngx_null_string, */ /* "207 Multi-Status" */
#define NGX_HTTP_LAST_2XX 207
#define NGX_HTTP_OFF_3XX (NGX_HTTP_LAST_2XX - 200)
/* ngx_null_string, */ /* "300 Multiple Choices" */
ngx_string("301 Moved Permanently"),
ngx_string("302 Moved Temporarily"),
ngx_string("303 See Other"),
ngx_string("304 Not Modified"),
ngx_null_string, /* "305 Use Proxy" */
ngx_null_string, /* "306 unused" */
ngx_string("307 Temporary Redirect"),
#define NGX_HTTP_LAST_3XX 308
#define NGX_HTTP_OFF_4XX (NGX_HTTP_LAST_3XX - 301 + NGX_HTTP_OFF_3XX)
ngx_string("400 Bad Request"),
ngx_string("401 Unauthorized"),
ngx_string("402 Payment Required"),
ngx_string("403 Forbidden"),
ngx_string("404 Not Found"),
ngx_string("405 Not Allowed"),
ngx_string("406 Not Acceptable"),
ngx_null_string, /* "407 Proxy Authentication Required" */
ngx_string("408 Request Time-out"),
ngx_string("409 Conflict"),
ngx_string("410 Gone"),
ngx_string("411 Length Required"),
ngx_string("412 Precondition Failed"),
ngx_string("413 Request Entity Too Large"),
ngx_string("414 Request-URI Too Large"),
ngx_string("415 Unsupported Media Type"),
ngx_string("416 Requested Range Not Satisfiable"),
/* ngx_null_string, */ /* "417 Expectation Failed" */
/* ngx_null_string, */ /* "418 unused" */
/* ngx_null_string, */ /* "419 unused" */
/* ngx_null_string, */ /* "420 unused" */
/* ngx_null_string, */ /* "421 unused" */
/* ngx_null_string, */ /* "422 Unprocessable Entity" */
/* ngx_null_string, */ /* "423 Locked" */
/* ngx_null_string, */ /* "424 Failed Dependency" */
#define NGX_HTTP_LAST_4XX 417
#define NGX_HTTP_OFF_5XX (NGX_HTTP_LAST_4XX - 400 + NGX_HTTP_OFF_4XX)
ngx_string("500 Internal Server Error"),
ngx_string("501 Not Implemented"),
ngx_string("502 Bad Gateway"),
ngx_string("503 Service Temporarily Unavailable"),
ngx_string("504 Gateway Time-out"),
ngx_null_string, /* "505 HTTP Version Not Supported" */
ngx_null_string, /* "506 Variant Also Negotiates" */
ngx_string("507 Insufficient Storage"),
/* ngx_null_string, */ /* "508 unused" */
/* ngx_null_string, */ /* "509 unused" */
/* ngx_null_string, */ /* "510 Not Extended" */
#define NGX_HTTP_LAST_5XX 508
};
ngx_rtmp_send_handler_t ngx_http_flv_send_handler = {
ngx_http_flv_http_append_meta,
ngx_http_flv_http_send_header,
ngx_http_flv_http_send_message,
ngx_http_flv_http_append_shared_bufs,
ngx_http_flv_http_free_shared_chain
};
static ngx_command_t ngx_http_flv_httpcommands[] = {
ngx_null_command
};
static ngx_http_module_t ngx_http_flv_httpmodule_ctx = {
NULL, /* preconfiguration */
ngx_http_flv_http_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_flv_http_create_conf, /* create location configuration */
ngx_http_flv_http_merge_conf /* merge location configuration */
};
ngx_module_t ngx_http_flv_httpmodule = {
NGX_MODULE_V1,
&ngx_http_flv_httpmodule_ctx, /* module context */
ngx_http_flv_httpcommands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_rtmp_module_t ngx_http_flv_rtmpmodule_ctx = {
NULL, /* preconfiguration */
ngx_http_flv_rtmp_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create service configuration */
NULL, /* merge service configuration */
NULL, /* create application configuration */
NULL, /* merge application configuration */
};
ngx_module_t ngx_http_flv_rtmpmodule = {
NGX_MODULE_V1,
&ngx_http_flv_rtmpmodule_ctx, /* module context */
NULL, /* module directives */
NGX_RTMP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_http_flv_headers_filter(ngx_http_request_t *r)
{
ngx_str_t value;
ngx_uint_t i, safe_status;
ngx_rtmp_header_val_t *h;
ngx_rtmp_headers_conf_t *conf;
conf = ngx_http_get_module_loc_conf(r, ngx_http_headers_filter_module);
if (conf->headers == NULL) {
return NGX_ERROR;
}
switch (r->headers_out.status) {
case NGX_HTTP_OK:
case NGX_HTTP_CREATED:
case NGX_HTTP_NO_CONTENT:
case NGX_HTTP_PARTIAL_CONTENT:
case NGX_HTTP_MOVED_PERMANENTLY:
case NGX_HTTP_MOVED_TEMPORARILY:
case NGX_HTTP_SEE_OTHER:
case NGX_HTTP_NOT_MODIFIED:
case NGX_HTTP_TEMPORARY_REDIRECT:
safe_status = 1;
break;
default:
safe_status = 0;
break;
}
if (conf->headers) {
h = conf->headers->elts;
for (i = 0; i < conf->headers->nelts; i++) {
if (!safe_status && !h[i].always) {
continue;
}
if (ngx_http_complex_value(r, &h[i].value, &value) != NGX_OK) {
return NGX_ERROR;
}
if (h[i].handler(r, &h[i], &value) != NGX_OK) {
return NGX_ERROR;
}
}
}
return NGX_OK;
}
static ngx_int_t
ngx_http_flv_send_message(ngx_rtmp_session_t *s, ngx_chain_t *out,
ngx_uint_t priority)
{
ngx_uint_t nmsg;
// modulo negative number may cause action undefined.
nmsg = (s->out_last - s->out_pos + s->out_queue) % s->out_queue + 1;
if (priority > 3) {
priority = 3;
}
/* drop packet?
* Note we always leave 1 slot free */
if (nmsg + priority * s->out_queue / 4 >= s->out_queue) {
/*
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"RTMP drop message bufs=%ui, priority=%ui",
nmsg, priority);
*/
ngx_log_error(NGX_LOG_WARN, s->connection->log, 0,
"HTTP FLV drop message bufs=%ui, priority=%ui, s->out_last=%d, s->out_pos=%d, s->out_queue=%d ",
nmsg, priority, s->out_last, s->out_pos, s->out_queue);
return NGX_AGAIN;
}
s->out[s->out_last++] = out;
s->out_last %= s->out_queue;
ngx_rtmp_acquire_shared_chain(out);
ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"HTTP FLV send nmsg=%ui, priority=%ui #%ui",
nmsg, priority, s->out_last);
if (priority && s->out_buffer && nmsg < s->out_cork) {
return NGX_OK;
}
if (!s->connection->write->active) {
ngx_http_flv_send(s->connection->write);
}
return NGX_OK;
}
static ngx_int_t
ngx_http_flv_play_local(ngx_rtmp_session_t *s)
{
static ngx_rtmp_play_t v;
ngx_memzero(&v, sizeof(ngx_rtmp_play_t));
ngx_memcpy(v.name, s->name.data, ngx_min(s->name.len, sizeof(v.name) - 1));
ngx_memcpy(v.args, s->args.data, ngx_min(s->args.len, sizeof(v.args) - 1));
return ngx_rtmp_cmd_play_local(s, &v);
}
static void
ngx_http_flv_close_session_handler(ngx_rtmp_session_t *s)
{
ngx_connection_t *c;
ngx_rtmp_core_srv_conf_t *cscf;
c = s->connection;
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
ngx_log_error(NGX_LOG_INFO, c->log, 0, "http_flv close session");
ngx_rtmp_fire_event(s, NGX_RTMP_DISCONNECT, NULL, NULL);
if (s->ping_evt.timer_set) {
ngx_del_timer(&s->ping_evt);
}
if (s->in_old_pool) {
ngx_destroy_pool(s->in_old_pool);
}
if (s->in_pool) {
ngx_destroy_pool(s->in_pool);
}
ngx_rtmp_free_handshake_buffers(s);
while (s->out_pos != s->out_last) {
ngx_rtmp_free_shared_chain(cscf, s->out[s->out_pos++]);
s->out_pos %= s->out_queue;
}
}
static ngx_int_t
ngx_http_flv_connect_local(ngx_rtmp_session_t *s)
{
static ngx_rtmp_connect_t v;
ngx_http_flv_rtmp_ctx_t *rtmpctx;
ngx_memzero(&v, sizeof(ngx_rtmp_connect_t));
ngx_memcpy(v.host, s->host.data, ngx_min(s->host.len, sizeof(v.host) - 1));
ngx_memcpy(v.app, s->app.data, ngx_min(s->app.len, sizeof(v.app) - 1));
*ngx_snprintf(v.tc_url, NGX_RTMP_MAX_URL, "http://%V/%V", &s->host, &s->app) = 0;
#define NGX_RTMP_SET_STRPAR(name) \
s->name.len = ngx_strlen(v.name); \
s->name.data = ngx_palloc(s->connection->pool, s->name.len); \
ngx_memcpy(s->name.data, v.name, s->name.len)
NGX_RTMP_SET_STRPAR(args);
NGX_RTMP_SET_STRPAR(tc_url);
#undef NGX_RTMP_SET_STRPAR
rtmpctx = ngx_rtmp_get_module_ctx(s, ngx_http_flv_rtmpmodule);
if (rtmpctx == NULL) {
rtmpctx = ngx_pcalloc(s->pool, sizeof(ngx_http_flv_rtmp_ctx_t));
ngx_rtmp_set_ctx(s, rtmpctx, ngx_http_flv_rtmpmodule);
}
return ngx_rtmp_cmd_connect_local(s, &v);
}
static void
ngx_http_flv_cleanup(void *data)
{
ngx_http_request_t *r = data;
ngx_rtmp_session_t *s;
ngx_http_flv_http_ctx_t *httpctx;
httpctx = ngx_http_get_module_ctx(r, ngx_http_flv_httpmodule);
s = httpctx->rs;
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, "http_flv close connection");
-- ngx_http_flv_naccepted;
ngx_http_flv_close_session_handler(s);
}
static ngx_int_t
ngx_http_flv_http_handler(ngx_http_request_t *r)
{
ngx_http_flv_httploc_conf_t *hlcf;
ngx_http_cleanup_t *cln;
ngx_http_flv_http_ctx_t *httpctx;
ngx_rtmp_session_t *s;
ngx_int_t rc = 0;
ngx_int_t nslash;
u_char *p;
size_t i;
hlcf = ngx_http_get_module_loc_conf(r, ngx_http_flv_httpmodule);
if (hlcf == NULL) {
return NGX_DECLINED;
}
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))
|| r->headers_in.host == NULL) {
return NGX_HTTP_NOT_ALLOWED;
}
if (r->uri.data[r->uri.len - 1] == '/' &&
r->uri.len > ngx_strlen(".flv")) {
return NGX_DECLINED;
}
nslash = 0;
for (i = 0; i < r->uri.len; ++ i) {
if (r->uri.data[i] == '/') {
++ nslash;
} else if (r->uri.data[i] == '?') {
break;
}
}
if (nslash > 3 || nslash < 2) {
return NGX_DECLINED;
}
if (!(r->uri.data[r->uri.len - 1] == 'v' &&
r->uri.data[r->uri.len - 2] == 'l' &&
r->uri.data[r->uri.len - 3] == 'f' &&
r->uri.data[r->uri.len - 4] == '.')) {
return NGX_DECLINED;
}
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"http_flv handle uri: '%V' args: '%V'", &r->uri, &r->args);
// init session
ngx_http_flv_init_connection(r);
httpctx = ngx_http_get_module_ctx(r, ngx_http_flv_httpmodule);
if (httpctx == NULL) {
return NGX_DECLINED;
}
// get rtmp session
s = httpctx->rs;
s->proto = NGX_PROTO_TYPE_HTTP_FLV_PULL;
s->host_mask = NGX_RTMP_HOSTNAME_HTTP_FLV|NGX_RTMP_HOSTNAME_SUB;
p = ngx_strrlchr(r->uri.data + r->uri.len, r->uri.data + 1, '/');
if (!p) {
return NGX_DECLINED;
}
// get app
s->app.data = r->uri.data + 1;
s->app.len = p - s->app.data;
// get name
s->name.data = p + 1;
s->name.len = r->uri.data + r->uri.len - s->name.data - 4;
// get host
s->host = r->headers_in.host->value;
p = ngx_strlchr(s->host.data, s->host.data + s->host.len, ':');
if (p) {
s->host.len = p - s->host.data;
}
// restructure app & host
p = ngx_strlchr(s->app.data, s->app.data + s->app.len, '/');
if (p) {
s->host.data = s->app.data;
s->host.len = p - s->host.data;
s->app.data = p + 1;
s->app.len = s->app.len - s->host.len - 1;
}
// get args
s->args.len = r->args.len;
s->args.data = ngx_palloc(s->connection->pool, s->args.len);
ngx_memcpy(s->args.data, r->args.data, s->args.len);
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"http_flv handle host: '%V' app: '%V' name: '%V' args: '%V'",
&s->host, &s->app, &s->name, &s->args);
if (ngx_http_flv_connect_local(s) != NGX_OK) {
return NGX_DECLINED;
}
r->main->count++;
r->allow_ranges = 0;
r->read_event_handler = ngx_http_test_reading;
cln = ngx_http_cleanup_add(r, 0);
if (cln == NULL) {
return NGX_DECLINED;
}
cln->handler = ngx_http_flv_cleanup;
cln->data = r;
return NGX_OK;
}
static ngx_int_t
ngx_http_flv_http_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_flv_http_handler;
return NGX_OK;
}
static void *
ngx_http_flv_http_create_conf(ngx_conf_t *cf)
{
ngx_http_flv_httploc_conf_t *hlcf;
hlcf = ngx_palloc(cf->pool, sizeof(ngx_http_flv_httploc_conf_t));
if (hlcf == NULL) {
return NULL;
}
return hlcf;
}
static char *
ngx_http_flv_http_merge_conf(ngx_conf_t *cf, void *parent, void *child)
{
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_flv_connect_end(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
if (s->proto != NGX_PROTO_TYPE_HTTP_FLV_PULL) {
return NGX_OK;
}
return ngx_http_flv_play_local(s);
}
ngx_chain_t *
ngx_http_flv_append_shared_bufs(ngx_rtmp_core_srv_conf_t *cscf, ngx_chain_t *hd, ngx_chain_t *in)
{
ngx_chain_t *tail, *head, *ptag, prepkt;
ngx_buf_t prebuf;
uint32_t psize, psizebuf;
u_char *p, *ph;
head = ptag = in;
for (psize = 0, tail = ptag; ptag; tail = ptag, ptag = ptag->next) {
psize += (ptag->buf->last - ptag->buf->pos);
}
psize += NGX_RTMP_MAX_FLV_TAG_HEADER;
// set PreviousTagSize
ph = (u_char*)&psizebuf;
p = (u_char*)&psize;
*ph++ = p[3];
*ph++ = p[2];
*ph++ = p[1];
*ph++ = p[0];
ngx_memzero(&prebuf, sizeof(prebuf));
prebuf.start = prebuf.pos = (u_char*)&psizebuf;
prebuf.end = prebuf.last = (u_char*)(((u_char*)&psizebuf) + sizeof(psizebuf));
prepkt.buf = &prebuf;
prepkt.next = NULL;
// link chain of PreviousTagSize after the last packet
tail->next = &prepkt;
// alloc a new tag
ptag = ngx_rtmp_append_shared_bufs(cscf, hd, head);
// rebase pointer
tail->next = NULL;
if (!ptag) {
return NULL;
}
return ptag;
}
void
ngx_http_flv_prepare_message(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_rtmp_header_t *lh, ngx_chain_t *out)
{
ngx_chain_t *head, *ptag;
uint32_t psize;
u_char *p, *ph;
head = ptag = out;
for (psize = 0; ptag; ptag = ptag->next) {
psize += (ptag->buf->last - ptag->buf->pos);
}
psize -= 4;
/* tag header */
head->buf->pos -= NGX_RTMP_MAX_FLV_TAG_HEADER;
ph = head->buf->pos;
*ph++ = (u_char)h->type;
p = (u_char*)&psize;
*ph++ = p[2];
*ph++ = p[1];
*ph++ = p[0];
p = (u_char*)&h->timestamp;
*ph++ = p[2];
*ph++ = p[1];
*ph++ = p[0];
*ph++ = p[3];
*ph++ = 0;
*ph++ = 0;
*ph++ = 0;
}
static ngx_chain_t *
ngx_http_flv_http_append_meta(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in)
{
ngx_rtmp_core_srv_conf_t *cscf;
ngx_chain_t *meta;
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
if (cscf == NULL) {
return NULL;
}
meta = ngx_http_flv_append_shared_bufs(cscf, NULL, in);
if (meta != NULL) {
ngx_http_flv_prepare_message(s, h, NULL, meta);
}
return meta;
}
static void
ngx_http_flv_http_send_header(ngx_rtmp_session_t *s, ngx_rtmp_session_t *ps)
{
u_char *p;
size_t len;
ngx_str_t host, *status_line;
ngx_buf_t *b;
ngx_uint_t status, i, port;
ngx_chain_t out, *pkt;
ngx_list_part_t *part;
ngx_table_elt_t *header;
ngx_connection_t *c;
ngx_http_request_t *r;
ngx_rtmp_core_srv_conf_t *cscf;
ngx_http_core_loc_conf_t *clcf;
ngx_http_core_srv_conf_t *hcscf;
ngx_rtmp_codec_ctx_t *codec_ctx;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
u_char addr[NGX_SOCKADDR_STRLEN];
u_char flv_header[] = "FLV\x1\0\0\0\0\x9\0\0\0\0";
r = s->connection->data;
/* set HTTP status code */
r->headers_out.status = NGX_HTTP_OK;
/* set HTTP header */
ngx_http_flv_headers_filter(r);
if (r->header_sent) {
return;
}
r->header_sent = 1;
if (r != r->main) {
return;
}
if (r->http_version < NGX_HTTP_VERSION_10) {
return;
}
if (r->method == NGX_HTTP_HEAD) {
r->header_only = 1;
}
if (r->headers_out.last_modified_time != -1) {
if (r->headers_out.status != NGX_HTTP_OK
&& r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT
&& r->headers_out.status != NGX_HTTP_NOT_MODIFIED)
{
r->headers_out.last_modified_time = -1;
r->headers_out.last_modified = NULL;
}
}
len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1
/* the end of the header */
+ sizeof(CRLF) - 1
+ sizeof(flv_header) - 1;
/* status line */
if (r->headers_out.status_line.len) {
len += r->headers_out.status_line.len;
status_line = &r->headers_out.status_line;
#if (NGX_SUPPRESS_WARN)
status = 0;
#endif
} else {
status = r->headers_out.status;
if (status >= NGX_HTTP_OK
&& status < NGX_HTTP_LAST_2XX)
{
/* 2XX */
if (status == NGX_HTTP_NO_CONTENT) {
r->header_only = 1;
ngx_str_null(&r->headers_out.content_type);
r->headers_out.last_modified_time = -1;
r->headers_out.last_modified = NULL;
r->headers_out.content_length = NULL;
r->headers_out.content_length_n = -1;
}
status -= NGX_HTTP_OK;
status_line = &ngx_http_status_lines[status];
len += ngx_http_status_lines[status].len;
} else if (status >= NGX_HTTP_MOVED_PERMANENTLY
&& status < NGX_HTTP_LAST_3XX)
{
/* 3XX */
if (status == NGX_HTTP_NOT_MODIFIED) {
r->header_only = 1;
}
status = status - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_OFF_3XX;
status_line = &ngx_http_status_lines[status];
len += ngx_http_status_lines[status].len;
} else if (status >= NGX_HTTP_BAD_REQUEST
&& status < NGX_HTTP_LAST_4XX)
{
/* 4XX */
status = status - NGX_HTTP_BAD_REQUEST
+ NGX_HTTP_OFF_4XX;
status_line = &ngx_http_status_lines[status];
len += ngx_http_status_lines[status].len;
} else if (status >= NGX_HTTP_INTERNAL_SERVER_ERROR
&& status < NGX_HTTP_LAST_5XX)
{
/* 5XX */
status = status - NGX_HTTP_INTERNAL_SERVER_ERROR
+ NGX_HTTP_OFF_5XX;
status_line = &ngx_http_status_lines[status];
len += ngx_http_status_lines[status].len;
} else {
len += NGX_INT_T_LEN + 1 /* SP */;
status_line = NULL;
}
if (status_line && status_line->len == 0) {
status = r->headers_out.status;
len += NGX_INT_T_LEN + 1 /* SP */;
status_line = NULL;
}
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (r->headers_out.server == NULL) {
len += clcf->server_tokens ? sizeof(ngx_http_server_full_string) - 1:
sizeof(ngx_http_server_string) - 1;
}
if (r->headers_out.date == NULL) {
len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
}
if (r->headers_out.content_type.len) {
len += sizeof("Content-Type: ") - 1
+ r->headers_out.content_type.len + 2;
if (r->headers_out.content_type_len == r->headers_out.content_type.len
&& r->headers_out.charset.len)
{
len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
}
}
if (r->headers_out.content_length == NULL
&& r->headers_out.content_length_n >= 0)
{
len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;
}
if (r->headers_out.last_modified == NULL
&& r->headers_out.last_modified_time != -1)
{
len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
}
c = r->connection;
if (r->headers_out.location
&& r->headers_out.location->value.len
&& r->headers_out.location->value.data[0] == '/')
{
r->headers_out.location->hash = 0;
if (clcf->server_name_in_redirect) {
hcscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
host = hcscf->server_name;
} else if (r->headers_in.server.len) {
host = r->headers_in.server;
} else {
host.len = NGX_SOCKADDR_STRLEN;
host.data = addr;
if (ngx_connection_local_sockaddr(c, &host, 0) != NGX_OK) {
return;
}
}
switch (c->local_sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
port = ntohs(sin6->sin6_port);
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
port = 0;
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) c->local_sockaddr;
port = ntohs(sin->sin_port);
break;
}
len += sizeof("Location: https://") - 1
+ host.len
+ r->headers_out.location->value.len + 2;
if (clcf->port_in_redirect) {
#if (NGX_HTTP_SSL)
if (c->ssl)
port = (port == 443) ? 0 : port;
else
#endif
port = (port == 80) ? 0 : port;
} else {
port = 0;
}
if (port) {
len += sizeof(":65535") - 1;
}
} else {
ngx_str_null(&host);
port = 0;
}
if (r->chunked) {
len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
}
if (r->headers_out.status == NGX_HTTP_SWITCHING_PROTOCOLS) {
len += sizeof("Connection: upgrade" CRLF) - 1;
} else if (r->keepalive) {
len += sizeof("Connection: keep-alive" CRLF) - 1;
/*
* MSIE and Opera ignore the "Keep-Alive: timeout=<N>" header.
* MSIE keeps the connection alive for about 60-65 seconds.
* Opera keeps the connection alive very long.
* Mozilla keeps the connection alive for N plus about 1-10 seconds.
* Konqueror keeps the connection alive for about N seconds.
*/
if (clcf->keepalive_header) {
len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2;
}
} else {
len += sizeof("Connection: close" CRLF) - 1;
}
#if (NGX_HTTP_GZIP)
if (r->gzip_vary) {
if (clcf->gzip_vary) {
len += sizeof("Vary: Accept-Encoding" CRLF) - 1;
} else {
r->gzip_vary = 0;