-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_block_access_module.c
1664 lines (1189 loc) · 42.7 KB
/
ngx_http_block_access_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
// Header files
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
// Structures
// Location configuration structure
typedef struct {
// URL variables lengths
ngx_array_t *urlVariablesLengths;
// URL variables values
ngx_array_t *urlVariablesValues;
// Blocked address ranges
ngx_array_t *blockedAddressRanges;
// Unblocked locations
ngx_array_t *unblockedLocations;
// Allowed top-level domains
ngx_array_t *allowedTopLevelDomains;
// Allowed methods
ngx_array_t *allowedMethods;
// Required headers
ngx_array_t *requiredHeaders;
} ngx_http_block_access_conf_t;
// Blocked address range structure
typedef struct {
// Type
sa_family_t type;
// Start of range
union {
uint32_t ipv4StartOfRange;
struct in6_addr ipv6StartOfRange;
};
// End of range
union {
uint32_t ipv4EndOfRange;
struct in6_addr ipv6EndOfRange;
};
} BlockedAddressRange;
// Required header structure
typedef struct {
// Key
ngx_str_t key;
// Value
ngx_regex_compile_t value;
// Method
ngx_uint_t method;
} RequiredHeader;
// Request context structure
typedef struct {
// Done
ngx_uint_t done;
// Status
ngx_uint_t status;
// Request
ngx_http_request_t *request;
} RequestContext;
// Function prototypes
// Block access setup
static char *blockAccessSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data);
// Block setup
static char *blockSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data);
// Unblock setup
static char *unblockSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data);
// Allow top-level domain setup
static char *allowTopLevelDomainSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data);
// Allow method setup
static char *allowMethodSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data);
// Require header setup
static char *requireHeaderSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data);
// Postconfiguration
static ngx_int_t postconfiguration(ngx_conf_t *configuration);
// Create location configuration
static void *createLocationConfiguration(ngx_conf_t *configuration);
// Merge location configuration
static char *mergeLocationConfiguration(ngx_conf_t *configuration, void *parent, void *child);
// Access handler
static ngx_int_t accessHandler(ngx_http_request_t *request);
// Resolve handler
static void resolveHandler(ngx_resolver_ctx_t *context);
// Block IPv4 result
static ngx_uint_t blockIpv4Result(const ngx_array_t *blockedAddressRanges, const struct sockaddr_in *address);
// Block IPv6 result
static ngx_uint_t blockIpv6Result(const ngx_array_t *blockedAddressRanges, const struct sockaddr_in6 *address);
// Constants
// Directives
static ngx_command_t directives[] = {
// Block access
{
// Name
ngx_string("block_access"),
// Type
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
// Setup
blockAccessSetup,
// Configuration
NGX_HTTP_LOC_CONF_OFFSET,
// Offset
offsetof(ngx_http_block_access_conf_t, urlVariablesLengths),
// Reserved
NULL
},
// Block
{
// Name
ngx_string("block"),
// Type
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE12,
// Setup
blockSetup,
// Configuration
NGX_HTTP_LOC_CONF_OFFSET,
// Index
offsetof(ngx_http_block_access_conf_t, blockedAddressRanges),
// Reserved
NULL
},
// Unblock
{
// Name
ngx_string("unblock"),
// Type
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
// Setup
unblockSetup,
// Configuration
NGX_HTTP_LOC_CONF_OFFSET,
// Index
offsetof(ngx_http_block_access_conf_t, unblockedLocations),
// Reserved
NULL
},
// Allow top-level domain
{
// Name
ngx_string("allow_top_level_domain"),
// Type
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
// Setup
allowTopLevelDomainSetup,
// Configuration
NGX_HTTP_LOC_CONF_OFFSET,
// Index
offsetof(ngx_http_block_access_conf_t, allowedTopLevelDomains),
// Reserved
NULL
},
// Allow method
{
// Name
ngx_string("allow_method"),
// Type
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
// Setup
allowMethodSetup,
// Configuration
NGX_HTTP_LOC_CONF_OFFSET,
// Index
offsetof(ngx_http_block_access_conf_t, allowedMethods),
// Reserved
NULL
},
// Require header
{
// Name
ngx_string("require_header"),
// Type
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE23,
// Setup
requireHeaderSetup,
// Configuration
NGX_HTTP_LOC_CONF_OFFSET,
// Index
offsetof(ngx_http_block_access_conf_t, requiredHeaders),
// Reserved
NULL
},
// End of directives
ngx_null_command
};
// Context
static ngx_http_module_t context = {
// Preconfiguration
NULL,
// Postconfiguration
postconfiguration,
// Create main configuration
NULL,
// Initialize main configuration
NULL,
// Create server configuration
NULL,
// Merge server configuration
NULL,
// Create location configuration
createLocationConfiguration,
// Merge location configuration
mergeLocationConfiguration
};
// Module
ngx_module_t ngx_http_block_access_module = {
// Version header
NGX_MODULE_V1,
// Context
&context,
// Directives
directives,
// Type
NGX_HTTP_MODULE,
// initialize master
NULL,
// Initialize module
NULL,
// Initialize process
NULL,
// Initialize thread
NULL,
// Exit thread
NULL,
// Exit process
NULL,
// Exit master
NULL,
// Version footer
NGX_MODULE_V1_PADDING
};
// Supporting function implementation
// Block access setup
char *blockAccessSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data) {
// Get location configuration
ngx_http_block_access_conf_t *locationConfiguration = data;
// Get arguments
ngx_str_t *arguments = configuration->args->elts;
// Get URL
ngx_str_t *url = &arguments[1];
// Create script compile
ngx_http_script_compile_t scriptCompile;
ngx_memzero(&scriptCompile, sizeof(ngx_http_script_compile_t));
scriptCompile.cf = configuration;
scriptCompile.source = url;
scriptCompile.lengths = &locationConfiguration->urlVariablesLengths;
scriptCompile.values = &locationConfiguration->urlVariablesValues;
scriptCompile.variables = ngx_http_script_variables_count(url);
scriptCompile.complete_lengths = 1;
scriptCompile.complete_values = 1;
// Check if compiling script compile failed
if(ngx_http_script_compile(&scriptCompile) != NGX_OK) {
// Return configuration error
return NGX_CONF_ERROR;
}
// Return configuration ok
return NGX_CONF_OK;
}
// Block setup
char *blockSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data) {
// Get location configuration
const ngx_http_block_access_conf_t *locationConfiguration = data;
// Get arguments
const ngx_str_t *arguments = configuration->args->elts;
// Get start of range
const ngx_str_t *startOfRange = &arguments[1];
// Get end of range
const ngx_str_t *endOfRange = (configuration->args->nelts == 3) ? &arguments[2] : NULL;
// Get blocked address ranges
ngx_array_t **blockedAddressRanges = (ngx_array_t **)((char *)locationConfiguration + command->offset);
// Check if blocked address ranges doesn't exist
if(!*blockedAddressRanges) {
// Check if creating blocked address ranges failed
*blockedAddressRanges = ngx_array_create(configuration->pool, 1, sizeof(BlockedAddressRange));
if(!*blockedAddressRanges) {
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Check if adding value to blocked address ranges failed
BlockedAddressRange *blockedAddressRange = ngx_array_push(*blockedAddressRanges);
if(!blockedAddressRange) {
// Return configuration error
return NGX_CONF_ERROR;
}
// Get start of range as a string
char startOfRangeBuffer[startOfRange->len + sizeof((char)'\0')];
ngx_memcpy(startOfRangeBuffer, startOfRange->data, startOfRange->len);
startOfRangeBuffer[sizeof(startOfRangeBuffer) - sizeof((char)'\0')] = '\0';
// Check if start of range is an IPv4 address
struct sockaddr_in ipv4Address;
if(inet_pton(AF_INET, startOfRangeBuffer, &ipv4Address.sin_addr)) {
// Set blocked address range type and start or range
blockedAddressRange->type = AF_INET;
blockedAddressRange->ipv4StartOfRange = ntohl(ipv4Address.sin_addr.s_addr);
// Check if end of range is provided
if(endOfRange) {
// Get end of range as a string
char endOfRangeBuffer[endOfRange->len + sizeof((char)'\0')];
ngx_memcpy(endOfRangeBuffer, endOfRange->data, endOfRange->len);
endOfRangeBuffer[sizeof(endOfRangeBuffer) - sizeof((char)'\0')] = '\0';
// Check if end of range is an IPv4 address
if(inet_pton(AF_INET, endOfRangeBuffer, &ipv4Address.sin_addr)) {
// Set blocked address range end of range
blockedAddressRange->ipv4EndOfRange = ntohl(ipv4Address.sin_addr.s_addr);
// Check if blocked address range is invalid
if(blockedAddressRange->ipv4StartOfRange > blockedAddressRange->ipv4EndOfRange) {
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", endOfRange);
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Otherwise
else {
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", endOfRange);
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Otherwise
else {
// Set blocked address range to contain only the starting address
blockedAddressRange->ipv4EndOfRange = blockedAddressRange->ipv4StartOfRange;
}
}
// Otherwise
else {
// Check if start of range is an IPv4 address
struct sockaddr_in6 ipv6Address;
if(inet_pton(AF_INET6, startOfRangeBuffer, &ipv6Address.sin6_addr)) {
// Set blocked address range
blockedAddressRange->type = AF_INET6;
memcpy(&blockedAddressRange->ipv6StartOfRange, &ipv6Address.sin6_addr, sizeof(struct in6_addr));
// Check if end of range is provided
if(endOfRange) {
// Get end of range as a string
char endOfRangeBuffer[endOfRange->len + sizeof((char)'\0')];
ngx_memcpy(endOfRangeBuffer, endOfRange->data, endOfRange->len);
endOfRangeBuffer[sizeof(endOfRangeBuffer) - sizeof((char)'\0')] = '\0';
// Check if end of range is an IPv6 address
if(inet_pton(AF_INET6, endOfRangeBuffer, &ipv6Address.sin6_addr)) {
// Set blocked address range end of range
memcpy(&blockedAddressRange->ipv6EndOfRange, &ipv6Address.sin6_addr, sizeof(struct in6_addr));
// Check if blocked address range is invalid
if(memcmp(&blockedAddressRange->ipv6StartOfRange, &blockedAddressRange->ipv6EndOfRange, sizeof(struct in6_addr)) > 0) {
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", endOfRange);
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Otherwise
else {
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", endOfRange);
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Otherwise
else {
// Set blocked address range to contain only the starting address
memcpy(&blockedAddressRange->ipv6EndOfRange, &blockedAddressRange->ipv6StartOfRange, sizeof(struct in6_addr));
}
}
// Otherwise
else {
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", startOfRange);
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Return configuration ok
return NGX_CONF_OK;
}
// Unblock setup
char *unblockSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data) {
// Get location configuration
const ngx_http_block_access_conf_t *locationConfiguration = data;
// Get arguments
ngx_str_t *arguments = configuration->args->elts;
// Get location
ngx_str_t *location = &arguments[1];
// Check if case insensitive is provided
const ngx_uint_t caseInsensitive = location->len && location->data[0] == '~';
// Get unblocked locations
ngx_array_t **unblockedLocations = (ngx_array_t **)((char *)locationConfiguration + command->offset);
// Check if unblocked locations doesn't exist
if(!*unblockedLocations) {
// Check if creating unblocked locations failed
*unblockedLocations = ngx_array_create(configuration->pool, 1, sizeof(ngx_regex_compile_t));
if(!*unblockedLocations) {
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Check if adding value to unblocked locations failed
ngx_regex_compile_t *unblockedLocation = ngx_array_push(*unblockedLocations);
if(!unblockedLocation) {
// Return configuration error
return NGX_CONF_ERROR;
}
// Check if case insensitive
if(caseInsensitive) {
// Remove case insensitive character from location
--location->len;
++location->data;
}
// Check if location is invalid
if(!location->len) {
// Check if case insensitive
if(caseInsensitive) {
// Add case insensitive character to location
++location->len;
--location->data;
}
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", location);
// Return configuration error
return NGX_CONF_ERROR;
}
// Initialize unblocked location
ngx_memzero(unblockedLocation, sizeof(ngx_regex_compile_t));
unblockedLocation->pattern = *location;
unblockedLocation->pool = configuration->pool;
unblockedLocation->options = caseInsensitive ? NGX_REGEX_CASELESS : 0;
u_char errstr[NGX_MAX_CONF_ERRSTR];
unblockedLocation->err.len = sizeof(errstr);
unblockedLocation->err.data = errstr;
// Check if compiling unblocked location failed or the unblocked location contains captures
if(ngx_regex_compile(unblockedLocation) != NGX_OK || unblockedLocation->captures) {
// Check if case insensitive
if(caseInsensitive) {
// Add case insensitive character to location
++location->len;
--location->data;
}
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", location);
// Return configuration error
return NGX_CONF_ERROR;
}
// Return configuration ok
return NGX_CONF_OK;
}
// Allow top level domain setup
char *allowTopLevelDomainSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data) {
// Get location configuration
const ngx_http_block_access_conf_t *locationConfiguration = data;
// Get arguments
const ngx_str_t *arguments = configuration->args->elts;
// Get top-level domain
const ngx_str_t *topLevelDomain = &arguments[1];
// Get allowed top-level domains
ngx_array_t **allowedTopLevelDomains = (ngx_array_t **)((char *)locationConfiguration + command->offset);
// Check if allowed top-level domains doesn't exist
if(!*allowedTopLevelDomains) {
// Check if creating allowed top-level domains failed
*allowedTopLevelDomains = ngx_array_create(configuration->pool, 1, sizeof(ngx_str_t));
if(!*allowedTopLevelDomains) {
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Check if adding value to allowed top-level domains failed
ngx_str_t *allowedTopLevelDomain = ngx_array_push(*allowedTopLevelDomains);
if(!allowedTopLevelDomain) {
// Return configuration error
return NGX_CONF_ERROR;
}
// Set allowed top-level domain
*allowedTopLevelDomain = *topLevelDomain;
// Check if top-level domain is invalid
if(topLevelDomain->len < 3 || topLevelDomain->len > 64 || topLevelDomain->data[0] != '.' || topLevelDomain->data[1] == '-' || topLevelDomain->data[topLevelDomain->len - sizeof((char)'\0')] == '-') {
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", topLevelDomain);
// Return configuration error
return NGX_CONF_ERROR;
}
// Go through all characters in the top-level domain
for(size_t i = sizeof((char)'.'); i < topLevelDomain->len; ++i) {
// Check if character isn't alphanumeric or a hyphen
if(!isalnum(topLevelDomain->data[i]) && topLevelDomain->data[i] != '-') {
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", topLevelDomain);
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Return configuration ok
return NGX_CONF_OK;
}
// Allow method setup
char *allowMethodSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data) {
// Get location configuration
const ngx_http_block_access_conf_t *locationConfiguration = data;
// Get arguments
const ngx_str_t *arguments = configuration->args->elts;
// Get method
const ngx_str_t *method = &arguments[1];
// Get allowed methods
ngx_array_t **allowedMethods = (ngx_array_t **)((char *)locationConfiguration + command->offset);
// Check if allowed methods doesn't exist
if(!*allowedMethods) {
// Check if creating allowed methods failed
*allowedMethods = ngx_array_create(configuration->pool, 1, sizeof(ngx_uint_t));
if(!*allowedMethods) {
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Check if adding value to allowed methods failed
ngx_uint_t *allowedMethod = ngx_array_push(*allowedMethods);
if(!allowedMethod) {
// Return configuration error
return NGX_CONF_ERROR;
}
// Check if method is GET
if(method->len == sizeof("GET") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"GET", method->len)) {
// Set allowed method
*allowedMethod = NGX_HTTP_GET;
}
// Otherwise check if method is HEAD
else if(method->len == sizeof("HEAD") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"HEAD", method->len)) {
// Set allowed method
*allowedMethod = NGX_HTTP_HEAD;
}
// Otherwise check if method is POST
else if(method->len == sizeof("POST") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"POST", method->len)) {
// Set allowed method
*allowedMethod = NGX_HTTP_POST;
}
// Otherwise check if method is PUT
else if(method->len == sizeof("PUT") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"PUT", method->len)) {
// Set allowed method
*allowedMethod = NGX_HTTP_PUT;
}
// Otherwise check if method is DELETE
else if(method->len == sizeof("DELETE") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"DELETE", method->len)) {
// Set allowed method
*allowedMethod = NGX_HTTP_DELETE;
}
// Otherwise check if method is OPTIONS
else if(method->len == sizeof("OPTIONS") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"OPTIONS", method->len)) {
// Set allowed method
*allowedMethod = NGX_HTTP_OPTIONS;
}
// Otherwise check if method is TRACE
else if(method->len == sizeof("TRACE") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"TRACE", method->len)) {
// Set allowed method
*allowedMethod = NGX_HTTP_TRACE;
}
// Otherwise check if method is PATCH
else if(method->len == sizeof("PATCH") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"PATCH", method->len)) {
// Set allowed method
*allowedMethod = NGX_HTTP_PATCH;
}
// Otherwise
else {
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", method);
// Return configuration error
return NGX_CONF_ERROR;
}
// Return configuration ok
return NGX_CONF_OK;
}
// Require header setup
char *requireHeaderSetup(ngx_conf_t *configuration, ngx_command_t *command, void *data) {
// Get location configuration
const ngx_http_block_access_conf_t *locationConfiguration = data;
// Get arguments
ngx_str_t *arguments = configuration->args->elts;
// Get key
const ngx_str_t *key = &arguments[1];
// Get value
ngx_str_t *value = &arguments[2];
// Check if case insensitive is provided
const ngx_uint_t caseInsensitive = value->len && value->data[0] == '~';
// Get method
const ngx_str_t *method = (configuration->args->nelts == 4) ? &arguments[3] : NULL;
// Get required headers
ngx_array_t **requiredHeaders = (ngx_array_t **)((char *)locationConfiguration + command->offset);
// Check if required headers doesn't exist
if(!*requiredHeaders) {
// Check if creating required headers failed
*requiredHeaders = ngx_array_create(configuration->pool, 1, sizeof(RequiredHeader));
if(!*requiredHeaders) {
// Return configuration error
return NGX_CONF_ERROR;
}
}
// Check if adding value to required headers failed
RequiredHeader *requiredHeader = ngx_array_push(*requiredHeaders);
if(!requiredHeader) {
// Return configuration error
return NGX_CONF_ERROR;
}
// Check if key is invalid
if(!key->len) {
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", key);
// Return configuration error
return NGX_CONF_ERROR;
}
// Set required header's key
requiredHeader->key = *key;
// Check if case insensitive
if(caseInsensitive) {
// Remove case insensitive character from value
--value->len;
++value->data;
}
// Check if value is invalid
if(!value->len) {
// Check if case insensitive
if(caseInsensitive) {
// Add case insensitive character to value
++value->len;
--value->data;
}
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", value);
// Return configuration error
return NGX_CONF_ERROR;
}
// Initialize required header's value
ngx_memzero(&requiredHeader->value, sizeof(ngx_regex_compile_t));
requiredHeader->value.pattern = *value;
requiredHeader->value.pool = configuration->pool;
requiredHeader->value.options = caseInsensitive ? NGX_REGEX_CASELESS : 0;
u_char errstr[NGX_MAX_CONF_ERRSTR];
requiredHeader->value.err.len = sizeof(errstr);
requiredHeader->value.err.data = errstr;
// Check if compiling required header's value failed or the required header's value contains captures
if(ngx_regex_compile(&requiredHeader->value) != NGX_OK || requiredHeader->value.captures) {
// Check if case insensitive
if(caseInsensitive) {
// Add case insensitive character to value
++value->len;
--value->data;
}
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", value);
// Return configuration error
return NGX_CONF_ERROR;
}
// Check if method isn't provided
if(!method) {
// Set required header's method
requiredHeader->method = UINTMAX_MAX;
}
// Otherwise check if method is GET
else if(method->len == sizeof("GET") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"GET", method->len)) {
// Set required header's method
requiredHeader->method = NGX_HTTP_GET;
}
// Otherwise check if method is HEAD
else if(method->len == sizeof("HEAD") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"HEAD", method->len)) {
// Set required header's method
requiredHeader->method = NGX_HTTP_HEAD;
}
// Otherwise check if method is POST
else if(method->len == sizeof("POST") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"POST", method->len)) {
// Set required header's method
requiredHeader->method = NGX_HTTP_POST;
}
// Otherwise check if method is PUT
else if(method->len == sizeof("PUT") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"PUT", method->len)) {
// Set required header's method
requiredHeader->method = NGX_HTTP_PUT;
}
// Otherwise check if method is DELETE
else if(method->len == sizeof("DELETE") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"DELETE", method->len)) {
// Set required header's method
requiredHeader->method = NGX_HTTP_DELETE;
}
// Otherwise check if method is OPTIONS
else if(method->len == sizeof("OPTIONS") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"OPTIONS", method->len)) {
// Set required header's method
requiredHeader->method = NGX_HTTP_OPTIONS;
}
// Otherwise check if method is TRACE
else if(method->len == sizeof("TRACE") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"TRACE", method->len)) {
// Set required header's method
requiredHeader->method = NGX_HTTP_TRACE;
}
// Otherwise check if method is PATCH
else if(method->len == sizeof("PATCH") - sizeof((char)'\0') && !ngx_strncasecmp(method->data, (u_char *)"PATCH", method->len)) {
// Set required header's method
requiredHeader->method = NGX_HTTP_PATCH;
}
// Otherwise
else {
// Log error
ngx_conf_log_error(NGX_LOG_EMERG, configuration, 0, "invalid parameter \"%V\"", method);
// Return configuration error
return NGX_CONF_ERROR;
}
// Return configuration ok
return NGX_CONF_OK;
}
// Postconfiguration
ngx_int_t postconfiguration(ngx_conf_t *configuration) {
// Get core configuration
ngx_http_core_main_conf_t *coreConfiguration = ngx_http_conf_get_module_main_conf(configuration, ngx_http_core_module);
// Check if adding handler to HTTP access phase handler failed
ngx_http_handler_pt *handler = ngx_array_push(&coreConfiguration->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if(!handler) {