1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 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_portable.h"
65 #include "util_filter.h"
66 #include "ap_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 a single HTTP header field
90 * @param r The current request
91 * @param fieldname The Header field to send
92 * @param fieldval The value of the header
93 * @deffunc int ap_send_header_field(request_rec *r, const char *fieldname, const char *fieldval)
95 AP_DECLARE_NONSTD(int) ap_send_header_field(request_rec *r, const char *fieldname,
96 const char *fieldval);
99 * Send the minimal part of an HTTP response header.
100 * @param r The current request
101 * @warning Modules should be very careful about using this, and should
102 * prefer ap_send_http_header(). Much of the HTTP/1.1 implementation
103 * correctness depends on code in ap_send_http_header().
104 * @deffunc void ap_basic_http_header(request_rec *r)
106 AP_DECLARE(void) ap_basic_http_header(request_rec *r);
109 * Send the Status-Line and header fields for HTTP response
110 * @param l The current request
111 * @deffunc void ap_send_http_header(request_rec *l)
113 AP_DECLARE(void) ap_send_http_header(request_rec *l);
115 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, ap_bucket_brigade *b);
117 /* Send the response to special method requests */
119 AP_DECLARE(int) ap_send_http_trace(request_rec *r);
120 int ap_send_http_options(request_rec *r);
122 /* Finish up stuff after a request */
125 * Called at completion of sending the response. It sends the terminating
126 * protocol information.
127 * @param r The current request
128 * @deffunc void ap_finalize_request_protocol(request_rec *r)
130 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
133 * Send error back to client.
134 * @param r The current request
135 * @param recursive_error last arg indicates error status in case we get
136 * an error in the process of trying to deal with an ErrorDocument
137 * to handle some other error. In that case, we print the default
138 * report for the first thing that went wrong, and more briefly report
139 * on the problem with the ErrorDocument.
140 * @deffunc void ap_send_error_response(request_rec *r, int recursive_error)
142 AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
144 /* Set last modified header line from the lastmod date of the associated file.
145 * Also, set content length.
147 * May return an error status, typically HTTP_NOT_MODIFIED (that when the
148 * permit_cache argument is set to one).
152 * Set the content length for this request
153 * @param r The current request
154 * @param length The new content length
155 * @return Always 0, can be safely ignored
156 * @deffunc int ap_set_content_length(request_rec *r, long length)
158 AP_DECLARE(int) ap_set_content_length(request_rec *r, long length);
160 * Set the keepalive status for this request
161 * @param r The current request
162 * @return 1 if keepalive can be set, 0 otherwise
163 * @deffunc int ap_set_keepalive(request_rec *r)
165 AP_DECLARE(int) ap_set_keepalive(request_rec *r);
167 * Return the latest rational time from a request/mtime pair. Mtime is
168 * returned unless it's in the future, in which case we return the current time.
169 * @param r The current request
170 * @param mtime The last modified time
171 * @return the latest rational time.
172 * @deffunc apr_time_t ap_rationalize_mtime(request_rec *r, apr_time_t mtime)
174 AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
176 * Construct an entity tag from the resource information. If it's a real
177 * file, build in some of the file characteristics.
178 * @param r The current request
179 * @param force_weak Force the entity tag to be weak - it could be modified
180 * again in as short an interval.
181 * @return The entity tag
182 * @deffunc char *ap_make_etag(request_rec *r, int force_weak)
184 AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
186 * Set the E-tag outgoing header
187 * @param The current request
188 * @deffunc void ap_set_etag(request_rec *r)
190 AP_DECLARE(void) ap_set_etag(request_rec *r);
192 * Set the last modified time for the file being sent
193 * @param r The current request
194 * @deffunc void ap_set_last_modified(request_rec *r)
196 AP_DECLARE(void) ap_set_last_modified(request_rec *r);
198 * Implements condition GET rules for HTTP/1.1 specification. This function
199 * inspects the client headers and determines if the response fulfills
200 * the requirements specified.
201 * @param r The current request
202 * @return 1 if the response fulfills the condition GET rules, 0 otherwise
203 * @deffunc int ap_meets_conditions(request_rec *r)
205 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
207 /* Other ways to send stuff at the client. All of these keep track
208 * of bytes_sent automatically. This indirection is intended to make
209 * it a little more painless to slide things like HTTP-NG packetization
210 * underneath the main body of the code later. In the meantime, it lets
211 * us centralize a bit of accounting (bytes_sent).
213 * These also return the number of bytes written by the call.
214 * They should only be called with a timeout registered, for obvious reaasons.
215 * (Ditto the send_header stuff).
219 * Send an entire file to the client, using sendfile if supported by the
221 * @param fd The file to send.
222 * @param r The current request
223 * @param offset Offset into the file to start sending.
224 * @param length Amount of data to send
225 * @param nbytes Amount of data actually sent
226 * @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);
228 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
229 apr_size_t length, apr_size_t *nbytes);
231 * Send an MMAP'ed file to the client
232 * @param mm The MMAP'ed file to send
233 * @param r The current request
234 * @param offset The offset into the MMAP to start sending
235 * @param length The amount of data to send
236 * @return The number of bytes sent
237 * @deffunc size_t ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset, size_t length)
239 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
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
248 * @param nelts Number of preallocated extension slots
249 * @return Pointer to the newly created structure.
250 * @deffunc ap_method_list_t ap_make_method_list(apr_pool_t *p, int nelts)
252 AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
253 AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
254 ap_method_list_t *src);
255 AP_DECLARE_NONSTD(void) ap_method_list_do(int (*comp) (void *urec, const char *mname,
258 const ap_method_list_t *ml, ...);
259 AP_DECLARE(void) ap_method_list_vdo(int (*comp) (void *urec, const char *mname,
261 void *rec, const ap_method_list_t *ml,
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 cmd->methods_limited.
269 * @return 1 if method is in the list, otherwise 0
270 * @deffunc int ap_method_in_list(const char *method, ap_method_list_t *l)
272 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
275 * Add an HTTP method name to an ap_method_list_t structure if it isn't
278 * @param method String containing the name of the method to check.
279 * @param l Pointer to a method list, such as cmd->methods_limited.
281 * @deffunc void ap_method_in_list(ap_method_list_t *l, const char *method)
283 AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
286 * Remove an HTTP method name from an ap_method_list_t structure.
288 * @param l Pointer to a method list, such as cmd->methods_limited.
289 * @param method String containing the name of the method to remove.
291 * @deffunc void ap_method_list_remove(ap_method_list_t *l, const char *method)
293 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
297 * Reset a method list to be completely empty.
299 * @param l Pointer to a method list, such as cmd->methods_limited.
301 * @deffunc void ap_clear_method_list(ap_method_list_t *l)
303 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
305 /* Hmmm... could macrofy these for now, and maybe forever, though the
306 * definitions of the macros would get a whole lot hairier.
310 * Output one character for this request
311 * @param c the character to output
312 * @param r the current request
313 * @return The number of bytes sent
314 * @deffunc int ap_rputc(int c, request_rec *r)
316 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
318 * Output a string for the current request
319 * @param str The string to output
320 * @param r The current request
321 * @return The number of bytes sent
322 * @deffunc int ap_rputs(const char *str, request_rec *r)
324 AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
326 * Write a buffer for the current request
327 * @param buf The buffer to write
328 * @param nbyte The number of bytes to send from the buffer
329 * @param r The current request
330 * @return The number of bytes sent
331 * @deffunc int ap_rwrite(const void *buf, int nbyte, request_rec *r)
333 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,...);
343 * Output data to the client in a printf format
344 * @param r The current request
345 * @param fmt The format string
346 * @param vlist The arguments to use to fill out the format string
347 * @return The number of bytes sent
348 * @deffunc int ap_vrprintf(request_rec *r, const char *fmt, va_list vlist)
350 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
352 * Output data to the client in a printf format
353 * @param r The current request
354 * @param fmt The format string
355 * @param ... The arguments to use to fill out the format string
356 * @return The number of bytes sent
357 * @deffunc int ap_rprintf(request_rec *r, const char *fmt, ...)
359 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
360 __attribute__((format(printf,2,3)));
362 * Flush all of the data for the current request to the client
363 * @param r The current request
364 * @return The number of bytes sent
365 * @deffunc int ap_rflush(request_rec *r)
367 AP_DECLARE(int) ap_rflush(request_rec *r);
370 * Index used in custom_responses array for a specific error code
371 * (only use outside protocol.c is in getting them configured).
372 * @param status HTTP status code
373 * @return The index of the response
374 * @deffunc int ap_index_of_response(int status)
376 AP_DECLARE(int) ap_index_of_response(int status);
379 * Return the Status-Line for a given status code (excluding the
380 * HTTP-Version field). If an invalid or unknown status code is
381 * passed, "500 Internal Server Error" will be returned.
382 * @param status The HTTP status code
383 * @return The Status-Line
384 * @deffunc const char *ap_get_status_line(int status)
386 AP_DECLARE(const char *) ap_get_status_line(int status);
388 /* Reading a block of data from the client connection (e.g., POST arg) */
391 * Setup the client to allow Apache to read the request body.
392 * @param r The current request
393 * @param read_policy How the server should interpret a chunked
394 * transfer-encoding. One of: <PRE>
395 * REQUEST_NO_BODY Send 413 error if message has any body
396 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
397 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
398 * REQUEST_CHUNKED_PASS Pass the chunks to me without removal.
400 * @return either OK or an error code
401 * @deffunc int ap_setup_cleint_block(request_rec *r, int read_policy)
403 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
405 * Determine if the client has sent any data. This also sends a
406 * 100 Continue resposne to HTTP/1.1 clients, so modules should not be called
407 * until the module is ready to read content.
408 * @warning Never call this function more than once.
409 * @param r The current request
410 * @return 0 if there is no message to read, 1 otherwise
411 * @deffunc int ap_should_client_block(request_rec *r)
413 AP_DECLARE(int) ap_should_client_block(request_rec *r);
415 * Call this in a loop. It will put data into a buffer and return the length
417 * @param r The current request
418 * @param buffer The buffer in which to store the data
419 * @param bufsiz The size of the buffer
420 * @return Number of bytes inserted into the buffer. When done reading, 0
421 * if EOF, or -1 if there was an error
422 * @deffunc long ap_get_client_block(request_rec *r, char *buffer, int bufsiz)
424 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);
438 /* Sending a byterange */
441 * Setup the request to send Byte Range requests
442 * @param r the current request
443 * @return 1 if request was setup for byte range requests, 0 otherwise
444 * @deffunc int ap_set_byterange(request_rec *r)
446 AP_DECLARE(int) ap_set_byterange(request_rec *r);
448 * Send one byte range chunk for a byte range request
449 * @param r The current request
450 * @param offset Set to the position it should be after the chunk is sent
451 * @param length Set to the length in should be after the chunk is sent
452 * @deffunc int ap_each_byterange(request_rec *r, apr_off_t *offset, apr_size_t *length)
454 AP_DECLARE(int) ap_each_byterange(request_rec *r, apr_off_t *offset,
457 * Setup the output headers so that the client knows how to authenticate
458 * itself the next time, if an authentication request failed. This function
459 * works for both basic and digest authentication
460 * @param r The current request
461 * @deffunc void ap_note_auth_failure(request_rec *r)
463 AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
465 * Setup the output headers so that the client knows how to authenticate
466 * itself the next time, if an authentication request failed. This function
467 * works only for basic authentication
468 * @param r The current request
469 * @deffunc void ap_note_basic_auth_failure(request_rec *r)
471 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
473 * Setup the output headers so that the client knows how to authenticate
474 * itself the next time, if an authentication request failed. This function
475 * works only for digest authentication
476 * @param r The current request
477 * @deffunc void ap_note_digest_auth_failure(request_rec *r)
479 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
481 * Get the password from the request headers
482 * @param r The current request
483 * @param pw The password as set in the headers
484 * @return 0 (OK) if it set the 'pw' argument (and assured
485 * a correct value in r->connection->user); otherwise it returns
486 * an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
487 * really confused, HTTP_UNAUTHORIZED if no authentication at all
488 * seemed to be in use, or DECLINED if there was authentication but
489 * it wasn't Basic (in which case, the caller should presumably
491 * @deffunc int ap_get_basic_auth_pw(request_rec *r, const char **pw)
493 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
496 * Setting up the protocol fields for subsidiary requests...
497 * Also, a wrapup function to keep the internal accounting straight.
500 void ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
501 void ap_finalize_sub_req_protocol(request_rec *sub_r);
504 * parse_uri: break apart the uri
505 * @warning Side Effects: <PRE>
506 * - sets r->args to rest after '?' (or NULL if no '?')
507 * - sets r->uri to request uri (without r->args part)
508 * - sets r->hostname (if not set already) from request (scheme://host:port)
510 * @param r The current request
511 * @param uri The uri to break apart
512 * @deffunc void ap_parse_uri(request_rec *r, const char *uri)
514 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
517 * Get the method number associated with the given string, assumed to
518 * contain an HTTP method. Returns M_INVALID if not recognized.
519 * @param method A string containing a valid HTTP method
520 * @return The method number
521 * @deffunc int ap_method_number_of(const char *method)
523 AP_DECLARE(int) ap_method_number_of(const char *method);
526 * Get the method name associated with the given internal method
527 * number. Returns NULL if not recognized.
528 * @param methnum An integer value corresponding to an internal method number
529 * @return The name corresponding to the method number
530 * @deffunc const char *ap_method_name_of(int methnum)
532 AP_DECLARE(const char *) ap_method_name_of(int methnum);
534 apr_status_t http_filter(ap_filter_t *f, ap_bucket_brigade *b, ap_input_mode_t mode);
535 apr_status_t dechunk_filter(ap_filter_t *f, ap_bucket_brigade *b, ap_input_mode_t mode);
539 * post_read_request --- run right after read_request or internal_redirect,
540 * and not run during any subrequests.
543 * This hook allows modules to affect the request immediately after the request
544 * has been read, and before any other phases have been processes. This allows
545 * modules to make decisions based upon the input header fields
546 * @param r The current request
547 * @return OK or DECLINED
548 * @deffunc ap_run_post_read_request(request_rec *r)
550 AP_DECLARE_HOOK(int,post_read_request,(request_rec *))
552 * This hook allows modules to perform any module-specific logging activities
553 * over and above the normal server things.
554 * @param r The current request
555 * @return OK, DECLINED, or HTTP_...
556 * @deffunc int ap_run_log_transaction(request_rec *r)
558 AP_DECLARE_HOOK(int,log_transaction,(request_rec *))
560 * This hook allows modules to retrieve the http method from a request. This
561 * allows Apache modules to easily extend the methods that Apache understands
562 * @param r The current request
563 * @return The http method from the request
564 * @deffunc const char *ap_run_http_method(const request_rec *r)
566 AP_DECLARE_HOOK(const char *,http_method,(const request_rec *))
568 * Return the default port from the current request
569 * @param r The current request
570 * @return The current port
571 * @deffunc unsigned short ap_run_default_port(const request_rec *r)
573 AP_DECLARE_HOOK(unsigned short,default_port,(const request_rec *))
579 #endif /* !APACHE_HTTP_PROTOCOL_H */