1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
59 #ifndef APACHE_HTTP_PROTOCOL_H
60 #define APACHE_HTTP_PROTOCOL_H
63 #include "apr_hooks.h"
64 #include "apr_portable.h"
66 #include "apr_buckets.h"
67 #include "util_filter.h"
74 * @package HTTP protocol handling
78 * Prototypes for routines which either talk directly back to the user,
79 * or control the ones that eventually do.
83 * Read a request and fill in the fields.
84 * @param c The current connection
85 * @return The new request_rec
87 request_rec *ap_read_request(conn_rec *c);
89 /* Finish up stuff after a request */
92 * Called at completion of sending the response. It sends the terminating
93 * protocol information.
94 * @param r The current request
95 * @deffunc void ap_finalize_request_protocol(request_rec *r)
97 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
100 * Send error back to client.
101 * @param r The current request
102 * @param recursive_error last arg indicates error status in case we get
103 * an error in the process of trying to deal with an ErrorDocument
104 * to handle some other error. In that case, we print the default
105 * report for the first thing that went wrong, and more briefly report
106 * on the problem with the ErrorDocument.
107 * @deffunc void ap_send_error_response(request_rec *r, int recursive_error)
109 AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
111 /* Set last modified header line from the lastmod date of the associated file.
112 * Also, set content length.
114 * May return an error status, typically HTTP_NOT_MODIFIED (that when the
115 * permit_cache argument is set to one).
119 * Set the content length for this request
120 * @param r The current request
121 * @param length The new content length
122 * @deffunc void ap_set_content_length(request_rec *r, apr_off_t length)
124 AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
127 * Set the keepalive status for this request
128 * @param r The current request
129 * @return 1 if keepalive can be set, 0 otherwise
130 * @deffunc int ap_set_keepalive(request_rec *r)
132 AP_DECLARE(int) ap_set_keepalive(request_rec *r);
135 * Return the latest rational time from a request/mtime pair. Mtime is
136 * returned unless it's in the future, in which case we return the current time.
137 * @param r The current request
138 * @param mtime The last modified time
139 * @return the latest rational time.
140 * @deffunc apr_time_t ap_rationalize_mtime(request_rec *r, apr_time_t mtime)
142 AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
145 * Build the content-type that should be sent to the client from the
146 * content-type specified. The following rules are followed:
147 * - if type is NULL, type is set to ap_default_type(r)
148 * - if charset adding is disabled, stop processing and return type.
149 * - then, if there are no parameters on type, add the default charset
151 * @param r The current request
152 * @return The content-type
153 * @deffunc const char *ap_make_content_type(request_rec *r, const char *type);
155 AP_DECLARE(const char *) ap_make_content_type(request_rec *r, const char *type);
158 * Construct an entity tag from the resource information. If it's a real
159 * file, build in some of the file characteristics.
160 * @param r The current request
161 * @param force_weak Force the entity tag to be weak - it could be modified
162 * again in as short an interval.
163 * @return The entity tag
164 * @deffunc char *ap_make_etag(request_rec *r, int force_weak)
166 AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
169 * Set the E-tag outgoing header
170 * @param The current request
171 * @deffunc void ap_set_etag(request_rec *r)
173 AP_DECLARE(void) ap_set_etag(request_rec *r);
176 * Set the last modified time for the file being sent
177 * @param r The current request
178 * @deffunc void ap_set_last_modified(request_rec *r)
180 AP_DECLARE(void) ap_set_last_modified(request_rec *r);
183 * Implements condition GET rules for HTTP/1.1 specification. This function
184 * inspects the client headers and determines if the response fulfills
185 * the requirements specified.
186 * @param r The current request
187 * @return 1 if the response fulfills the condition GET rules, 0 otherwise
188 * @deffunc int ap_meets_conditions(request_rec *r)
190 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
192 /* Other ways to send stuff at the client. All of these keep track
193 * of bytes_sent automatically. This indirection is intended to make
194 * it a little more painless to slide things like HTTP-NG packetization
195 * underneath the main body of the code later. In the meantime, it lets
196 * us centralize a bit of accounting (bytes_sent).
198 * These also return the number of bytes written by the call.
199 * They should only be called with a timeout registered, for obvious reaasons.
200 * (Ditto the send_header stuff).
204 * Send an entire file to the client, using sendfile if supported by the
206 * @param fd The file to send.
207 * @param r The current request
208 * @param offset Offset into the file to start sending.
209 * @param length Amount of data to send
210 * @param nbytes Amount of data actually sent
211 * @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);
213 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
214 apr_size_t length, apr_size_t *nbytes);
218 * Send an MMAP'ed file to the client
219 * @param mm The MMAP'ed file to send
220 * @param r The current request
221 * @param offset The offset into the MMAP to start sending
222 * @param length The amount of data to send
223 * @return The number of bytes sent
224 * @deffunc size_t ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset, size_t length)
226 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
231 * Create a new method list with the specified number of preallocated
232 * slots for extension methods.
234 * @param p Pointer to a pool in which the structure should be
236 * @param nelts Number of preallocated extension slots
237 * @return Pointer to the newly created structure.
238 * @deffunc ap_method_list_t ap_make_method_list(apr_pool_t *p, int nelts)
240 AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
241 AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
242 ap_method_list_t *src);
243 AP_DECLARE_NONSTD(void) ap_method_list_do(int (*comp) (void *urec, const char *mname,
246 const ap_method_list_t *ml, ...);
247 AP_DECLARE(void) ap_method_list_vdo(int (*comp) (void *urec, const char *mname,
249 void *rec, const ap_method_list_t *ml,
252 * Search for an HTTP method name in an ap_method_list_t structure, and
253 * return true if found.
255 * @param method String containing the name of the method to check.
256 * @param l Pointer to a method list, such as cmd->methods_limited.
257 * @return 1 if method is in the list, otherwise 0
258 * @deffunc int ap_method_in_list(const char *method, ap_method_list_t *l)
260 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
263 * Add an HTTP method name to an ap_method_list_t structure if it isn't
266 * @param method String containing the name of the method to check.
267 * @param l Pointer to a method list, such as cmd->methods_limited.
269 * @deffunc void ap_method_in_list(ap_method_list_t *l, const char *method)
271 AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
274 * Remove an HTTP method name from an ap_method_list_t structure.
276 * @param l Pointer to a method list, such as cmd->methods_limited.
277 * @param method String containing the name of the method to remove.
279 * @deffunc void ap_method_list_remove(ap_method_list_t *l, const char *method)
281 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
285 * Reset a method list to be completely empty.
287 * @param l Pointer to a method list, such as cmd->methods_limited.
289 * @deffunc void ap_clear_method_list(ap_method_list_t *l)
291 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
293 /* Hmmm... could macrofy these for now, and maybe forever, though the
294 * definitions of the macros would get a whole lot hairier.
298 * Output one character for this request
299 * @param c the character to output
300 * @param r the current request
301 * @return The number of bytes sent
302 * @deffunc int ap_rputc(int c, request_rec *r)
304 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
307 * Output a string for the current request
308 * @param str The string to output
309 * @param r The current request
310 * @return The number of bytes sent
311 * @deffunc int ap_rputs(const char *str, request_rec *r)
313 AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
316 * Write a buffer for the current request
317 * @param buf The buffer to write
318 * @param nbyte The number of bytes to send from the buffer
319 * @param r The current request
320 * @return The number of bytes sent
321 * @deffunc int ap_rwrite(const void *buf, int nbyte, request_rec *r)
323 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
326 * Write an unspecified number of strings to the request
327 * @param r The current request
328 * @param ... The strings to write
329 * @return The number of bytes sent
330 * @deffunc int ap_rvputs(request_rec *r, ...)
332 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...);
335 * Output data to the client in a printf format
336 * @param r The current request
337 * @param fmt The format string
338 * @param vlist The arguments to use to fill out the format string
339 * @return The number of bytes sent
340 * @deffunc int ap_vrprintf(request_rec *r, const char *fmt, va_list vlist)
342 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
345 * Output data to the client in a printf format
346 * @param r The current request
347 * @param fmt The format string
348 * @param ... The arguments to use to fill out the format string
349 * @return The number of bytes sent
350 * @deffunc int ap_rprintf(request_rec *r, const char *fmt, ...)
352 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
353 __attribute__((format(printf,2,3)));
355 * Flush all of the data for the current request to the client
356 * @param r The current request
357 * @return The number of bytes sent
358 * @deffunc int ap_rflush(request_rec *r)
360 AP_DECLARE(int) ap_rflush(request_rec *r);
363 * Index used in custom_responses array for a specific error code
364 * (only use outside protocol.c is in getting them configured).
365 * @param status HTTP status code
366 * @return The index of the response
367 * @deffunc int ap_index_of_response(int status)
369 AP_DECLARE(int) ap_index_of_response(int status);
372 * Return the Status-Line for a given status code (excluding the
373 * HTTP-Version field). If an invalid or unknown status code is
374 * passed, "500 Internal Server Error" will be returned.
375 * @param status The HTTP status code
376 * @return The Status-Line
377 * @deffunc const char *ap_get_status_line(int status)
379 AP_DECLARE(const char *) ap_get_status_line(int status);
381 /* Reading a block of data from the client connection (e.g., POST arg) */
384 * Setup the client to allow Apache to read the request body.
385 * @param r The current request
386 * @param read_policy How the server should interpret a chunked
387 * transfer-encoding. One of: <pre>
388 * REQUEST_NO_BODY Send 413 error if message has any body
389 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
390 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
392 * @return either OK or an error code
393 * @deffunc int ap_setup_cleint_block(request_rec *r, int read_policy)
395 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
398 * Determine if the client has sent any data. This also sends a
399 * 100 Continue resposne to HTTP/1.1 clients, so modules should not be called
400 * until the module is ready to read content.
401 * @warning Never call this function more than once.
402 * @param r The current request
403 * @return 0 if there is no message to read, 1 otherwise
404 * @deffunc int ap_should_client_block(request_rec *r)
406 AP_DECLARE(int) ap_should_client_block(request_rec *r);
409 * Call this in a loop. It will put data into a buffer and return the length
411 * @param r The current request
412 * @param buffer The buffer in which to store the data
413 * @param bufsiz The size of the buffer
414 * @return Number of bytes inserted into the buffer. When done reading, 0
415 * if EOF, or -1 if there was an error
416 * @deffunc long ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz)
418 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
421 * In HTTP/1.1, any method can have a body. However, most GET handlers
422 * wouldn't know what to do with a request body if they received one.
423 * This helper routine tests for and reads any message body in the request,
424 * simply discarding whatever it receives. We need to do this because
425 * failing to read the request body would cause it to be interpreted
426 * as the next request on a persistent connection.
427 * @param r The current request
428 * @return error status if request is malformed, OK otherwise
429 * @deffunc int ap_discard_request_body(request_rec *r)
431 AP_DECLARE(int) ap_discard_request_body(request_rec *r);
435 * Setup the output headers so that the client knows how to authenticate
436 * itself the next time, if an authentication request failed. This function
437 * works for both basic and digest authentication
438 * @param r The current request
439 * @deffunc void ap_note_auth_failure(request_rec *r)
441 AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
444 * Setup the output headers so that the client knows how to authenticate
445 * itself the next time, if an authentication request failed. This function
446 * works only for basic authentication
447 * @param r The current request
448 * @deffunc void ap_note_basic_auth_failure(request_rec *r)
450 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
453 * Setup the output headers so that the client knows how to authenticate
454 * itself the next time, if an authentication request failed. This function
455 * works only for digest authentication
456 * @param r The current request
457 * @deffunc void ap_note_digest_auth_failure(request_rec *r)
459 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
462 * Get the password from the request headers
463 * @param r The current request
464 * @param pw The password as set in the headers
465 * @return 0 (OK) if it set the 'pw' argument (and assured
466 * a correct value in r->connection->user); otherwise it returns
467 * an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
468 * really confused, HTTP_UNAUTHORIZED if no authentication at all
469 * seemed to be in use, or DECLINED if there was authentication but
470 * it wasn't Basic (in which case, the caller should presumably
472 * @deffunc int ap_get_basic_auth_pw(request_rec *r, const char **pw)
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: <pre>
479 * - sets r->args to rest after '?' (or NULL if no '?')
480 * - sets r->uri to request uri (without r->args part)
481 * - 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
485 * @deffunc void ap_parse_uri(request_rec *r, const char *uri)
487 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
489 AP_CORE_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
492 * Get the method number associated with the given string, assumed to
493 * contain an HTTP method. Returns M_INVALID if not recognized.
494 * @param method A string containing a valid HTTP method
495 * @return The method number
496 * @deffunc int ap_method_number_of(const char *method)
498 AP_DECLARE(int) ap_method_number_of(const char *method);
501 * Get the method name associated with the given internal method
502 * number. Returns NULL if not recognized.
503 * @param methnum An integer value corresponding to an internal method number
504 * @return The name corresponding to the method number
505 * @deffunc const char *ap_method_name_of(int methnum)
507 AP_DECLARE(const char *) ap_method_name_of(int methnum);
512 * post_read_request --- run right after read_request or internal_redirect,
513 * and not run during any subrequests.
516 * This hook allows modules to affect the request immediately after the request
517 * has been read, and before any other phases have been processes. This allows
518 * modules to make decisions based upon the input header fields
519 * @param r The current request
520 * @return OK or DECLINED
521 * @deffunc ap_run_post_read_request(request_rec *r)
523 AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
526 * This hook allows modules to perform any module-specific logging activities
527 * over and above the normal server things.
528 * @param r The current request
529 * @return OK, DECLINED, or HTTP_...
530 * @deffunc int ap_run_log_transaction(request_rec *r)
532 AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
535 * This hook allows modules to retrieve the http method from a request. This
536 * allows Apache modules to easily extend the methods that Apache understands
537 * @param r The current request
538 * @return The http method from the request
539 * @deffunc const char *ap_run_http_method(const request_rec *r)
541 AP_DECLARE_HOOK(const char *,http_method,(const request_rec *r))
544 * Return the default port from the current request
545 * @param r The current request
546 * @return The current port
547 * @deffunc apr_port_t ap_run_default_port(const request_rec *r)
549 AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
551 typedef struct ap_bucket_error ap_bucket_error;
554 * A bucket referring to an HTTP error
555 * This bucket can be passed down the filter stack to indicate that an
556 * HTTP error occurred while running a filter. In order for this bucket
557 * to be used successfully, it MUST be sent as the first bucket in the
558 * first brigade to be sent from a given filter.
560 struct ap_bucket_error {
561 /** The error code */
563 /** The error string */
567 AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
570 * Make the bucket passed in an error bucket
571 * @param b The bucket to make into an error bucket
572 * @param error The HTTP error code to put in the bucket.
573 * @param buf An optional error string to put in the bucket.
574 * @param p A pool to allocate out of.
575 * @return The new bucket, or NULL if allocation failed
576 * @deffunc apr_bucket *ap_bucket_error_make(apr_bucket *b, int error, const char *buf, apr_pool_t *p)
578 AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
579 const char *buf, apr_pool_t *p);
582 * Create a bucket referring to an HTTP error.
583 * @param error The HTTP error code to put in the bucket.
584 * @param buf An optional error string to put in the bucket.
585 * @param p A pool to allocate out of.
586 * @return The new bucket, or NULL if allocation failed
587 * @deffunc apr_bucket *ap_bucket_error_create(int error, const char *buf, apr_pool_t *p)
589 AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error,
590 const char *buf, apr_pool_t *p);
592 AP_DECLARE_NONSTD(apr_status_t) ap_byterange_filter(ap_filter_t *f, apr_bucket_brigade *b);
593 AP_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b);
594 AP_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(ap_filter_t *,
595 apr_bucket_brigade *);
596 AP_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(ap_filter_t *f, apr_bucket_brigade *b);
599 * Setting up the protocol fields for subsidiary requests...
600 * Also, a wrapup function to keep the internal accounting straight.
602 void ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
603 void ap_finalize_sub_req_protocol(request_rec *sub_r);
609 #endif /* !APACHE_HTTP_PROTOCOL_H */