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.
18 * @file http_protocol.h
19 * @brief HTTP protocol handling
21 * @defgroup APACHE_CORE_PROTO HTTP Protocol Handling
22 * @ingroup APACHE_CORE
26 #ifndef APACHE_HTTP_PROTOCOL_H
27 #define APACHE_HTTP_PROTOCOL_H
30 #include "apr_portable.h"
32 #include "apr_buckets.h"
33 #include "util_filter.h"
40 * This hook allows modules to insert filters for the current error response
41 * @param r the current request
44 AP_DECLARE_HOOK(void,insert_error_filter,(request_rec *r))
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.
49 AP_DECLARE_DATA extern ap_filter_rec_t *ap_old_write_func;
52 * Prototypes for routines which either talk directly back to the user,
53 * or control the ones that eventually do.
57 * Read a request and fill in the fields.
58 * @param c The current connection
59 * @return The new request_rec
61 request_rec *ap_read_request(conn_rec *c);
64 * Read the mime-encoded headers.
65 * @param r The current request
67 AP_DECLARE(void) ap_get_mime_headers(request_rec *r);
70 * Optimized version of ap_get_mime_headers() that requires a
71 * temporary brigade to work with
72 * @param r The current request
73 * @param bb temp brigade
75 AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r,
76 apr_bucket_brigade *bb);
78 /* Finish up stuff after a request */
81 * Called at completion of sending the response. It sends the terminating
82 * protocol information.
83 * @param r The current request
85 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
88 * Send error back to client.
89 * @param r The current request
90 * @param recursive_error last arg indicates error status in case we get
91 * an error in the process of trying to deal with an ErrorDocument
92 * to handle some other error. In that case, we print the default
93 * report for the first thing that went wrong, and more briefly report
94 * on the problem with the ErrorDocument.
96 AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
98 /* Set last modified header line from the lastmod date of the associated file.
99 * Also, set content length.
101 * May return an error status, typically HTTP_NOT_MODIFIED (that when the
102 * permit_cache argument is set to one).
106 * Set the content length for this request
107 * @param r The current request
108 * @param length The new content length
110 AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
113 * Set the keepalive status for this request
114 * @param r The current request
115 * @return 1 if keepalive can be set, 0 otherwise
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.
126 AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
129 * Build the content-type that should be sent to the client from the
130 * content-type specified. The following rules are followed:
131 * - if type is NULL or "", return NULL (do not set content-type).
132 * - if charset adding is disabled, stop processing and return type.
133 * - then, if there are no parameters on type, add the default charset
135 * @param r The current request
136 * @param type The content type
137 * @return The content-type
139 AP_DECLARE(const char *) ap_make_content_type(request_rec *r,
143 * Precompile metadata structures used by ap_make_content_type()
144 * @param pool The pool to use for allocations
146 AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool);
149 * Construct an entity tag from the resource information. If it's a real
150 * file, build in some of the file characteristics.
151 * @param r The current request
152 * @param force_weak Force the entity tag to be weak - it could be modified
153 * again in as short an interval.
154 * @return The entity tag
156 AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
159 * Set the E-tag outgoing header
160 * @param r The current request
162 AP_DECLARE(void) ap_set_etag(request_rec *r);
165 * Set the last modified time for the file being sent
166 * @param r The current request
168 AP_DECLARE(void) ap_set_last_modified(request_rec *r);
171 * Implements condition GET rules for HTTP/1.1 specification. This function
172 * inspects the client headers and determines if the response fulfills
173 * the requirements specified.
174 * @param r The current request
175 * @return OK if the response fulfills the condition GET rules, some
176 * other status code otherwise
178 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
180 /* Other ways to send stuff at the client. All of these keep track
181 * of bytes_sent automatically. This indirection is intended to make
182 * it a little more painless to slide things like HTTP-NG packetization
183 * underneath the main body of the code later. In the meantime, it lets
184 * us centralize a bit of accounting (bytes_sent).
186 * These also return the number of bytes written by the call.
187 * They should only be called with a timeout registered, for obvious reaasons.
188 * (Ditto the send_header stuff).
192 * Send an entire file to the client, using sendfile if supported by the
194 * @param fd The file to send.
195 * @param r The current request
196 * @param offset Offset into the file to start sending.
197 * @param length Amount of data to send
198 * @param nbytes Amount of data actually sent
200 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
201 apr_size_t length, apr_size_t *nbytes);
205 * Send an MMAP'ed file to the client
206 * @param mm The MMAP'ed file to send
207 * @param r The current request
208 * @param offset The offset into the MMAP to start sending
209 * @param length The amount of data to send
210 * @return The number of bytes sent
212 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
218 * Register a new request method, and return the offset that will be
219 * associated with that method.
221 * @param p The pool to create registered method numbers from.
222 * @param methname The name of the new method to register.
223 * @return Ab int value representing an offset into a bitmask.
225 AP_DECLARE(int) ap_method_register(apr_pool_t *p, const char *methname);
228 * Initialize the method_registry and allocate memory for it.
230 * @param p Pool to allocate memory for the registry from.
232 AP_DECLARE(void) ap_method_registry_init(apr_pool_t *p);
235 * This is a convenience macro to ease with checking a mask
236 * against a method name.
238 #define AP_METHOD_CHECK_ALLOWED(mask, methname) \
239 ((mask) & (AP_METHOD_BIT << ap_method_number_of((methname))))
242 * Create a new method list with the specified number of preallocated
243 * slots for extension methods.
245 * @param p Pointer to a pool in which the structure should be
247 * @param nelts Number of preallocated extension slots
248 * @return Pointer to the newly created structure.
250 AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
256 * @param dest List to copy to
257 * @param src List to copy from
259 AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
260 ap_method_list_t *src);
263 * Search for an HTTP method name in an ap_method_list_t structure, and
264 * return true if found.
266 * @param method String containing the name of the method to check.
267 * @param l Pointer to a method list, such as r->allowed_methods.
268 * @return 1 if method is in the list, otherwise 0
270 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
273 * Add an HTTP method name to an ap_method_list_t structure if it isn't
276 * @param method String containing the name of the method to check.
277 * @param l Pointer to a method list, such as r->allowed_methods.
280 AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
283 * Remove an HTTP method name from an ap_method_list_t structure.
285 * @param l Pointer to a method list, such as r->allowed_methods.
286 * @param method String containing the name of the method to remove.
289 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
293 * Reset a method list to be completely empty.
295 * @param l Pointer to a method list, such as r->allowed_methods.
298 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
301 * Set the content type for this request (r->content_type).
302 * @param r The current request
303 * @param ct The new content type
304 * @warning This function must be called to set r->content_type in order
305 * for the AddOutputFilterByType directive to work correctly.
307 AP_DECLARE(void) ap_set_content_type(request_rec *r, const char *ct);
309 /* Hmmm... could macrofy these for now, and maybe forever, though the
310 * definitions of the macros would get a whole lot hairier.
314 * Output one character for this request
315 * @param c the character to output
316 * @param r the current request
317 * @return The number of bytes sent
319 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
322 * Output a string for the current request
323 * @param str The string to output
324 * @param r The current request
325 * @return The number of bytes sent
327 AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
330 * Write a buffer for the current request
331 * @param buf The buffer to write
332 * @param nbyte The number of bytes to send from the buffer
333 * @param r The current request
334 * @return The number of bytes sent
336 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
339 * Write an unspecified number of strings to the request
340 * @param r The current request
341 * @param ... The strings to write
342 * @return The number of bytes sent
344 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...);
347 * Output data to the client in a printf format
348 * @param r The current request
349 * @param fmt The format string
350 * @param vlist The arguments to use to fill out the format string
351 * @return The number of bytes sent
353 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
356 * Output data to the client in a printf format
357 * @param r The current request
358 * @param fmt The format string
359 * @param ... The arguments to use to fill out the format string
360 * @return The number of bytes sent
362 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
363 __attribute__((format(printf,2,3)));
366 * Flush all of the data for the current request to the client
367 * @param r The current request
368 * @return The number of bytes sent
370 AP_DECLARE(int) ap_rflush(request_rec *r);
373 * Index used in custom_responses array for a specific error code
374 * (only use outside protocol.c is in getting them configured).
375 * @param status HTTP status code
376 * @return The index of the response
378 AP_DECLARE(int) ap_index_of_response(int status);
381 * Return the Status-Line for a given status code (excluding the
382 * HTTP-Version field). If an invalid or unknown status code is
383 * passed, "500 Internal Server Error" will be returned.
384 * @param status The HTTP status code
385 * @return The Status-Line
387 AP_DECLARE(const char *) ap_get_status_line(int status);
389 /* Reading a block of data from the client connection (e.g., POST arg) */
392 * Setup the client to allow Apache to read the request body.
393 * @param r The current request
394 * @param read_policy How the server should interpret a chunked
395 * transfer-encoding. One of: <pre>
396 * REQUEST_NO_BODY Send 413 error if message has any body
397 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
398 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
400 * @return either OK or an error code
402 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
405 * Determine if the client has sent any data. This also sends a
406 * 100 Continue response to HTTP/1.1 clients, so modules should not be called
407 * until the module is ready to read content.
408 * @warning Never call this function more than once.
409 * @param r The current request
410 * @return 0 if there is no message to read, 1 otherwise
412 AP_DECLARE(int) ap_should_client_block(request_rec *r);
415 * Call this in a loop. It will put data into a buffer and return the length
417 * @param r The current request
418 * @param buffer The buffer in which to store the data
419 * @param bufsiz The size of the buffer
420 * @return Number of bytes inserted into the buffer. When done reading, 0
421 * if EOF, or -1 if there was an error
423 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
426 * In HTTP/1.1, any method can have a body. However, most GET handlers
427 * wouldn't know what to do with a request body if they received one.
428 * This helper routine tests for and reads any message body in the request,
429 * simply discarding whatever it receives. We need to do this because
430 * failing to read the request body would cause it to be interpreted
431 * as the next request on a persistent connection.
432 * @param r The current request
433 * @return error status if request is malformed, OK otherwise
435 AP_DECLARE(int) ap_discard_request_body(request_rec *r);
438 * Setup the output headers so that the client knows how to authenticate
439 * itself the next time, if an authentication request failed.
440 * @param r The current request
442 AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
445 * @deprecated @see ap_note_auth_failure
447 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
450 * @deprecated @see ap_note_auth_failure
452 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
455 * This hook allows modules to add support for a specific auth type to
456 * ap_note_auth_failure
457 * @param r the current request
458 * @param auth_type the configured auth_type
459 * @return OK, DECLINED
461 AP_DECLARE_HOOK(int, note_auth_failure, (request_rec *r, const char *auth_type))
464 * Get the password from the request headers
465 * @param r The current request
466 * @param pw The password as set in the headers
467 * @return 0 (OK) if it set the 'pw' argument (and assured
468 * a correct value in r->user); otherwise it returns
469 * an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
470 * really confused, HTTP_UNAUTHORIZED if no authentication at all
471 * seemed to be in use, or DECLINED if there was authentication but
472 * it wasn't Basic (in which case, the caller should presumably
475 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
478 * parse_uri: break apart the uri
479 * @warning Side Effects:
480 * @li sets r->args to rest after '?' (or NULL if no '?')
481 * @li sets r->uri to request uri (without r->args part)
482 * @li sets r->hostname (if not set already) from request (scheme://host:port)
483 * @param r The current request
484 * @param uri The uri to break apart
486 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
489 * Get the next line of input for the request
490 * @param s The buffer into which to read the line
491 * @param n The size of the buffer
492 * @param r The request
493 * @param fold Whether to merge continuation lines
494 * @return The length of the line, if successful
495 * n, if the line is too big to fit in the buffer
496 * -1 for miscellaneous errors
498 AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
501 * Get the next line of input for the request
503 * Note: on ASCII boxes, ap_rgetline is a macro which simply calls
504 * ap_rgetline_core to get the line of input.
506 * on EBCDIC boxes, ap_rgetline is a wrapper function which
507 * translates ASCII protocol lines to the local EBCDIC code page
508 * after getting the line of input.
510 * @param s Pointer to the pointer to the buffer into which the line
511 * should be read; if *s==NULL, a buffer of the necessary size
512 * to hold the data will be allocated from the request pool
513 * @param n The size of the buffer
514 * @param read The length of the line.
515 * @param r The request
516 * @param fold Whether to merge continuation lines
517 * @param bb Working brigade to use when reading buckets
518 * @return APR_SUCCESS, if successful
519 * APR_ENOSPC, if the line is too big to fit in the buffer
520 * Other errors where appropriate
522 #if APR_CHARSET_EBCDIC
523 AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
525 request_rec *r, int fold,
526 apr_bucket_brigade *bb);
527 #else /* ASCII box */
528 #define ap_rgetline(s, n, read, r, fold, bb) \
529 ap_rgetline_core((s), (n), (read), (r), (fold), (bb))
532 /** @see ap_rgetline */
533 AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
535 request_rec *r, int fold,
536 apr_bucket_brigade *bb);
539 * Get the method number associated with the given string, assumed to
540 * contain an HTTP method. Returns M_INVALID if not recognized.
541 * @param method A string containing a valid HTTP method
542 * @return The method number
544 AP_DECLARE(int) ap_method_number_of(const char *method);
547 * Get the method name associated with the given internal method
548 * number. Returns NULL if not recognized.
549 * @param p A pool to use for temporary allocations.
550 * @param methnum An integer value corresponding to an internal method number
551 * @return The name corresponding to the method number
553 AP_DECLARE(const char *) ap_method_name_of(apr_pool_t *p, int methnum);
558 * pre_read_request --- run right before read_request_line(),
559 * and not run during any subrequests.
562 * This hook allows modules to affect the request or connection immediately before
563 * the request has been read, and before any other phases have been processes.
564 * @param r The current request of the soon-to-be-read request
565 * @param c The connection
568 AP_DECLARE_HOOK(void,pre_read_request,(request_rec *r, conn_rec *c))
571 * post_read_request --- run right after read_request or internal_redirect,
572 * and not run during any subrequests.
575 * This hook allows modules to affect the request immediately after the request
576 * has been read, and before any other phases have been processes. This allows
577 * modules to make decisions based upon the input header fields
578 * @param r The current request
579 * @return OK or DECLINED
581 AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
584 * This hook allows modules to perform any module-specific logging activities
585 * over and above the normal server things.
586 * @param r The current request
587 * @return OK, DECLINED, or HTTP_...
589 AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
592 * This hook allows modules to retrieve the http scheme for a request. This
593 * allows Apache modules to easily extend the schemes that Apache understands
594 * @param r The current request
595 * @return The http scheme from the request
597 AP_DECLARE_HOOK(const char *,http_scheme,(const request_rec *r))
600 * Return the default port from the current request
601 * @param r The current request
602 * @return The current port
604 AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
606 /** @see ap_bucket_type_error */
607 typedef struct ap_bucket_error ap_bucket_error;
610 * @struct ap_bucket_error
611 * @brief A bucket referring to an HTTP error
613 * This bucket can be passed down the filter stack to indicate that an
614 * HTTP error occurred while running a filter. In order for this bucket
615 * to be used successfully, it MUST be sent as the first bucket in the
616 * first brigade to be sent from a given filter.
618 struct ap_bucket_error {
619 /** Number of buckets using this memory */
620 apr_bucket_refcount refcount;
621 /** The error code */
623 /** The error string */
627 /** @see ap_bucket_type_error */
628 AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
631 * Determine if a bucket is an error bucket
632 * @param e The bucket to inspect
633 * @return true or false
635 #define AP_BUCKET_IS_ERROR(e) (e->type == &ap_bucket_type_error)
638 * Make the bucket passed in an error bucket
639 * @param b The bucket to make into an error bucket
640 * @param error The HTTP error code to put in the bucket.
641 * @param buf An optional error string to put in the bucket.
642 * @param p A pool to allocate out of.
643 * @return The new bucket, or NULL if allocation failed
645 AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
646 const char *buf, apr_pool_t *p);
649 * Create a bucket referring to an HTTP error.
650 * @param error The HTTP error code to put in the bucket.
651 * @param buf An optional error string to put in the bucket.
652 * @param p A pool to allocate the error string out of.
653 * @param list The bucket allocator from which to allocate the bucket
654 * @return The new bucket, or NULL if allocation failed
656 AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error, const char *buf,
658 apr_bucket_alloc_t *list);
660 AP_DECLARE_NONSTD(apr_status_t) ap_byterange_filter(ap_filter_t *f, apr_bucket_brigade *b);
661 AP_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b);
662 AP_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(ap_filter_t *,
663 apr_bucket_brigade *);
664 AP_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(ap_filter_t *f, apr_bucket_brigade *b);
667 * Sett up the protocol fields for subsidiary requests
668 * @param rnew New Sub Request
669 * @param r current request
671 AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
674 * A wrapup function to keep the internal accounting straight.
675 * Indicates that there is no more content coming.
676 * @param sub_r Subrequest that is now compete
678 AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub_r);
681 * Send an interim (HTTP 1xx) response immediately.
682 * @param r The request
683 * @param send_headers Whether to send&clear headers in r->headers_out
685 AP_DECLARE(void) ap_send_interim_response(request_rec *r, int send_headers);
691 #endif /* !APACHE_HTTP_PROTOCOL_H */