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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef APACHE_HTTP_PROTOCOL_H
18 #define APACHE_HTTP_PROTOCOL_H
21 #include "apr_hooks.h"
22 #include "apr_portable.h"
24 #include "apr_buckets.h"
25 #include "util_filter.h"
32 * @package HTTP protocol handling
36 * This hook allows modules to insert filters for the current error response
37 * @param r the current request
40 AP_DECLARE_HOOK(void,insert_error_filter,(request_rec *r))
42 /* This is an optimization. We keep a record of the filter_rec that
43 * stores the old_write filter, so that we can avoid strcmp's later.
45 AP_DECLARE_DATA extern ap_filter_rec_t *ap_old_write_func;
48 * Prototypes for routines which either talk directly back to the user,
49 * or control the ones that eventually do.
53 * Read a request and fill in the fields.
54 * @param c The current connection
55 * @return The new request_rec
57 request_rec *ap_read_request(conn_rec *c);
60 * Read the mime-encoded headers.
61 * @param r The current request
63 AP_DECLARE(void) ap_get_mime_headers(request_rec *r);
66 * Optimized version of ap_get_mime_headers() that requires a
67 * temporary brigade to work with
68 * @param r The current request
69 * @param bb temp brigade
71 AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r,
72 apr_bucket_brigade *bb);
74 /* Finish up stuff after a request */
77 * Called at completion of sending the response. It sends the terminating
78 * protocol information.
79 * @param r The current request
80 * @deffunc void ap_finalize_request_protocol(request_rec *r)
82 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
85 * Send error back to client.
86 * @param r The current request
87 * @param recursive_error last arg indicates error status in case we get
88 * an error in the process of trying to deal with an ErrorDocument
89 * to handle some other error. In that case, we print the default
90 * report for the first thing that went wrong, and more briefly report
91 * on the problem with the ErrorDocument.
92 * @deffunc void ap_send_error_response(request_rec *r, int recursive_error)
94 AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
96 /* Set last modified header line from the lastmod date of the associated file.
97 * Also, set content length.
99 * May return an error status, typically HTTP_NOT_MODIFIED (that when the
100 * permit_cache argument is set to one).
104 * Set the content length for this request
105 * @param r The current request
106 * @param length The new content length
107 * @deffunc void ap_set_content_length(request_rec *r, apr_off_t length)
109 AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
112 * Set the keepalive status for this request
113 * @param r The current request
114 * @return 1 if keepalive can be set, 0 otherwise
115 * @deffunc int ap_set_keepalive(request_rec *r)
117 AP_DECLARE(int) ap_set_keepalive(request_rec *r);
120 * Return the latest rational time from a request/mtime pair. Mtime is
121 * returned unless it's in the future, in which case we return the current time.
122 * @param r The current request
123 * @param mtime The last modified time
124 * @return the latest rational time.
125 * @deffunc apr_time_t ap_rationalize_mtime(request_rec *r, apr_time_t mtime)
127 AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
130 * Build the content-type that should be sent to the client from the
131 * content-type specified. The following rules are followed:
132 * - if type is NULL, type is set to ap_default_type(r)
133 * - if charset adding is disabled, stop processing and return type.
134 * - then, if there are no parameters on type, add the default charset
136 * @param r The current request
137 * @return The content-type
138 * @deffunc const char *ap_make_content_type(request_rec *r, const char *type);
140 AP_DECLARE(const char *) ap_make_content_type(request_rec *r,
145 * Precompile metadata structures used by ap_make_content_type()
146 * @param r The pool to use for allocations
147 * @deffunc void ap_setup_make_content_type(apr_pool_t *pool)
149 AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool);
150 #endif /* CORE_PRIVATE */
153 * Construct an entity tag from the resource information. If it's a real
154 * file, build in some of the file characteristics.
155 * @param r The current request
156 * @param force_weak Force the entity tag to be weak - it could be modified
157 * again in as short an interval.
158 * @return The entity tag
159 * @deffunc char *ap_make_etag(request_rec *r, int force_weak)
161 AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
164 * Set the E-tag outgoing header
165 * @param The current request
166 * @deffunc void ap_set_etag(request_rec *r)
168 AP_DECLARE(void) ap_set_etag(request_rec *r);
171 * Set the last modified time for the file being sent
172 * @param r The current request
173 * @deffunc void ap_set_last_modified(request_rec *r)
175 AP_DECLARE(void) ap_set_last_modified(request_rec *r);
178 * Implements condition GET rules for HTTP/1.1 specification. This function
179 * inspects the client headers and determines if the response fulfills
180 * the requirements specified.
181 * @param r The current request
182 * @return OK if the response fulfills the condition GET rules, some
183 * other status code otherwise
184 * @deffunc int ap_meets_conditions(request_rec *r)
186 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
188 /* Other ways to send stuff at the client. All of these keep track
189 * of bytes_sent automatically. This indirection is intended to make
190 * it a little more painless to slide things like HTTP-NG packetization
191 * underneath the main body of the code later. In the meantime, it lets
192 * us centralize a bit of accounting (bytes_sent).
194 * These also return the number of bytes written by the call.
195 * They should only be called with a timeout registered, for obvious reaasons.
196 * (Ditto the send_header stuff).
200 * Send an entire file to the client, using sendfile if supported by the
202 * @param fd The file to send.
203 * @param r The current request
204 * @param offset Offset into the file to start sending.
205 * @param length Amount of data to send
206 * @param nbytes Amount of data actually sent
207 * @deffunc apr_status_t ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset, apr_size_t length, apr_size_t *nbytes);
209 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
210 apr_size_t length, apr_size_t *nbytes);
214 * Send an MMAP'ed file to the client
215 * @param mm The MMAP'ed file to send
216 * @param r The current request
217 * @param offset The offset into the MMAP to start sending
218 * @param length The amount of data to send
219 * @return The number of bytes sent
220 * @deffunc size_t ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset, size_t length)
222 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
228 * Register a new request method, and return the offset that will be
229 * associated with that method.
231 * @param p The pool to create registered method numbers from.
232 * @param methname The name of the new method to register.
233 * @return Ab int value representing an offset into a bitmask.
235 AP_DECLARE(int) ap_method_register(apr_pool_t *p, const char *methname);
238 * Initialize the method_registry and allocate memory for it.
240 * @param p Pool to allocate memory for the registry from.
242 AP_DECLARE(void) ap_method_registry_init(apr_pool_t *p);
245 * This is a convenience macro to ease with checking a mask
246 * against a method name.
248 #define AP_METHOD_CHECK_ALLOWED(mask, methname) \
249 ((mask) & (AP_METHOD_BIT << ap_method_number_of((methname))))
252 * Create a new method list with the specified number of preallocated
253 * slots for extension methods.
255 * @param p Pointer to a pool in which the structure should be
257 * @param nelts Number of preallocated extension slots
258 * @return Pointer to the newly created structure.
259 * @deffunc ap_method_list_t ap_make_method_list(apr_pool_t *p, int nelts)
261 AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
262 AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
263 ap_method_list_t *src);
264 AP_DECLARE_NONSTD(void) ap_method_list_do(int (*comp) (void *urec, const char *mname,
267 const ap_method_list_t *ml, ...);
268 AP_DECLARE(void) ap_method_list_vdo(int (*comp) (void *urec, const char *mname,
270 void *rec, const ap_method_list_t *ml,
273 * Search for an HTTP method name in an ap_method_list_t structure, and
274 * return true if found.
276 * @param method String containing the name of the method to check.
277 * @param l Pointer to a method list, such as cmd->methods_limited.
278 * @return 1 if method is in the list, otherwise 0
279 * @deffunc int ap_method_in_list(const char *method, ap_method_list_t *l)
281 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
284 * Add an HTTP method name to an ap_method_list_t structure if it isn't
287 * @param method String containing the name of the method to check.
288 * @param l Pointer to a method list, such as cmd->methods_limited.
290 * @deffunc void ap_method_in_list(ap_method_list_t *l, const char *method)
292 AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
295 * Remove an HTTP method name from an ap_method_list_t structure.
297 * @param l Pointer to a method list, such as cmd->methods_limited.
298 * @param method String containing the name of the method to remove.
300 * @deffunc void ap_method_list_remove(ap_method_list_t *l, const char *method)
302 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
306 * Reset a method list to be completely empty.
308 * @param l Pointer to a method list, such as cmd->methods_limited.
310 * @deffunc void ap_clear_method_list(ap_method_list_t *l)
312 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
315 * Set the content type for this request (r->content_type).
316 * @param r The current request
317 * @param ct The new content type
318 * @deffunc void ap_set_content_type(request_rec *r, const char* ct)
319 * @warning This function must be called to set r->content_type in order
320 * for the AddOutputFilterByType directive to work correctly.
322 AP_DECLARE(void) ap_set_content_type(request_rec *r, const char *ct);
324 /* Hmmm... could macrofy these for now, and maybe forever, though the
325 * definitions of the macros would get a whole lot hairier.
329 * Output one character for this request
330 * @param c the character to output
331 * @param r the current request
332 * @return The number of bytes sent
333 * @deffunc int ap_rputc(int c, request_rec *r)
335 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
338 * Output a string for the current request
339 * @param str The string to output
340 * @param r The current request
341 * @return The number of bytes sent
342 * @deffunc int ap_rputs(const char *str, request_rec *r)
344 AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
347 * Write a buffer for the current request
348 * @param buf The buffer to write
349 * @param nbyte The number of bytes to send from the buffer
350 * @param r The current request
351 * @return The number of bytes sent
352 * @deffunc int ap_rwrite(const void *buf, int nbyte, request_rec *r)
354 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
357 * Write an unspecified number of strings to the request
358 * @param r The current request
359 * @param ... The strings to write
360 * @return The number of bytes sent
361 * @deffunc int ap_rvputs(request_rec *r, ...)
363 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...);
366 * Output data to the client in a printf format
367 * @param r The current request
368 * @param fmt The format string
369 * @param vlist The arguments to use to fill out the format string
370 * @return The number of bytes sent
371 * @deffunc int ap_vrprintf(request_rec *r, const char *fmt, va_list vlist)
373 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
376 * Output data to the client in a printf format
377 * @param r The current request
378 * @param fmt The format string
379 * @param ... The arguments to use to fill out the format string
380 * @return The number of bytes sent
381 * @deffunc int ap_rprintf(request_rec *r, const char *fmt, ...)
383 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
384 __attribute__((format(printf,2,3)));
386 * Flush all of the data for the current request to the client
387 * @param r The current request
388 * @return The number of bytes sent
389 * @deffunc int ap_rflush(request_rec *r)
391 AP_DECLARE(int) ap_rflush(request_rec *r);
394 * Index used in custom_responses array for a specific error code
395 * (only use outside protocol.c is in getting them configured).
396 * @param status HTTP status code
397 * @return The index of the response
398 * @deffunc int ap_index_of_response(int status)
400 AP_DECLARE(int) ap_index_of_response(int status);
403 * Return the Status-Line for a given status code (excluding the
404 * HTTP-Version field). If an invalid or unknown status code is
405 * passed, "500 Internal Server Error" will be returned.
406 * @param status The HTTP status code
407 * @return The Status-Line
408 * @deffunc const char *ap_get_status_line(int status)
410 AP_DECLARE(const char *) ap_get_status_line(int status);
412 /* Reading a block of data from the client connection (e.g., POST arg) */
415 * Setup the client to allow Apache to read the request body.
416 * @param r The current request
417 * @param read_policy How the server should interpret a chunked
418 * transfer-encoding. One of: <pre>
419 * REQUEST_NO_BODY Send 413 error if message has any body
420 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
421 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
423 * @return either OK or an error code
424 * @deffunc int ap_setup_client_block(request_rec *r, int read_policy)
426 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
429 * Determine if the client has sent any data. This also sends a
430 * 100 Continue response to HTTP/1.1 clients, so modules should not be called
431 * until the module is ready to read content.
432 * @warning Never call this function more than once.
433 * @param r The current request
434 * @return 0 if there is no message to read, 1 otherwise
435 * @deffunc int ap_should_client_block(request_rec *r)
437 AP_DECLARE(int) ap_should_client_block(request_rec *r);
440 * Call this in a loop. It will put data into a buffer and return the length
442 * @param r The current request
443 * @param buffer The buffer in which to store the data
444 * @param bufsiz The size of the buffer
445 * @return Number of bytes inserted into the buffer. When done reading, 0
446 * if EOF, or -1 if there was an error
447 * @deffunc long ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz)
449 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
452 * In HTTP/1.1, any method can have a body. However, most GET handlers
453 * wouldn't know what to do with a request body if they received one.
454 * This helper routine tests for and reads any message body in the request,
455 * simply discarding whatever it receives. We need to do this because
456 * failing to read the request body would cause it to be interpreted
457 * as the next request on a persistent connection.
458 * @param r The current request
459 * @return error status if request is malformed, OK otherwise
460 * @deffunc int ap_discard_request_body(request_rec *r)
462 AP_DECLARE(int) ap_discard_request_body(request_rec *r);
466 * Setup the output headers so that the client knows how to authenticate
467 * itself the next time, if an authentication request failed. This function
468 * works for both basic and digest authentication
469 * @param r The current request
470 * @deffunc void ap_note_auth_failure(request_rec *r)
472 AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
475 * Setup the output headers so that the client knows how to authenticate
476 * itself the next time, if an authentication request failed. This function
477 * works only for basic authentication
478 * @param r The current request
479 * @deffunc void ap_note_basic_auth_failure(request_rec *r)
481 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
484 * Setup the output headers so that the client knows how to authenticate
485 * itself the next time, if an authentication request failed. This function
486 * works only for digest authentication
487 * @param r The current request
488 * @deffunc void ap_note_digest_auth_failure(request_rec *r)
490 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
493 * Get the password from the request headers
494 * @param r The current request
495 * @param pw The password as set in the headers
496 * @return 0 (OK) if it set the 'pw' argument (and assured
497 * a correct value in r->user); otherwise it returns
498 * an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
499 * really confused, HTTP_UNAUTHORIZED if no authentication at all
500 * seemed to be in use, or DECLINED if there was authentication but
501 * it wasn't Basic (in which case, the caller should presumably
503 * @deffunc int ap_get_basic_auth_pw(request_rec *r, const char **pw)
505 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
508 * parse_uri: break apart the uri
509 * @warning Side Effects: <pre>
510 * - sets r->args to rest after '?' (or NULL if no '?')
511 * - sets r->uri to request uri (without r->args part)
512 * - sets r->hostname (if not set already) from request (scheme://host:port)
514 * @param r The current request
515 * @param uri The uri to break apart
516 * @deffunc void ap_parse_uri(request_rec *r, const char *uri)
518 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
521 * Get the next line of input for the request
522 * @param s The buffer into which to read the line
523 * @param n The size of the buffer
524 * @param r The request
525 * @param fold Whether to merge continuation lines
526 * @return The length of the line, if successful
527 * n, if the line is too big to fit in the buffer
528 * -1 for miscellaneous errors
529 * @deffunc int ap_method_number_of(const char *method)
531 AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
534 * Get the next line of input for the request
536 * Note: on ASCII boxes, ap_rgetline is a macro which simply calls
537 * ap_rgetline_core to get the line of input.
539 * on EBCDIC boxes, ap_rgetline is a wrapper function which
540 * translates ASCII protocol lines to the local EBCDIC code page
541 * after getting the line of input.
543 * @param s Pointer to the pointer to the buffer into which the line
544 * should be read; if *s==NULL, a buffer of the necessary size
545 * to hold the data will be allocated from the request pool
546 * @param n The size of the buffer
547 * @param read The length of the line.
548 * @param r The request
549 * @param fold Whether to merge continuation lines
550 * @param bb Working brigade to use when reading buckets
551 * @return APR_SUCCESS, if successful
552 * APR_ENOSPC, if the line is too big to fit in the buffer
553 * Other errors where appropriate
555 #if APR_CHARSET_EBCDIC
556 AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
558 request_rec *r, int fold,
559 apr_bucket_brigade *bb);
560 #else /* ASCII box */
561 #define ap_rgetline(s, n, read, r, fold, bb) \
562 ap_rgetline_core((s), (n), (read), (r), (fold), (bb))
564 AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
566 request_rec *r, int fold,
567 apr_bucket_brigade *bb);
570 * Get the method number associated with the given string, assumed to
571 * contain an HTTP method. Returns M_INVALID if not recognized.
572 * @param method A string containing a valid HTTP method
573 * @return The method number
575 AP_DECLARE(int) ap_method_number_of(const char *method);
578 * Get the method name associated with the given internal method
579 * number. Returns NULL if not recognized.
580 * @param p A pool to use for temporary allocations.
581 * @param methnum An integer value corresponding to an internal method number
582 * @return The name corresponding to the method number
584 AP_DECLARE(const char *) ap_method_name_of(apr_pool_t *p, int methnum);
589 * post_read_request --- run right after read_request or internal_redirect,
590 * and not run during any subrequests.
593 * This hook allows modules to affect the request immediately after the request
594 * has been read, and before any other phases have been processes. This allows
595 * modules to make decisions based upon the input header fields
596 * @param r The current request
597 * @return OK or DECLINED
598 * @deffunc ap_run_post_read_request(request_rec *r)
600 AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
603 * This hook allows modules to perform any module-specific logging activities
604 * over and above the normal server things.
605 * @param r The current request
606 * @return OK, DECLINED, or HTTP_...
607 * @deffunc int ap_run_log_transaction(request_rec *r)
609 AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
612 * This hook allows modules to retrieve the http method from a request. This
613 * allows Apache modules to easily extend the methods that Apache understands
614 * @param r The current request
615 * @return The http method from the request
616 * @deffunc const char *ap_run_http_method(const request_rec *r)
618 AP_DECLARE_HOOK(const char *,http_method,(const request_rec *r))
621 * Return the default port from the current request
622 * @param r The current request
623 * @return The current port
624 * @deffunc apr_port_t ap_run_default_port(const request_rec *r)
626 AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
628 typedef struct ap_bucket_error ap_bucket_error;
631 * A bucket referring to an HTTP error
632 * This bucket can be passed down the filter stack to indicate that an
633 * HTTP error occurred while running a filter. In order for this bucket
634 * to be used successfully, it MUST be sent as the first bucket in the
635 * first brigade to be sent from a given filter.
637 struct ap_bucket_error {
638 /** Number of buckets using this memory */
639 apr_bucket_refcount refcount;
640 /** The error code */
642 /** The error string */
646 AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
649 * Determine if a bucket is an error bucket
650 * @param e The bucket to inspect
651 * @return true or false
653 #define AP_BUCKET_IS_ERROR(e) (e->type == &ap_bucket_type_error)
656 * Make the bucket passed in an error bucket
657 * @param b The bucket to make into an error bucket
658 * @param error The HTTP error code to put in the bucket.
659 * @param buf An optional error string to put in the bucket.
660 * @param p A pool to allocate out of.
661 * @return The new bucket, or NULL if allocation failed
662 * @deffunc apr_bucket *ap_bucket_error_make(apr_bucket *b, int error, const char *buf, apr_pool_t *p)
664 AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
665 const char *buf, apr_pool_t *p);
668 * Create a bucket referring to an HTTP error.
669 * @param error The HTTP error code to put in the bucket.
670 * @param buf An optional error string to put in the bucket.
671 * @param p A pool to allocate the error string out of.
672 * @param list The bucket allocator from which to allocate the bucket
673 * @return The new bucket, or NULL if allocation failed
674 * @deffunc apr_bucket *ap_bucket_error_create(int error, const char *buf, apr_pool_t *p, apr_bucket_alloc_t *list)
676 AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error, const char *buf,
678 apr_bucket_alloc_t *list);
680 AP_DECLARE_NONSTD(apr_status_t) ap_byterange_filter(ap_filter_t *f, apr_bucket_brigade *b);
681 AP_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b);
682 AP_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(ap_filter_t *,
683 apr_bucket_brigade *);
684 AP_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(ap_filter_t *f, apr_bucket_brigade *b);
687 * Setting up the protocol fields for subsidiary requests...
688 * Also, a wrapup function to keep the internal accounting straight.
690 AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
691 AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub_r);
697 #endif /* !APACHE_HTTP_PROTOCOL_H */