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 OK if the response fulfills the condition GET rules, some
182 * other status code otherwise
183 * @deffunc int ap_meets_conditions(request_rec *r)
185 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
187 /* Other ways to send stuff at the client. All of these keep track
188 * of bytes_sent automatically. This indirection is intended to make
189 * it a little more painless to slide things like HTTP-NG packetization
190 * underneath the main body of the code later. In the meantime, it lets
191 * us centralize a bit of accounting (bytes_sent).
193 * These also return the number of bytes written by the call.
194 * They should only be called with a timeout registered, for obvious reaasons.
195 * (Ditto the send_header stuff).
199 * Send an entire file to the client, using sendfile if supported by the
201 * @param fd The file to send.
202 * @param r The current request
203 * @param offset Offset into the file to start sending.
204 * @param length Amount of data to send
205 * @param nbytes Amount of data actually sent
206 * @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);
208 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
209 apr_size_t length, apr_size_t *nbytes);
213 * Send an MMAP'ed file to the client
214 * @param mm The MMAP'ed file to send
215 * @param r The current request
216 * @param offset The offset into the MMAP to start sending
217 * @param length The amount of data to send
218 * @return The number of bytes sent
219 * @deffunc size_t ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset, size_t length)
221 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
227 * Register a new request method, and return the offset that will be
228 * associated with that method.
230 * @param p The pool to create registered method numbers from.
231 * @param methname The name of the new method to register.
232 * @return Ab int value representing an offset into a bitmask.
234 AP_DECLARE(int) ap_method_register(apr_pool_t *p, const char *methname);
237 * Initialize the method_registry and allocate memory for it.
239 * @param p Pool to allocate memory for the registry from.
241 AP_DECLARE(void) ap_method_registry_init(apr_pool_t *p);
244 * This is a convenience macro to ease with checking a mask
245 * against a method name.
247 #define AP_METHOD_CHECK_ALLOWED(mask, methname) \
248 ((mask) & (AP_METHOD_BIT << ap_method_number_of((methname))))
251 * Create a new method list with the specified number of preallocated
252 * slots for extension methods.
254 * @param p Pointer to a pool in which the structure should be
256 * @param nelts Number of preallocated extension slots
257 * @return Pointer to the newly created structure.
258 * @deffunc ap_method_list_t ap_make_method_list(apr_pool_t *p, int nelts)
260 AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
261 AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
262 ap_method_list_t *src);
263 AP_DECLARE_NONSTD(void) ap_method_list_do(int (*comp) (void *urec, const char *mname,
266 const ap_method_list_t *ml, ...);
267 AP_DECLARE(void) ap_method_list_vdo(int (*comp) (void *urec, const char *mname,
269 void *rec, const ap_method_list_t *ml,
272 * Search for an HTTP method name in an ap_method_list_t structure, and
273 * return true if found.
275 * @param method String containing the name of the method to check.
276 * @param l Pointer to a method list, such as cmd->methods_limited.
277 * @return 1 if method is in the list, otherwise 0
278 * @deffunc int ap_method_in_list(const char *method, ap_method_list_t *l)
280 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
283 * Add an HTTP method name to an ap_method_list_t structure if it isn't
286 * @param method String containing the name of the method to check.
287 * @param l Pointer to a method list, such as cmd->methods_limited.
289 * @deffunc void ap_method_in_list(ap_method_list_t *l, const char *method)
291 AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
294 * Remove an HTTP method name from an ap_method_list_t structure.
296 * @param l Pointer to a method list, such as cmd->methods_limited.
297 * @param method String containing the name of the method to remove.
299 * @deffunc void ap_method_list_remove(ap_method_list_t *l, const char *method)
301 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
305 * Reset a method list to be completely empty.
307 * @param l Pointer to a method list, such as cmd->methods_limited.
309 * @deffunc void ap_clear_method_list(ap_method_list_t *l)
311 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
314 * Set the content type for this request (r->content_type).
315 * @param r The current request
316 * @param ct The new content type
317 * @deffunc void ap_set_content_type(request_rec *r, const char* ct)
318 * @warning This function must be called to set r->content_type in order
319 * for the AddOutputFilterByType directive to work correctly.
321 AP_DECLARE(void) ap_set_content_type(request_rec *r, const char *ct);
323 /* Hmmm... could macrofy these for now, and maybe forever, though the
324 * definitions of the macros would get a whole lot hairier.
328 * Output one character for this request
329 * @param c the character to output
330 * @param r the current request
331 * @return The number of bytes sent
332 * @deffunc int ap_rputc(int c, request_rec *r)
334 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
337 * Output a string for the current request
338 * @param str The string to output
339 * @param r The current request
340 * @return The number of bytes sent
341 * @deffunc int ap_rputs(const char *str, request_rec *r)
343 AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
346 * Write a buffer for the current request
347 * @param buf The buffer to write
348 * @param nbyte The number of bytes to send from the buffer
349 * @param r The current request
350 * @return The number of bytes sent
351 * @deffunc int ap_rwrite(const void *buf, int nbyte, request_rec *r)
353 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
356 * Write an unspecified number of strings to the request
357 * @param r The current request
358 * @param ... The strings to write
359 * @return The number of bytes sent
360 * @deffunc int ap_rvputs(request_rec *r, ...)
362 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...);
365 * Output data to the client in a printf format
366 * @param r The current request
367 * @param fmt The format string
368 * @param vlist The arguments to use to fill out the format string
369 * @return The number of bytes sent
370 * @deffunc int ap_vrprintf(request_rec *r, const char *fmt, va_list vlist)
372 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
375 * Output data to the client in a printf format
376 * @param r The current request
377 * @param fmt The format string
378 * @param ... The arguments to use to fill out the format string
379 * @return The number of bytes sent
380 * @deffunc int ap_rprintf(request_rec *r, const char *fmt, ...)
382 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
383 __attribute__((format(printf,2,3)));
385 * Flush all of the data for the current request to the client
386 * @param r The current request
387 * @return The number of bytes sent
388 * @deffunc int ap_rflush(request_rec *r)
390 AP_DECLARE(int) ap_rflush(request_rec *r);
393 * Index used in custom_responses array for a specific error code
394 * (only use outside protocol.c is in getting them configured).
395 * @param status HTTP status code
396 * @return The index of the response
397 * @deffunc int ap_index_of_response(int status)
399 AP_DECLARE(int) ap_index_of_response(int status);
402 * Return the Status-Line for a given status code (excluding the
403 * HTTP-Version field). If an invalid or unknown status code is
404 * passed, "500 Internal Server Error" will be returned.
405 * @param status The HTTP status code
406 * @return The Status-Line
407 * @deffunc const char *ap_get_status_line(int status)
409 AP_DECLARE(const char *) ap_get_status_line(int status);
411 /* Reading a block of data from the client connection (e.g., POST arg) */
414 * Setup the client to allow Apache to read the request body.
415 * @param r The current request
416 * @param read_policy How the server should interpret a chunked
417 * transfer-encoding. One of: <pre>
418 * REQUEST_NO_BODY Send 413 error if message has any body
419 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
420 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
422 * @return either OK or an error code
423 * @deffunc int ap_setup_client_block(request_rec *r, int read_policy)
425 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
428 * Determine if the client has sent any data. This also sends a
429 * 100 Continue response to HTTP/1.1 clients, so modules should not be called
430 * until the module is ready to read content.
431 * @warning Never call this function more than once.
432 * @param r The current request
433 * @return 0 if there is no message to read, 1 otherwise
434 * @deffunc int ap_should_client_block(request_rec *r)
436 AP_DECLARE(int) ap_should_client_block(request_rec *r);
439 * Call this in a loop. It will put data into a buffer and return the length
441 * @param r The current request
442 * @param buffer The buffer in which to store the data
443 * @param bufsiz The size of the buffer
444 * @return Number of bytes inserted into the buffer. When done reading, 0
445 * if EOF, or -1 if there was an error
446 * @deffunc long ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz)
448 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
451 * In HTTP/1.1, any method can have a body. However, most GET handlers
452 * wouldn't know what to do with a request body if they received one.
453 * This helper routine tests for and reads any message body in the request,
454 * simply discarding whatever it receives. We need to do this because
455 * failing to read the request body would cause it to be interpreted
456 * as the next request on a persistent connection.
457 * @param r The current request
458 * @return error status if request is malformed, OK otherwise
459 * @deffunc int ap_discard_request_body(request_rec *r)
461 AP_DECLARE(int) ap_discard_request_body(request_rec *r);
465 * Setup the output headers so that the client knows how to authenticate
466 * itself the next time, if an authentication request failed. This function
467 * works for both basic and digest authentication
468 * @param r The current request
469 * @deffunc void ap_note_auth_failure(request_rec *r)
471 AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
474 * Setup the output headers so that the client knows how to authenticate
475 * itself the next time, if an authentication request failed. This function
476 * works only for basic authentication
477 * @param r The current request
478 * @deffunc void ap_note_basic_auth_failure(request_rec *r)
480 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
483 * Setup the output headers so that the client knows how to authenticate
484 * itself the next time, if an authentication request failed. This function
485 * works only for digest authentication
486 * @param r The current request
487 * @deffunc void ap_note_digest_auth_failure(request_rec *r)
489 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
492 * Get the password from the request headers
493 * @param r The current request
494 * @param pw The password as set in the headers
495 * @return 0 (OK) if it set the 'pw' argument (and assured
496 * a correct value in r->user); otherwise it returns
497 * an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
498 * really confused, HTTP_UNAUTHORIZED if no authentication at all
499 * seemed to be in use, or DECLINED if there was authentication but
500 * it wasn't Basic (in which case, the caller should presumably
502 * @deffunc int ap_get_basic_auth_pw(request_rec *r, const char **pw)
504 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
507 * parse_uri: break apart the uri
508 * @warning Side Effects: <pre>
509 * - sets r->args to rest after '?' (or NULL if no '?')
510 * - sets r->uri to request uri (without r->args part)
511 * - sets r->hostname (if not set already) from request (scheme://host:port)
513 * @param r The current request
514 * @param uri The uri to break apart
515 * @deffunc void ap_parse_uri(request_rec *r, const char *uri)
517 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
520 * Get the next line of input for the request
521 * @param s The buffer into which to read the line
522 * @param n The size of the buffer
523 * @param r The request
524 * @param fold Whether to merge continuation lines
525 * @return The length of the line, if successful
526 * n, if the line is too big to fit in the buffer
527 * -1 for miscellaneous errors
528 * @deffunc int ap_method_number_of(const char *method)
530 AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
533 * Get the next line of input for the request
535 * Note: on ASCII boxes, ap_rgetline is a macro which simply calls
536 * ap_rgetline_core to get the line of input.
538 * on EBCDIC boxes, ap_rgetline is a wrapper function which
539 * translates ASCII protocol lines to the local EBCDIC code page
540 * after getting the line of input.
542 * @param s Pointer to the pointer to the buffer into which the line
543 * should be read; if *s==NULL, a buffer of the necessary size
544 * to hold the data will be allocated from the request pool
545 * @param n The size of the buffer
546 * @param read The length of the line.
547 * @param r The request
548 * @param fold Whether to merge continuation lines
549 * @param bb Working brigade to use when reading buckets
550 * @return APR_SUCCESS, if successful
551 * APR_ENOSPC, if the line is too big to fit in the buffer
552 * Other errors where appropriate
554 #if APR_CHARSET_EBCDIC
555 AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
557 request_rec *r, int fold,
558 apr_bucket_brigade *bb);
559 #else /* ASCII box */
560 #define ap_rgetline(s, n, read, r, fold, bb) \
561 ap_rgetline_core((s), (n), (read), (r), (fold), (bb))
563 AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
565 request_rec *r, int fold,
566 apr_bucket_brigade *bb);
569 * Get the method number associated with the given string, assumed to
570 * contain an HTTP method. Returns M_INVALID if not recognized.
571 * @param method A string containing a valid HTTP method
572 * @return The method number
574 AP_DECLARE(int) ap_method_number_of(const char *method);
577 * Get the method name associated with the given internal method
578 * number. Returns NULL if not recognized.
579 * @param p A pool to use for temporary allocations.
580 * @param methnum An integer value corresponding to an internal method number
581 * @return The name corresponding to the method number
583 AP_DECLARE(const char *) ap_method_name_of(apr_pool_t *p, int methnum);
588 * post_read_request --- run right after read_request or internal_redirect,
589 * and not run during any subrequests.
592 * This hook allows modules to affect the request immediately after the request
593 * has been read, and before any other phases have been processes. This allows
594 * modules to make decisions based upon the input header fields
595 * @param r The current request
596 * @return OK or DECLINED
597 * @deffunc ap_run_post_read_request(request_rec *r)
599 AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
602 * This hook allows modules to perform any module-specific logging activities
603 * over and above the normal server things.
604 * @param r The current request
605 * @return OK, DECLINED, or HTTP_...
606 * @deffunc int ap_run_log_transaction(request_rec *r)
608 AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
611 * This hook allows modules to retrieve the http method from a request. This
612 * allows Apache modules to easily extend the methods that Apache understands
613 * @param r The current request
614 * @return The http method from the request
615 * @deffunc const char *ap_run_http_method(const request_rec *r)
617 AP_DECLARE_HOOK(const char *,http_method,(const request_rec *r))
620 * Return the default port from the current request
621 * @param r The current request
622 * @return The current port
623 * @deffunc apr_port_t ap_run_default_port(const request_rec *r)
625 AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
627 typedef struct ap_bucket_error ap_bucket_error;
630 * A bucket referring to an HTTP error
631 * This bucket can be passed down the filter stack to indicate that an
632 * HTTP error occurred while running a filter. In order for this bucket
633 * to be used successfully, it MUST be sent as the first bucket in the
634 * first brigade to be sent from a given filter.
636 struct ap_bucket_error {
637 /** Number of buckets using this memory */
638 apr_bucket_refcount refcount;
639 /** The error code */
641 /** The error string */
645 AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
648 * Determine if a bucket is an error bucket
649 * @param e The bucket to inspect
650 * @return true or false
652 #define AP_BUCKET_IS_ERROR(e) (e->type == &ap_bucket_type_error)
655 * Make the bucket passed in an error bucket
656 * @param b The bucket to make into an error bucket
657 * @param error The HTTP error code to put in the bucket.
658 * @param buf An optional error string to put in the bucket.
659 * @param p A pool to allocate out of.
660 * @return The new bucket, or NULL if allocation failed
661 * @deffunc apr_bucket *ap_bucket_error_make(apr_bucket *b, int error, const char *buf, apr_pool_t *p)
663 AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
664 const char *buf, apr_pool_t *p);
667 * Create a bucket referring to an HTTP error.
668 * @param error The HTTP error code to put in the bucket.
669 * @param buf An optional error string to put in the bucket.
670 * @param p A pool to allocate the error string out of.
671 * @param list The bucket allocator from which to allocate the bucket
672 * @return The new bucket, or NULL if allocation failed
673 * @deffunc apr_bucket *ap_bucket_error_create(int error, const char *buf, apr_pool_t *p, apr_bucket_alloc_t *list)
675 AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error, const char *buf,
677 apr_bucket_alloc_t *list);
679 AP_DECLARE_NONSTD(apr_status_t) ap_byterange_filter(ap_filter_t *f, apr_bucket_brigade *b);
680 AP_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b);
681 AP_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(ap_filter_t *,
682 apr_bucket_brigade *);
683 AP_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(ap_filter_t *f, apr_bucket_brigade *b);
686 * Setting up the protocol fields for subsidiary requests...
687 * Also, a wrapup function to keep the internal accounting straight.
689 AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
690 AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub_r);
696 #endif /* !APACHE_HTTP_PROTOCOL_H */