Coverage Report

Created: 2026-08-31 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/usr/include/apache2/http_protocol.h
Line
Count
Source
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
 * contributor license agreements.  See the NOTICE file distributed with
3
 * this work for additional information regarding copyright ownership.
4
 * The ASF licenses this file to You under the Apache License, Version 2.0
5
 * (the "License"); you may not use this file except in compliance with
6
 * the License.  You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
/**
18
 * @file  http_protocol.h
19
 * @brief HTTP protocol handling
20
 *
21
 * @defgroup APACHE_CORE_PROTO HTTP Protocol Handling
22
 * @ingroup  APACHE_CORE
23
 * @{
24
 */
25
26
#ifndef APACHE_HTTP_PROTOCOL_H
27
#define APACHE_HTTP_PROTOCOL_H
28
29
#include "httpd.h"
30
#include "apr_portable.h"
31
#include "apr_mmap.h"
32
#include "apr_buckets.h"
33
#include "util_filter.h"
34
35
#ifdef __cplusplus
36
extern "C" {
37
#endif
38
39
/**
40
 * This hook allows modules to insert filters for the current error response
41
 * @param r the current request
42
 * @ingroup hooks
43
 */
44
AP_DECLARE_HOOK(void,insert_error_filter,(request_rec *r))
45
46
/** This is an optimization.  We keep a record of the filter_rec that
47
 * stores the old_write filter, so that we can avoid strcmp's later.
48
 */
49
AP_DECLARE_DATA extern ap_filter_rec_t *ap_old_write_func;
50
51
/*
52
 * Prototypes for routines which either talk directly back to the user,
53
 * or control the ones that eventually do.
54
 */
55
56
/**
57
 * Read an empty request and set reasonable defaults.
58
 * @param c The current connection
59
 * @return The new request_rec
60
 */
61
AP_DECLARE(request_rec *) ap_create_request(conn_rec *c);
62
63
/**
64
 * Read a request and fill in the fields.
65
 * @param c The current connection
66
 * @return The new request_rec
67
 */
68
request_rec *ap_read_request(conn_rec *c);
69
70
/**
71
 * Parse and validate the request line.
72
 * @param r The current request
73
 * @return 1 on success, 0 on failure
74
 */
75
AP_DECLARE(int) ap_parse_request_line(request_rec *r);
76
77
/**
78
 * Validate the request header and select vhost.
79
 * @param r The current request
80
 * @return 1 on success, 0 on failure
81
 */
82
AP_DECLARE(int) ap_check_request_header(request_rec *r);
83
84
/**
85
 * Read the mime-encoded headers.
86
 * @param r The current request
87
 */
88
AP_DECLARE(void) ap_get_mime_headers(request_rec *r);
89
90
/**
91
 * Optimized version of ap_get_mime_headers() that requires a
92
 * temporary brigade to work with
93
 * @param r The current request
94
 * @param bb temp brigade
95
 */
96
AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r,
97
                                          apr_bucket_brigade *bb);
98
99
/**
100
 * Run post_read_request hook and validate.
101
 * @param r The current request
102
 * @return OK or HTTP_...
103
 */
104
AP_DECLARE(int) ap_post_read_request(request_rec *r);
105
106
/* Finish up stuff after a request */
107
108
/**
109
 * Called at completion of sending the response.  It sends the terminating
110
 * protocol information.
111
 * @param r The current request
112
 */
113
AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
114
115
/**
116
 * Send error back to client.
117
 * @param r The current request
118
 * @param recursive_error last arg indicates error status in case we get
119
 *      an error in the process of trying to deal with an ErrorDocument
120
 *      to handle some other error.  In that case, we print the default
121
 *      report for the first thing that went wrong, and more briefly report
122
 *      on the problem with the ErrorDocument.
123
 */
124
AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
125
126
/* Set last modified header line from the lastmod date of the associated file.
127
 * Also, set content length.
128
 *
129
 * May return an error status, typically HTTP_NOT_MODIFIED (that when the
130
 * permit_cache argument is set to one).
131
 */
132
133
/**
134
 * Set the content length for this request
135
 * @param r The current request
136
 * @param length The new content length
137
 */
138
AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
139
140
/**
141
 * Set the keepalive status for this request
142
 * @param r The current request
143
 * @return 1 if keepalive can be set, 0 otherwise
144
 */
145
AP_DECLARE(int) ap_set_keepalive(request_rec *r);
146
147
/**
148
 * Return the latest rational time from a request/mtime pair.  Mtime is
149
 * returned unless it's in the future, in which case we return the current time.
150
 * @param r The current request
151
 * @param mtime The last modified time
152
 * @return the latest rational time.
153
 */
154
AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
155
156
/**
157
 * Build the content-type that should be sent to the client from the
158
 * content-type specified.  The following rules are followed:
159
 *    - if type is NULL or "", return NULL (do not set content-type).
160
 *    - if charset adding is disabled, stop processing and return type.
161
 *    - then, if there are no parameters on type, add the default charset
162
 *    - return type
163
 * @param r The current request
164
 * @param type The content type
165
 * @return The content-type
166
 */
167
AP_DECLARE(const char *) ap_make_content_type(request_rec *r,
168
                                              const char *type);
169
170
/**
171
 * Precompile metadata structures used by ap_make_content_type()
172
 * @param pool The pool to use for allocations
173
 */
174
AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool);
175
176
/** A structure with the ingredients for a file based etag */
177
typedef struct etag_rec etag_rec;
178
179
/**
180
 * @brief A structure with the ingredients for a file based etag
181
 */
182
struct etag_rec {
183
    /** Optional vary list validator */
184
    const char *vlist_validator;
185
    /** Time when the request started */
186
    apr_time_t request_time;
187
    /** finfo.protection (st_mode) set to zero if no such file */
188
    apr_finfo_t *finfo;
189
    /** File pathname used when generating a digest */
190
    const char *pathname;
191
    /** File descriptor used when generating a digest */
192
    apr_file_t *fd;
193
    /** Force a non-digest etag to be weak */
194
    int force_weak;
195
};
196
197
/**
198
 * Construct an entity tag from the resource information.  If it's a real
199
 * file, build in some of the file characteristics.
200
 * @param r The current request
201
 * @param force_weak Force the entity tag to be weak - it could be modified
202
 *                   again in as short an interval.
203
 * @return The entity tag
204
 */
205
AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
206
207
/**
208
 * Construct an entity tag from information provided in the etag_rec
209
 * structure.
210
 * @param r The current request
211
 * @param er The etag record, containing ingredients for the etag.
212
 */
213
AP_DECLARE(char *) ap_make_etag_ex(request_rec *r, etag_rec *er);
214
215
/**
216
 * Set the E-tag outgoing header
217
 * @param r The current request
218
 */
219
AP_DECLARE(void) ap_set_etag(request_rec *r);
220
221
/**
222
 * Set the E-tag outgoing header, with the option of forcing a strong ETag.
223
 * @param r The current request
224
 * @param fd The file descriptor
225
 */
226
AP_DECLARE(void) ap_set_etag_fd(request_rec *r, apr_file_t *fd);
227
228
/**
229
 * Set the last modified time for the file being sent
230
 * @param r The current request
231
 */
232
AP_DECLARE(void) ap_set_last_modified(request_rec *r);
233
234
typedef enum {
235
    AP_CONDITION_NONE,
236
    AP_CONDITION_NOMATCH,
237
    AP_CONDITION_WEAK,
238
    AP_CONDITION_STRONG
239
} ap_condition_e;
240
241
/**
242
 * Tests conditional request rules for the If-Match header.
243
 * @param r The current request
244
 * @param headers The response headers to check against
245
 * @return AP_CONDITION_NONE if the header is missing, AP_CONDITION_NOMATCH
246
 *         if the header does not match, AP_CONDITION_STRONG for a strong
247
 *         match. Weak matches are not permitted for the If-Match header.
248
 */
249
AP_DECLARE(ap_condition_e) ap_condition_if_match(request_rec *r,
250
        apr_table_t *headers);
251
252
/**
253
 * Tests conditional request rules for the If-Unmodified-Since header.
254
 * @param r The current request
255
 * @param headers The response headers to check against
256
 * @return AP_CONDITION_NONE if the header is missing, AP_CONDITION_NOMATCH
257
 *         if the header does not match, AP_CONDITION_WEAK if a weak match
258
 *         was present and allowed by RFC2616, AP_CONDITION_STRONG for a
259
 *         strong match.
260
 */
261
AP_DECLARE(ap_condition_e) ap_condition_if_unmodified_since(request_rec *r,
262
        apr_table_t *headers);
263
264
/**
265
 * Tests conditional request rules for the If-None-Match header.
266
 * @param r The current request
267
 * @param headers The response headers to check against
268
 * @return AP_CONDITION_NONE if the header is missing, AP_CONDITION_NOMATCH
269
 *         if the header does not match, AP_CONDITION_WEAK if a weak match
270
 *         was present and allowed by RFC2616, AP_CONDITION_STRONG for a
271
 *         strong match.
272
 */
273
AP_DECLARE(ap_condition_e) ap_condition_if_none_match(request_rec *r,
274
        apr_table_t *headers);
275
276
/**
277
 * Tests conditional request rules for the If-Modified-Since header.
278
 * @param r The current request
279
 * @param headers The response headers to check against
280
 * @return AP_CONDITION_NONE if the header is missing, AP_CONDITION_NOMATCH
281
 *         if the header does not match, AP_CONDITION_WEAK if a weak match
282
 *         was present and allowed by RFC2616, AP_CONDITION_STRONG for a
283
 *         strong match.
284
 */
285
AP_DECLARE(ap_condition_e) ap_condition_if_modified_since(request_rec *r,
286
        apr_table_t *headers);
287
288
/**
289
 * Tests conditional request rules for the If-Range header.
290
 * @param r The current request
291
 * @param headers The response headers to check against
292
 * @return AP_CONDITION_NONE if either the If-Range or Range header is
293
 *         missing, AP_CONDITION_NOMATCH if the header does not match,
294
 *         AP_CONDITION_STRONG for a strong match. Weak matches are not
295
 *         permitted for the If-Range header.
296
 */
297
AP_DECLARE(ap_condition_e) ap_condition_if_range(request_rec *r,
298
        apr_table_t *headers);
299
300
/**
301
 * Implements condition GET rules for HTTP/1.1 specification.  This function
302
 * inspects the client headers and determines if the response fulfills
303
 * the requirements specified.
304
 * @param r The current request
305
 * @return OK if the response fulfills the condition GET rules, some
306
 *         other status code otherwise
307
 */
308
AP_DECLARE(int) ap_meets_conditions(request_rec *r);
309
310
/* Other ways to send stuff at the client.  All of these keep track
311
 * of bytes_sent automatically.  This indirection is intended to make
312
 * it a little more painless to slide things like HTTP-NG packetization
313
 * underneath the main body of the code later.  In the meantime, it lets
314
 * us centralize a bit of accounting (bytes_sent).
315
 *
316
 * These also return the number of bytes written by the call.
317
 * They should only be called with a timeout registered, for obvious reaasons.
318
 * (Ditto the send_header stuff).
319
 */
320
321
/**
322
 * Send an entire file to the client, using sendfile if supported by the
323
 * current platform
324
 * @param fd The file to send.
325
 * @param r The current request
326
 * @param offset Offset into the file to start sending.
327
 * @param length Amount of data to send
328
 * @param nbytes Amount of data actually sent
329
 */
330
AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
331
                                   apr_size_t length, apr_size_t *nbytes);
332
333
#if APR_HAS_MMAP
334
/**
335
 * Send an MMAP'ed file to the client
336
 * @param mm The MMAP'ed file to send
337
 * @param r The current request
338
 * @param offset The offset into the MMAP to start sending
339
 * @param length The amount of data to send
340
 * @return The number of bytes sent
341
 */
342
AP_DECLARE(apr_size_t) ap_send_mmap(apr_mmap_t *mm,
343
                                    request_rec *r,
344
                                    apr_size_t offset,
345
                                    apr_size_t length);
346
#endif
347
348
349
/**
350
 * Register a new request method, and return the offset that will be
351
 * associated with that method.
352
 *
353
 * @param p        The pool to create registered method numbers from.
354
 * @param methname The name of the new method to register.
355
 * @return         An int value representing an offset into a bitmask.
356
 */
357
AP_DECLARE(int) ap_method_register(apr_pool_t *p, const char *methname);
358
359
/**
360
 * Initialize the method_registry and allocate memory for it.
361
 *
362
 * @param p Pool to allocate memory for the registry from.
363
 */
364
AP_DECLARE(void) ap_method_registry_init(apr_pool_t *p);
365
366
/**
367
 * This is a convenience macro to ease with checking a mask
368
 * against a method name.
369
 */
370
#define AP_METHOD_CHECK_ALLOWED(mask, methname) \
371
    ((mask) & (AP_METHOD_BIT << ap_method_number_of((methname))))
372
373
/**
374
 * Create a new method list with the specified number of preallocated
375
 * slots for extension methods.
376
 *
377
 * @param   p       Pointer to a pool in which the structure should be
378
 *                  allocated.
379
 * @param   nelts   Number of preallocated extension slots
380
 * @return  Pointer to the newly created structure.
381
 */
382
AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
383
384
385
/**
386
 * Copy a method list
387
 *
388
 * @param   dest List to copy to
389
 * @param   src  List to copy from
390
 */
391
AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
392
                                     ap_method_list_t *src);
393
394
/**
395
 * Search for an HTTP method name in an ap_method_list_t structure, and
396
 * return true if found.
397
 *
398
 * @param   method  String containing the name of the method to check.
399
 * @param   l       Pointer to a method list, such as r->allowed_methods.
400
 * @return  1 if method is in the list, otherwise 0
401
 */
402
AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
403
404
/**
405
 * Add an HTTP method name to an ap_method_list_t structure if it isn't
406
 * already listed.
407
 *
408
 * @param   method  String containing the name of the method to check.
409
 * @param   l       Pointer to a method list, such as r->allowed_methods.
410
 * @return  None.
411
 */
412
AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
413
414
/**
415
 * Remove an HTTP method name from an ap_method_list_t structure.
416
 *
417
 * @param   l       Pointer to a method list, such as r->allowed_methods.
418
 * @param   method  String containing the name of the method to remove.
419
 * @return  None.
420
 */
421
AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
422
                                       const char *method);
423
424
/**
425
 * Reset a method list to be completely empty.
426
 *
427
 * @param   l       Pointer to a method list, such as r->allowed_methods.
428
 * @return  None.
429
 */
430
AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
431
432
/**
433
 * Set the content type for this request (r->content_type).
434
 * @param r The current request
435
 * @param ct The new content type
436
 * @warning This function must be called to set r->content_type in order
437
 * for the AddOutputFilterByType directive to work correctly.
438
 */
439
AP_DECLARE(void) ap_set_content_type(request_rec *r, const char *ct);
440
441
/**
442
 * Set the content type for this request (r->content_type).
443
 * @param r The current request
444
 * @param ct The new content type
445
 * @param trusted If non-zero, The content-type should come from a
446
 *        trusted source such as server configuration rather
447
 *        than application output.
448
 * for the AddOutputFilterByType directive to work correctly.
449
 */
450
AP_DECLARE(void) ap_set_content_type_ex(request_rec *r, const char *ct, int trusted);
451
452
/**
453
 * Set the Accept-Ranges header for this response
454
 * @param r The current request
455
 */
456
AP_DECLARE(void) ap_set_accept_ranges(request_rec *r);
457
458
459
/* Hmmm... could macrofy these for now, and maybe forever, though the
460
 * definitions of the macros would get a whole lot hairier.
461
 */
462
463
/**
464
 * Output one character for this request
465
 * @param c the character to output
466
 * @param r the current request
467
 * @return The number of bytes sent
468
 */
469
AP_DECLARE(int) ap_rputc(int c, request_rec *r);
470
471
/**
472
 * Write a buffer for the current request
473
 * @param buf The buffer to write
474
 * @param nbyte The number of bytes to send from the buffer
475
 * @param r The current request
476
 * @return The number of bytes sent
477
 */
478
AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
479
480
/**
481
 * Output a string for the current request
482
 * @param str The string to output
483
 * @param r The current request
484
 * @return The number of bytes sent
485
 * @note ap_rputs may be implemented as macro or inline function
486
 */
487
static APR_INLINE int ap_rputs(const char *str, request_rec *r)
488
0
{
489
0
    apr_size_t len;
490
0
491
0
    len = strlen(str);
492
0
493
0
    for (;;) {
494
0
        if (len <= INT_MAX) {
495
0
            return ap_rwrite(str, (int)len, r);
496
0
        }
497
0
        else {
498
0
            int rc;
499
0
500
0
            rc = ap_rwrite(str, INT_MAX, r);
501
0
            if (rc < 0) {
502
0
                return rc;
503
0
            }
504
0
            else {
505
0
                str += INT_MAX;
506
0
                len -= INT_MAX;
507
0
            }
508
0
        }
509
0
    }
510
0
}
Unexecuted instantiation: authz.c:ap_rputs
Unexecuted instantiation: url.c:ap_rputs
Unexecuted instantiation: util.c:ap_rputs
511
512
/**
513
 * Write an unspecified number of strings to the request
514
 * @param r The current request
515
 * @param ... The strings to write
516
 * @return The number of bytes sent
517
 */
518
AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...)
519
                       AP_FN_ATTR_SENTINEL;
520
521
/**
522
 * Output data to the client in a printf format
523
 * @param r The current request
524
 * @param fmt The format string
525
 * @param vlist The arguments to use to fill out the format string
526
 * @return The number of bytes sent
527
 */
528
AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
529
530
/**
531
 * Output data to the client in a printf format
532
 * @param r The current request
533
 * @param fmt The format string
534
 * @param ... The arguments to use to fill out the format string
535
 * @return The number of bytes sent
536
 */
537
AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
538
                                __attribute__((format(printf,2,3)));
539
540
/**
541
 * Flush all of the data for the current request to the client
542
 * @param r The current request
543
 * @return 0 on success, -1 if an error occurred
544
 */
545
AP_DECLARE(int) ap_rflush(request_rec *r);
546
547
/**
548
 * Index used in custom_responses array for a specific error code
549
 * (only use outside protocol.c is in getting them configured).
550
 * @param status HTTP status code
551
 * @return The index of the response
552
 */
553
AP_DECLARE(int) ap_index_of_response(int status);
554
555
/**
556
 * Return the Status-Line for a given status code (excluding the
557
 * HTTP-Version field). If an invalid or unknown status code is
558
 * passed, "500 Internal Server Error" will be returned.
559
 * @param status The HTTP status code
560
 * @return The Status-Line
561
 */
562
AP_DECLARE(const char *) ap_get_status_line(int status);
563
564
/**
565
 * Return the Status-Line for a given status code (excluding the
566
 * HTTP-Version field). If an invalid status code is passed,
567
 * "500 Internal Server Error" will be returned, whereas an unknown
568
 * status will be returned like "xxx Status xxx".
569
 * @param p The pool to allocate from when status is unknown
570
 * @param status The HTTP status code
571
 * @return The Status-Line
572
 */
573
AP_DECLARE(const char *) ap_get_status_line_ex(apr_pool_t *p, int status);
574
575
/* Reading a block of data from the client connection (e.g., POST arg) */
576
577
/**
578
 * Setup the client to allow Apache to read the request body.
579
 * @param r The current request
580
 * @param read_policy How the server should interpret a chunked
581
 *                    transfer-encoding.  One of: <pre>
582
 *    REQUEST_NO_BODY          Send 413 error if message has any body
583
 *    REQUEST_CHUNKED_ERROR    Send 411 error if body without Content-Length
584
 *    REQUEST_CHUNKED_DECHUNK  If chunked, remove the chunks for me.
585
 * </pre>
586
 * @return either OK or an error code
587
 */
588
AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
589
590
/**
591
 * Determine if the client has sent any data.  This also sends a
592
 * 100 Continue response to HTTP/1.1 clients, so modules should not be called
593
 * until the module is ready to read content.
594
 * @warning Never call this function more than once.
595
 * @param r The current request
596
 * @return 0 if there is no message to read, 1 otherwise
597
 */
598
AP_DECLARE(int) ap_should_client_block(request_rec *r);
599
600
/**
601
 * Call this in a loop.  It will put data into a buffer and return the length
602
 * of the input block
603
 * @param r The current request
604
 * @param buffer The buffer in which to store the data
605
 * @param bufsiz The size of the buffer
606
 * @return Number of bytes inserted into the buffer.  When done reading, 0
607
 *         if EOF, or -1 if there was an error
608
 */
609
AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
610
611
/**
612
 * Map specific APR codes returned by the filter stack to HTTP error
613
 * codes, or the default status code provided. Use it as follows:
614
 *
615
 * return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
616
 *
617
 * If the filter has already handled the error, AP_FILTER_ERROR will
618
 * be returned, which is cleanly passed through.
619
 *
620
 * These mappings imply that the filter stack is reading from the
621
 * downstream client, the proxy will map these codes differently.
622
 * @param rv APR status code
623
 * @param status Default HTTP code should the APR code not be recognised
624
 * @return Mapped HTTP status code
625
 */
626
AP_DECLARE(int) ap_map_http_request_error(apr_status_t rv, int status);
627
628
/**
629
 * In HTTP/1.1, any method can have a body.  However, most GET handlers
630
 * wouldn't know what to do with a request body if they received one.
631
 * This helper routine tests for and reads any message body in the request,
632
 * simply discarding whatever it receives.  We need to do this because
633
 * failing to read the request body would cause it to be interpreted
634
 * as the next request on a persistent connection.
635
 * @param r The current request
636
 * @return error status if request is malformed, OK otherwise
637
 */
638
AP_DECLARE(int) ap_discard_request_body(request_rec *r);
639
640
/**
641
 * Setup the output headers so that the client knows how to authenticate
642
 * itself the next time, if an authentication request failed.
643
 * @param r The current request
644
 */
645
AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
646
647
/**
648
 * @deprecated @see ap_note_auth_failure
649
 */
650
AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
651
652
/**
653
 * @deprecated @see ap_note_auth_failure
654
 */
655
AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
656
657
/**
658
 * This hook allows modules to add support for a specific auth type to
659
 * ap_note_auth_failure
660
 * @param r the current request
661
 * @param auth_type the configured auth_type
662
 * @return OK, DECLINED
663
 */
664
AP_DECLARE_HOOK(int, note_auth_failure, (request_rec *r, const char *auth_type))
665
666
/**
667
 * Get the password from the request headers. This function has multiple side
668
 * effects due to its prior use in the old authentication framework.
669
 * ap_get_basic_auth_components() should be preferred.
670
 *
671
 * @deprecated @see ap_get_basic_auth_components
672
 * @param r The current request
673
 * @param pw The password as set in the headers
674
 * @return 0 (OK) if it set the 'pw' argument (and assured
675
 *         a correct value in r->user); otherwise it returns
676
 *         an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
677
 *         really confused, HTTP_UNAUTHORIZED if no authentication at all
678
 *         seemed to be in use, or DECLINED if there was authentication but
679
 *         it wasn't Basic (in which case, the caller should presumably
680
 *         decline as well).
681
 */
682
AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
683
684
#define AP_GET_BASIC_AUTH_PW_NOTE "AP_GET_BASIC_AUTH_PW_NOTE"
685
686
/**
687
 * Get the username and/or password from the request's Basic authentication
688
 * headers. Unlike ap_get_basic_auth_pw(), calling this function has no side
689
 * effects on the passed request_rec.
690
 *
691
 * @param r The current request
692
 * @param username If not NULL, set to the username sent by the client
693
 * @param password If not NULL, set to the password sent by the client
694
 * @return APR_SUCCESS if the credentials were successfully parsed and returned;
695
 *         APR_EINVAL if there was no authentication header sent or if the
696
 *         client was not using the Basic authentication scheme. username and
697
 *         password are unchanged on failure.
698
 */
699
AP_DECLARE(apr_status_t) ap_get_basic_auth_components(const request_rec *r,
700
                                                      const char **username,
701
                                                      const char **password);
702
703
/**
704
 * parse_uri: break apart the uri
705
 * @warning Side Effects:
706
 *    @li sets r->args to rest after '?' (or NULL if no '?')
707
 *    @li sets r->uri to request uri (without r->args part)
708
 *    @li sets r->hostname (if not set already) from request (scheme://host:port)
709
 * @param r The current request
710
 * @param uri The uri to break apart
711
 */
712
AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
713
714
#define AP_GETLINE_FOLD 1 /* Whether to merge continuation lines */
715
#define AP_GETLINE_CRLF 2 /* Whether line ends must be in the form CR LF */
716
#define AP_GETLINE_NOSPC_EOL 4 /* Whether to consume up to and including the
717
                                  end of line on APR_ENOSPC */
718
719
/**
720
 * Get the next line of input for the request
721
 * @param s The buffer into which to read the line
722
 * @param n The size of the buffer
723
 * @param r The request
724
 * @param flags Bit flag of multiple parsing options
725
 *              AP_GETLINE_FOLD Whether to merge continuation lines
726
 *              AP_GETLINE_CRLF Whether line ends must be in the form CR LF
727
 * @return The length of the line, if successful
728
 *         n, if the line is too big to fit in the buffer
729
 *         -1 for miscellaneous errors
730
 */
731
AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int flags);
732
733
/**
734
 * Get the next line of input for the request
735
 *
736
 * Note: on ASCII boxes, ap_rgetline is a macro which simply calls
737
 *       ap_rgetline_core to get the line of input.
738
 *
739
 *       on EBCDIC boxes, ap_rgetline is a wrapper function which
740
 *       translates ASCII protocol lines to the local EBCDIC code page
741
 *       after getting the line of input.
742
 *
743
 * @param s Pointer to the pointer to the buffer into which the line
744
 *          should be read; if *s==NULL, a buffer of the necessary size
745
 *          to hold the data will be allocated from the request pool
746
 * @param n The size of the buffer
747
 * @param read The length of the line.
748
 * @param r The request
749
 * @param flags Bit flag of multiple parsing options
750
 *              AP_GETLINE_FOLD Whether to merge continuation lines
751
 *              AP_GETLINE_CRLF Whether line ends must be in the form CR LF
752
 * @param bb Working brigade to use when reading buckets
753
 * @return APR_SUCCESS, if successful
754
 *         APR_ENOSPC, if the line is too big to fit in the buffer
755
 *         Other errors where appropriate
756
 */
757
#if APR_CHARSET_EBCDIC
758
AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
759
                                     apr_size_t *read,
760
                                     request_rec *r, int flags,
761
                                     apr_bucket_brigade *bb);
762
#else /* ASCII box */
763
#define ap_rgetline(s, n, read, r, fold, bb) \
764
        ap_rgetline_core((s), (n), (read), (r), (fold), (bb))
765
#endif
766
767
/** @see ap_rgetline */
768
AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
769
                                          apr_size_t *read,
770
                                          request_rec *r, int flags,
771
                                          apr_bucket_brigade *bb);
772
773
/**
774
 * Get the method number associated with the given string, assumed to
775
 * contain an HTTP method.  Returns M_INVALID if not recognized.
776
 * @param method A string containing a valid HTTP method
777
 * @return The method number
778
 */
779
AP_DECLARE(int) ap_method_number_of(const char *method);
780
781
/**
782
 * Get the method name associated with the given internal method
783
 * number.  Returns NULL if not recognized.
784
 * @param p A pool to use for temporary allocations.
785
 * @param methnum An integer value corresponding to an internal method number
786
 * @return The name corresponding to the method number
787
 */
788
AP_DECLARE(const char *) ap_method_name_of(apr_pool_t *p, int methnum);
789
790
791
/* Hooks */
792
/*
793
 * pre_read_request --- run right before read_request_line(),
794
 *                  and not run during any subrequests.
795
 */
796
/**
797
 * This hook allows modules to affect the request or connection immediately before
798
 * the request has been read, and before any other phases have been processes.
799
 * @param r The current request of the soon-to-be-read request
800
 * @param c The connection
801
 * @return None/void
802
 */
803
AP_DECLARE_HOOK(void,pre_read_request,(request_rec *r, conn_rec *c))
804
805
/*
806
 * post_read_request --- run right after read_request or internal_redirect,
807
 *                  and not run during any subrequests.
808
 */
809
/**
810
 * This hook allows modules to affect the request immediately after the request
811
 * has been read, and before any other phases have been processes.  This allows
812
 * modules to make decisions based upon the input header fields
813
 * @param r The current request
814
 * @return OK or DECLINED
815
 */
816
AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
817
818
/**
819
 * This hook allows modules to perform any module-specific logging activities
820
 * over and above the normal server things.
821
 * @param r The current request
822
 * @return OK, DECLINED, or HTTP_...
823
 */
824
AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
825
826
/**
827
 * This hook allows modules to retrieve the http scheme for a request.  This
828
 * allows Apache modules to easily extend the schemes that Apache understands
829
 * @param r The current request
830
 * @return The http scheme from the request
831
 */
832
AP_DECLARE_HOOK(const char *,http_scheme,(const request_rec *r))
833
834
/**
835
 * Return the default port from the current request
836
 * @param r The current request
837
 * @return The current port
838
 */
839
AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
840
841
842
#define AP_PROTOCOL_HTTP1        "http/1.1"
843
844
/**
845
 * Determine the list of protocols available for a connection/request. This may
846
 * be collected with or without any request sent, in which case the request is 
847
 * NULL. Or it may be triggered by the request received, e.g. through the 
848
 * "Upgrade" header.
849
 *
850
 * This hook will be run whenever protocols are being negotiated (ALPN as
851
 * one example). It may also be invoked at other times, e.g. when the server
852
 * wants to advertise protocols it is capable of switching to.
853
 * 
854
 * The identifiers for protocols are taken from the TLS extension type ALPN:
855
 * https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xml
856
 *
857
 * If no protocols are added to the proposals, the server not perform any
858
 * switch. If the protocol selected from the proposals is the protocol
859
 * already in place, also no protocol switch will be invoked.
860
 *
861
 * The client may already have announced the protocols it is willing to
862
 * accept. These will then be listed as offers. This parameter may also
863
 * be NULL, indicating that offers from the client are not known and
864
 * the hooks should propose all protocols that are valid for the
865
 * current connection/request.
866
 *
867
 * All hooks are run, unless one returns an error. Proposals may contain
868
 * duplicates. The order in which proposals are added is usually ignored.
869
 * 
870
 * @param c The current connection
871
 * @param r The current request or NULL
872
 * @param s The server/virtual host selected
873
 * @param offers A list of protocol identifiers offered by the client or
874
 *               NULL to indicated that the hooks are free to propose 
875
 * @param proposals The list of protocol identifiers proposed by the hooks
876
 * @return OK or DECLINED
877
 * @bug This API or implementation and order of operations should be considered
878
 * experimental and will continue to evolve in future 2.4 releases, with
879
 * a corresponding minor module magic number (MMN) bump to indicate the
880
 * API revision level.
881
 */
882
AP_DECLARE_HOOK(int,protocol_propose,(conn_rec *c, request_rec *r,
883
                                      server_rec *s,
884
                                      const apr_array_header_t *offers,
885
                                      apr_array_header_t *proposals))
886
887
/**
888
 * Perform a protocol switch on the connection. The exact requirements for
889
 * that depend on the protocol in place and the one switched to. The first 
890
 * protocol module to handle the switch is the last module run.
891
 * 
892
 * For a connection level switch (r == NULL), the handler must on return
893
 * leave the conn_rec in a state suitable for processing the switched
894
 * protocol, e.g. correct filters in place.
895
 *
896
 * For a request triggered switch (r != NULL), the protocol switch is done
897
 * before the response is sent out. When switching from "http/1.1" via Upgrade
898
 * header, the 101 intermediate response will have been sent. The
899
 * hook needs then to process the connection until it can be closed. Which
900
 * the server will enforce on hook return.
901
 * Any error the hook might encounter must already be sent by the hook itself
902
 * to the client in whatever form the new protocol requires.
903
 *
904
 * @param c The current connection
905
 * @param r The current request or NULL
906
 * @param s The server/virtual host selected
907
 * @param protocol The protocol identifier we try to switch to
908
 * @return OK or DECLINED
909
 * @bug This API or implementation and order of operations should be considered
910
 * experimental and will continue to evolve in future 2.4 releases, with
911
 * a corresponding minor module magic number (MMN) bump to indicate the
912
 * API revision level.
913
 */
914
AP_DECLARE_HOOK(int,protocol_switch,(conn_rec *c, request_rec *r,
915
                                     server_rec *s,
916
                                     const char *protocol))
917
918
/**
919
 * Return the protocol used on the connection. Modules implementing
920
 * protocol switching must register here and return the correct protocol
921
 * identifier for connections they switched.
922
 *
923
 * To find out the protocol for the current connection, better call
924
 * @see ap_get_protocol which internally uses this hook.
925
 *
926
 * @param c The current connection
927
 * @return The identifier of the protocol in place or NULL
928
 * @bug This API or implementation and order of operations should be considered
929
 * experimental and will continue to evolve in future 2.4 releases, with
930
 * a corresponding minor module magic number (MMN) bump to indicate the
931
 * API revision level.
932
 */
933
AP_DECLARE_HOOK(const char *,protocol_get,(const conn_rec *c))
934
935
/**
936
 * Get the protocols that the connection and optional request may
937
 * upgrade to - besides the protocol currently active on the connection. These
938
 * values may be used to announce to a client what choices it has.
939
 *
940
 * If report_all == 0, only protocols more preferable than the one currently
941
 * being used, are reported. Otherwise, all available protocols beside the
942
 * current one are being reported.
943
 *
944
 * @param c The current connection
945
 * @param r The current request or NULL
946
 * @param s The server/virtual host selected or NULL
947
 * @param report_all include also protocols less preferred than the current one
948
 * @param pupgrades on return, possible protocols to upgrade to in descending order 
949
 *                 of preference. Maybe NULL if none are available.    
950
 * @bug This API or implementation and order of operations should be considered
951
 * experimental and will continue to evolve in future 2.4 releases, with
952
 * a corresponding minor module magic number (MMN) bump to indicate the
953
 * API revision level.
954
 */
955
AP_DECLARE(apr_status_t) ap_get_protocol_upgrades(conn_rec *c, request_rec *r, 
956
                                                  server_rec *s, int report_all, 
957
                                                  const apr_array_header_t **pupgrades);
958
                                                  
959
/**
960
 * Select a protocol for the given connection and optional request. Will return
961
 * the protocol identifier selected which may be the protocol already in place
962
 * on the connection. The selected protocol will be NULL if non of the given
963
 * choices could be agreed upon (e.g. no proposal as made).
964
 *
965
 * A special case is where the choices itself is NULL (instead of empty). In
966
 * this case there are no restrictions imposed on protocol selection.
967
 *
968
 * @param c The current connection
969
 * @param r The current request or NULL
970
 * @param s The server/virtual host selected
971
 * @param choices A list of protocol identifiers, normally the clients whishes
972
 * @return The selected protocol or NULL if no protocol could be agreed upon
973
 * @bug This API or implementation and order of operations should be considered
974
 * experimental and will continue to evolve in future 2.4 releases, with
975
 * a corresponding minor module magic number (MMN) bump to indicate the
976
 * API revision level.
977
 */
978
AP_DECLARE(const char *) ap_select_protocol(conn_rec *c, request_rec *r, 
979
                                            server_rec *s,
980
                                            const apr_array_header_t *choices);
981
982
/**
983
 * Perform the actual protocol switch. The protocol given must have been
984
 * selected before on the very same connection and request pair.
985
 *
986
 * @param c The current connection
987
 * @param r The current request or NULL
988
 * @param s The server/virtual host selected
989
 * @param protocol the protocol to switch to
990
 * @return APR_SUCCESS, if caller may continue processing as usual
991
 *         APR_EOF,     if caller needs to stop processing the connection
992
 *         APR_EINVAL,  if the protocol is already in place
993
 *         APR_NOTIMPL, if no module performed the switch
994
 *         Other errors where appropriate
995
 * @bug This API or implementation and order of operations should be considered
996
 * experimental and will continue to evolve in future 2.4 releases, with
997
 * a corresponding minor module magic number (MMN) bump to indicate the
998
 * API revision level.
999
 */
1000
AP_DECLARE(apr_status_t) ap_switch_protocol(conn_rec *c, request_rec *r, 
1001
                                            server_rec *s,
1002
                                            const char *protocol);
1003
1004
/**
1005
 * Call the protocol_get hook to determine the protocol currently in use
1006
 * for the given connection.
1007
 *
1008
 * Unless another protocol has been switch to, will default to
1009
 * @see AP_PROTOCOL_HTTP1 and modules implementing a  new protocol must
1010
 * report a switched connection via the protocol_get hook.
1011
 *
1012
 * @param c The connection to determine the protocol for
1013
 * @return the protocol in use, never NULL
1014
 * @bug This API or implementation and order of operations should be considered
1015
 * experimental and will continue to evolve in future 2.4 releases, with
1016
 * a corresponding minor module magic number (MMN) bump to indicate the
1017
 * API revision level.
1018
 */
1019
AP_DECLARE(const char *) ap_get_protocol(conn_rec *c);
1020
1021
/**
1022
 * Check if the given protocol is an allowed choice on the given
1023
 * combination of connection, request and server. 
1024
 *
1025
 * When server is NULL, it is taken from request_rec, unless
1026
 * request_rec is NULL. Then it is taken from the connection base
1027
 * server.
1028
 *
1029
 * @param c The current connection
1030
 * @param r The current request or NULL
1031
 * @param s The server/virtual host selected or NULL
1032
 * @param protocol the protocol to switch to
1033
 * @return != 0 iff protocol is allowed
1034
 * @bug This API or implementation and order of operations should be considered
1035
 * experimental and will continue to evolve in future 2.4 releases, with
1036
 * a corresponding minor module magic number (MMN) bump to indicate the
1037
 * API revision level.
1038
 */
1039
AP_DECLARE(int) ap_is_allowed_protocol(conn_rec *c, request_rec *r,
1040
                                       server_rec *s, const char *protocol);
1041
1042
/** @see ap_bucket_type_error */
1043
typedef struct ap_bucket_error ap_bucket_error;
1044
1045
/**
1046
 * @struct ap_bucket_error
1047
 * @brief  A bucket referring to an HTTP error
1048
 *
1049
 * This bucket can be passed down the filter stack to indicate that an
1050
 * HTTP error occurred while running a filter.  In order for this bucket
1051
 * to be used successfully, it MUST be sent as the first bucket in the
1052
 * first brigade to be sent from a given filter.
1053
 */
1054
struct ap_bucket_error {
1055
    /** Number of buckets using this memory */
1056
    apr_bucket_refcount refcount;
1057
    /** The error code */
1058
    int status;
1059
    /** The error string */
1060
    const char    *data;
1061
};
1062
1063
/** @see ap_bucket_type_error */
1064
AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
1065
1066
/**
1067
 * Determine if a bucket is an error bucket
1068
 * @param e The bucket to inspect
1069
 * @return true or false
1070
 */
1071
#define AP_BUCKET_IS_ERROR(e)         (e->type == &ap_bucket_type_error)
1072
1073
/**
1074
 * Make the bucket passed in an error bucket
1075
 * @param b The bucket to make into an error bucket
1076
 * @param error The HTTP error code to put in the bucket.
1077
 * @param buf An optional error string to put in the bucket.
1078
 * @param p A pool to allocate out of.
1079
 * @return The new bucket, or NULL if allocation failed
1080
 */
1081
AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
1082
                const char *buf, apr_pool_t *p);
1083
1084
/**
1085
 * Create a bucket referring to an HTTP error.
1086
 * @param error The HTTP error code to put in the bucket.
1087
 * @param buf An optional error string to put in the bucket.
1088
 * @param p A pool to allocate the error string out of.
1089
 * @param list The bucket allocator from which to allocate the bucket
1090
 * @return The new bucket, or NULL if allocation failed
1091
 */
1092
AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error, const char *buf,
1093
                                                apr_pool_t *p,
1094
                                                apr_bucket_alloc_t *list);
1095
1096
AP_DECLARE_NONSTD(apr_status_t) ap_byterange_filter(ap_filter_t *f, apr_bucket_brigade *b);
1097
AP_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b);
1098
AP_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(ap_filter_t *,
1099
                                                              apr_bucket_brigade *);
1100
AP_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(ap_filter_t *f, apr_bucket_brigade *b);
1101
1102
/**
1103
 * Sett up the protocol fields for subsidiary requests
1104
 * @param rnew New Sub Request
1105
 * @param r current request
1106
 */
1107
AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
1108
1109
/**
1110
 * A wrapup function to keep the internal accounting straight.
1111
 * Indicates that there is no more content coming.
1112
 * @param sub_r Subrequest that is now compete
1113
 */
1114
AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub_r);
1115
1116
/**
1117
 * Send an interim (HTTP 1xx) response immediately.
1118
 * @param r The request
1119
 * @param send_headers Whether to send&clear headers in r->headers_out
1120
 */
1121
AP_DECLARE(void) ap_send_interim_response(request_rec *r, int send_headers);
1122
1123
1124
1125
#ifdef __cplusplus
1126
}
1127
#endif
1128
1129
#endif  /* !APACHE_HTTP_PROTOCOL_H */
1130
/** @} */