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);
106 /* Send the response to special method requests */
108 AP_DECLARE(int) ap_send_http_trace(request_rec *r);
109 int ap_send_http_options(request_rec *r);
111 /* Finish up stuff after a request */
114 * Called at completion of sending the response. It sends the terminating
115 * protocol information.
116 * @param r The current request
117 * @deffunc void ap_finalize_request_protocol(request_rec *r)
119 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
122 * Send error back to client.
123 * @param r The current request
124 * @param recursive_error last arg indicates error status in case we get
125 * an error in the process of trying to deal with an ErrorDocument
126 * to handle some other error. In that case, we print the default
127 * report for the first thing that went wrong, and more briefly report
128 * on the problem with the ErrorDocument.
129 * @deffunc void ap_send_error_response(request_rec *r, int recursive_error)
131 AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
133 /* Set last modified header line from the lastmod date of the associated file.
134 * Also, set content length.
136 * May return an error status, typically HTTP_NOT_MODIFIED (that when the
137 * permit_cache argument is set to one).
141 * Set the content length for this request
142 * @param r The current request
143 * @param length The new content length
144 * @deffunc void ap_set_content_length(request_rec *r, apr_off_t length)
146 AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
149 * Set the keepalive status for this request
150 * @param r The current request
151 * @return 1 if keepalive can be set, 0 otherwise
152 * @deffunc int ap_set_keepalive(request_rec *r)
154 AP_DECLARE(int) ap_set_keepalive(request_rec *r);
157 * Return the latest rational time from a request/mtime pair. Mtime is
158 * returned unless it's in the future, in which case we return the current time.
159 * @param r The current request
160 * @param mtime The last modified time
161 * @return the latest rational time.
162 * @deffunc apr_time_t ap_rationalize_mtime(request_rec *r, apr_time_t mtime)
164 AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
167 * Construct an entity tag from the resource information. If it's a real
168 * file, build in some of the file characteristics.
169 * @param r The current request
170 * @param force_weak Force the entity tag to be weak - it could be modified
171 * again in as short an interval.
172 * @return The entity tag
173 * @deffunc char *ap_make_etag(request_rec *r, int force_weak)
175 AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
178 * Set the E-tag outgoing header
179 * @param The current request
180 * @deffunc void ap_set_etag(request_rec *r)
182 AP_DECLARE(void) ap_set_etag(request_rec *r);
185 * Set the last modified time for the file being sent
186 * @param r The current request
187 * @deffunc void ap_set_last_modified(request_rec *r)
189 AP_DECLARE(void) ap_set_last_modified(request_rec *r);
192 * Implements condition GET rules for HTTP/1.1 specification. This function
193 * inspects the client headers and determines if the response fulfills
194 * the requirements specified.
195 * @param r The current request
196 * @return 1 if the response fulfills the condition GET rules, 0 otherwise
197 * @deffunc int ap_meets_conditions(request_rec *r)
199 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
201 /* Other ways to send stuff at the client. All of these keep track
202 * of bytes_sent automatically. This indirection is intended to make
203 * it a little more painless to slide things like HTTP-NG packetization
204 * underneath the main body of the code later. In the meantime, it lets
205 * us centralize a bit of accounting (bytes_sent).
207 * These also return the number of bytes written by the call.
208 * They should only be called with a timeout registered, for obvious reaasons.
209 * (Ditto the send_header stuff).
213 * Send an entire file to the client, using sendfile if supported by the
215 * @param fd The file to send.
216 * @param r The current request
217 * @param offset Offset into the file to start sending.
218 * @param length Amount of data to send
219 * @param nbytes Amount of data actually sent
220 * @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);
222 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
223 apr_size_t length, apr_size_t *nbytes);
227 * Send an MMAP'ed file to the client
228 * @param mm The MMAP'ed file to send
229 * @param r The current request
230 * @param offset The offset into the MMAP to start sending
231 * @param length The amount of data to send
232 * @return The number of bytes sent
233 * @deffunc size_t ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset, size_t length)
235 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
240 * Create a new method list with the specified number of preallocated
241 * slots for extension methods.
243 * @param p Pointer to a pool in which the structure should be
245 * @param nelts Number of preallocated extension slots
246 * @return Pointer to the newly created structure.
247 * @deffunc ap_method_list_t ap_make_method_list(apr_pool_t *p, int nelts)
249 AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
250 AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
251 ap_method_list_t *src);
252 AP_DECLARE_NONSTD(void) ap_method_list_do(int (*comp) (void *urec, const char *mname,
255 const ap_method_list_t *ml, ...);
256 AP_DECLARE(void) ap_method_list_vdo(int (*comp) (void *urec, const char *mname,
258 void *rec, const ap_method_list_t *ml,
261 * Search for an HTTP method name in an ap_method_list_t structure, and
262 * return true if found.
264 * @param method String containing the name of the method to check.
265 * @param l Pointer to a method list, such as cmd->methods_limited.
266 * @return 1 if method is in the list, otherwise 0
267 * @deffunc int ap_method_in_list(const char *method, ap_method_list_t *l)
269 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
272 * Add an HTTP method name to an ap_method_list_t structure if it isn't
275 * @param method String containing the name of the method to check.
276 * @param l Pointer to a method list, such as cmd->methods_limited.
278 * @deffunc void ap_method_in_list(ap_method_list_t *l, const char *method)
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 cmd->methods_limited.
286 * @param method String containing the name of the method to remove.
288 * @deffunc void ap_method_list_remove(ap_method_list_t *l, const char *method)
290 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
294 * Reset a method list to be completely empty.
296 * @param l Pointer to a method list, such as cmd->methods_limited.
298 * @deffunc void ap_clear_method_list(ap_method_list_t *l)
300 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
302 /* Hmmm... could macrofy these for now, and maybe forever, though the
303 * definitions of the macros would get a whole lot hairier.
307 * Output one character for this request
308 * @param c the character to output
309 * @param r the current request
310 * @return The number of bytes sent
311 * @deffunc int ap_rputc(int c, request_rec *r)
313 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
316 * Output a string for the current request
317 * @param str The string to output
318 * @param r The current request
319 * @return The number of bytes sent
320 * @deffunc int ap_rputs(const char *str, request_rec *r)
322 AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
325 * Write a buffer for the current request
326 * @param buf The buffer to write
327 * @param nbyte The number of bytes to send from the buffer
328 * @param r The current request
329 * @return The number of bytes sent
330 * @deffunc int ap_rwrite(const void *buf, int nbyte, request_rec *r)
332 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
335 * Write an unspecified number of strings to the request
336 * @param r The current request
337 * @param ... The strings to write
338 * @return The number of bytes sent
339 * @deffunc int ap_rvputs(request_rec *r, ...)
341 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...);
344 * Output data to the client in a printf format
345 * @param r The current request
346 * @param fmt The format string
347 * @param vlist The arguments to use to fill out the format string
348 * @return The number of bytes sent
349 * @deffunc int ap_vrprintf(request_rec *r, const char *fmt, va_list vlist)
351 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
354 * Output data to the client in a printf format
355 * @param r The current request
356 * @param fmt The format string
357 * @param ... The arguments to use to fill out the format string
358 * @return The number of bytes sent
359 * @deffunc int ap_rprintf(request_rec *r, const char *fmt, ...)
361 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
362 __attribute__((format(printf,2,3)));
364 * Flush all of the data for the current request to the client
365 * @param r The current request
366 * @return The number of bytes sent
367 * @deffunc int ap_rflush(request_rec *r)
369 AP_DECLARE(int) ap_rflush(request_rec *r);
372 * Index used in custom_responses array for a specific error code
373 * (only use outside protocol.c is in getting them configured).
374 * @param status HTTP status code
375 * @return The index of the response
376 * @deffunc int ap_index_of_response(int status)
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
386 * @deffunc const char *ap_get_status_line(int status)
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.
401 * @return either OK or an error code
402 * @deffunc int ap_setup_cleint_block(request_rec *r, int read_policy)
404 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
407 * Determine if the client has sent any data. This also sends a
408 * 100 Continue resposne to HTTP/1.1 clients, so modules should not be called
409 * until the module is ready to read content.
410 * @warning Never call this function more than once.
411 * @param r The current request
412 * @return 0 if there is no message to read, 1 otherwise
413 * @deffunc int ap_should_client_block(request_rec *r)
415 AP_DECLARE(int) ap_should_client_block(request_rec *r);
418 * Call this in a loop. It will put data into a buffer and return the length
420 * @param r The current request
421 * @param buffer The buffer in which to store the data
422 * @param bufsiz The size of the buffer
423 * @return Number of bytes inserted into the buffer. When done reading, 0
424 * if EOF, or -1 if there was an error
425 * @deffunc long ap_get_client_block(request_rec *r, char *buffer, int bufsiz)
427 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, int bufsiz);
430 * In HTTP/1.1, any method can have a body. However, most GET handlers
431 * wouldn't know what to do with a request body if they received one.
432 * This helper routine tests for and reads any message body in the request,
433 * simply discarding whatever it receives. We need to do this because
434 * failing to read the request body would cause it to be interpreted
435 * as the next request on a persistent connection.
436 * @param r The current request
437 * @return error status if request is malformed, OK otherwise
438 * @deffunc int ap_discard_request_body(request_rec *r)
440 AP_DECLARE(int) ap_discard_request_body(request_rec *r);
443 /* Sending a byterange */
446 * Setup the output headers so that the client knows how to authenticate
447 * itself the next time, if an authentication request failed. This function
448 * works for both basic and digest authentication
449 * @param r The current request
450 * @deffunc void ap_note_auth_failure(request_rec *r)
452 AP_DECLARE(void) ap_note_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 basic authentication
458 * @param r The current request
459 * @deffunc void ap_note_basic_auth_failure(request_rec *r)
461 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
464 * Setup the output headers so that the client knows how to authenticate
465 * itself the next time, if an authentication request failed. This function
466 * works only for digest authentication
467 * @param r The current request
468 * @deffunc void ap_note_digest_auth_failure(request_rec *r)
470 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
473 * Get the password from the request headers
474 * @param r The current request
475 * @param pw The password as set in the headers
476 * @return 0 (OK) if it set the 'pw' argument (and assured
477 * a correct value in r->connection->user); otherwise it returns
478 * an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
479 * really confused, HTTP_UNAUTHORIZED if no authentication at all
480 * seemed to be in use, or DECLINED if there was authentication but
481 * it wasn't Basic (in which case, the caller should presumably
483 * @deffunc int ap_get_basic_auth_pw(request_rec *r, const char **pw)
485 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
488 * Setting up the protocol fields for subsidiary requests...
489 * Also, a wrapup function to keep the internal accounting straight.
492 void ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
493 void ap_finalize_sub_req_protocol(request_rec *sub_r);
496 * parse_uri: break apart the uri
497 * @warning Side Effects: <PRE>
498 * - sets r->args to rest after '?' (or NULL if no '?')
499 * - sets r->uri to request uri (without r->args part)
500 * - sets r->hostname (if not set already) from request (scheme://host:port)
502 * @param r The current request
503 * @param uri The uri to break apart
504 * @deffunc void ap_parse_uri(request_rec *r, const char *uri)
506 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
508 AP_CORE_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
511 * Get the method number associated with the given string, assumed to
512 * contain an HTTP method. Returns M_INVALID if not recognized.
513 * @param method A string containing a valid HTTP method
514 * @return The method number
515 * @deffunc int ap_method_number_of(const char *method)
517 AP_DECLARE(int) ap_method_number_of(const char *method);
520 * Get the method name associated with the given internal method
521 * number. Returns NULL if not recognized.
522 * @param methnum An integer value corresponding to an internal method number
523 * @return The name corresponding to the method number
524 * @deffunc const char *ap_method_name_of(int methnum)
526 AP_DECLARE(const char *) ap_method_name_of(int methnum);
531 * post_read_request --- run right after read_request or internal_redirect,
532 * and not run during any subrequests.
535 * This hook allows modules to affect the request immediately after the request
536 * has been read, and before any other phases have been processes. This allows
537 * modules to make decisions based upon the input header fields
538 * @param r The current request
539 * @return OK or DECLINED
540 * @deffunc ap_run_post_read_request(request_rec *r)
542 AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
545 * This hook allows modules to perform any module-specific logging activities
546 * over and above the normal server things.
547 * @param r The current request
548 * @return OK, DECLINED, or HTTP_...
549 * @deffunc int ap_run_log_transaction(request_rec *r)
551 AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
554 * This hook allows modules to retrieve the http method from a request. This
555 * allows Apache modules to easily extend the methods that Apache understands
556 * @param r The current request
557 * @return The http method from the request
558 * @deffunc const char *ap_run_http_method(const request_rec *r)
560 AP_DECLARE_HOOK(const char *,http_method,(const request_rec *r))
563 * Return the default port from the current request
564 * @param r The current request
565 * @return The current port
566 * @deffunc apr_port_t ap_run_default_port(const request_rec *r)
568 AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
570 typedef struct ap_bucket_error ap_bucket_error;
573 * A bucket referring to an HTTP error
574 * This bucket can be passed down the filter stack to indicate that an
575 * HTTP error occurred while running a filter. In order for this bucket
576 * to be used successfully, it MUST be sent as the first bucket in the
577 * first brigade to be sent from a given filter.
579 struct ap_bucket_error {
580 /** The error code */
582 /** The error string */
586 AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
589 * Make the bucket passed in an error bucket
590 * @param b The bucket to make into an error bucket
591 * @param error The HTTP error code to put in the bucket.
592 * @param buf An optional error string to put in the bucket.
593 * @param p A pool to allocate out of.
594 * @return The new bucket, or NULL if allocation failed
595 * @deffunc apr_bucket *ap_bucket_error_make(apr_bucket *b, int error, const char *buf, apr_pool_t *p)
597 AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
598 const char *buf, apr_pool_t *p);
601 * Create a bucket referring to an HTTP error.
602 * @param error The HTTP error code to put in the bucket.
603 * @param buf An optional error string to put in the bucket.
604 * @param p A pool to allocate out of.
605 * @return The new bucket, or NULL if allocation failed
606 * @deffunc apr_bucket *ap_bucket_error_create(int error, const char *buf, apr_pool_t *p)
608 AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error,
609 const char *buf, apr_pool_t *p);
615 #endif /* !APACHE_HTTP_PROTOCOL_H */