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