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"
71 * @package HTTP protocol handling
75 * Prototypes for routines which either talk directly back to the user,
76 * or control the ones that eventually do.
80 * Read a request and fill in the fields.
81 * @param c The current connection
82 * @return The new request_rec
84 request_rec *ap_read_request(conn_rec *c);
87 * Send a single HTTP header field
88 * @param r The current request
89 * @param fieldname The Header field to send
90 * @param fieldval The value of the header
91 * @deffunc int ap_send_header_field(request_rec *r, const char *fieldname, const char *fieldval)
93 API_EXPORT_NONSTD(int) ap_send_header_field(request_rec *r, const char *fieldname,
94 const char *fieldval);
97 * Send the minimal part of an HTTP response header.
98 * @param r The current request
99 * @warning Modules should be very careful about using this, and should
100 * prefer ap_send_http_header(). Much of the HTTP/1.1 implementation
101 * correctness depends on code in ap_send_http_header().
102 * @deffunc void ap_basic_http_header(request_rec *r)
104 API_EXPORT(void) ap_basic_http_header(request_rec *r);
107 * Send the Status-Line and header fields for HTTP response
108 * @param l The current request
109 * @deffunc void ap_send_http_header(request_rec *l)
111 API_EXPORT(void) ap_send_http_header(request_rec *l);
113 /* Send the response to special method requests */
115 API_EXPORT(int) ap_send_http_trace(request_rec *r);
116 int ap_send_http_options(request_rec *r);
118 /* Finish up stuff after a request */
121 * Called at completion of sending the response. It sends the terminating
122 * protocol information.
123 * @param r The current request
124 * @deffunc void ap_finalize_request_protocol(request_rec *r)
126 API_EXPORT(void) ap_finalize_request_protocol(request_rec *r);
129 * Send error back to client.
130 * @param r The current request
131 * @param recursive_error last arg indicates error status in case we get
132 * an error in the process of trying to deal with an ErrorDocument
133 * to handle some other error. In that case, we print the default
134 * report for the first thing that went wrong, and more briefly report
135 * on the problem with the ErrorDocument.
136 * @deffunc void ap_send_error_response(request_rec *r, int recursive_error)
138 API_EXPORT(void) ap_send_error_response(request_rec *r, int recursive_error);
140 /* Set last modified header line from the lastmod date of the associated file.
141 * Also, set content length.
143 * May return an error status, typically HTTP_NOT_MODIFIED (that when the
144 * permit_cache argument is set to one).
148 * Set the content length for this request
149 * @param r The current request
150 * @param length The new content length
151 * @return Always 0, can be safely ignored
152 * @deffunc int ap_set_content_length(request_rec *r, long length)
154 API_EXPORT(int) ap_set_content_length(request_rec *r, long length);
156 * Set the keepalive status for this request
157 * @param r The current request
158 * @return 1 if keepalive can be set, 0 otherwise
159 * @deffunc int ap_set_keepalive(request_rec *r)
161 API_EXPORT(int) ap_set_keepalive(request_rec *r);
163 * Return the latest rational time from a request/mtime pair. Mtime is
164 * returned unless it's in the future, in which case we return the current time.
165 * @param r The current request
166 * @param mtime The last modified time
167 * @return the latest rational time.
168 * @deffunc apr_time_t ap_rationalize_mtime(request_rec *r, apr_time_t mtime)
170 API_EXPORT(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
172 * Construct an entity tag from the resource information. If it's a real
173 * file, build in some of the file characteristics.
174 * @param r The current request
175 * @param force_weak Force the entity tag to be weak - it could be modified
176 * again in as short an interval.
177 * @return The entity tag
178 * @deffunc char *ap_make_etag(request_rec *r, int force_weak)
180 API_EXPORT(char *) ap_make_etag(request_rec *r, int force_weak);
182 * Set the E-tag outgoing header
183 * @param The current request
184 * @deffunc void ap_set_etag(request_rec *r)
186 API_EXPORT(void) ap_set_etag(request_rec *r);
188 * Set the last modified time for the file being sent
189 * @param r The current request
190 * @deffunc void ap_set_last_modified(request_rec *r)
192 API_EXPORT(void) ap_set_last_modified(request_rec *r);
194 * Implements condition GET rules for HTTP/1.1 specification. This function
195 * inspects the client headers and determines if the response fulfills
196 * the requirements specified.
197 * @param r The current request
198 * @return 1 if the response fulfills the condition GET rules, 0 otherwise
199 * @deffunc int ap_meets_conditions(request_rec *r)
201 API_EXPORT(int) ap_meets_conditions(request_rec *r);
203 /* Other ways to send stuff at the client. All of these keep track
204 * of bytes_sent automatically. This indirection is intended to make
205 * it a little more painless to slide things like HTTP-NG packetization
206 * underneath the main body of the code later. In the meantime, it lets
207 * us centralize a bit of accounting (bytes_sent).
209 * These also return the number of bytes written by the call.
210 * They should only be called with a timeout registered, for obvious reaasons.
211 * (Ditto the send_header stuff).
215 * Send an entire file to the client, using sendfile if supported by the
217 * @param fd The file to send.
218 * @param r The current request
219 * @param offset Offset into the file to start sending.
220 * @param length Amount of data to send
221 * @param nbytes Amount of data actually sent
222 * @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);
224 API_EXPORT(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
225 apr_size_t length, apr_size_t *nbytes);
227 * Send the body of a response to the client
228 * @param f The BUFF structure associated with a client
229 * @param r The current request
230 * @return The number of bytes sent
231 * @deffunc long ap_send_fb(BUFF *f, request_rec *r)
233 API_EXPORT(long) ap_send_fb(BUFF *f, request_rec *r);
235 * Send a specified number of bytes from the body of the response to the client
236 * @param f the BUFF structure associated with a client
237 * @param r The current request
238 * @param length The number of bytes to send
239 * @return The number of bytes sent
240 * @deffunc long ap_send_fb_length(BUFF *f, request_rec *r, long length)
242 API_EXPORT(long) ap_send_fb_length(BUFF *f, request_rec *r, long length);
244 * Send an MMAP'ed file to the client
245 * @param mm The MMAP'ed file to send
246 * @param r The current request
247 * @param offset The offset into the MMAP to start sending
248 * @param length The amount of data to send
249 * @return The number of bytes sent
250 * @deffunc size_t ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset, size_t length)
252 API_EXPORT(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
256 * Create a new method list with the specified number of preallocated
257 * slots for extension methods.
259 * @param p Pointer to a pool in which the structure should be
261 * @param nelts Number of preallocated extension slots
262 * @return Pointer to the newly created structure.
263 * @deffunc ap_method_list_t ap_make_method_list(apr_pool_t *p, int nelts)
265 API_EXPORT(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
266 API_EXPORT(void) ap_copy_method_list(ap_method_list_t *dest,
267 ap_method_list_t *src);
268 API_EXPORT_NONSTD(void) ap_method_list_do(int (*comp) (void *urec, const char *mname,
271 const ap_method_list_t *ml, ...);
272 API_EXPORT(void) ap_method_list_vdo(int (*comp) (void *urec, const char *mname,
274 void *rec, const ap_method_list_t *ml,
277 * Search for an HTTP method name in an ap_method_list_t structure, and
278 * return true if found.
280 * @param method String containing the name of the method to check.
281 * @param l Pointer to a method list, such as cmd->methods_limited.
282 * @return 1 if method is in the list, otherwise 0
283 * @deffunc int ap_method_in_list(const char *method, ap_method_list_t *l)
285 API_EXPORT(int) ap_method_in_list(ap_method_list_t *l, const char *method);
288 * Add an HTTP method name to an ap_method_list_t structure if it isn't
291 * @param method String containing the name of the method to check.
292 * @param l Pointer to a method list, such as cmd->methods_limited.
294 * @deffunc void ap_method_in_list(ap_method_list_t *l, const char *method)
296 API_EXPORT(void) ap_method_list_add(ap_method_list_t *l, const char *method);
299 * Remove an HTTP method name from an ap_method_list_t structure.
301 * @param l Pointer to a method list, such as cmd->methods_limited.
302 * @param method String containing the name of the method to remove.
304 * @deffunc void ap_method_list_remove(ap_method_list_t *l, const char *method)
306 API_EXPORT(void) ap_method_list_remove(ap_method_list_t *l,
310 * Reset a method list to be completely empty.
312 * @param l Pointer to a method list, such as cmd->methods_limited.
314 * @deffunc void ap_clear_method_list(ap_method_list_t *l)
316 API_EXPORT(void) ap_clear_method_list(ap_method_list_t *l);
318 /* Hmmm... could macrofy these for now, and maybe forever, though the
319 * definitions of the macros would get a whole lot hairier.
323 * Output one character for this request
324 * @param c the character to output
325 * @param r the current request
326 * @return The number of bytes sent
327 * @deffunc int ap_rputc(int c, request_rec *r)
329 API_EXPORT(int) ap_rputc(int c, request_rec *r);
331 * Output a string for the current request
332 * @param str The string to output
333 * @param r The current request
334 * @return The number of bytes sent
335 * @deffunc int ap_rputs(const char *str, request_rec *r)
337 API_EXPORT(int) ap_rputs(const char *str, request_rec *r);
339 * Write a buffer for the current request
340 * @param buf The buffer to write
341 * @param nbyte The number of bytes to send from the buffer
342 * @param r The current request
343 * @return The number of bytes sent
344 * @deffunc int ap_rwrite(const void *buf, int nbyte, request_rec *r)
346 API_EXPORT(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
348 * Write an unspecified number of strings to the request
349 * @param r The current request
350 * @param ... The strings to write
351 * @return The number of bytes sent
352 * @deffunc int ap_rvputs(request_rec *r, ...)
354 API_EXPORT_NONSTD(int) ap_rvputs(request_rec *r,...);
356 * Output data to the client in a printf format
357 * @param r The current request
358 * @param fmt The format string
359 * @param vlist The arguments to use to fill out the format string
360 * @return The number of bytes sent
361 * @deffunc int ap_vrprintf(request_rec *r, const char *fmt, va_list vlist)
363 API_EXPORT(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
365 * Output data to the client in a printf format
366 * @param r The current request
367 * @param fmt The format string
368 * @param ... The arguments to use to fill out the format string
369 * @return The number of bytes sent
370 * @deffunc int ap_rprintf(request_rec *r, const char *fmt, ...)
372 API_EXPORT_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
373 __attribute__((format(printf,2,3)));
375 * Flush all of the data for the current request to the client
376 * @param r The current request
377 * @return The number of bytes sent
378 * @deffunc int ap_rflush(request_rec *r)
380 API_EXPORT(int) ap_rflush(request_rec *r);
383 * Index used in custom_responses array for a specific error code
384 * (only use outside protocol.c is in getting them configured).
385 * @param status HTTP status code
386 * @return The index of the response
387 * @deffunc int ap_index_of_response(int status)
389 API_EXPORT(int) ap_index_of_response(int status);
392 * Return the Status-Line for a given status code (excluding the
393 * HTTP-Version field). If an invalid or unknown status code is
394 * passed, "500 Internal Server Error" will be returned.
395 * @param status The HTTP status code
396 * @return The Status-Line
397 * @deffunc const char *ap_get_status_line(int status)
399 API_EXPORT(const char *) ap_get_status_line(int status);
401 /* Reading a block of data from the client connection (e.g., POST arg) */
404 * Setup the client to allow Apache to read the request body.
405 * @param r The current request
406 * @param read_policy How the server should interpret a chunked
407 * transfer-encoding. One of: <PRE>
408 * REQUEST_NO_BODY Send 413 error if message has any body
409 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
410 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
411 * REQUEST_CHUNKED_PASS Pass the chunks to me without removal.
413 * @return either OK or an error code
414 * @deffunc int ap_setup_cleint_block(request_rec *r, int read_policy)
416 API_EXPORT(int) ap_setup_client_block(request_rec *r, int read_policy);
418 * Determine if the client has sent any data. This also sends a
419 * 100 Continue resposne to HTTP/1.1 clients, so modules should not be called
420 * until the module is ready to read content.
421 * @warning Never call this function more than once.
422 * @param r The current request
423 * @return 0 if there is no message to read, 1 otherwise
424 * @deffunc int ap_should_client_block(request_rec *r)
426 API_EXPORT(int) ap_should_client_block(request_rec *r);
428 * Call this in a loop. It will put data into a buffer and return the length
430 * @param r The current request
431 * @param buffer The buffer in which to store the data
432 * @param bufsiz The size of the buffer
433 * @return Number of bytes inserted into the buffer. When done reading, 0
434 * if EOF, or -1 if there was an error
435 * @deffunc long ap_get_client_block(request_rec *r, char *buffer, int bufsiz)
437 API_EXPORT(long) ap_get_client_block(request_rec *r, char *buffer, int bufsiz);
439 * In HTTP/1.1, any method can have a body. However, most GET handlers
440 * wouldn't know what to do with a request body if they received one.
441 * This helper routine tests for and reads any message body in the request,
442 * simply discarding whatever it receives. We need to do this because
443 * failing to read the request body would cause it to be interpreted
444 * as the next request on a persistent connection.
445 * @param r The current request
446 * @return error status if request is malformed, OK otherwise
447 * @deffunc int ap_discard_request_body(request_rec *r)
449 API_EXPORT(int) ap_discard_request_body(request_rec *r);
451 /* Sending a byterange */
454 * Setup the request to send Byte Range requests
455 * @param r the current request
456 * @return 1 if request was setup for byte range requests, 0 otherwise
457 * @deffunc int ap_set_byterange(request_rec *r)
459 API_EXPORT(int) ap_set_byterange(request_rec *r);
461 * Send one byte range chunk for a byte range request
462 * @param r The current request
463 * @param offset Set to the position it should be after the chunk is sent
464 * @param length Set to the length in should be after the chunk is sent
465 * @deffunc int ap_each_byterange(request_rec *r, apr_off_t *offset, apr_size_t *length)
467 API_EXPORT(int) ap_each_byterange(request_rec *r, apr_off_t *offset,
470 * Setup the output headers so that the client knows how to authenticate
471 * itself the next time, if an authentication request failed. This function
472 * works for both basic and digest authentication
473 * @param r The current request
474 * @deffunc void ap_note_auth_failure(request_rec *r)
476 API_EXPORT(void) ap_note_auth_failure(request_rec *r);
478 * Setup the output headers so that the client knows how to authenticate
479 * itself the next time, if an authentication request failed. This function
480 * works only for basic authentication
481 * @param r The current request
482 * @deffunc void ap_note_basic_auth_failure(request_rec *r)
484 API_EXPORT(void) ap_note_basic_auth_failure(request_rec *r);
486 * Setup the output headers so that the client knows how to authenticate
487 * itself the next time, if an authentication request failed. This function
488 * works only for digest authentication
489 * @param r The current request
490 * @deffunc void ap_note_digest_auth_failure(request_rec *r)
492 API_EXPORT(void) ap_note_digest_auth_failure(request_rec *r);
494 * Get the password from the request headers
495 * @param r The current request
496 * @param pw The password as set in the headers
497 * @return 0 (OK) if it set the 'pw' argument (and assured
498 * a correct value in r->connection->user); otherwise it returns
499 * an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
500 * really confused, HTTP_UNAUTHORIZED if no authentication at all
501 * seemed to be in use, or DECLINED if there was authentication but
502 * it wasn't Basic (in which case, the caller should presumably
504 * @deffunc int ap_get_basic_auth_pw(request_rec *r, const char **pw)
506 API_EXPORT(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
509 * Setting up the protocol fields for subsidiary requests...
510 * Also, a wrapup function to keep the internal accounting straight.
513 void ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
514 void ap_finalize_sub_req_protocol(request_rec *sub_r);
517 * parse_uri: break apart the uri
518 * @warning Side Effects: <PRE>
519 * - sets r->args to rest after '?' (or NULL if no '?')
520 * - sets r->uri to request uri (without r->args part)
521 * - sets r->hostname (if not set already) from request (scheme://host:port)
523 * @param r The current request
524 * @param uri The uri to break apart
525 * @deffunc void ap_parse_uri(request_rec *r, const char *uri)
527 CORE_EXPORT(void) ap_parse_uri(request_rec *r, const char *uri);
530 * Get the method number associated with the given string, assumed to
531 * contain an HTTP method. Returns M_INVALID if not recognized.
532 * @param method A string containing a valid HTTP method
533 * @return The method number
534 * @deffunc int ap_method_number_of(const char *method)
536 API_EXPORT(int) ap_method_number_of(const char *method);
539 * Get the method name associated with the given internal method
540 * number. Returns NULL if not recognized.
541 * @param methnum An integer value corresponding to an internal method number
542 * @return The name corresponding to the method number
543 * @deffunc const char *ap_method_name_of(int methnum)
545 API_EXPORT(const char *) ap_method_name_of(int methnum);
549 * post_read_request --- run right after read_request or internal_redirect,
550 * and not run during any subrequests.
553 * This hook allows modules to affect the request immediately after the request
554 * has been read, and before any other phases have been processes. This allows
555 * modules to make decisions based upon the input header fields
556 * @param r The current request
557 * @return OK or DECLINED
558 * @deffunc ap_run_post_read_request(request_rec *r)
560 AP_DECLARE_HOOK(int,post_read_request,(request_rec *))
562 * This hook allows modules to perform any module-specific logging activities
563 * over and above the normal server things.
564 * @param r The current request
565 * @return OK, DECLINED, or HTTP_...
566 * @deffunc int ap_run_log_transaction(request_rec *r)
568 AP_DECLARE_HOOK(int,log_transaction,(request_rec *))
570 * This hook allows modules to retrieve the http method from a request. This
571 * allows Apache modules to easily extend the methods that Apache understands
572 * @param r The current request
573 * @return The http method from the request
574 * @deffunc const char *ap_run_http_method(const request_rec *r)
576 AP_DECLARE_HOOK(const char *,http_method,(const request_rec *))
578 * Return the default port from the current request
579 * @param r The current request
580 * @return The current port
581 * @deffunc unsigned short ap_run_default_port(const request_rec *r)
583 AP_DECLARE_HOOK(unsigned short,default_port,(const request_rec *))
589 #endif /* !APACHE_HTTP_PROTOCOL_H */