Newer
Older
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
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* http_protocol.c --- routines which directly communicate with the client.
*
* Code originally by Rob McCool; much redone by Robert S. Thau
* and the Apache Software Foundation.
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_buckets.h"
#include "apr_lib.h"
#include "apr_signal.h"
#define APR_WANT_STDIO /* for sscanf */
#define APR_WANT_STRFUNC
#define APR_WANT_MEMFUNC
#include "apr_want.h"
#include "util_filter.h"
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_main.h"
#include "http_request.h"
#include "http_vhost.h"
#include "http_log.h" /* For errors detected in basic auth common
* support code... */
#include "apr_date.h" /* For apr_date_parse_http and APR_DATE_BAD */
#include "util_charset.h"
#include "util_ebcdic.h"
#include "util_time.h"
#include "ap_mpm.h"
#include "mod_core.h"
#if APR_HAVE_STDARG_H
#include <stdarg.h>
#endif
#if APR_HAVE_UNISTD_H
#include <unistd.h>
#endif
APLOG_USE_MODULE(http);
/* New Apache routine to map status codes into array indicies
* e.g. 100 -> 0, 101 -> 1, 200 -> 2 ...
* The number of status lines must equal the value of
* RESPONSE_CODES (httpd.h) and must be listed in order.
* No gaps are allowed between X00 and the largest Xnn
* for any X (see ap_index_of_response).
* When adding a new code here, add a define to httpd.h
* as well.
*/
static const char * const status_lines[RESPONSE_CODES] =
{
"100 Continue",
"101 Switching Protocols",
"102 Processing",
#define LEVEL_200 3
"200 OK",
"201 Created",
"202 Accepted",
"203 Non-Authoritative Information",
"204 No Content",
"205 Reset Content",
"206 Partial Content",
"207 Multi-Status",
"208 Already Reported",
NULL, /* 209 */
NULL, /* 210 */
NULL, /* 211 */
NULL, /* 212 */
NULL, /* 213 */
NULL, /* 214 */
NULL, /* 215 */
NULL, /* 216 */
NULL, /* 217 */
NULL, /* 218 */
NULL, /* 219 */
NULL, /* 220 */
NULL, /* 221 */
NULL, /* 222 */
NULL, /* 223 */
NULL, /* 224 */
NULL, /* 225 */
"226 IM Used",
#define LEVEL_300 30
"300 Multiple Choices",
"301 Moved Permanently",
"302 Found",
"303 See Other",
"304 Not Modified",
"305 Use Proxy",
NULL, /* 306 */
"307 Temporary Redirect",
"308 Permanent Redirect",
#define LEVEL_400 39
"400 Bad Request",
"401 Unauthorized",
"402 Payment Required",
"403 Forbidden",
"404 Not Found",
"405 Method Not Allowed",
"406 Not Acceptable",
"407 Proxy Authentication Required",
"408 Request Timeout",
"409 Conflict",
"410 Gone",
"411 Length Required",
"412 Precondition Failed",
"413 Request Entity Too Large",
"414 Request-URI Too Long",
"415 Unsupported Media Type",
"416 Requested Range Not Satisfiable",
"417 Expectation Failed",
NULL, /* 418 */
NULL, /* 419 */
NULL, /* 420 */
"421 Misdirected Request",
"422 Unprocessable Entity",
"423 Locked",
"424 Failed Dependency",
NULL, /* 425 */
"426 Upgrade Required",
NULL, /* 427 */
"428 Precondition Required",
"429 Too Many Requests",
NULL, /* 430 */
"431 Request Header Fields Too Large",
NULL, /* 432 */
NULL, /* 433 */
NULL, /* 434 */
NULL, /* 435 */
NULL, /* 436 */
NULL, /* 437 */
NULL, /* 438 */
NULL, /* 439 */
NULL, /* 440 */
NULL, /* 441 */
NULL, /* 442 */
NULL, /* 443 */
NULL, /* 444 */
NULL, /* 445 */
NULL, /* 446 */
NULL, /* 447 */
NULL, /* 448 */
NULL, /* 449 */
NULL, /* 450 */
"451 Unavailable For Legal Reasons",
#define LEVEL_500 91
"500 Internal Server Error",
"501 Not Implemented",
"502 Bad Gateway",
"503 Service Unavailable",
"504 Gateway Timeout",
"505 HTTP Version Not Supported",
"506 Variant Also Negotiates",
"507 Insufficient Storage",
"508 Loop Detected",
NULL, /* 509 */
"510 Not Extended",
"511 Network Authentication Required"
};
APR_HOOK_STRUCT(
APR_HOOK_LINK(insert_error_filter)
)
AP_IMPLEMENT_HOOK_VOID(insert_error_filter, (request_rec *r), (r))
/* The index of the first bit field that is used to index into a limit
* bitmask. M_INVALID + 1 to METHOD_NUMBER_LAST.
*/
#define METHOD_NUMBER_FIRST (M_INVALID + 1)
/* The max method number. Method numbers are used to shift bitmasks,
* so this cannot exceed 63, and all bits high is equal to -1, which is a
* special flag, so the last bit used has index 62.
*/
#define METHOD_NUMBER_LAST 62
static int is_mpm_running(void)
{
int mpm_state = 0;
if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state)) {
return 0;
}
if (mpm_state == AP_MPMQ_STOPPING) {
return 0;
}
return 1;
}
AP_DECLARE(int) ap_set_keepalive(request_rec *r)
{
int ka_sent = 0;
int left = r->server->keep_alive_max - r->connection->keepalives;
int wimpy = ap_find_token(r->pool,
apr_table_get(r->headers_out, "Connection"),
"close");
const char *conn = apr_table_get(r->headers_in, "Connection");
/* The following convoluted conditional determines whether or not
* the current connection should remain persistent after this response
* (a.k.a. HTTP Keep-Alive) and whether or not the output message
* body should use the HTTP/1.1 chunked transfer-coding. In English,
*
* IF we have not marked this connection as errored;
* and the client isn't expecting 100-continue (PR47087 - more
* input here could be the client continuing when we're
* closing the request).
* and the response body has a defined length due to the status code
* being 304 or 204, the request method being HEAD, already
* having defined Content-Length or Transfer-Encoding: chunked, or
* the request version being HTTP/1.1 and thus capable of being set
* as chunked [we know the (r->chunked = 1) side-effect is ugly];
* and the server configuration enables keep-alive;
* and the server configuration has a reasonable inter-request timeout;
* and there is no maximum # requests or the max hasn't been reached;
* and the response status does not require a close;
* and the response generator has not already indicated close;
* and the client did not request non-persistence (Connection: close);
* and we haven't been configured to ignore the buggy twit
* or they're a buggy twit coming through a HTTP/1.1 proxy
* and the client is requesting an HTTP/1.0-style keep-alive
* or the client claims to be HTTP/1.1 compliant (perhaps a proxy);
* and this MPM process is not already exiting
* THEN we can be persistent, which requires more headers be output.
*
* Note that the condition evaluation order is extremely important.
*/
if ((r->connection->keepalive != AP_CONN_CLOSE)
&& !r->expecting_100
&& ((r->status == HTTP_NOT_MODIFIED)
|| (r->status == HTTP_NO_CONTENT)
|| r->header_only
|| apr_table_get(r->headers_out, "Content-Length")
|| ap_find_last_token(r->pool,
apr_table_get(r->headers_out,
"Transfer-Encoding"),
"chunked")
|| ((r->proto_num >= HTTP_VERSION(1,1))
&& (r->chunked = 1))) /* THIS CODE IS CORRECT, see above. */
&& r->server->keep_alive
&& (r->server->keep_alive_timeout > 0)
&& ((r->server->keep_alive_max == 0)
|| (left > 0))
&& !ap_status_drops_connection(r->status)
&& !wimpy
&& !ap_find_token(r->pool, conn, "close")
&& (!apr_table_get(r->subprocess_env, "nokeepalive")
|| apr_table_get(r->headers_in, "Via"))
&& ((ka_sent = ap_find_token(r->pool, conn, "keep-alive"))
|| (r->proto_num >= HTTP_VERSION(1,1)))
&& is_mpm_running()) {
r->connection->keepalive = AP_CONN_KEEPALIVE;
r->connection->keepalives++;
/* If they sent a Keep-Alive token, send one back */
if (ka_sent) {
if (r->server->keep_alive_max) {
apr_table_setn(r->headers_out, "Keep-Alive",
apr_psprintf(r->pool, "timeout=%d, max=%d",
(int)apr_time_sec(r->server->keep_alive_timeout),
left));
}
else {
apr_table_setn(r->headers_out, "Keep-Alive",
apr_psprintf(r->pool, "timeout=%d",
(int)apr_time_sec(r->server->keep_alive_timeout)));
}
apr_table_mergen(r->headers_out, "Connection", "Keep-Alive");
}
return 1;
}
/* Otherwise, we need to indicate that we will be closing this
* connection immediately after the current response.
*
* We only really need to send "close" to HTTP/1.1 clients, but we
* always send it anyway, because a broken proxy may identify itself
* as HTTP/1.0, but pass our request along with our HTTP/1.1 tag
* to a HTTP/1.1 client. Better safe than sorry.
*/
if (!wimpy) {
apr_table_mergen(r->headers_out, "Connection", "close");
}
/*
* If we had previously been a keepalive connection and this
* is the last one, then bump up the number of keepalives
* we've had
*/
if ((r->connection->keepalive != AP_CONN_CLOSE)
&& r->server->keep_alive_max
&& !left) {
r->connection->keepalives++;
}
r->connection->keepalive = AP_CONN_CLOSE;
return 0;
}
AP_DECLARE(ap_condition_e) ap_condition_if_match(request_rec *r,
apr_table_t *headers)
{
const char *if_match, *etag;
/* A server MUST use the strong comparison function (see section 13.3.3)
* to compare the entity tags in If-Match.
*/
if ((if_match = apr_table_get(r->headers_in, "If-Match")) != NULL) {
if (if_match[0] == '*'
|| ((etag = apr_table_get(headers, "ETag")) != NULL
&& ap_find_etag_strong(r->pool, if_match, etag))) {
return AP_CONDITION_STRONG;
}
else {
return AP_CONDITION_NOMATCH;
}
}
return AP_CONDITION_NONE;
}
AP_DECLARE(ap_condition_e) ap_condition_if_unmodified_since(request_rec *r,
apr_table_t *headers)
{
const char *if_unmodified;
if_unmodified = apr_table_get(r->headers_in, "If-Unmodified-Since");
if (if_unmodified) {
apr_int64_t mtime, reqtime;
apr_time_t ius = apr_time_sec(apr_date_parse_http(if_unmodified));
/* All of our comparisons must be in seconds, because that's the
* highest time resolution the HTTP specification allows.
*/
mtime = apr_time_sec(apr_date_parse_http(
apr_table_get(headers, "Last-Modified")));
if (mtime == APR_DATE_BAD) {
mtime = apr_time_sec(r->mtime ? r->mtime : apr_time_now());
}
reqtime = apr_time_sec(apr_date_parse_http(
apr_table_get(headers, "Date")));
if (!reqtime) {
reqtime = apr_time_sec(r->request_time);
}
if ((ius != APR_DATE_BAD) && (mtime > ius)) {
if (reqtime < mtime + 60) {
if (apr_table_get(r->headers_in, "Range")) {
/* weak matches not allowed with Range requests */
return AP_CONDITION_NOMATCH;
}
else {
return AP_CONDITION_WEAK;
}
}
else {
return AP_CONDITION_STRONG;
}
}
else {
return AP_CONDITION_NOMATCH;
}
}
return AP_CONDITION_NONE;
}
AP_DECLARE(ap_condition_e) ap_condition_if_none_match(request_rec *r,
apr_table_t *headers)
{
const char *if_nonematch, *etag;
if_nonematch = apr_table_get(r->headers_in, "If-None-Match");
if (if_nonematch != NULL) {
if (if_nonematch[0] == '*') {
return AP_CONDITION_STRONG;
}
/* See section 13.3.3 for rules on how to determine if two entities tags
* match. The weak comparison function can only be used with GET or HEAD
* requests.
*/
if (r->method_number == M_GET) {
if ((etag = apr_table_get(headers, "ETag")) != NULL) {
if (apr_table_get(r->headers_in, "Range")) {
if (ap_find_etag_strong(r->pool, if_nonematch, etag)) {
return AP_CONDITION_STRONG;
}
}
else {
if (ap_find_etag_weak(r->pool, if_nonematch, etag)) {
return AP_CONDITION_WEAK;
}
}
}
}
else if ((etag = apr_table_get(headers, "ETag")) != NULL
&& ap_find_etag_strong(r->pool, if_nonematch, etag)) {
return AP_CONDITION_STRONG;
}
return AP_CONDITION_NOMATCH;
}
return AP_CONDITION_NONE;
}
AP_DECLARE(ap_condition_e) ap_condition_if_modified_since(request_rec *r,
apr_table_t *headers)
{
const char *if_modified_since;
if ((if_modified_since = apr_table_get(r->headers_in, "If-Modified-Since"))
!= NULL) {
apr_int64_t mtime;
apr_int64_t ims, reqtime;
/* All of our comparisons must be in seconds, because that's the
* highest time resolution the HTTP specification allows.
*/
mtime = apr_time_sec(apr_date_parse_http(
apr_table_get(headers, "Last-Modified")));
if (mtime == APR_DATE_BAD) {
mtime = apr_time_sec(r->mtime ? r->mtime : apr_time_now());
}
reqtime = apr_time_sec(apr_date_parse_http(
apr_table_get(headers, "Date")));
if (!reqtime) {
reqtime = apr_time_sec(r->request_time);
}
ims = apr_time_sec(apr_date_parse_http(if_modified_since));
if (ims >= mtime && ims <= reqtime) {
if (reqtime < mtime + 60) {
if (apr_table_get(r->headers_in, "Range")) {
/* weak matches not allowed with Range requests */
return AP_CONDITION_NOMATCH;
}
else {
return AP_CONDITION_WEAK;
}
}
else {
return AP_CONDITION_STRONG;
}
}
else {
return AP_CONDITION_NOMATCH;
}
}
return AP_CONDITION_NONE;
}
AP_DECLARE(ap_condition_e) ap_condition_if_range(request_rec *r,
apr_table_t *headers)
{
const char *if_range, *etag;
if ((if_range = apr_table_get(r->headers_in, "If-Range"))
&& apr_table_get(r->headers_in, "Range")) {
if (if_range[0] == '"') {
if ((etag = apr_table_get(headers, "ETag"))
&& !strcmp(if_range, etag)) {
return AP_CONDITION_STRONG;
}
else {
return AP_CONDITION_NOMATCH;
}
}
else {
apr_int64_t mtime;
apr_int64_t rtime, reqtime;
/* All of our comparisons must be in seconds, because that's the
* highest time resolution the HTTP specification allows.
*/
mtime = apr_time_sec(apr_date_parse_http(
apr_table_get(headers, "Last-Modified")));
if (mtime == APR_DATE_BAD) {
mtime = apr_time_sec(r->mtime ? r->mtime : apr_time_now());
}
reqtime = apr_time_sec(apr_date_parse_http(
apr_table_get(headers, "Date")));
if (!reqtime) {
reqtime = apr_time_sec(r->request_time);
}
rtime = apr_time_sec(apr_date_parse_http(if_range));
if (rtime == mtime) {
if (reqtime < mtime + 60) {
/* weak matches not allowed with Range requests */
return AP_CONDITION_NOMATCH;
}
else {
return AP_CONDITION_STRONG;
}
}
else {
return AP_CONDITION_NOMATCH;
}
}
}
return AP_CONDITION_NONE;
}
AP_DECLARE(int) ap_meets_conditions(request_rec *r)
{
int not_modified = -1; /* unset by default */
ap_condition_e cond;
/* Check for conditional requests --- note that we only want to do
* this if we are successful so far and we are not processing a
* subrequest or an ErrorDocument.
*
* The order of the checks is important, since ETag checks are supposed
* to be more accurate than checks relative to the modification time.
* However, not all documents are guaranteed to *have* ETags, and some
* might have Last-Modified values w/o ETags, so this gets a little
* complicated.
*/
if (!ap_is_HTTP_SUCCESS(r->status) || r->no_local_copy) {
return OK;
}
/* If an If-Match request-header field was given
* AND the field value is not "*" (meaning match anything)
* AND if our strong ETag does not match any entity tag in that field,
* respond with a status of 412 (Precondition Failed).
*/
cond = ap_condition_if_match(r, r->headers_out);
if (AP_CONDITION_NOMATCH == cond) {
return HTTP_PRECONDITION_FAILED;
}
/* Else if a valid If-Unmodified-Since request-header field was given
* AND the requested resource has been modified since the time
* specified in this field, then the server MUST
* respond with a status of 412 (Precondition Failed).
*/
cond = ap_condition_if_unmodified_since(r, r->headers_out);
if (AP_CONDITION_NOMATCH == cond) {
not_modified = 0;
}
else if (cond >= AP_CONDITION_WEAK) {
return HTTP_PRECONDITION_FAILED;
}
/* If an If-None-Match request-header field was given
* AND the field value is "*" (meaning match anything)
* OR our ETag matches any of the entity tags in that field, fail.
*
* If the request method was GET or HEAD, failure means the server
* SHOULD respond with a 304 (Not Modified) response.
* For all other request methods, failure means the server MUST
* respond with a status of 412 (Precondition Failed).
*
* GET or HEAD allow weak etag comparison, all other methods require
* strong comparison. We can only use weak if it's not a range request.
*/
cond = ap_condition_if_none_match(r, r->headers_out);
if (AP_CONDITION_NOMATCH == cond) {
not_modified = 0;
}
else if (cond >= AP_CONDITION_WEAK) {
if (r->method_number == M_GET) {
if (not_modified) {
not_modified = 1;
}
}
else {
return HTTP_PRECONDITION_FAILED;
}
}
/* If a valid If-Modified-Since request-header field was given
* AND it is a GET or HEAD request
* AND the requested resource has not been modified since the time
* specified in this field, then the server MUST
* respond with a status of 304 (Not Modified).
* A date later than the server's current request time is invalid.
*/
cond = ap_condition_if_modified_since(r, r->headers_out);
if (AP_CONDITION_NOMATCH == cond) {
not_modified = 0;
}
else if (cond >= AP_CONDITION_WEAK) {
if (r->method_number == M_GET) {
if (not_modified) {
not_modified = 1;
}
}
}
/* If an If-Range and an Range header is present, we must return
* 200 OK. The byterange filter will convert it to a range response.
*/
cond = ap_condition_if_range(r, r->headers_out);
if (cond > AP_CONDITION_NONE) {
return OK;
}
if (not_modified == 1) {
return HTTP_NOT_MODIFIED;
}
return OK;
}
/**
* Singleton registry of additional methods. This maps new method names
* such as "MYGET" to methnums, which are int offsets into bitmasks.
*
* This follows the same technique as standard M_GET, M_POST, etc. These
* are dynamically assigned when modules are loaded and <Limit GET MYGET>
* directives are processed.
*/
static apr_hash_t *methods_registry = NULL;
static int cur_method_number = METHOD_NUMBER_FIRST;
/* internal function to register one method/number pair */
static void register_one_method(apr_pool_t *p, const char *methname,
int methnum)
{
int *pnum = apr_palloc(p, sizeof(*pnum));
*pnum = methnum;
apr_hash_set(methods_registry, methname, APR_HASH_KEY_STRING, pnum);
}
/* This internal function is used to clear the method registry
* and reset the cur_method_number counter.
*/
static apr_status_t ap_method_registry_destroy(void *notused)
{
methods_registry = NULL;
cur_method_number = METHOD_NUMBER_FIRST;
return APR_SUCCESS;
}
AP_DECLARE(void) ap_method_registry_init(apr_pool_t *p)
{
methods_registry = apr_hash_make(p);
apr_pool_cleanup_register(p, NULL,
ap_method_registry_destroy,
apr_pool_cleanup_null);
/* put all the standard methods into the registry hash to ease the
* mapping operations between name and number
* HEAD is a special-instance of the GET method and shares the same ID
*/
register_one_method(p, "GET", M_GET);
register_one_method(p, "HEAD", M_GET);
register_one_method(p, "PUT", M_PUT);
register_one_method(p, "POST", M_POST);
register_one_method(p, "DELETE", M_DELETE);
register_one_method(p, "CONNECT", M_CONNECT);
register_one_method(p, "OPTIONS", M_OPTIONS);
register_one_method(p, "TRACE", M_TRACE);
register_one_method(p, "PATCH", M_PATCH);
register_one_method(p, "PROPFIND", M_PROPFIND);
register_one_method(p, "PROPPATCH", M_PROPPATCH);
register_one_method(p, "MKCOL", M_MKCOL);
register_one_method(p, "COPY", M_COPY);
register_one_method(p, "MOVE", M_MOVE);
register_one_method(p, "LOCK", M_LOCK);
register_one_method(p, "UNLOCK", M_UNLOCK);
register_one_method(p, "VERSION-CONTROL", M_VERSION_CONTROL);
register_one_method(p, "CHECKOUT", M_CHECKOUT);
register_one_method(p, "UNCHECKOUT", M_UNCHECKOUT);
register_one_method(p, "CHECKIN", M_CHECKIN);
register_one_method(p, "UPDATE", M_UPDATE);
register_one_method(p, "LABEL", M_LABEL);
register_one_method(p, "REPORT", M_REPORT);
register_one_method(p, "MKWORKSPACE", M_MKWORKSPACE);
register_one_method(p, "MKACTIVITY", M_MKACTIVITY);
register_one_method(p, "BASELINE-CONTROL", M_BASELINE_CONTROL);
register_one_method(p, "MERGE", M_MERGE);
}
AP_DECLARE(int) ap_method_register(apr_pool_t *p, const char *methname)
{
int *methnum;
if (methods_registry == NULL) {
ap_method_registry_init(p);
}
if (methname == NULL) {
return M_INVALID;
}
/* Check if the method was previously registered. If it was
* return the associated method number.
*/
methnum = (int *)apr_hash_get(methods_registry, methname,
APR_HASH_KEY_STRING);
if (methnum != NULL)
return *methnum;
if (cur_method_number > METHOD_NUMBER_LAST) {
/* The method registry has run out of dynamically
* assignable method numbers. Log this and return M_INVALID.
*/
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, APLOGNO(01610)
"Maximum new request methods %d reached while "
"registering method %s.",
METHOD_NUMBER_LAST, methname);
return M_INVALID;
}
register_one_method(p, methname, cur_method_number);
return cur_method_number++;
}
#define UNKNOWN_METHOD (-1)
static int lookup_builtin_method(const char *method, apr_size_t len)
{
/* Note: the following code was generated by the "shilka" tool from
the "cocom" parsing/compilation toolkit. It is an optimized lookup
based on analysis of the input keywords. Postprocessing was done
on the shilka output, but the basic structure and analysis is
from there. Should new HTTP methods be added, then manual insertion
into this code is fine, or simply re-running the shilka tool on
the appropriate input. */
/* Note: it is also quite reasonable to just use our method_registry,
but I'm assuming (probably incorrectly) we want more speed here
(based on the optimizations the previous code was doing). */
switch (len)
{
case 3:
switch (method[0])
{
case 'P':
return (method[1] == 'U'
&& method[2] == 'T'
? M_PUT : UNKNOWN_METHOD);
case 'G':
return (method[1] == 'E'
&& method[2] == 'T'
? M_GET : UNKNOWN_METHOD);
default:
return UNKNOWN_METHOD;
}
case 4:
switch (method[0])
{
case 'H':
return (method[1] == 'E'
&& method[2] == 'A'
&& method[3] == 'D'
? M_GET : UNKNOWN_METHOD);
case 'P':
return (method[1] == 'O'
&& method[2] == 'S'
&& method[3] == 'T'
? M_POST : UNKNOWN_METHOD);
case 'M':
return (method[1] == 'O'
&& method[2] == 'V'
&& method[3] == 'E'
? M_MOVE : UNKNOWN_METHOD);
case 'L':
return (method[1] == 'O'
&& method[2] == 'C'
&& method[3] == 'K'
? M_LOCK : UNKNOWN_METHOD);
case 'C':
return (method[1] == 'O'
&& method[2] == 'P'
&& method[3] == 'Y'
? M_COPY : UNKNOWN_METHOD);
default:
return UNKNOWN_METHOD;
}
case 5:
switch (method[2])
{
case 'T':
return (memcmp(method, "PATCH", 5) == 0
? M_PATCH : UNKNOWN_METHOD);
case 'R':
return (memcmp(method, "MERGE", 5) == 0
? M_MERGE : UNKNOWN_METHOD);
case 'C':
return (memcmp(method, "MKCOL", 5) == 0
? M_MKCOL : UNKNOWN_METHOD);
case 'B':
return (memcmp(method, "LABEL", 5) == 0
? M_LABEL : UNKNOWN_METHOD);
case 'A':
return (memcmp(method, "TRACE", 5) == 0
? M_TRACE : UNKNOWN_METHOD);
default:
return UNKNOWN_METHOD;
}
case 6:
switch (method[0])
{
case 'U':
switch (method[5])
{
case 'K':
return (memcmp(method, "UNLOCK", 6) == 0
? M_UNLOCK : UNKNOWN_METHOD);
case 'E':
return (memcmp(method, "UPDATE", 6) == 0
? M_UPDATE : UNKNOWN_METHOD);
default:
return UNKNOWN_METHOD;
}
case 'R':
return (memcmp(method, "REPORT", 6) == 0
? M_REPORT : UNKNOWN_METHOD);
case 'D':
return (memcmp(method, "DELETE", 6) == 0
? M_DELETE : UNKNOWN_METHOD);
default:
return UNKNOWN_METHOD;
}
case 7:
switch (method[1])
{
case 'P':
return (memcmp(method, "OPTIONS", 7) == 0
? M_OPTIONS : UNKNOWN_METHOD);
case 'O':
return (memcmp(method, "CONNECT", 7) == 0
? M_CONNECT : UNKNOWN_METHOD);
case 'H':
return (memcmp(method, "CHECKIN", 7) == 0
? M_CHECKIN : UNKNOWN_METHOD);
default:
return UNKNOWN_METHOD;
}
case 8:
switch (method[0])
{
case 'P':
return (memcmp(method, "PROPFIND", 8) == 0
? M_PROPFIND : UNKNOWN_METHOD);
case 'C':
return (memcmp(method, "CHECKOUT", 8) == 0
? M_CHECKOUT : UNKNOWN_METHOD);
default:
return UNKNOWN_METHOD;
}
case 9:
return (memcmp(method, "PROPPATCH", 9) == 0
? M_PROPPATCH : UNKNOWN_METHOD);
case 10:
switch (method[0])
{
case 'U':
return (memcmp(method, "UNCHECKOUT", 10) == 0
? M_UNCHECKOUT : UNKNOWN_METHOD);
case 'M':
return (memcmp(method, "MKACTIVITY", 10) == 0
? M_MKACTIVITY : UNKNOWN_METHOD);
default:
return UNKNOWN_METHOD;
}
case 11:
return (memcmp(method, "MKWORKSPACE", 11) == 0
? M_MKWORKSPACE : UNKNOWN_METHOD);
case 15:
return (memcmp(method, "VERSION-CONTROL", 15) == 0
? M_VERSION_CONTROL : UNKNOWN_METHOD);
case 16:
return (memcmp(method, "BASELINE-CONTROL", 16) == 0
? M_BASELINE_CONTROL : UNKNOWN_METHOD);
default:
return UNKNOWN_METHOD;
}
/* NOTREACHED */
}
/* Get the method number associated with the given string, assumed to
* contain an HTTP method. Returns M_INVALID if not recognized.
*
* This is the first step toward placing method names in a configurable
* list. Hopefully it (and other routines) can eventually be moved to
* something like a mod_http_methods.c, complete with config stuff.
*/
AP_DECLARE(int) ap_method_number_of(const char *method)
{
int len = strlen(method);
int which = lookup_builtin_method(method, len);
if (which != UNKNOWN_METHOD)
return which;
/* check if the method has been dynamically registered */
if (methods_registry != NULL) {
int *methnum = apr_hash_get(methods_registry, method, len);
if (methnum != NULL) {
return *methnum;
}
}
return M_INVALID;
}
/*
* Turn a known method number into a name.
*/
AP_DECLARE(const char *) ap_method_name_of(apr_pool_t *p, int methnum)
{
apr_hash_index_t *hi = apr_hash_first(p, methods_registry);
/* scan through the hash table, looking for a value that matches
the provided method number. */
for (; hi; hi = apr_hash_next(hi)) {
const void *key;
void *val;
apr_hash_this(hi, &key, NULL, &val);
if (*(int *)val == methnum)
return key;
}
/* it wasn't found in the hash */
return NULL;
}
/* The index is found by its offset from the x00 code of each level.
* Although this is fast, it will need to be replaced if some nutcase
* decides to define a high-numbered code before the lower numbers.
* If that sad event occurs, replace the code below with a linear search
* from status_lines[shortcut[i]] to status_lines[shortcut[i+1]-1];
* or use NULL to fill the gaps.
*/
AP_DECLARE(int) ap_index_of_response(int status)
{
static int shortcut[6] = {0, LEVEL_200, LEVEL_300, LEVEL_400,
LEVEL_500, RESPONSE_CODES};
int i, pos;
if (status < 100) { /* Below 100 is illegal for HTTP status */
return LEVEL_500;
}