switch to a 60 bit hash
[httpd-crcsyncproxy.git] / include / http_protocol.h
blobbf405af4e58450e57ceb43113e11ea39e418373c
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 /**
18 * @file http_protocol.h
19 * @brief HTTP protocol handling
21 * @defgroup APACHE_CORE_PROTO HTTP Protocol Handling
22 * @ingroup APACHE_CORE
23 * @{
26 #ifndef APACHE_HTTP_PROTOCOL_H
27 #define APACHE_HTTP_PROTOCOL_H
29 #include "httpd.h"
30 #include "apr_hooks.h"
31 #include "apr_portable.h"
32 #include "apr_mmap.h"
33 #include "apr_buckets.h"
34 #include "util_filter.h"
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
40 /**
41 * This hook allows modules to insert filters for the current error response
42 * @param r the current request
43 * @ingroup hooks
45 AP_DECLARE_HOOK(void,insert_error_filter,(request_rec *r))
47 /** This is an optimization. We keep a record of the filter_rec that
48 * stores the old_write filter, so that we can avoid strcmp's later.
50 AP_DECLARE_DATA extern ap_filter_rec_t *ap_old_write_func;
53 * Prototypes for routines which either talk directly back to the user,
54 * or control the ones that eventually do.
57 /**
58 * Read a request and fill in the fields.
59 * @param c The current connection
60 * @return The new request_rec
61 */
62 request_rec *ap_read_request(conn_rec *c);
64 /**
65 * Read the mime-encoded headers.
66 * @param r The current request
68 AP_DECLARE(void) ap_get_mime_headers(request_rec *r);
70 /**
71 * Optimized version of ap_get_mime_headers() that requires a
72 * temporary brigade to work with
73 * @param r The current request
74 * @param bb temp brigade
76 AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r,
77 apr_bucket_brigade *bb);
79 /* Finish up stuff after a request */
81 /**
82 * Called at completion of sending the response. It sends the terminating
83 * protocol information.
84 * @param r The current request
86 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
88 /**
89 * Send error back to client.
90 * @param r The current request
91 * @param recursive_error last arg indicates error status in case we get
92 * an error in the process of trying to deal with an ErrorDocument
93 * to handle some other error. In that case, we print the default
94 * report for the first thing that went wrong, and more briefly report
95 * on the problem with the ErrorDocument.
97 AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
99 /* Set last modified header line from the lastmod date of the associated file.
100 * Also, set content length.
102 * May return an error status, typically HTTP_NOT_MODIFIED (that when the
103 * permit_cache argument is set to one).
107 * Set the content length for this request
108 * @param r The current request
109 * @param length The new content length
111 AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
114 * Set the keepalive status for this request
115 * @param r The current request
116 * @return 1 if keepalive can be set, 0 otherwise
118 AP_DECLARE(int) ap_set_keepalive(request_rec *r);
121 * Return the latest rational time from a request/mtime pair. Mtime is
122 * returned unless it's in the future, in which case we return the current time.
123 * @param r The current request
124 * @param mtime The last modified time
125 * @return the latest rational time.
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 or "", return NULL (do not set content-type).
133 * - if charset adding is disabled, stop processing and return type.
134 * - then, if there are no parameters on type, add the default charset
135 * - return type
136 * @param r The current request
137 * @param type The content type
138 * @return The content-type
140 AP_DECLARE(const char *) ap_make_content_type(request_rec *r,
141 const char *type);
144 * Precompile metadata structures used by ap_make_content_type()
145 * @param pool The pool to use for allocations
147 AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool);
150 * Construct an entity tag from the resource information. If it's a real
151 * file, build in some of the file characteristics.
152 * @param r The current request
153 * @param force_weak Force the entity tag to be weak - it could be modified
154 * again in as short an interval.
155 * @return The entity tag
157 AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
160 * Set the E-tag outgoing header
161 * @param r The current request
163 AP_DECLARE(void) ap_set_etag(request_rec *r);
166 * Set the last modified time for the file being sent
167 * @param r The current request
169 AP_DECLARE(void) ap_set_last_modified(request_rec *r);
172 * Implements condition GET rules for HTTP/1.1 specification. This function
173 * inspects the client headers and determines if the response fulfills
174 * the requirements specified.
175 * @param r The current request
176 * @return OK if the response fulfills the condition GET rules, some
177 * other status code otherwise
179 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
181 /* Other ways to send stuff at the client. All of these keep track
182 * of bytes_sent automatically. This indirection is intended to make
183 * it a little more painless to slide things like HTTP-NG packetization
184 * underneath the main body of the code later. In the meantime, it lets
185 * us centralize a bit of accounting (bytes_sent).
187 * These also return the number of bytes written by the call.
188 * They should only be called with a timeout registered, for obvious reaasons.
189 * (Ditto the send_header stuff).
193 * Send an entire file to the client, using sendfile if supported by the
194 * current platform
195 * @param fd The file to send.
196 * @param r The current request
197 * @param offset Offset into the file to start sending.
198 * @param length Amount of data to send
199 * @param nbytes Amount of data actually sent
201 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
202 apr_size_t length, apr_size_t *nbytes);
204 #if APR_HAS_MMAP
206 * Send an MMAP'ed file to the client
207 * @param mm The MMAP'ed file to send
208 * @param r The current request
209 * @param offset The offset into the MMAP to start sending
210 * @param length The amount of data to send
211 * @return The number of bytes sent
213 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
214 size_t length);
215 #endif
219 * Register a new request method, and return the offset that will be
220 * associated with that method.
222 * @param p The pool to create registered method numbers from.
223 * @param methname The name of the new method to register.
224 * @return Ab int value representing an offset into a bitmask.
226 AP_DECLARE(int) ap_method_register(apr_pool_t *p, const char *methname);
229 * Initialize the method_registry and allocate memory for it.
231 * @param p Pool to allocate memory for the registry from.
233 AP_DECLARE(void) ap_method_registry_init(apr_pool_t *p);
236 * This is a convenience macro to ease with checking a mask
237 * against a method name.
239 #define AP_METHOD_CHECK_ALLOWED(mask, methname) \
240 ((mask) & (AP_METHOD_BIT << ap_method_number_of((methname))))
243 * Create a new method list with the specified number of preallocated
244 * slots for extension methods.
246 * @param p Pointer to a pool in which the structure should be
247 * allocated.
248 * @param nelts Number of preallocated extension slots
249 * @return Pointer to the newly created structure.
251 AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
255 * Copy a method list
257 * @param dest List to copy to
258 * @param src List to copy from
260 AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
261 ap_method_list_t *src);
264 * Search for an HTTP method name in an ap_method_list_t structure, and
265 * return true if found.
267 * @param method String containing the name of the method to check.
268 * @param l Pointer to a method list, such as r->allowed_methods.
269 * @return 1 if method is in the list, otherwise 0
271 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
274 * Add an HTTP method name to an ap_method_list_t structure if it isn't
275 * already listed.
277 * @param method String containing the name of the method to check.
278 * @param l Pointer to a method list, such as r->allowed_methods.
279 * @return None.
281 AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
284 * Remove an HTTP method name from an ap_method_list_t structure.
286 * @param l Pointer to a method list, such as r->allowed_methods.
287 * @param method String containing the name of the method to remove.
288 * @return None.
290 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
291 const char *method);
294 * Reset a method list to be completely empty.
296 * @param l Pointer to a method list, such as r->allowed_methods.
297 * @return None.
299 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
302 * Set the content type for this request (r->content_type).
303 * @param r The current request
304 * @param ct The new content type
305 * @warning This function must be called to set r->content_type in order
306 * for the AddOutputFilterByType directive to work correctly.
308 AP_DECLARE(void) ap_set_content_type(request_rec *r, const char *ct);
310 /* Hmmm... could macrofy these for now, and maybe forever, though the
311 * definitions of the macros would get a whole lot hairier.
315 * Output one character for this request
316 * @param c the character to output
317 * @param r the current request
318 * @return The number of bytes sent
320 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
323 * Output a string for the current request
324 * @param str The string to output
325 * @param r The current request
326 * @return The number of bytes sent
328 AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
331 * Write a buffer for the current request
332 * @param buf The buffer to write
333 * @param nbyte The number of bytes to send from the buffer
334 * @param r The current request
335 * @return The number of bytes sent
337 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
340 * Write an unspecified number of strings to the request
341 * @param r The current request
342 * @param ... The strings to write
343 * @return The number of bytes sent
345 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...);
348 * Output data to the client in a printf format
349 * @param r The current request
350 * @param fmt The format string
351 * @param vlist The arguments to use to fill out the format string
352 * @return The number of bytes sent
354 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
357 * Output data to the client in a printf format
358 * @param r The current request
359 * @param fmt The format string
360 * @param ... The arguments to use to fill out the format string
361 * @return The number of bytes sent
363 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
364 __attribute__((format(printf,2,3)));
367 * Flush all of the data for the current request to the client
368 * @param r The current request
369 * @return The number of bytes sent
371 AP_DECLARE(int) ap_rflush(request_rec *r);
374 * Index used in custom_responses array for a specific error code
375 * (only use outside protocol.c is in getting them configured).
376 * @param status HTTP status code
377 * @return The index of the response
379 AP_DECLARE(int) ap_index_of_response(int status);
381 /**
382 * Return the Status-Line for a given status code (excluding the
383 * HTTP-Version field). If an invalid or unknown status code is
384 * passed, "500 Internal Server Error" will be returned.
385 * @param status The HTTP status code
386 * @return The Status-Line
388 AP_DECLARE(const char *) ap_get_status_line(int status);
390 /* Reading a block of data from the client connection (e.g., POST arg) */
393 * Setup the client to allow Apache to read the request body.
394 * @param r The current request
395 * @param read_policy How the server should interpret a chunked
396 * transfer-encoding. One of: <pre>
397 * REQUEST_NO_BODY Send 413 error if message has any body
398 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
399 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
400 * </pre>
401 * @return either OK or an error code
403 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
406 * Determine if the client has sent any data. This also sends a
407 * 100 Continue response to HTTP/1.1 clients, so modules should not be called
408 * until the module is ready to read content.
409 * @warning Never call this function more than once.
410 * @param r The current request
411 * @return 0 if there is no message to read, 1 otherwise
413 AP_DECLARE(int) ap_should_client_block(request_rec *r);
416 * Call this in a loop. It will put data into a buffer and return the length
417 * of the input block
418 * @param r The current request
419 * @param buffer The buffer in which to store the data
420 * @param bufsiz The size of the buffer
421 * @return Number of bytes inserted into the buffer. When done reading, 0
422 * if EOF, or -1 if there was an error
424 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
427 * In HTTP/1.1, any method can have a body. However, most GET handlers
428 * wouldn't know what to do with a request body if they received one.
429 * This helper routine tests for and reads any message body in the request,
430 * simply discarding whatever it receives. We need to do this because
431 * failing to read the request body would cause it to be interpreted
432 * as the next request on a persistent connection.
433 * @param r The current request
434 * @return error status if request is malformed, OK otherwise
436 AP_DECLARE(int) ap_discard_request_body(request_rec *r);
439 * Setup the output headers so that the client knows how to authenticate
440 * itself the next time, if an authentication request failed. This function
441 * works for both basic and digest authentication
442 * @param r The current request
444 AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
447 * Setup the output headers so that the client knows how to authenticate
448 * itself the next time, if an authentication request failed. This function
449 * works only for basic authentication
450 * @param r The current request
452 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
455 * Setup the output headers so that the client knows how to authenticate
456 * itself the next time, if an authentication request failed. This function
457 * works only for digest authentication
458 * @param r The current request
460 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
463 * Get the password from the request headers
464 * @param r The current request
465 * @param pw The password as set in the headers
466 * @return 0 (OK) if it set the 'pw' argument (and assured
467 * a correct value in r->user); otherwise it returns
468 * an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
469 * really confused, HTTP_UNAUTHORIZED if no authentication at all
470 * seemed to be in use, or DECLINED if there was authentication but
471 * it wasn't Basic (in which case, the caller should presumably
472 * decline as well).
474 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
477 * parse_uri: break apart the uri
478 * @warning Side Effects:
479 * @li sets r->args to rest after '?' (or NULL if no '?')
480 * @li sets r->uri to request uri (without r->args part)
481 * @li sets r->hostname (if not set already) from request (scheme://host:port)
482 * @param r The current request
483 * @param uri The uri to break apart
485 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
488 * Get the next line of input for the request
489 * @param s The buffer into which to read the line
490 * @param n The size of the buffer
491 * @param r The request
492 * @param fold Whether to merge continuation lines
493 * @return The length of the line, if successful
494 * n, if the line is too big to fit in the buffer
495 * -1 for miscellaneous errors
497 AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
500 * Get the next line of input for the request
502 * Note: on ASCII boxes, ap_rgetline is a macro which simply calls
503 * ap_rgetline_core to get the line of input.
505 * on EBCDIC boxes, ap_rgetline is a wrapper function which
506 * translates ASCII protocol lines to the local EBCDIC code page
507 * after getting the line of input.
509 * @param s Pointer to the pointer to the buffer into which the line
510 * should be read; if *s==NULL, a buffer of the necessary size
511 * to hold the data will be allocated from the request pool
512 * @param n The size of the buffer
513 * @param read The length of the line.
514 * @param r The request
515 * @param fold Whether to merge continuation lines
516 * @param bb Working brigade to use when reading buckets
517 * @return APR_SUCCESS, if successful
518 * APR_ENOSPC, if the line is too big to fit in the buffer
519 * Other errors where appropriate
521 #if APR_CHARSET_EBCDIC
522 AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
523 apr_size_t *read,
524 request_rec *r, int fold,
525 apr_bucket_brigade *bb);
526 #else /* ASCII box */
527 #define ap_rgetline(s, n, read, r, fold, bb) \
528 ap_rgetline_core((s), (n), (read), (r), (fold), (bb))
529 #endif
531 /** @see ap_rgetline */
532 AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
533 apr_size_t *read,
534 request_rec *r, int fold,
535 apr_bucket_brigade *bb);
538 * Get the method number associated with the given string, assumed to
539 * contain an HTTP method. Returns M_INVALID if not recognized.
540 * @param method A string containing a valid HTTP method
541 * @return The method number
543 AP_DECLARE(int) ap_method_number_of(const char *method);
546 * Get the method name associated with the given internal method
547 * number. Returns NULL if not recognized.
548 * @param p A pool to use for temporary allocations.
549 * @param methnum An integer value corresponding to an internal method number
550 * @return The name corresponding to the method number
552 AP_DECLARE(const char *) ap_method_name_of(apr_pool_t *p, int methnum);
555 /* Hooks */
557 * post_read_request --- run right after read_request or internal_redirect,
558 * and not run during any subrequests.
561 * This hook allows modules to affect the request immediately after the request
562 * has been read, and before any other phases have been processes. This allows
563 * modules to make decisions based upon the input header fields
564 * @param r The current request
565 * @return OK or DECLINED
567 AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
570 * This hook allows modules to perform any module-specific logging activities
571 * over and above the normal server things.
572 * @param r The current request
573 * @return OK, DECLINED, or HTTP_...
575 AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
578 * This hook allows modules to retrieve the http scheme for a request. This
579 * allows Apache modules to easily extend the schemes that Apache understands
580 * @param r The current request
581 * @return The http scheme from the request
583 AP_DECLARE_HOOK(const char *,http_scheme,(const request_rec *r))
586 * Return the default port from the current request
587 * @param r The current request
588 * @return The current port
590 AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
592 /** @see ap_bucket_type_error */
593 typedef struct ap_bucket_error ap_bucket_error;
596 * @struct ap_bucket_error
597 * @brief A bucket referring to an HTTP error
599 * This bucket can be passed down the filter stack to indicate that an
600 * HTTP error occurred while running a filter. In order for this bucket
601 * to be used successfully, it MUST be sent as the first bucket in the
602 * first brigade to be sent from a given filter.
604 struct ap_bucket_error {
605 /** Number of buckets using this memory */
606 apr_bucket_refcount refcount;
607 /** The error code */
608 int status;
609 /** The error string */
610 const char *data;
613 /** @see ap_bucket_type_error */
614 AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
617 * Determine if a bucket is an error bucket
618 * @param e The bucket to inspect
619 * @return true or false
621 #define AP_BUCKET_IS_ERROR(e) (e->type == &ap_bucket_type_error)
624 * Make the bucket passed in an error bucket
625 * @param b The bucket to make into an error bucket
626 * @param error The HTTP error code to put in the bucket.
627 * @param buf An optional error string to put in the bucket.
628 * @param p A pool to allocate out of.
629 * @return The new bucket, or NULL if allocation failed
631 AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
632 const char *buf, apr_pool_t *p);
635 * Create a bucket referring to an HTTP error.
636 * @param error The HTTP error code to put in the bucket.
637 * @param buf An optional error string to put in the bucket.
638 * @param p A pool to allocate the error string out of.
639 * @param list The bucket allocator from which to allocate the bucket
640 * @return The new bucket, or NULL if allocation failed
642 AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error, const char *buf,
643 apr_pool_t *p,
644 apr_bucket_alloc_t *list);
646 AP_DECLARE_NONSTD(apr_status_t) ap_byterange_filter(ap_filter_t *f, apr_bucket_brigade *b);
647 AP_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b);
648 AP_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(ap_filter_t *,
649 apr_bucket_brigade *);
650 AP_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(ap_filter_t *f, apr_bucket_brigade *b);
653 * Sett up the protocol fields for subsidiary requests
654 * @param rnew New Sub Request
655 * @param r current request
657 AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
660 * A wrapup function to keep the internal accounting straight.
661 * Indicates that there is no more content coming.
662 * @param sub_r Subrequest that is now compete
664 AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub_r);
667 * Send an interim (HTTP 1xx) response immediately.
668 * @param r The request
669 * @param send_headers Whether to send&clear headers in r->headers_out
671 AP_DECLARE(void) ap_send_interim_response(request_rec *r, int send_headers);
673 #ifdef __cplusplus
675 #endif
677 #endif /* !APACHE_HTTP_PROTOCOL_H */
678 /** @} */