1 /* Copyright 1999-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef APACHE_HTTP_PROTOCOL_H
17 #define APACHE_HTTP_PROTOCOL_H
20 #include "apr_hooks.h"
21 #include "apr_portable.h"
23 #include "apr_buckets.h"
24 #include "util_filter.h"
31 * @package HTTP protocol handling
35 * This hook allows modules to insert filters for the current error response
36 * @param r the current request
39 AP_DECLARE_HOOK(void,insert_error_filter,(request_rec *r))
41 /* This is an optimization. We keep a record of the filter_rec that
42 * stores the old_write filter, so that we can avoid strcmp's later.
44 AP_DECLARE_DATA extern ap_filter_rec_t *ap_old_write_func;
47 * Prototypes for routines which either talk directly back to the user,
48 * or control the ones that eventually do.
52 * Read a request and fill in the fields.
53 * @param c The current connection
54 * @return The new request_rec
56 request_rec *ap_read_request(conn_rec *c);
59 * Read the mime-encoded headers.
60 * @param r The current request
62 AP_DECLARE(void) ap_get_mime_headers(request_rec *r);
65 * Optimized version of ap_get_mime_headers() that requires a
66 * temporary brigade to work with
67 * @param r The current request
68 * @param bb temp brigade
70 AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r,
71 apr_bucket_brigade *bb);
73 /* Finish up stuff after a request */
76 * Called at completion of sending the response. It sends the terminating
77 * protocol information.
78 * @param r The current request
79 * @deffunc void ap_finalize_request_protocol(request_rec *r)
81 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
84 * Send error back to client.
85 * @param r The current request
86 * @param recursive_error last arg indicates error status in case we get
87 * an error in the process of trying to deal with an ErrorDocument
88 * to handle some other error. In that case, we print the default
89 * report for the first thing that went wrong, and more briefly report
90 * on the problem with the ErrorDocument.
91 * @deffunc void ap_send_error_response(request_rec *r, int recursive_error)
93 AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
95 /* Set last modified header line from the lastmod date of the associated file.
96 * Also, set content length.
98 * May return an error status, typically HTTP_NOT_MODIFIED (that when the
99 * permit_cache argument is set to one).
103 * Set the content length for this request
104 * @param r The current request
105 * @param length The new content length
106 * @deffunc void ap_set_content_length(request_rec *r, apr_off_t length)
108 AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
111 * Set the keepalive status for this request
112 * @param r The current request
113 * @return 1 if keepalive can be set, 0 otherwise
114 * @deffunc int ap_set_keepalive(request_rec *r)
116 AP_DECLARE(int) ap_set_keepalive(request_rec *r);
119 * Return the latest rational time from a request/mtime pair. Mtime is
120 * returned unless it's in the future, in which case we return the current time.
121 * @param r The current request
122 * @param mtime The last modified time
123 * @return the latest rational time.
124 * @deffunc apr_time_t ap_rationalize_mtime(request_rec *r, apr_time_t mtime)
126 AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
129 * Build the content-type that should be sent to the client from the
130 * content-type specified. The following rules are followed:
131 * - if type is NULL, type is set to ap_default_type(r)
132 * - if charset adding is disabled, stop processing and return type.
133 * - then, if there are no parameters on type, add the default charset
135 * @param r The current request
136 * @return The content-type
137 * @deffunc const char *ap_make_content_type(request_rec *r, const char *type);
139 AP_DECLARE(const char *) ap_make_content_type(request_rec *r,
144 * Precompile metadata structures used by ap_make_content_type()
145 * @param r The pool to use for allocations
146 * @deffunc void ap_setup_make_content_type(apr_pool_t *pool)
148 AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool);
149 #endif /* CORE_PRIVATE */
152 * Construct an entity tag from the resource information. If it's a real
153 * file, build in some of the file characteristics.
154 * @param r The current request
155 * @param force_weak Force the entity tag to be weak - it could be modified
156 * again in as short an interval.
157 * @return The entity tag
158 * @deffunc char *ap_make_etag(request_rec *r, int force_weak)
160 AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
163 * Set the E-tag outgoing header
164 * @param The current request
165 * @deffunc void ap_set_etag(request_rec *r)
167 AP_DECLARE(void) ap_set_etag(request_rec *r);
170 * Set the last modified time for the file being sent
171 * @param r The current request
172 * @deffunc void ap_set_last_modified(request_rec *r)
174 AP_DECLARE(void) ap_set_last_modified(request_rec *r);
177 * Implements condition GET rules for HTTP/1.1 specification. This function
178 * inspects the client headers and determines if the response fulfills
179 * the requirements specified.
180 * @param r The current request
181 * @return 1 if the response fulfills the condition GET rules, 0 otherwise
182 * @deffunc int ap_meets_conditions(request_rec *r)
184 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
186 /* Other ways to send stuff at the client. All of these keep track
187 * of bytes_sent automatically. This indirection is intended to make
188 * it a little more painless to slide things like HTTP-NG packetization
189 * underneath the main body of the code later. In the meantime, it lets
190 * us centralize a bit of accounting (bytes_sent).
192 * These also return the number of bytes written by the call.
193 * They should only be called with a timeout registered, for obvious reaasons.
194 * (Ditto the send_header stuff).
198 * Send an entire file to the client, using sendfile if supported by the
200 * @param fd The file to send.
201 * @param r The current request
202 * @param offset Offset into the file to start sending.
203 * @param length Amount of data to send
204 * @param nbytes Amount of data actually sent
205 * @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);
207 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
208 apr_size_t length, apr_size_t *nbytes);
212 * Send an MMAP'ed file to the client
213 * @param mm The MMAP'ed file to send
214 * @param r The current request
215 * @param offset The offset into the MMAP to start sending
216 * @param length The amount of data to send
217 * @return The number of bytes sent
218 * @deffunc size_t ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset, size_t length)
220 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
226 * Register a new request method, and return the offset that will be
227 * associated with that method.
229 * @param p The pool to create registered method numbers from.
230 * @param methname The name of the new method to register.
231 * @return Ab int value representing an offset into a bitmask.
233 AP_DECLARE(int) ap_method_register(apr_pool_t *p, const char *methname);
236 * Initialize the method_registry and allocate memory for it.
238 * @param p Pool to allocate memory for the registry from.
240 AP_DECLARE(void) ap_method_registry_init(apr_pool_t *p);
243 * This is a convenience macro to ease with checking a mask
244 * against a method name.
246 #define AP_METHOD_CHECK_ALLOWED(mask, methname) \
247 ((mask) & (AP_METHOD_BIT << ap_method_number_of((methname))))
250 * Create a new method list with the specified number of preallocated
251 * slots for extension methods.
253 * @param p Pointer to a pool in which the structure should be
255 * @param nelts Number of preallocated extension slots
256 * @return Pointer to the newly created structure.
257 * @deffunc ap_method_list_t ap_make_method_list(apr_pool_t *p, int nelts)
259 AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
260 AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
261 ap_method_list_t *src);
262 AP_DECLARE_NONSTD(void) ap_method_list_do(int (*comp) (void *urec, const char *mname,
265 const ap_method_list_t *ml, ...);
266 AP_DECLARE(void) ap_method_list_vdo(int (*comp) (void *urec, const char *mname,
268 void *rec, const ap_method_list_t *ml,
271 * Search for an HTTP method name in an ap_method_list_t structure, and
272 * return true if found.
274 * @param method String containing the name of the method to check.
275 * @param l Pointer to a method list, such as cmd->methods_limited.
276 * @return 1 if method is in the list, otherwise 0
277 * @deffunc int ap_method_in_list(const char *method, ap_method_list_t *l)
279 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
282 * Add an HTTP method name to an ap_method_list_t structure if it isn't
285 * @param method String containing the name of the method to check.
286 * @param l Pointer to a method list, such as cmd->methods_limited.
288 * @deffunc void ap_method_in_list(ap_method_list_t *l, const char *method)
290 AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
293 * Remove an HTTP method name from an ap_method_list_t structure.
295 * @param l Pointer to a method list, such as cmd->methods_limited.
296 * @param method String containing the name of the method to remove.
298 * @deffunc void ap_method_list_remove(ap_method_list_t *l, const char *method)
300 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
304 * Reset a method list to be completely empty.
306 * @param l Pointer to a method list, such as cmd->methods_limited.
308 * @deffunc void ap_clear_method_list(ap_method_list_t *l)
310 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
313 * Set the content type for this request (r->content_type).
314 * @param r The current request
315 * @param ct The new content type
316 * @deffunc void ap_set_content_type(request_rec *r, const char* ct)
317 * @warning This function must be called to set r->content_type in order
318 * for the AddOutputFilterByType directive to work correctly.
320 AP_DECLARE(void) ap_set_content_type(request_rec *r, const char *ct);
322 /* Hmmm... could macrofy these for now, and maybe forever, though the
323 * definitions of the macros would get a whole lot hairier.
327 * Output one character for this request
328 * @param c the character to output
329 * @param r the current request
330 * @return The number of bytes sent
331 * @deffunc int ap_rputc(int c, request_rec *r)
333 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
336 * Output a string for the current request
337 * @param str The string to output
338 * @param r The current request
339 * @return The number of bytes sent
340 * @deffunc int ap_rputs(const char *str, request_rec *r)
342 AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
345 * Write a buffer for the current request
346 * @param buf The buffer to write
347 * @param nbyte The number of bytes to send from the buffer
348 * @param r The current request
349 * @return The number of bytes sent
350 * @deffunc int ap_rwrite(const void *buf, int nbyte, request_rec *r)
352 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
355 * Write an unspecified number of strings to the request
356 * @param r The current request
357 * @param ... The strings to write
358 * @return The number of bytes sent
359 * @deffunc int ap_rvputs(request_rec *r, ...)
361 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...);
364 * Output data to the client in a printf format
365 * @param r The current request
366 * @param fmt The format string
367 * @param vlist The arguments to use to fill out the format string
368 * @return The number of bytes sent
369 * @deffunc int ap_vrprintf(request_rec *r, const char *fmt, va_list vlist)
371 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
374 * Output data to the client in a printf format
375 * @param r The current request
376 * @param fmt The format string
377 * @param ... The arguments to use to fill out the format string
378 * @return The number of bytes sent
379 * @deffunc int ap_rprintf(request_rec *r, const char *fmt, ...)
381 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
382 __attribute__((format(printf,2,3)));
384 * Flush all of the data for the current request to the client
385 * @param r The current request
386 * @return The number of bytes sent
387 * @deffunc int ap_rflush(request_rec *r)
389 AP_DECLARE(int) ap_rflush(request_rec *r);
392 * Index used in custom_responses array for a specific error code
393 * (only use outside protocol.c is in getting them configured).
394 * @param status HTTP status code
395 * @return The index of the response
396 * @deffunc int ap_index_of_response(int status)
398 AP_DECLARE(int) ap_index_of_response(int status);
401 * Return the Status-Line for a given status code (excluding the
402 * HTTP-Version field). If an invalid or unknown status code is
403 * passed, "500 Internal Server Error" will be returned.
404 * @param status The HTTP status code
405 * @return The Status-Line
406 * @deffunc const char *ap_get_status_line(int status)
408 AP_DECLARE(const char *) ap_get_status_line(int status);
410 /* Reading a block of data from the client connection (e.g., POST arg) */
413 * Setup the client to allow Apache to read the request body.
414 * @param r The current request
415 * @param read_policy How the server should interpret a chunked
416 * transfer-encoding. One of: <pre>
417 * REQUEST_NO_BODY Send 413 error if message has any body
418 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
419 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
421 * @return either OK or an error code
422 * @deffunc int ap_setup_client_block(request_rec *r, int read_policy)
424 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
427 * Determine if the client has sent any data. This also sends a
428 * 100 Continue response to HTTP/1.1 clients, so modules should not be called
429 * until the module is ready to read content.
430 * @warning Never call this function more than once.
431 * @param r The current request
432 * @return 0 if there is no message to read, 1 otherwise
433 * @deffunc int ap_should_client_block(request_rec *r)
435 AP_DECLARE(int) ap_should_client_block(request_rec *r);
438 * Call this in a loop. It will put data into a buffer and return the length
440 * @param r The current request
441 * @param buffer The buffer in which to store the data
442 * @param bufsiz The size of the buffer
443 * @return Number of bytes inserted into the buffer. When done reading, 0
444 * if EOF, or -1 if there was an error
445 * @deffunc long ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz)
447 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
450 * In HTTP/1.1, any method can have a body. However, most GET handlers
451 * wouldn't know what to do with a request body if they received one.
452 * This helper routine tests for and reads any message body in the request,
453 * simply discarding whatever it receives. We need to do this because
454 * failing to read the request body would cause it to be interpreted
455 * as the next request on a persistent connection.
456 * @param r The current request
457 * @return error status if request is malformed, OK otherwise
458 * @deffunc int ap_discard_request_body(request_rec *r)
460 AP_DECLARE(int) ap_discard_request_body(request_rec *r);
464 * Setup the output headers so that the client knows how to authenticate
465 * itself the next time, if an authentication request failed. This function
466 * works for both basic and digest authentication
467 * @param r The current request
468 * @deffunc void ap_note_auth_failure(request_rec *r)
470 AP_DECLARE(void) ap_note_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 basic authentication
476 * @param r The current request
477 * @deffunc void ap_note_basic_auth_failure(request_rec *r)
479 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
482 * Setup the output headers so that the client knows how to authenticate
483 * itself the next time, if an authentication request failed. This function
484 * works only for digest authentication
485 * @param r The current request
486 * @deffunc void ap_note_digest_auth_failure(request_rec *r)
488 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
491 * Get the password from the request headers
492 * @param r The current request
493 * @param pw The password as set in the headers
494 * @return 0 (OK) if it set the 'pw' argument (and assured
495 * a correct value in r->user); otherwise it returns
496 * an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
497 * really confused, HTTP_UNAUTHORIZED if no authentication at all
498 * seemed to be in use, or DECLINED if there was authentication but
499 * it wasn't Basic (in which case, the caller should presumably
501 * @deffunc int ap_get_basic_auth_pw(request_rec *r, const char **pw)
503 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
506 * parse_uri: break apart the uri
507 * @warning Side Effects: <pre>
508 * - sets r->args to rest after '?' (or NULL if no '?')
509 * - sets r->uri to request uri (without r->args part)
510 * - sets r->hostname (if not set already) from request (scheme://host:port)
512 * @param r The current request
513 * @param uri The uri to break apart
514 * @deffunc void ap_parse_uri(request_rec *r, const char *uri)
516 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
519 * Get the next line of input for the request
520 * @param s The buffer into which to read the line
521 * @param n The size of the buffer
522 * @param r The request
523 * @param fold Whether to merge continuation lines
524 * @return The length of the line, if successful
525 * n, if the line is too big to fit in the buffer
526 * -1 for miscellaneous errors
527 * @deffunc int ap_method_number_of(const char *method)
529 AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
532 * Get the next line of input for the request
534 * Note: on ASCII boxes, ap_rgetline is a macro which simply calls
535 * ap_rgetline_core to get the line of input.
537 * on EBCDIC boxes, ap_rgetline is a wrapper function which
538 * translates ASCII protocol lines to the local EBCDIC code page
539 * after getting the line of input.
541 * @param s Pointer to the pointer to the buffer into which the line
542 * should be read; if *s==NULL, a buffer of the necessary size
543 * to hold the data will be allocated from the request pool
544 * @param n The size of the buffer
545 * @param read The length of the line.
546 * @param r The request
547 * @param fold Whether to merge continuation lines
548 * @param bb Working brigade to use when reading buckets
549 * @return APR_SUCCESS, if successful
550 * APR_ENOSPC, if the line is too big to fit in the buffer
551 * Other errors where appropriate
553 #if APR_CHARSET_EBCDIC
554 AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
556 request_rec *r, int fold,
557 apr_bucket_brigade *bb);
558 #else /* ASCII box */
559 #define ap_rgetline(s, n, read, r, fold, bb) \
560 ap_rgetline_core((s), (n), (read), (r), (fold), (bb))
562 AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
564 request_rec *r, int fold,
565 apr_bucket_brigade *bb);
568 * Get the method number associated with the given string, assumed to
569 * contain an HTTP method. Returns M_INVALID if not recognized.
570 * @param method A string containing a valid HTTP method
571 * @return The method number
573 AP_DECLARE(int) ap_method_number_of(const char *method);
576 * Get the method name associated with the given internal method
577 * number. Returns NULL if not recognized.
578 * @param p A pool to use for temporary allocations.
579 * @param methnum An integer value corresponding to an internal method number
580 * @return The name corresponding to the method number
582 AP_DECLARE(const char *) ap_method_name_of(apr_pool_t *p, int methnum);
587 * post_read_request --- run right after read_request or internal_redirect,
588 * and not run during any subrequests.
591 * This hook allows modules to affect the request immediately after the request
592 * has been read, and before any other phases have been processes. This allows
593 * modules to make decisions based upon the input header fields
594 * @param r The current request
595 * @return OK or DECLINED
596 * @deffunc ap_run_post_read_request(request_rec *r)
598 AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
601 * This hook allows modules to perform any module-specific logging activities
602 * over and above the normal server things.
603 * @param r The current request
604 * @return OK, DECLINED, or HTTP_...
605 * @deffunc int ap_run_log_transaction(request_rec *r)
607 AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
610 * This hook allows modules to retrieve the http method from a request. This
611 * allows Apache modules to easily extend the methods that Apache understands
612 * @param r The current request
613 * @return The http method from the request
614 * @deffunc const char *ap_run_http_method(const request_rec *r)
616 AP_DECLARE_HOOK(const char *,http_method,(const request_rec *r))
619 * Return the default port from the current request
620 * @param r The current request
621 * @return The current port
622 * @deffunc apr_port_t ap_run_default_port(const request_rec *r)
624 AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
626 typedef struct ap_bucket_error ap_bucket_error;
629 * A bucket referring to an HTTP error
630 * This bucket can be passed down the filter stack to indicate that an
631 * HTTP error occurred while running a filter. In order for this bucket
632 * to be used successfully, it MUST be sent as the first bucket in the
633 * first brigade to be sent from a given filter.
635 struct ap_bucket_error {
636 /** Number of buckets using this memory */
637 apr_bucket_refcount refcount;
638 /** The error code */
640 /** The error string */
644 AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
647 * Determine if a bucket is an error bucket
648 * @param e The bucket to inspect
649 * @return true or false
651 #define AP_BUCKET_IS_ERROR(e) (e->type == &ap_bucket_type_error)
654 * Make the bucket passed in an error bucket
655 * @param b The bucket to make into an error bucket
656 * @param error The HTTP error code to put in the bucket.
657 * @param buf An optional error string to put in the bucket.
658 * @param p A pool to allocate out of.
659 * @return The new bucket, or NULL if allocation failed
660 * @deffunc apr_bucket *ap_bucket_error_make(apr_bucket *b, int error, const char *buf, apr_pool_t *p)
662 AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
663 const char *buf, apr_pool_t *p);
666 * Create a bucket referring to an HTTP error.
667 * @param error The HTTP error code to put in the bucket.
668 * @param buf An optional error string to put in the bucket.
669 * @param p A pool to allocate the error string out of.
670 * @param list The bucket allocator from which to allocate the bucket
671 * @return The new bucket, or NULL if allocation failed
672 * @deffunc apr_bucket *ap_bucket_error_create(int error, const char *buf, apr_pool_t *p, apr_bucket_alloc_t *list)
674 AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error, const char *buf,
676 apr_bucket_alloc_t *list);
678 AP_DECLARE_NONSTD(apr_status_t) ap_byterange_filter(ap_filter_t *f, apr_bucket_brigade *b);
679 AP_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b);
680 AP_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(ap_filter_t *,
681 apr_bucket_brigade *);
682 AP_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(ap_filter_t *f, apr_bucket_brigade *b);
685 * Setting up the protocol fields for subsidiary requests...
686 * Also, a wrapup function to keep the internal accounting straight.
688 AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
689 AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub_r);
695 #endif /* !APACHE_HTTP_PROTOCOL_H */