]> granicus.if.org Git - apache/blob - include/http_protocol.h
Fix the return type of ap_http_header_filter().
[apache] / include / http_protocol.h
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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
17  *    distribution.
18  *
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.
25  *
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.
30  *
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.
34  *
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
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
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/>.
53  *
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.
57  */
58
59 #ifndef APACHE_HTTP_PROTOCOL_H
60 #define APACHE_HTTP_PROTOCOL_H
61
62 #include "ap_hooks.h"
63 #include "apr_portable.h"
64 #include "apr_mmap.h"
65 #include "util_filter.h"
66 #include "ap_buckets.h"
67
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71
72 /**
73  * @package HTTP protocol handling
74  */
75
76 /*
77  * Prototypes for routines which either talk directly back to the user,
78  * or control the ones that eventually do.
79  */
80
81 /**
82  * Read a request and fill in the fields.
83  * @param c The current connection
84  * @return The new request_rec
85  */ 
86 request_rec *ap_read_request(conn_rec *c);
87
88 /**
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)
94  */
95 AP_DECLARE_NONSTD(int) ap_send_header_field(request_rec *r, const char *fieldname,
96                       const char *fieldval);
97
98 /**
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)
105  */
106 AP_DECLARE(void) ap_basic_http_header(request_rec *r);
107
108 /**
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)
112  */
113 AP_DECLARE(void) ap_send_http_header(request_rec *l);
114
115 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, ap_bucket_brigade *b);
116
117 /* Send the response to special method requests */
118
119 AP_DECLARE(int) ap_send_http_trace(request_rec *r);
120 int ap_send_http_options(request_rec *r);
121
122 /* Finish up stuff after a request */
123
124 /**
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)
129  */
130 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
131
132 /**
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)
141  */
142 AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
143
144 /* Set last modified header line from the lastmod date of the associated file.
145  * Also, set content length.
146  *
147  * May return an error status, typically HTTP_NOT_MODIFIED (that when the
148  * permit_cache argument is set to one).
149  */
150
151 /**
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)
157  */
158 AP_DECLARE(int) ap_set_content_length(request_rec *r, long length);
159 /**
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)
164  */
165 AP_DECLARE(int) ap_set_keepalive(request_rec *r);
166 /**
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)
173  */
174 AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
175 /**
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)
183  */ 
184 AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
185 /**
186  * Set the E-tag outgoing header
187  * @param The current request
188  * @deffunc void ap_set_etag(request_rec *r)
189  */
190 AP_DECLARE(void) ap_set_etag(request_rec *r);
191 /**
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)
195  */
196 AP_DECLARE(void) ap_set_last_modified(request_rec *r);
197 /**
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)
204  */
205 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
206
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).
212  *
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).
216  */
217
218 /**
219  * Send an entire file to the client, using sendfile if supported by the 
220  * current platform
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);
227  */
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);
230 /**
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)
238  */
239 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
240                              size_t length);
241
242 /**
243  * Create a new method list with the specified number of preallocated
244  * slots for extension methods.
245  *
246  * @param   p       Pointer to a pool in which the structure should be
247  *                  allocated.
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)
251  */
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,
256                                                        int mnum),
257                                           void *rec,
258                                           const ap_method_list_t *ml, ...);
259 AP_DECLARE(void) ap_method_list_vdo(int (*comp) (void *urec, const char *mname,
260                                                  int mnum),
261                                     void *rec, const ap_method_list_t *ml,
262                                     va_list vp);
263 /**
264  * Search for an HTTP method name in an ap_method_list_t structure, and
265  * return true if found.
266  *
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)
271  */
272 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
273
274 /**
275  * Add an HTTP method name to an ap_method_list_t structure if it isn't
276  * already listed.
277  *
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.
280  * @return  None.
281  * @deffunc void ap_method_in_list(ap_method_list_t *l, const char *method)
282  */
283 AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
284     
285 /**
286  * Remove an HTTP method name from an ap_method_list_t structure.
287  *
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.
290  * @return  None.
291  * @deffunc void ap_method_list_remove(ap_method_list_t *l, const char *method)
292  */
293 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
294                                        const char *method);
295
296 /**
297  * Reset a method list to be completely empty.
298  *
299  * @param   l       Pointer to a method list, such as cmd->methods_limited.
300  * @return  None.
301  * @deffunc void ap_clear_method_list(ap_method_list_t *l)
302  */
303 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
304     
305 /* Hmmm... could macrofy these for now, and maybe forever, though the
306  * definitions of the macros would get a whole lot hairier.
307  */
308
309 /**
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)
315  */
316 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
317 /**
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)
323  */
324 AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
325 /**
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)
332  */
333 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
334 /**
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, ...)
340  */
341 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...);
342 /**
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)
349  */
350 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
351 /**
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, ...)
358  */
359 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
360                                 __attribute__((format(printf,2,3)));
361 /**
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)
366  */
367 AP_DECLARE(int) ap_rflush(request_rec *r);
368
369 /**
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)
375  */
376 AP_DECLARE(int) ap_index_of_response(int status);
377
378 /** 
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)
385  */
386 AP_DECLARE(const char *) ap_get_status_line(int status);
387
388 /* Reading a block of data from the client connection (e.g., POST arg) */
389
390 /**
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.
399  * </PRE>
400  * @return either OK or an error code
401  * @deffunc int ap_setup_cleint_block(request_rec *r, int read_policy)
402  */
403 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
404 /**
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)
412  */
413 AP_DECLARE(int) ap_should_client_block(request_rec *r);
414 /**
415  * Call this in a loop.  It will put data into a buffer and return the length
416  * of the input block
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)
423  */
424 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, int bufsiz);
425 /**
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)
435  */
436 AP_DECLARE(int) ap_discard_request_body(request_rec *r);
437
438 /* Sending a byterange */
439
440 /**
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)
445  */
446 AP_DECLARE(int) ap_set_byterange(request_rec *r);
447 /**
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)
453  */
454 AP_DECLARE(int) ap_each_byterange(request_rec *r, apr_off_t *offset,
455                                   apr_size_t *length);
456 /**
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)
462  */ 
463 AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
464 /**
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)
470  */ 
471 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
472 /**
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)
478  */ 
479 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
480 /**
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 
490  *         decline as well).
491  * @deffunc int ap_get_basic_auth_pw(request_rec *r, const char **pw)
492  */
493 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
494
495 /*
496  * Setting up the protocol fields for subsidiary requests...
497  * Also, a wrapup function to keep the internal accounting straight.
498  */
499
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);
502
503 /**
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)
509  * </PRE>
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)
513  */
514 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
515
516 /**
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)
522  */
523 AP_DECLARE(int) ap_method_number_of(const char *method);
524
525 /**
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)
531  */
532 AP_DECLARE(const char *) ap_method_name_of(int methnum);
533
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);
536
537   /* Hooks */
538   /*
539    * post_read_request --- run right after read_request or internal_redirect,
540    *                  and not run during any subrequests.
541    */
542 /**
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)
549  */
550 AP_DECLARE_HOOK(int,post_read_request,(request_rec *))
551 /**
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)
557  */
558 AP_DECLARE_HOOK(int,log_transaction,(request_rec *))
559 /**
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)
565  */
566 AP_DECLARE_HOOK(const char *,http_method,(const request_rec *))
567 /**
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)
572  */
573 AP_DECLARE_HOOK(unsigned short,default_port,(const request_rec *))
574
575 #ifdef __cplusplus
576 }
577 #endif
578
579 #endif  /* !APACHE_HTTP_PROTOCOL_H */