]> granicus.if.org Git - apache/blob - include/http_protocol.h
fix a warning in a call to apr_psprintf()
[apache] / include / http_protocol.h
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2001 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 "httpd.h"
63 #include "apr_hooks.h"
64 #include "apr_portable.h"
65 #include "apr_mmap.h"
66 #include "apr_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 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)
96  */
97 AP_DECLARE(void) ap_basic_http_header(request_rec *r, apr_bucket_brigade *bb);
98
99 /**
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)
103  */
104 AP_DECLARE(void) ap_send_http_header(request_rec *r);
105
106
107 /* Finish up stuff after a request */
108
109 /**
110  * Called at completion of sending the response.  It sends the terminating
111  * protocol information.
112  * @param r The current request
113  * @deffunc void ap_finalize_request_protocol(request_rec *r)
114  */
115 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
116
117 /**
118  * Send error back to client.
119  * @param r The current request
120  * @param recursive_error last arg indicates error status in case we get 
121  *      an error in the process of trying to deal with an ErrorDocument 
122  *      to handle some other error.  In that case, we print the default 
123  *      report for the first thing that went wrong, and more briefly report 
124  *      on the problem with the ErrorDocument.
125  * @deffunc void ap_send_error_response(request_rec *r, int recursive_error)
126  */
127 AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
128
129 /* Set last modified header line from the lastmod date of the associated file.
130  * Also, set content length.
131  *
132  * May return an error status, typically HTTP_NOT_MODIFIED (that when the
133  * permit_cache argument is set to one).
134  */
135
136 /**
137  * Set the content length for this request
138  * @param r The current request
139  * @param length The new content length
140  * @deffunc void ap_set_content_length(request_rec *r, apr_off_t length)
141  */
142 AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
143
144 /**
145  * Set the keepalive status for this request
146  * @param r The current request
147  * @return 1 if keepalive can be set, 0 otherwise
148  * @deffunc int ap_set_keepalive(request_rec *r)
149  */
150 AP_DECLARE(int) ap_set_keepalive(request_rec *r);
151
152 /**
153  * Return the latest rational time from a request/mtime pair.  Mtime is 
154  * returned unless it's in the future, in which case we return the current time.
155  * @param r The current request
156  * @param mtime The last modified time
157  * @return the latest rational time.
158  * @deffunc apr_time_t ap_rationalize_mtime(request_rec *r, apr_time_t mtime)
159  */
160 AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
161
162 /**
163  * Construct an entity tag from the resource information.  If it's a real
164  * file, build in some of the file characteristics.
165  * @param r The current request
166  * @param force_weak Force the entity tag to be weak - it could be modified
167  *                   again in as short an interval.
168  * @return The entity tag
169  * @deffunc char *ap_make_etag(request_rec *r, int force_weak)
170  */ 
171 AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
172
173 /**
174  * Set the E-tag outgoing header
175  * @param The current request
176  * @deffunc void ap_set_etag(request_rec *r)
177  */
178 AP_DECLARE(void) ap_set_etag(request_rec *r);
179
180 /**
181  * Set the last modified time for the file being sent
182  * @param r The current request
183  * @deffunc void ap_set_last_modified(request_rec *r)
184  */
185 AP_DECLARE(void) ap_set_last_modified(request_rec *r);
186
187 /**
188  * Implements condition GET rules for HTTP/1.1 specification.  This function
189  * inspects the client headers and determines if the response fulfills 
190  * the requirements specified.
191  * @param r The current request
192  * @return 1 if the response fulfills the condition GET rules, 0 otherwise
193  * @deffunc int ap_meets_conditions(request_rec *r)
194  */
195 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
196
197 /* Other ways to send stuff at the client.  All of these keep track
198  * of bytes_sent automatically.  This indirection is intended to make
199  * it a little more painless to slide things like HTTP-NG packetization
200  * underneath the main body of the code later.  In the meantime, it lets
201  * us centralize a bit of accounting (bytes_sent).
202  *
203  * These also return the number of bytes written by the call.
204  * They should only be called with a timeout registered, for obvious reaasons.
205  * (Ditto the send_header stuff).
206  */
207
208 /**
209  * Send an entire file to the client, using sendfile if supported by the 
210  * current platform
211  * @param fd The file to send.
212  * @param r The current request
213  * @param offset Offset into the file to start sending.
214  * @param length Amount of data to send
215  * @param nbytes Amount of data actually sent
216  * @deffunc apr_status_t ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset, apr_size_t length, apr_size_t *nbytes);
217  */
218 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset, 
219                                    apr_size_t length, apr_size_t *nbytes);
220
221 #if APR_HAS_MMAP
222 /**
223  * Send an MMAP'ed file to the client
224  * @param mm The MMAP'ed file to send
225  * @param r The current request
226  * @param offset The offset into the MMAP to start sending
227  * @param length The amount of data to send
228  * @return The number of bytes sent
229  * @deffunc size_t ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset, size_t length)
230  */
231 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
232                              size_t length);
233 #endif
234
235 /**
236  * Create a new method list with the specified number of preallocated
237  * slots for extension methods.
238  *
239  * @param   p       Pointer to a pool in which the structure should be
240  *                  allocated.
241  * @param   nelts   Number of preallocated extension slots
242  * @return  Pointer to the newly created structure.
243  * @deffunc ap_method_list_t ap_make_method_list(apr_pool_t *p, int nelts)
244  */
245 AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
246 AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
247                                      ap_method_list_t *src);
248 AP_DECLARE_NONSTD(void) ap_method_list_do(int (*comp) (void *urec, const char *mname,
249                                                        int mnum),
250                                           void *rec,
251                                           const ap_method_list_t *ml, ...);
252 AP_DECLARE(void) ap_method_list_vdo(int (*comp) (void *urec, const char *mname,
253                                                  int mnum),
254                                     void *rec, const ap_method_list_t *ml,
255                                     va_list vp);
256 /**
257  * Search for an HTTP method name in an ap_method_list_t structure, and
258  * return true if found.
259  *
260  * @param   method  String containing the name of the method to check.
261  * @param   l       Pointer to a method list, such as cmd->methods_limited.
262  * @return  1 if method is in the list, otherwise 0
263  * @deffunc int ap_method_in_list(const char *method, ap_method_list_t *l)
264  */
265 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
266
267 /**
268  * Add an HTTP method name to an ap_method_list_t structure if it isn't
269  * already listed.
270  *
271  * @param   method  String containing the name of the method to check.
272  * @param   l       Pointer to a method list, such as cmd->methods_limited.
273  * @return  None.
274  * @deffunc void ap_method_in_list(ap_method_list_t *l, const char *method)
275  */
276 AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
277     
278 /**
279  * Remove an HTTP method name from an ap_method_list_t structure.
280  *
281  * @param   l       Pointer to a method list, such as cmd->methods_limited.
282  * @param   method  String containing the name of the method to remove.
283  * @return  None.
284  * @deffunc void ap_method_list_remove(ap_method_list_t *l, const char *method)
285  */
286 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
287                                        const char *method);
288
289 /**
290  * Reset a method list to be completely empty.
291  *
292  * @param   l       Pointer to a method list, such as cmd->methods_limited.
293  * @return  None.
294  * @deffunc void ap_clear_method_list(ap_method_list_t *l)
295  */
296 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
297     
298 /* Hmmm... could macrofy these for now, and maybe forever, though the
299  * definitions of the macros would get a whole lot hairier.
300  */
301
302 /**
303  * Output one character for this request
304  * @param c the character to output
305  * @param r the current request
306  * @return The number of bytes sent
307  * @deffunc int ap_rputc(int c, request_rec *r)
308  */
309 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
310
311 /**
312  * Output a string for the current request
313  * @param str The string to output
314  * @param r The current request
315  * @return The number of bytes sent
316  * @deffunc int ap_rputs(const char *str, request_rec *r)
317  */
318 AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
319
320 /**
321  * Write a buffer for the current request
322  * @param buf The buffer to write
323  * @param nbyte The number of bytes to send from the buffer
324  * @param r The current request
325  * @return The number of bytes sent
326  * @deffunc int ap_rwrite(const void *buf, int nbyte, request_rec *r)
327  */
328 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
329
330 /**
331  * Write an unspecified number of strings to the request
332  * @param r The current request
333  * @param ... The strings to write
334  * @return The number of bytes sent
335  * @deffunc int ap_rvputs(request_rec *r, ...)
336  */
337 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...);
338
339 /**
340  * Output data to the client in a printf format
341  * @param r The current request
342  * @param fmt The format string
343  * @param vlist The arguments to use to fill out the format string
344  * @return The number of bytes sent
345  * @deffunc int ap_vrprintf(request_rec *r, const char *fmt, va_list vlist)
346  */
347 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
348
349 /**
350  * Output data to the client in a printf format
351  * @param r The current request
352  * @param fmt The format string
353  * @param ... The arguments to use to fill out the format string
354  * @return The number of bytes sent
355  * @deffunc int ap_rprintf(request_rec *r, const char *fmt, ...)
356  */
357 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
358                                 __attribute__((format(printf,2,3)));
359 /**
360  * Flush all of the data for the current request to the client
361  * @param r The current request
362  * @return The number of bytes sent
363  * @deffunc int ap_rflush(request_rec *r)
364  */
365 AP_DECLARE(int) ap_rflush(request_rec *r);
366
367 /**
368  * Index used in custom_responses array for a specific error code
369  * (only use outside protocol.c is in getting them configured).
370  * @param status HTTP status code
371  * @return The index of the response
372  * @deffunc int ap_index_of_response(int status)
373  */
374 AP_DECLARE(int) ap_index_of_response(int status);
375
376 /** 
377  * Return the Status-Line for a given status code (excluding the
378  * HTTP-Version field). If an invalid or unknown status code is
379  * passed, "500 Internal Server Error" will be returned. 
380  * @param status The HTTP status code
381  * @return The Status-Line
382  * @deffunc const char *ap_get_status_line(int status)
383  */
384 AP_DECLARE(const char *) ap_get_status_line(int status);
385
386 /* Reading a block of data from the client connection (e.g., POST arg) */
387
388 /**
389  * Setup the client to allow Apache to read the request body.
390  * @param r The current request
391  * @param read_policy How the server should interpret a chunked 
392  *                    transfer-encoding.  One of: <PRE>
393  *    REQUEST_NO_BODY          Send 413 error if message has any body
394  *    REQUEST_CHUNKED_ERROR    Send 411 error if body without Content-Length
395  *    REQUEST_CHUNKED_DECHUNK  If chunked, remove the chunks for me.
396  * </PRE>
397  * @return either OK or an error code
398  * @deffunc int ap_setup_cleint_block(request_rec *r, int read_policy)
399  */
400 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
401
402 /**
403  * Determine if the client has sent any data.  This also sends a 
404  * 100 Continue resposne to HTTP/1.1 clients, so modules should not be called
405  * until the module is ready to read content.
406  * @warning Never call this function more than once.
407  * @param r The current request
408  * @return 0 if there is no message to read, 1 otherwise
409  * @deffunc int ap_should_client_block(request_rec *r)
410  */
411 AP_DECLARE(int) ap_should_client_block(request_rec *r);
412
413 /**
414  * Call this in a loop.  It will put data into a buffer and return the length
415  * of the input block
416  * @param r The current request
417  * @param buffer The buffer in which to store the data
418  * @param bufsiz The size of the buffer
419  * @return Number of bytes inserted into the buffer.  When done reading, 0
420  *         if EOF, or -1 if there was an error
421  * @deffunc long ap_get_client_block(request_rec *r, char *buffer, int bufsiz)
422  */
423 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, int bufsiz);
424
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
439 /* Sending a byterange */
440
441 /**
442  * Setup the output headers so that the client knows how to authenticate
443  * itself the next time, if an authentication request failed.  This function
444  * works for both basic and digest authentication
445  * @param r The current request
446  * @deffunc void ap_note_auth_failure(request_rec *r)
447  */ 
448 AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
449
450 /**
451  * Setup the output headers so that the client knows how to authenticate
452  * itself the next time, if an authentication request failed.  This function
453  * works only for basic authentication
454  * @param r The current request
455  * @deffunc void ap_note_basic_auth_failure(request_rec *r)
456  */ 
457 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
458
459 /**
460  * Setup the output headers so that the client knows how to authenticate
461  * itself the next time, if an authentication request failed.  This function
462  * works only for digest authentication
463  * @param r The current request
464  * @deffunc void ap_note_digest_auth_failure(request_rec *r)
465  */ 
466 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
467
468 /**
469  * Get the password from the request headers
470  * @param r The current request
471  * @param pw The password as set in the headers
472  * @return 0 (OK) if it set the 'pw' argument (and assured
473  *         a correct value in r->connection->user); otherwise it returns 
474  *         an error code, either HTTP_INTERNAL_SERVER_ERROR if things are 
475  *         really confused, HTTP_UNAUTHORIZED if no authentication at all 
476  *         seemed to be in use, or DECLINED if there was authentication but 
477  *         it wasn't Basic (in which case, the caller should presumably 
478  *         decline as well).
479  * @deffunc int ap_get_basic_auth_pw(request_rec *r, const char **pw)
480  */
481 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
482
483 /**
484  * parse_uri: break apart the uri
485  * @warning Side Effects: <PRE>
486  *    - sets r->args to rest after '?' (or NULL if no '?')
487  *    - sets r->uri to request uri (without r->args part)
488  *    - sets r->hostname (if not set already) from request (scheme://host:port)
489  * </PRE>
490  * @param r The current request
491  * @param uri The uri to break apart
492  * @deffunc void ap_parse_uri(request_rec *r, const char *uri)
493  */
494 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
495
496 AP_CORE_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
497
498 /**
499  * Get the method number associated with the given string, assumed to
500  * contain an HTTP method.  Returns M_INVALID if not recognized.
501  * @param method A string containing a valid HTTP method
502  * @return The method number
503  * @deffunc int ap_method_number_of(const char *method)
504  */
505 AP_DECLARE(int) ap_method_number_of(const char *method);
506
507 /**
508  * Get the method name associated with the given internal method
509  * number.  Returns NULL if not recognized.
510  * @param methnum An integer value corresponding to an internal method number
511  * @return The name corresponding to the method number
512  * @deffunc const char *ap_method_name_of(int methnum)
513  */
514 AP_DECLARE(const char *) ap_method_name_of(int methnum);
515
516
517   /* Hooks */
518   /*
519    * post_read_request --- run right after read_request or internal_redirect,
520    *                  and not run during any subrequests.
521    */
522 /**
523  * This hook allows modules to affect the request immediately after the request
524  * has been read, and before any other phases have been processes.  This allows
525  * modules to make decisions based upon the input header fields
526  * @param r The current request
527  * @return OK or DECLINED
528  * @deffunc ap_run_post_read_request(request_rec *r)
529  */
530 AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
531
532 /**
533  * This hook allows modules to perform any module-specific logging activities
534  * over and above the normal server things.
535  * @param r The current request
536  * @return OK, DECLINED, or HTTP_...
537  * @deffunc int ap_run_log_transaction(request_rec *r)
538  */
539 AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
540
541 /**
542  * This hook allows modules to retrieve the http method from a request.  This
543  * allows Apache modules to easily extend the methods that Apache understands
544  * @param r The current request
545  * @return The http method from the request
546  * @deffunc const char *ap_run_http_method(const request_rec *r)
547  */
548 AP_DECLARE_HOOK(const char *,http_method,(const request_rec *r))
549
550 /**
551  * Return the default port from the current request
552  * @param r The current request
553  * @return The current port
554  * @deffunc apr_port_t ap_run_default_port(const request_rec *r)
555  */
556 AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
557
558 typedef struct ap_bucket_error ap_bucket_error;
559
560 /**
561  * A bucket referring to an HTTP error
562  * This bucket can be passed down the filter stack to indicate that an
563  * HTTP error occurred while running a filter.  In order for this bucket
564  * to be used successfully, it MUST be sent as the first bucket in the
565  * first brigade to be sent from a given filter.
566  */
567 struct ap_bucket_error {
568     /** The error code */
569     int status;
570     /** The error string */
571     const char    *start;
572 };
573
574 AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
575
576 /**
577  * Make the bucket passed in an error bucket
578  * @param b The bucket to make into an error bucket
579  * @param error The HTTP error code to put in the bucket. 
580  * @param buf An optional error string to put in the bucket.
581  * @param p A pool to allocate out of.
582  * @return The new bucket, or NULL if allocation failed
583  * @deffunc apr_bucket *ap_bucket_error_make(apr_bucket *b, int error, const char *buf, apr_pool_t *p)
584  */
585 AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
586                 const char *buf, apr_pool_t *p);
587
588 /**
589  * Create a bucket referring to an HTTP error.
590  * @param error The HTTP error code to put in the bucket. 
591  * @param buf An optional error string to put in the bucket.
592  * @param p A pool to allocate out of.
593  * @return The new bucket, or NULL if allocation failed
594  * @deffunc apr_bucket *ap_bucket_error_create(int error, const char *buf, apr_pool_t *p)
595  */
596 AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error,
597                 const char *buf, apr_pool_t *p);
598
599 #ifdef __cplusplus
600 }
601 #endif
602
603 #endif  /* !APACHE_HTTP_PROTOCOL_H */