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"
73 * @package HTTP protocol handling
77 * Prototypes for routines which either talk directly back to the user,
78 * or control the ones that eventually do.
82 * Read a request and fill in the fields.
83 * @param c The current connection
84 * @return The new request_rec
86 request_rec *ap_read_request(conn_rec *c);
89 * Send the minimal part of an HTTP response header.
90 * @param r The current request
91 * @param bb The brigade to add the header to.
92 * @warning Modules should be very careful about using this, and should
93 * prefer ap_send_http_header(). Much of the HTTP/1.1 implementation
94 * correctness depends on code in ap_send_http_header().
95 * @deffunc void ap_basic_http_header(request_rec *r, apr_bucket_brigade *bb)
97 AP_DECLARE(void) ap_basic_http_header(request_rec *r, apr_bucket_brigade *bb);
100 * Send the Status-Line and header fields for HTTP response
101 * @param r The current request
102 * @deffunc void ap_send_http_header(request_rec *r)
104 AP_DECLARE(void) ap_send_http_header(request_rec *r);
107 /* Finish up stuff after a request */
110 * Called at completion of sending the response. It sends the terminating
111 * protocol information.
112 * @param r The current request
113 * @deffunc void ap_finalize_request_protocol(request_rec *r)
115 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
118 * Send error back to client.
119 * @param r The current request
120 * @param recursive_error last arg indicates error status in case we get
121 * an error in the process of trying to deal with an ErrorDocument
122 * to handle some other error. In that case, we print the default
123 * report for the first thing that went wrong, and more briefly report
124 * on the problem with the ErrorDocument.
125 * @deffunc void ap_send_error_response(request_rec *r, int recursive_error)
127 AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
129 /* Set last modified header line from the lastmod date of the associated file.
130 * Also, set content length.
132 * May return an error status, typically HTTP_NOT_MODIFIED (that when the
133 * permit_cache argument is set to one).
137 * Set the content length for this request
138 * @param r The current request
139 * @param length The new content length
140 * @deffunc void ap_set_content_length(request_rec *r, apr_off_t length)
142 AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
145 * Set the keepalive status for this request
146 * @param r The current request
147 * @return 1 if keepalive can be set, 0 otherwise
148 * @deffunc int ap_set_keepalive(request_rec *r)
150 AP_DECLARE(int) ap_set_keepalive(request_rec *r);
153 * Return the latest rational time from a request/mtime pair. Mtime is
154 * returned unless it's in the future, in which case we return the current time.
155 * @param r The current request
156 * @param mtime The last modified time
157 * @return the latest rational time.
158 * @deffunc apr_time_t ap_rationalize_mtime(request_rec *r, apr_time_t mtime)
160 AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
163 * Construct an entity tag from the resource information. If it's a real
164 * file, build in some of the file characteristics.
165 * @param r The current request
166 * @param force_weak Force the entity tag to be weak - it could be modified
167 * again in as short an interval.
168 * @return The entity tag
169 * @deffunc char *ap_make_etag(request_rec *r, int force_weak)
171 AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
174 * Set the E-tag outgoing header
175 * @param The current request
176 * @deffunc void ap_set_etag(request_rec *r)
178 AP_DECLARE(void) ap_set_etag(request_rec *r);
181 * Set the last modified time for the file being sent
182 * @param r The current request
183 * @deffunc void ap_set_last_modified(request_rec *r)
185 AP_DECLARE(void) ap_set_last_modified(request_rec *r);
188 * Implements condition GET rules for HTTP/1.1 specification. This function
189 * inspects the client headers and determines if the response fulfills
190 * the requirements specified.
191 * @param r The current request
192 * @return 1 if the response fulfills the condition GET rules, 0 otherwise
193 * @deffunc int ap_meets_conditions(request_rec *r)
195 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
197 /* Other ways to send stuff at the client. All of these keep track
198 * of bytes_sent automatically. This indirection is intended to make
199 * it a little more painless to slide things like HTTP-NG packetization
200 * underneath the main body of the code later. In the meantime, it lets
201 * us centralize a bit of accounting (bytes_sent).
203 * These also return the number of bytes written by the call.
204 * They should only be called with a timeout registered, for obvious reaasons.
205 * (Ditto the send_header stuff).
209 * Send an entire file to the client, using sendfile if supported by the
211 * @param fd The file to send.
212 * @param r The current request
213 * @param offset Offset into the file to start sending.
214 * @param length Amount of data to send
215 * @param nbytes Amount of data actually sent
216 * @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);
218 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
219 apr_size_t length, apr_size_t *nbytes);
223 * Send an MMAP'ed file to the client
224 * @param mm The MMAP'ed file to send
225 * @param r The current request
226 * @param offset The offset into the MMAP to start sending
227 * @param length The amount of data to send
228 * @return The number of bytes sent
229 * @deffunc size_t ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset, size_t length)
231 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
236 * Create a new method list with the specified number of preallocated
237 * slots for extension methods.
239 * @param p Pointer to a pool in which the structure should be
241 * @param nelts Number of preallocated extension slots
242 * @return Pointer to the newly created structure.
243 * @deffunc ap_method_list_t ap_make_method_list(apr_pool_t *p, int nelts)
245 AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
246 AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
247 ap_method_list_t *src);
248 AP_DECLARE_NONSTD(void) ap_method_list_do(int (*comp) (void *urec, const char *mname,
251 const ap_method_list_t *ml, ...);
252 AP_DECLARE(void) ap_method_list_vdo(int (*comp) (void *urec, const char *mname,
254 void *rec, const ap_method_list_t *ml,
257 * Search for an HTTP method name in an ap_method_list_t structure, and
258 * return true if found.
260 * @param method String containing the name of the method to check.
261 * @param l Pointer to a method list, such as cmd->methods_limited.
262 * @return 1 if method is in the list, otherwise 0
263 * @deffunc int ap_method_in_list(const char *method, ap_method_list_t *l)
265 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
268 * Add an HTTP method name to an ap_method_list_t structure if it isn't
271 * @param method String containing the name of the method to check.
272 * @param l Pointer to a method list, such as cmd->methods_limited.
274 * @deffunc void ap_method_in_list(ap_method_list_t *l, const char *method)
276 AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
279 * Remove an HTTP method name from an ap_method_list_t structure.
281 * @param l Pointer to a method list, such as cmd->methods_limited.
282 * @param method String containing the name of the method to remove.
284 * @deffunc void ap_method_list_remove(ap_method_list_t *l, const char *method)
286 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
290 * Reset a method list to be completely empty.
292 * @param l Pointer to a method list, such as cmd->methods_limited.
294 * @deffunc void ap_clear_method_list(ap_method_list_t *l)
296 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
298 /* Hmmm... could macrofy these for now, and maybe forever, though the
299 * definitions of the macros would get a whole lot hairier.
303 * Output one character for this request
304 * @param c the character to output
305 * @param r the current request
306 * @return The number of bytes sent
307 * @deffunc int ap_rputc(int c, request_rec *r)
309 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
312 * Output a string for the current request
313 * @param str The string to output
314 * @param r The current request
315 * @return The number of bytes sent
316 * @deffunc int ap_rputs(const char *str, request_rec *r)
318 AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
321 * Write a buffer for the current request
322 * @param buf The buffer to write
323 * @param nbyte The number of bytes to send from the buffer
324 * @param r The current request
325 * @return The number of bytes sent
326 * @deffunc int ap_rwrite(const void *buf, int nbyte, request_rec *r)
328 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
331 * Write an unspecified number of strings to the request
332 * @param r The current request
333 * @param ... The strings to write
334 * @return The number of bytes sent
335 * @deffunc int ap_rvputs(request_rec *r, ...)
337 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...);
340 * Output data to the client in a printf format
341 * @param r The current request
342 * @param fmt The format string
343 * @param vlist The arguments to use to fill out the format string
344 * @return The number of bytes sent
345 * @deffunc int ap_vrprintf(request_rec *r, const char *fmt, va_list vlist)
347 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
350 * Output data to the client in a printf format
351 * @param r The current request
352 * @param fmt The format string
353 * @param ... The arguments to use to fill out the format string
354 * @return The number of bytes sent
355 * @deffunc int ap_rprintf(request_rec *r, const char *fmt, ...)
357 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
358 __attribute__((format(printf,2,3)));
360 * Flush all of the data for the current request to the client
361 * @param r The current request
362 * @return The number of bytes sent
363 * @deffunc int ap_rflush(request_rec *r)
365 AP_DECLARE(int) ap_rflush(request_rec *r);
368 * Index used in custom_responses array for a specific error code
369 * (only use outside protocol.c is in getting them configured).
370 * @param status HTTP status code
371 * @return The index of the response
372 * @deffunc int ap_index_of_response(int status)
374 AP_DECLARE(int) ap_index_of_response(int status);
377 * Return the Status-Line for a given status code (excluding the
378 * HTTP-Version field). If an invalid or unknown status code is
379 * passed, "500 Internal Server Error" will be returned.
380 * @param status The HTTP status code
381 * @return The Status-Line
382 * @deffunc const char *ap_get_status_line(int status)
384 AP_DECLARE(const char *) ap_get_status_line(int status);
386 /* Reading a block of data from the client connection (e.g., POST arg) */
389 * Setup the client to allow Apache to read the request body.
390 * @param r The current request
391 * @param read_policy How the server should interpret a chunked
392 * transfer-encoding. One of: <PRE>
393 * REQUEST_NO_BODY Send 413 error if message has any body
394 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
395 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
397 * @return either OK or an error code
398 * @deffunc int ap_setup_cleint_block(request_rec *r, int read_policy)
400 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
403 * Determine if the client has sent any data. This also sends a
404 * 100 Continue resposne to HTTP/1.1 clients, so modules should not be called
405 * until the module is ready to read content.
406 * @warning Never call this function more than once.
407 * @param r The current request
408 * @return 0 if there is no message to read, 1 otherwise
409 * @deffunc int ap_should_client_block(request_rec *r)
411 AP_DECLARE(int) ap_should_client_block(request_rec *r);
414 * Call this in a loop. It will put data into a buffer and return the length
416 * @param r The current request
417 * @param buffer The buffer in which to store the data
418 * @param bufsiz The size of the buffer
419 * @return Number of bytes inserted into the buffer. When done reading, 0
420 * if EOF, or -1 if there was an error
421 * @deffunc long ap_get_client_block(request_rec *r, char *buffer, int bufsiz)
423 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, int 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
434 * @deffunc int ap_discard_request_body(request_rec *r)
436 AP_DECLARE(int) ap_discard_request_body(request_rec *r);
439 /* Sending a byterange */
442 * Setup the output headers so that the client knows how to authenticate
443 * itself the next time, if an authentication request failed. This function
444 * works for both basic and digest authentication
445 * @param r The current request
446 * @deffunc void ap_note_auth_failure(request_rec *r)
448 AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
451 * Setup the output headers so that the client knows how to authenticate
452 * itself the next time, if an authentication request failed. This function
453 * works only for basic authentication
454 * @param r The current request
455 * @deffunc void ap_note_basic_auth_failure(request_rec *r)
457 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
460 * Setup the output headers so that the client knows how to authenticate
461 * itself the next time, if an authentication request failed. This function
462 * works only for digest authentication
463 * @param r The current request
464 * @deffunc void ap_note_digest_auth_failure(request_rec *r)
466 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
469 * Get the password from the request headers
470 * @param r The current request
471 * @param pw The password as set in the headers
472 * @return 0 (OK) if it set the 'pw' argument (and assured
473 * a correct value in r->connection->user); otherwise it returns
474 * an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
475 * really confused, HTTP_UNAUTHORIZED if no authentication at all
476 * seemed to be in use, or DECLINED if there was authentication but
477 * it wasn't Basic (in which case, the caller should presumably
479 * @deffunc int ap_get_basic_auth_pw(request_rec *r, const char **pw)
481 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
484 * parse_uri: break apart the uri
485 * @warning Side Effects: <PRE>
486 * - sets r->args to rest after '?' (or NULL if no '?')
487 * - sets r->uri to request uri (without r->args part)
488 * - sets r->hostname (if not set already) from request (scheme://host:port)
490 * @param r The current request
491 * @param uri The uri to break apart
492 * @deffunc void ap_parse_uri(request_rec *r, const char *uri)
494 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
496 AP_CORE_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
499 * Get the method number associated with the given string, assumed to
500 * contain an HTTP method. Returns M_INVALID if not recognized.
501 * @param method A string containing a valid HTTP method
502 * @return The method number
503 * @deffunc int ap_method_number_of(const char *method)
505 AP_DECLARE(int) ap_method_number_of(const char *method);
508 * Get the method name associated with the given internal method
509 * number. Returns NULL if not recognized.
510 * @param methnum An integer value corresponding to an internal method number
511 * @return The name corresponding to the method number
512 * @deffunc const char *ap_method_name_of(int methnum)
514 AP_DECLARE(const char *) ap_method_name_of(int methnum);
519 * post_read_request --- run right after read_request or internal_redirect,
520 * and not run during any subrequests.
523 * This hook allows modules to affect the request immediately after the request
524 * has been read, and before any other phases have been processes. This allows
525 * modules to make decisions based upon the input header fields
526 * @param r The current request
527 * @return OK or DECLINED
528 * @deffunc ap_run_post_read_request(request_rec *r)
530 AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
533 * This hook allows modules to perform any module-specific logging activities
534 * over and above the normal server things.
535 * @param r The current request
536 * @return OK, DECLINED, or HTTP_...
537 * @deffunc int ap_run_log_transaction(request_rec *r)
539 AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
542 * This hook allows modules to retrieve the http method from a request. This
543 * allows Apache modules to easily extend the methods that Apache understands
544 * @param r The current request
545 * @return The http method from the request
546 * @deffunc const char *ap_run_http_method(const request_rec *r)
548 AP_DECLARE_HOOK(const char *,http_method,(const request_rec *r))
551 * Return the default port from the current request
552 * @param r The current request
553 * @return The current port
554 * @deffunc apr_port_t ap_run_default_port(const request_rec *r)
556 AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
558 typedef struct ap_bucket_error ap_bucket_error;
561 * A bucket referring to an HTTP error
562 * This bucket can be passed down the filter stack to indicate that an
563 * HTTP error occurred while running a filter. In order for this bucket
564 * to be used successfully, it MUST be sent as the first bucket in the
565 * first brigade to be sent from a given filter.
567 struct ap_bucket_error {
568 /** The error code */
570 /** The error string */
574 AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
577 * Make the bucket passed in an error bucket
578 * @param b The bucket to make into an error bucket
579 * @param error The HTTP error code to put in the bucket.
580 * @param buf An optional error string to put in the bucket.
581 * @param p A pool to allocate out of.
582 * @return The new bucket, or NULL if allocation failed
583 * @deffunc apr_bucket *ap_bucket_error_make(apr_bucket *b, int error, const char *buf, apr_pool_t *p)
585 AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
586 const char *buf, apr_pool_t *p);
589 * Create a bucket referring to an HTTP error.
590 * @param error The HTTP error code to put in the bucket.
591 * @param buf An optional error string to put in the bucket.
592 * @param p A pool to allocate out of.
593 * @return The new bucket, or NULL if allocation failed
594 * @deffunc apr_bucket *ap_bucket_error_create(int error, const char *buf, apr_pool_t *p)
596 AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error,
597 const char *buf, apr_pool_t *p);
603 #endif /* !APACHE_HTTP_PROTOCOL_H */