forked from kaltura/nginx-vod-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_vod_request_parse.c
967 lines (828 loc) · 23.5 KB
/
ngx_http_vod_request_parse.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
#include "ngx_http_vod_request_parse.h"
#include "ngx_http_vod_module.h"
#include "ngx_http_vod_conf.h"
#include "ngx_http_vod_utils.h"
#include "vod/filters/rate_filter.h"
#include "vod/parse_utils.h"
// macros
#define skip_dash(start_pos, end_pos) \
if (start_pos >= end_pos) \
{ \
return NGX_OK; \
} \
if (*start_pos == '-') \
{ \
start_pos++; \
if (start_pos >= end_pos) \
{ \
return NGX_OK; \
} \
}
// typedefs
typedef ngx_int_t(*ngx_http_vod_param_parser_t)(ngx_str_t* value, void* output, int offset);
typedef struct {
int name_conf_offset;
char* name;
ngx_http_vod_param_parser_t parser;
int target_offset;
} ngx_http_vod_uri_param_def_t;
typedef struct {
ngx_str_t prefix;
ngx_str_t middle_parts[MAX_SUB_URIS];
ngx_str_t postfix;
uint32_t parts_count;
} ngx_http_vod_multi_uri_t;
bool_t
ngx_http_vod_parse_string(
const ngx_http_vod_match_definition_t* match_def,
u_char* start_pos,
u_char* end_pos,
void* output)
{
uint64_t value;
u_char* delim_pos;
for (;;)
{
switch (match_def->match_type)
{
case MATCH_END:
return start_pos == end_pos;
case MATCH_FIXED_STRING:
if (end_pos - start_pos < (ssize_t)match_def->string.len ||
ngx_memcmp(start_pos, match_def->string.data, match_def->string.len) != 0)
{
return FALSE;
}
start_pos += match_def->string.len;
break;
case MATCH_NUMBER:
value = 0;
for (; start_pos < end_pos && *start_pos >= '0' && *start_pos <= '9'; start_pos++)
{
value = value * 10 + *start_pos - '0';
}
*(uint64_t*)((u_char*)output + match_def->target_offset) = value;
break;
case MATCH_DELIM_STRING:
delim_pos = memchr(start_pos, match_def->delim, end_pos - start_pos);
if (delim_pos == NULL)
{
return FALSE;
}
((ngx_str_t*)((u_char*)output + match_def->target_offset))->data = start_pos;
((ngx_str_t*)((u_char*)output + match_def->target_offset))->len = delim_pos - start_pos;
start_pos = delim_pos + 1;
break;
}
match_def++;
}
}
bool_t
ngx_http_vod_split_uri_file_name(
ngx_str_t* uri,
int components,
ngx_str_t* path,
ngx_str_t* file_name)
{
u_char* cur_pos = uri->data + uri->len - 1;
for (cur_pos = uri->data + uri->len - 1; cur_pos >= uri->data; cur_pos--)
{
if (*cur_pos != '/')
{
continue;
}
components--;
if (components > 0)
{
continue;
}
path->data = uri->data;
path->len = cur_pos - uri->data;
file_name->data = cur_pos + 1;
file_name->len = uri->data + uri->len - file_name->data;
return TRUE;
}
return FALSE;
}
u_char*
ngx_http_vod_extract_uint32_token_reverse(u_char* start_pos, u_char* end_pos, uint32_t* result)
{
uint32_t multiplier;
uint32_t value = 0;
for (multiplier = 1;
end_pos > start_pos && end_pos[-1] >= '0' && end_pos[-1] <= '9';
end_pos--, multiplier *= 10)
{
value += (end_pos[-1] - '0') * multiplier;
}
*result = value;
return end_pos;
}
u_char*
ngx_http_vod_extract_track_tokens(u_char* start_pos, u_char* end_pos, uint32_t* result)
{
uint32_t stream_index;
int media_type;
result[MEDIA_TYPE_VIDEO] = 0;
result[MEDIA_TYPE_AUDIO] = 0;
for (;;)
{
switch (*start_pos)
{
case 'v':
media_type = MEDIA_TYPE_VIDEO;
break;
case 'a':
media_type = MEDIA_TYPE_AUDIO;
break;
default:
return start_pos;
}
start_pos++; // skip the v/a
if (start_pos >= end_pos)
{
// no index => all streams of the media type
result[media_type] = 0xffffffff;
break;
}
if (*start_pos >= '0' && *start_pos <= '9')
{
stream_index = *start_pos - '0';
start_pos++;
if (start_pos < end_pos && *start_pos >= '0' && *start_pos <= '9')
{
stream_index = stream_index * 10 + *start_pos - '0';
start_pos++;
}
}
else
{
stream_index = 0;
}
if (stream_index == 0)
{
// no index => all streams of the media type
result[media_type] = 0xffffffff;
}
else
{
result[media_type] |= (1 << (stream_index - 1));
}
if (start_pos >= end_pos)
{
break;
}
if (*start_pos == '-')
{
start_pos++;
if (start_pos >= end_pos)
{
break;
}
}
}
return NULL;
}
ngx_int_t
ngx_http_vod_parse_uri_file_name(
ngx_http_request_t* r,
u_char* start_pos,
u_char* end_pos,
uint32_t flags,
request_params_t* result)
{
uint32_t default_tracks_mask;
uint32_t* tracks_mask;
uint32_t* end_mask;
uint32_t* cur_mask;
uint32_t masks_per_sequence;
uint32_t sequence_index;
uint32_t clip_index;
language_id_t lang_id;
default_tracks_mask = (flags & PARSE_FILE_NAME_MULTI_STREAMS_PER_TYPE) ? 0xffffffff : 1;
result->tracks_mask[MEDIA_TYPE_VIDEO] = default_tracks_mask;
result->tracks_mask[MEDIA_TYPE_AUDIO] = default_tracks_mask;
result->sequences_mask = 0xffffffff;
result->clip_index = INVALID_CLIP_INDEX;
// segment index
if ((flags & PARSE_FILE_NAME_EXPECT_SEGMENT_INDEX) != 0)
{
if (start_pos < end_pos && *start_pos == '-')
{
start_pos++; // skip the -
}
// igogo debug
start_pos = parse_utils_extract_uint64_token(start_pos, end_pos, &result->segment_start_time_millis);
if (start_pos < end_pos && *start_pos == '-')
{
start_pos++; // skip the -
}
start_pos = parse_utils_extract_uint32_token(start_pos, end_pos, &result->segment_duration_millis);
if (start_pos < end_pos && *start_pos == '-')
{
start_pos++; // skip the -
}
// igogo debug end
start_pos = parse_utils_extract_uint32_token(start_pos, end_pos, &result->segment_index);
if (result->segment_index <= 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_parse_uri_file_name: failed to parse segment index");
return NGX_HTTP_BAD_REQUEST;
}
result->segment_index--; // convert to 0-based
}
skip_dash(start_pos, end_pos);
// clip index
if (*start_pos == 'c')
{
start_pos++; // skip the c
start_pos = parse_utils_extract_uint32_token(start_pos, end_pos, &clip_index);
if (clip_index <= 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_parse_uri_file_name: failed to parse clip index");
return NGX_HTTP_BAD_REQUEST;
}
result->clip_index = clip_index - 1;
skip_dash(start_pos, end_pos);
}
// sequence id
if (*start_pos == 's')
{
start_pos++; // skip the s
result->sequence_id.data = start_pos;
while (start_pos < end_pos && *start_pos != '-')
{
start_pos++;
}
result->sequence_id.len = start_pos - result->sequence_id.data;
skip_dash(start_pos, end_pos);
}
// sequence (file) index
if (*start_pos == 'f')
{
tracks_mask = result->tracks_mask;
masks_per_sequence = 0;
result->sequences_mask = 0;
for (;;)
{
start_pos++; // skip the f
if (start_pos >= end_pos || *start_pos < '1' || *start_pos > '9')
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_parse_uri_file_name: missing index following sequence selector");
return NGX_HTTP_BAD_REQUEST;
}
sequence_index = *start_pos - '0';
start_pos++; // skip the digit
if (start_pos < end_pos && *start_pos >= '0' && *start_pos <= '9')
{
sequence_index = sequence_index * 10 + *start_pos - '0';
if (sequence_index > MAX_SEQUENCES)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_parse_uri_file_name: sequence index too big");
return NGX_HTTP_BAD_REQUEST;
}
start_pos++; // skip the digit
}
sequence_index--; // Note: sequence_index cannot be 0 here
result->sequences_mask |= (1 << sequence_index);
skip_dash(start_pos, end_pos);
if (*start_pos == 'v' || *start_pos == 'a')
{
start_pos = ngx_http_vod_extract_track_tokens(
start_pos,
end_pos,
tracks_mask + masks_per_sequence * sequence_index);
if (start_pos == NULL)
{
return NGX_OK;
}
}
if (*start_pos != 'f')
{
break;
}
if (result->sequence_tracks_mask != NULL)
{
continue;
}
// more than one sequence, allocate the per sequence tracks mask
result->sequence_tracks_mask = ngx_palloc(r->pool,
sizeof(result->sequence_tracks_mask[0]) * MEDIA_TYPE_COUNT * MAX_SEQUENCES);
if (result->sequence_tracks_mask == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_parse_uri_file_name: ngx_palloc failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// initialize the mask with the default
cur_mask = result->sequence_tracks_mask;
end_mask = cur_mask + MEDIA_TYPE_COUNT * MAX_SEQUENCES;
for (; cur_mask < end_mask; cur_mask++)
{
*cur_mask = default_tracks_mask;
}
// copy the currently parsed mask to its place
tracks_mask = result->sequence_tracks_mask + sequence_index * MEDIA_TYPE_COUNT;
tracks_mask[MEDIA_TYPE_VIDEO] = result->tracks_mask[MEDIA_TYPE_VIDEO];
tracks_mask[MEDIA_TYPE_AUDIO] = result->tracks_mask[MEDIA_TYPE_AUDIO];
// restore the global mask to the default
result->tracks_mask[MEDIA_TYPE_VIDEO] = default_tracks_mask;
result->tracks_mask[MEDIA_TYPE_AUDIO] = default_tracks_mask;
// from now on, parse directly to the sequence tracks mask
tracks_mask = result->sequence_tracks_mask;
masks_per_sequence = MEDIA_TYPE_COUNT;
}
}
else if (*start_pos == 'v' || *start_pos == 'a')
{
// tracks
start_pos = ngx_http_vod_extract_track_tokens(start_pos, end_pos, result->tracks_mask);
if (start_pos == NULL)
{
return NGX_OK;
}
}
// languages
if (*start_pos == 'l')
{
result->langs_mask = ngx_pnalloc(r->pool, LANG_MASK_SIZE);
if (result->langs_mask == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_parse_uri_file_name: ngx_pnalloc failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_memzero(result->langs_mask, LANG_MASK_SIZE);
for (;;)
{
start_pos++; // skip the l
if (start_pos + LANG_ISO639_2_LEN > end_pos)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_parse_uri_file_name: language specifier length must be 3 characters");
return NGX_HTTP_BAD_REQUEST;
}
lang_id = lang_parse_iso639_2_code(iso639_2_str_to_int(start_pos));
if (lang_id == 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_parse_uri_file_name: failed to parse language specifier %*s", (size_t)3, start_pos);
return NGX_HTTP_BAD_REQUEST;
}
vod_set_bit(result->langs_mask, lang_id);
start_pos += LANG_ISO639_2_LEN;
skip_dash(start_pos, end_pos);
if (*start_pos != 'l')
{
break;
}
}
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_parse_uri_file_name: did not consume the whole name");
return NGX_HTTP_BAD_REQUEST;
}
static ngx_int_t
ngx_http_vod_parse_multi_uri(
ngx_http_request_t* r,
ngx_str_t* uri,
ngx_str_t* multi_uri_suffix,
ngx_http_vod_multi_uri_t* result)
{
u_char* cur_pos;
u_char* end_pos;
u_char* last_comma_pos;
uint32_t part_index;
result->prefix.data = uri->data;
result->prefix.len = uri->len;
if (uri->len < multi_uri_suffix->len ||
ngx_memcmp(multi_uri_suffix->data, uri->data + uri->len - multi_uri_suffix->len, multi_uri_suffix->len) != 0)
{
// not a multi uri
result->postfix.data = NULL;
result->postfix.len = 0;
result->middle_parts[0].data = NULL;
result->middle_parts[0].len = 0;
result->parts_count = 1;
return NGX_OK;
}
uri->len -= multi_uri_suffix->len;
end_pos = uri->data + uri->len;
last_comma_pos = NULL;
part_index = 0;
for (cur_pos = uri->data; cur_pos < end_pos; cur_pos++)
{
if (*cur_pos != ',')
{
continue;
}
if (last_comma_pos == NULL)
{
result->prefix.len = cur_pos - uri->data;
}
else
{
if (part_index >= MAX_SUB_URIS)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_parse_multi_uri: number of url parts exceeds the limit");
return NGX_HTTP_BAD_REQUEST;
}
result->middle_parts[part_index].data = last_comma_pos;
result->middle_parts[part_index].len = cur_pos - last_comma_pos;
part_index++;
}
last_comma_pos = cur_pos + 1;
}
if (last_comma_pos == NULL)
{
// no commas at all
result->postfix.data = NULL;
result->postfix.len = 0;
}
else
{
// 1 comma or more
result->postfix.data = last_comma_pos;
result->postfix.len = end_pos - last_comma_pos;
}
if (part_index == 0)
{
// no commas at all or a single comma
result->middle_parts[0].data = NULL;
result->middle_parts[0].len = 0;
result->parts_count = 1;
}
else
{
// 2 commas or more
result->parts_count = part_index;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_parse_uint32_param(ngx_str_t* value, void* output, int offset)
{
ngx_int_t result;
result = ngx_atoi(value->data, value->len);
if (result < 0)
{
return NGX_HTTP_BAD_REQUEST;
}
*(uint32_t*)((u_char*)output + offset) = result;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_parse_tracks_param(ngx_str_t* value, void* output, int offset)
{
uint32_t* tracks_mask = (uint32_t*)((u_char*)output + offset);
u_char* end_pos;
tracks_mask[MEDIA_TYPE_AUDIO] = 0;
tracks_mask[MEDIA_TYPE_VIDEO] = 0;
end_pos = parse_utils_extract_track_tokens(value->data, value->data + value->len, tracks_mask);
if (end_pos != value->data + value->len)
{
return NGX_HTTP_BAD_REQUEST;
}
return NGX_OK;
}
static ngx_http_vod_uri_param_def_t uri_param_defs[] = {
{ offsetof(ngx_http_vod_loc_conf_t, clip_to_param_name), "clip to", ngx_http_vod_parse_uint32_param, offsetof(media_clip_source_t, clip_to) },
{ offsetof(ngx_http_vod_loc_conf_t, clip_from_param_name), "clip from", ngx_http_vod_parse_uint32_param, offsetof(media_clip_source_t, clip_from) },
{ offsetof(ngx_http_vod_loc_conf_t, tracks_param_name), "tracks", ngx_http_vod_parse_tracks_param, offsetof(media_clip_source_t, tracks_mask) },
{ offsetof(ngx_http_vod_loc_conf_t, speed_param_name), "speed", NULL, 0 },
{ -1, NULL, NULL, 0}
};
static ngx_http_vod_uri_param_def_t pd_uri_param_defs[] = {
{ offsetof(ngx_http_vod_loc_conf_t, clip_to_param_name), "clip to", ngx_http_vod_parse_uint32_param, offsetof(media_clip_source_t, clip_to) },
{ offsetof(ngx_http_vod_loc_conf_t, clip_from_param_name), "clip from", ngx_http_vod_parse_uint32_param, offsetof(media_clip_source_t, clip_from) },
{ -1, NULL, NULL, 0 }
};
static ngx_int_t
ngx_http_vod_init_hash(
ngx_conf_t *cf,
ngx_http_vod_uri_param_def_t* elements,
ngx_http_vod_loc_conf_t* conf,
char* hash_name,
ngx_hash_t* output)
{
ngx_http_vod_uri_param_def_t *element;
ngx_array_t elements_arr;
ngx_hash_key_t *hash_key;
ngx_hash_init_t hash;
ngx_str_t* cur_key;
if (ngx_array_init(&elements_arr, cf->temp_pool, 32, sizeof(ngx_hash_key_t)) != NGX_OK)
{
return NGX_ERROR;
}
for (element = elements; element->name_conf_offset >= 0; element++)
{
cur_key = (ngx_str_t*)((u_char*)conf + element->name_conf_offset);
if (cur_key->len == 0)
{
break;
}
hash_key = ngx_array_push(&elements_arr);
if (hash_key == NULL)
{
return NGX_ERROR;
}
hash_key->key = *cur_key;
hash_key->key_hash = ngx_hash_key_lc(cur_key->data, cur_key->len);
hash_key->value = element;
}
hash.hash = output;
hash.key = ngx_hash_key_lc;
hash.max_size = 512;
hash.bucket_size = ngx_align(64, ngx_cacheline_size);
hash.name = hash_name;
hash.pool = cf->pool;
hash.temp_pool = NULL;
if (ngx_hash_init(&hash, elements_arr.elts, elements_arr.nelts) != NGX_OK)
{
return NGX_ERROR;
}
return NGX_OK;
}
ngx_int_t
ngx_http_vod_init_uri_params_hash(ngx_conf_t *cf, ngx_http_vod_loc_conf_t* conf)
{
ngx_int_t rc;
rc = ngx_http_vod_init_hash(
cf,
uri_param_defs,
conf,
"uri_params_hash",
&conf->uri_params_hash);
if (rc != NGX_OK)
{
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to initialize uri params hash");
return rc;
}
rc = ngx_http_vod_init_hash(
cf,
pd_uri_param_defs,
conf,
"pd_uri_params_hash",
&conf->pd_uri_params_hash);
if (rc != NGX_OK)
{
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to initialize progressive download uri params hash");
return rc;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_extract_uri_params(
ngx_http_request_t* r,
ngx_hash_t* params_hash,
ngx_str_t* uri,
media_sequence_t* sequence,
uint32_t* clip_id,
media_clip_source_t* source_clip,
media_clip_t** result)
{
ngx_http_vod_uri_param_def_t* param_def = NULL;
media_clip_rate_filter_t* rate_filter = NULL;
request_context_t request_context;
ngx_uint_t cur_key_hash = 0;
ngx_str_t cur_param;
ngx_int_t rc;
uint32_t parsed_params_mask = 0;
uint32_t param_index;
u_char param_name[MAX_URI_PARAM_NAME_LEN + 1];
u_char* param_name_end = param_name + sizeof(param_name);
u_char* param_name_pos = param_name;
u_char* copy_start = uri->data;
u_char* cur_pos;
u_char* end_pos = uri->data + uri->len;
u_char* last_slash = NULL;
u_char* p;
// set the source defaults
vod_memzero(source_clip, sizeof(*source_clip));
source_clip->base.type = MEDIA_CLIP_SOURCE;
source_clip->base.id = (*clip_id)++;
source_clip->clip_to = UINT_MAX;
source_clip->tracks_mask[MEDIA_TYPE_AUDIO] = 0xffffffff;
source_clip->tracks_mask[MEDIA_TYPE_VIDEO] = 0xffffffff;
source_clip->uri = *uri;
source_clip->sequence = sequence;
*result = &source_clip->base;
// allocate the stripped uri
p = ngx_palloc(r->pool, uri->len + 1);
if (p == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_extract_uri_params: ngx_palloc failed (1)");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
source_clip->stripped_uri.data = p;
for (cur_pos = uri->data; cur_pos <= end_pos; cur_pos++)
{
if (cur_pos < end_pos && *cur_pos != '/')
{
if (param_name_pos < param_name_end)
{
*param_name_pos = ngx_tolower(*cur_pos);
cur_key_hash = ngx_hash(cur_key_hash, *param_name_pos);
param_name_pos++;
}
continue;
}
if (last_slash == NULL)
{
last_slash = cur_pos;
cur_key_hash = 0;
param_name_pos = param_name;
continue;
}
if (param_def == NULL)
{
param_def = ngx_hash_find(params_hash, cur_key_hash, param_name, param_name_pos - param_name);
if (param_def != NULL)
{
p = ngx_copy(p, copy_start, last_slash - copy_start);
copy_start = last_slash;
}
}
else
{
param_index = param_def - uri_param_defs;
if ((parsed_params_mask & (1 << param_index)) == 0) // first instance of a param takes priority
{
parsed_params_mask |= (1 << param_index);
cur_param.data = last_slash + 1;
cur_param.len = cur_pos - (last_slash + 1);
if (param_def->name_conf_offset == offsetof(ngx_http_vod_loc_conf_t, speed_param_name))
{
request_context.pool = r->pool;
request_context.log = r->connection->log;
rc = rate_filter_create_from_string(
&request_context,
&cur_param,
&source_clip->base,
&rate_filter);
if (rc != VOD_OK)
{
return ngx_http_vod_status_to_ngx_error(rc);
}
if (rate_filter->rate.nom != rate_filter->rate.denom)
{
rate_filter->base.id = (*clip_id)++;
*result = &rate_filter->base;
}
}
else
{
rc = param_def->parser(&cur_param, source_clip, param_def->target_offset);
if (rc != NGX_OK)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_extract_uri_params: %s parser failed %i", param_def->name, rc);
return rc;
}
}
}
copy_start = cur_pos;
param_def = NULL;
}
last_slash = cur_pos;
cur_key_hash = 0;
param_name_pos = param_name;
}
if (source_clip->clip_from >= source_clip->clip_to)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_extract_uri_params: clip from %uD is larger than clip to %uD", source_clip->clip_from, source_clip->clip_to);
return NGX_HTTP_BAD_REQUEST;
}
p = ngx_copy(p, copy_start, end_pos - copy_start);
*p = '\0';
source_clip->stripped_uri.len = p - source_clip->stripped_uri.data;
source_clip->mapped_uri = source_clip->stripped_uri;
return NGX_OK;
}
ngx_int_t
ngx_http_vod_parse_uri_path(
ngx_http_request_t* r,
ngx_str_t* multi_uri_suffix,
ngx_hash_t* params_hash,
ngx_str_t* uri,
request_params_t* request_params,
media_set_t* media_set)
{
media_sequence_t* cur_sequence;
media_clip_source_t* cur_source;
media_clip_source_t* sources_head;
ngx_http_vod_multi_uri_t multi_uri;
media_clip_t** cur_clip_ptr;
media_clip_t* cur_clip;
ngx_str_t parts[3];
ngx_str_t cur_uri;
ngx_int_t rc;
uint32_t sequences_mask;
uint32_t parts_mask;
uint32_t clip_id = 1;
uint32_t i;
int uri_count;
media_set->uri = *uri; // must save the uri before calling ngx_http_vod_parse_multi_uri as it may change
rc = ngx_http_vod_parse_multi_uri(r, uri, multi_uri_suffix, &multi_uri);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_parse_uri_path: ngx_http_vod_parse_multi_uri failed", rc);
return rc;
}
if (multi_uri.parts_count > 1)
{
sequences_mask = request_params->sequences_mask;
request_params->sequences_mask = 0xffffffff; // reset the sequences mask so that it won't be applied again on the mapping request
}
else
{
sequences_mask = 0xffffffff;
}
parts_mask = (1 << multi_uri.parts_count) - 1;
uri_count = vod_get_number_of_set_bits(sequences_mask & parts_mask);
if (uri_count == 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_parse_uri_path: request has no uris");
return NGX_HTTP_BAD_REQUEST;
}
cur_sequence = ngx_palloc(r->pool,
(sizeof(*cur_sequence) + sizeof(*cur_source) + sizeof(*cur_clip_ptr)) * uri_count);
if (cur_sequence == NULL)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_parse_uri_path: ngx_palloc failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
media_set->sequences = cur_sequence;
cur_source = (void*)(cur_sequence + uri_count);
cur_clip_ptr = (void*)(cur_source + uri_count);
sources_head = NULL;
parts[0] = multi_uri.prefix;
parts[2] = multi_uri.postfix;
for (i = 0; i < multi_uri.parts_count; i++)
{
if ((sequences_mask & (1 << i)) == 0)
{
continue;
}
parts[1] = multi_uri.middle_parts[i];
rc = ngx_http_vod_merge_string_parts(r, parts, 3, &cur_uri);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_parse_uri_path: ngx_http_vod_merge_string_parts failed", rc);
return rc;
}
rc = ngx_http_vod_extract_uri_params(r, params_hash, &cur_uri, cur_sequence, &clip_id, cur_source, &cur_clip);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_parse_uri_path: ngx_http_vod_extract_uri_params failed", rc);
return rc;
}
if ((cur_source->tracks_mask[MEDIA_TYPE_AUDIO] & request_params->tracks_mask[MEDIA_TYPE_AUDIO]) == 0 &&
(cur_source->tracks_mask[MEDIA_TYPE_VIDEO] & request_params->tracks_mask[MEDIA_TYPE_VIDEO]) == 0)
{
continue;
}
*cur_clip_ptr = cur_clip;
cur_source->next = sources_head;
sources_head = cur_source;
cur_sequence->clips = cur_clip_ptr;
cur_sequence->index = i;
cur_sequence->stripped_uri = cur_source->stripped_uri;
cur_sequence->mapped_uri = cur_source->stripped_uri;
cur_sequence->id.len = 0;
cur_sequence->language = 0;
cur_sequence->label.len = 0;
cur_sequence->first_key_frame_offset = 0;
cur_sequence->key_frame_durations = NULL;
cur_source++;
cur_sequence++;
cur_clip_ptr++;
}
// need to test again since we filtered sub uris that didn't have any required tracks
media_set->sequence_count = cur_sequence - media_set->sequences;
if (media_set->sequence_count <= 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_parse_uri_path: request has no uris after track filtering");
return NGX_HTTP_BAD_REQUEST;
}
media_set->sources_head = sources_head;
media_set->sequences_end = cur_sequence;
media_set->has_multi_sequences = (multi_uri.parts_count > 1);
media_set->total_clip_count = 1;
media_set->clip_count = 1;
media_set->presentation_end = TRUE;
return NGX_OK;
}