1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file http_protocol.h
19 * @brief HTTP protocol handling
21 * @defgroup APACHE_CORE_PROTO HTTP Protocol Handling
22 * @ingroup APACHE_CORE
26 #ifndef APACHE_HTTP_PROTOCOL_H
27 #define APACHE_HTTP_PROTOCOL_H
30 #include "apr_portable.h"
32 #include "apr_buckets.h"
33 #include "util_filter.h"
40 * This hook allows modules to insert filters for the current error response
41 * @param r the current request
44 AP_DECLARE_HOOK(void,insert_error_filter,(request_rec *r))
46 /** This is an optimization. We keep a record of the filter_rec that
47 * stores the old_write filter, so that we can avoid strcmp's later.
49 AP_DECLARE_DATA extern ap_filter_rec_t *ap_old_write_func;
52 * Prototypes for routines which either talk directly back to the user,
53 * or control the ones that eventually do.
57 * Read a request and fill in the fields.
58 * @param c The current connection
59 * @return The new request_rec
61 request_rec *ap_read_request(conn_rec *c);
64 * Read the mime-encoded headers.
65 * @param r The current request
67 AP_DECLARE(void) ap_get_mime_headers(request_rec *r);
70 * Optimized version of ap_get_mime_headers() that requires a
71 * temporary brigade to work with
72 * @param r The current request
73 * @param bb temp brigade
75 AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r,
76 apr_bucket_brigade *bb);
78 /* Finish up stuff after a request */
81 * Called at completion of sending the response. It sends the terminating
82 * protocol information.
83 * @param r The current request
85 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
88 * Send error back to client.
89 * @param r The current request
90 * @param recursive_error last arg indicates error status in case we get
91 * an error in the process of trying to deal with an ErrorDocument
92 * to handle some other error. In that case, we print the default
93 * report for the first thing that went wrong, and more briefly report
94 * on the problem with the ErrorDocument.
96 AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
98 /* Set last modified header line from the lastmod date of the associated file.
99 * Also, set content length.
101 * May return an error status, typically HTTP_NOT_MODIFIED (that when the
102 * permit_cache argument is set to one).
106 * Set the content length for this request
107 * @param r The current request
108 * @param length The new content length
110 AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
113 * Set the keepalive status for this request
114 * @param r The current request
115 * @return 1 if keepalive can be set, 0 otherwise
117 AP_DECLARE(int) ap_set_keepalive(request_rec *r);
120 * Return the latest rational time from a request/mtime pair. Mtime is
121 * returned unless it's in the future, in which case we return the current time.
122 * @param r The current request
123 * @param mtime The last modified time
124 * @return the latest rational time.
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 or "", return NULL (do not set content-type).
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 * @param type The content type
137 * @return The content-type
139 AP_DECLARE(const char *) ap_make_content_type(request_rec *r,
143 * Precompile metadata structures used by ap_make_content_type()
144 * @param pool The pool to use for allocations
146 AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool);
149 * Construct an entity tag from the resource information. If it's a real
150 * file, build in some of the file characteristics.
151 * @param r The current request
152 * @param force_weak Force the entity tag to be weak - it could be modified
153 * again in as short an interval.
154 * @return The entity tag
156 AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
159 * Set the E-tag outgoing header
160 * @param r The current request
162 AP_DECLARE(void) ap_set_etag(request_rec *r);
165 * Set the last modified time for the file being sent
166 * @param r The current request
168 AP_DECLARE(void) ap_set_last_modified(request_rec *r);
171 * Implements condition GET rules for HTTP/1.1 specification. This function
172 * inspects the client headers and determines if the response fulfills
173 * the requirements specified.
174 * @param r The current request
175 * @return OK if the response fulfills the condition GET rules, some
176 * other status code otherwise
178 AP_DECLARE(int) ap_meets_conditions(request_rec *r);
180 /* Other ways to send stuff at the client. All of these keep track
181 * of bytes_sent automatically. This indirection is intended to make
182 * it a little more painless to slide things like HTTP-NG packetization
183 * underneath the main body of the code later. In the meantime, it lets
184 * us centralize a bit of accounting (bytes_sent).
186 * These also return the number of bytes written by the call.
187 * They should only be called with a timeout registered, for obvious reaasons.
188 * (Ditto the send_header stuff).
192 * Send an entire file to the client, using sendfile if supported by the
194 * @param fd The file to send.
195 * @param r The current request
196 * @param offset Offset into the file to start sending.
197 * @param length Amount of data to send
198 * @param nbytes Amount of data actually sent
200 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
201 apr_size_t length, apr_size_t *nbytes);
205 * Send an MMAP'ed file to the client
206 * @param mm The MMAP'ed file to send
207 * @param r The current request
208 * @param offset The offset into the MMAP to start sending
209 * @param length The amount of data to send
210 * @return The number of bytes sent
212 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
218 * Register a new request method, and return the offset that will be
219 * associated with that method.
221 * @param p The pool to create registered method numbers from.
222 * @param methname The name of the new method to register.
223 * @return Ab int value representing an offset into a bitmask.
225 AP_DECLARE(int) ap_method_register(apr_pool_t *p, const char *methname);
228 * Initialize the method_registry and allocate memory for it.
230 * @param p Pool to allocate memory for the registry from.
232 AP_DECLARE(void) ap_method_registry_init(apr_pool_t *p);
235 * This is a convenience macro to ease with checking a mask
236 * against a method name.
238 #define AP_METHOD_CHECK_ALLOWED(mask, methname) \
239 ((mask) & (AP_METHOD_BIT << ap_method_number_of((methname))))
242 * Create a new method list with the specified number of preallocated
243 * slots for extension methods.
245 * @param p Pointer to a pool in which the structure should be
247 * @param nelts Number of preallocated extension slots
248 * @return Pointer to the newly created structure.
250 AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
256 * @param dest List to copy to
257 * @param src List to copy from
259 AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
260 ap_method_list_t *src);
263 * Search for an HTTP method name in an ap_method_list_t structure, and
264 * return true if found.
266 * @param method String containing the name of the method to check.
267 * @param l Pointer to a method list, such as r->allowed_methods.
268 * @return 1 if method is in the list, otherwise 0
270 AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
273 * Add an HTTP method name to an ap_method_list_t structure if it isn't
276 * @param method String containing the name of the method to check.
277 * @param l Pointer to a method list, such as r->allowed_methods.
280 AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
283 * Remove an HTTP method name from an ap_method_list_t structure.
285 * @param l Pointer to a method list, such as r->allowed_methods.
286 * @param method String containing the name of the method to remove.
289 AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
293 * Reset a method list to be completely empty.
295 * @param l Pointer to a method list, such as r->allowed_methods.
298 AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
301 * Set the content type for this request (r->content_type).
302 * @param r The current request
303 * @param ct The new content type
304 * @warning This function must be called to set r->content_type in order
305 * for the AddOutputFilterByType directive to work correctly.
307 AP_DECLARE(void) ap_set_content_type(request_rec *r, const char *ct);
309 /* Hmmm... could macrofy these for now, and maybe forever, though the
310 * definitions of the macros would get a whole lot hairier.
314 * Output one character for this request
315 * @param c the character to output
316 * @param r the current request
317 * @return The number of bytes sent
319 AP_DECLARE(int) ap_rputc(int c, request_rec *r);
322 * Write a buffer for the current request
323 * @param buf The buffer to write
324 * @param nbyte The number of bytes to send from the buffer
325 * @param r The current request
326 * @return The number of bytes sent
328 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
331 * Output a string for the current request
332 * @param str The string to output
333 * @param r The current request
334 * @return The number of bytes sent
335 * @note ap_rputs may be implemented as macro or inline function
337 static inline int ap_rputs(const char *str, request_rec *r)
339 return ap_rwrite(str, strlen(str), r);
343 * Write an unspecified number of strings to the request
344 * @param r The current request
345 * @param ... The strings to write
346 * @return The number of bytes sent
348 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...)
349 __attribute__((sentinel));
352 * Output data to the client in a printf format
353 * @param r The current request
354 * @param fmt The format string
355 * @param vlist The arguments to use to fill out the format string
356 * @return The number of bytes sent
358 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
361 * Output data to the client in a printf format
362 * @param r The current request
363 * @param fmt The format string
364 * @param ... The arguments to use to fill out the format string
365 * @return The number of bytes sent
367 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
368 __attribute__((format(printf,2,3)));
371 * Flush all of the data for the current request to the client
372 * @param r The current request
373 * @return The number of bytes sent
375 AP_DECLARE(int) ap_rflush(request_rec *r);
378 * Index used in custom_responses array for a specific error code
379 * (only use outside protocol.c is in getting them configured).
380 * @param status HTTP status code
381 * @return The index of the response
383 AP_DECLARE(int) ap_index_of_response(int status);
386 * Return the Status-Line for a given status code (excluding the
387 * HTTP-Version field). If an invalid or unknown status code is
388 * passed, "500 Internal Server Error" will be returned.
389 * @param status The HTTP status code
390 * @return The Status-Line
392 AP_DECLARE(const char *) ap_get_status_line(int status);
394 /* Reading a block of data from the client connection (e.g., POST arg) */
397 * Setup the client to allow Apache to read the request body.
398 * @param r The current request
399 * @param read_policy How the server should interpret a chunked
400 * transfer-encoding. One of: <pre>
401 * REQUEST_NO_BODY Send 413 error if message has any body
402 * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
403 * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
405 * @return either OK or an error code
407 AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
410 * Determine if the client has sent any data. This also sends a
411 * 100 Continue response to HTTP/1.1 clients, so modules should not be called
412 * until the module is ready to read content.
413 * @warning Never call this function more than once.
414 * @param r The current request
415 * @return 0 if there is no message to read, 1 otherwise
417 AP_DECLARE(int) ap_should_client_block(request_rec *r);
420 * Call this in a loop. It will put data into a buffer and return the length
422 * @param r The current request
423 * @param buffer The buffer in which to store the data
424 * @param bufsiz The size of the buffer
425 * @return Number of bytes inserted into the buffer. When done reading, 0
426 * if EOF, or -1 if there was an error
428 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
431 * In HTTP/1.1, any method can have a body. However, most GET handlers
432 * wouldn't know what to do with a request body if they received one.
433 * This helper routine tests for and reads any message body in the request,
434 * simply discarding whatever it receives. We need to do this because
435 * failing to read the request body would cause it to be interpreted
436 * as the next request on a persistent connection.
437 * @param r The current request
438 * @return error status if request is malformed, OK otherwise
440 AP_DECLARE(int) ap_discard_request_body(request_rec *r);
443 * Setup the output headers so that the client knows how to authenticate
444 * itself the next time, if an authentication request failed.
445 * @param r The current request
447 AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
450 * @deprecated @see ap_note_auth_failure
452 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
455 * @deprecated @see ap_note_auth_failure
457 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
460 * This hook allows modules to add support for a specific auth type to
461 * ap_note_auth_failure
462 * @param r the current request
463 * @param auth_type the configured auth_type
464 * @return OK, DECLINED
466 AP_DECLARE_HOOK(int, note_auth_failure, (request_rec *r, const char *auth_type))
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->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
480 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
483 * parse_uri: break apart the uri
484 * @warning Side Effects:
485 * @li sets r->args to rest after '?' (or NULL if no '?')
486 * @li sets r->uri to request uri (without r->args part)
487 * @li sets r->hostname (if not set already) from request (scheme://host:port)
488 * @param r The current request
489 * @param uri The uri to break apart
491 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
494 * Get the next line of input for the request
495 * @param s The buffer into which to read the line
496 * @param n The size of the buffer
497 * @param r The request
498 * @param fold Whether to merge continuation lines
499 * @return The length of the line, if successful
500 * n, if the line is too big to fit in the buffer
501 * -1 for miscellaneous errors
503 AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
506 * Get the next line of input for the request
508 * Note: on ASCII boxes, ap_rgetline is a macro which simply calls
509 * ap_rgetline_core to get the line of input.
511 * on EBCDIC boxes, ap_rgetline is a wrapper function which
512 * translates ASCII protocol lines to the local EBCDIC code page
513 * after getting the line of input.
515 * @param s Pointer to the pointer to the buffer into which the line
516 * should be read; if *s==NULL, a buffer of the necessary size
517 * to hold the data will be allocated from the request pool
518 * @param n The size of the buffer
519 * @param read The length of the line.
520 * @param r The request
521 * @param fold Whether to merge continuation lines
522 * @param bb Working brigade to use when reading buckets
523 * @return APR_SUCCESS, if successful
524 * APR_ENOSPC, if the line is too big to fit in the buffer
525 * Other errors where appropriate
527 #if APR_CHARSET_EBCDIC
528 AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
530 request_rec *r, int fold,
531 apr_bucket_brigade *bb);
532 #else /* ASCII box */
533 #define ap_rgetline(s, n, read, r, fold, bb) \
534 ap_rgetline_core((s), (n), (read), (r), (fold), (bb))
537 /** @see ap_rgetline */
538 AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
540 request_rec *r, int fold,
541 apr_bucket_brigade *bb);
544 * Get the method number associated with the given string, assumed to
545 * contain an HTTP method. Returns M_INVALID if not recognized.
546 * @param method A string containing a valid HTTP method
547 * @return The method number
549 AP_DECLARE(int) ap_method_number_of(const char *method);
552 * Get the method name associated with the given internal method
553 * number. Returns NULL if not recognized.
554 * @param p A pool to use for temporary allocations.
555 * @param methnum An integer value corresponding to an internal method number
556 * @return The name corresponding to the method number
558 AP_DECLARE(const char *) ap_method_name_of(apr_pool_t *p, int methnum);
563 * pre_read_request --- run right before read_request_line(),
564 * and not run during any subrequests.
567 * This hook allows modules to affect the request or connection immediately before
568 * the request has been read, and before any other phases have been processes.
569 * @param r The current request of the soon-to-be-read request
570 * @param c The connection
573 AP_DECLARE_HOOK(void,pre_read_request,(request_rec *r, conn_rec *c))
576 * post_read_request --- run right after read_request or internal_redirect,
577 * and not run during any subrequests.
580 * This hook allows modules to affect the request immediately after the request
581 * has been read, and before any other phases have been processes. This allows
582 * modules to make decisions based upon the input header fields
583 * @param r The current request
584 * @return OK or DECLINED
586 AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
589 * This hook allows modules to perform any module-specific logging activities
590 * over and above the normal server things.
591 * @param r The current request
592 * @return OK, DECLINED, or HTTP_...
594 AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
597 * This hook allows modules to retrieve the http scheme for a request. This
598 * allows Apache modules to easily extend the schemes that Apache understands
599 * @param r The current request
600 * @return The http scheme from the request
602 AP_DECLARE_HOOK(const char *,http_scheme,(const request_rec *r))
605 * Return the default port from the current request
606 * @param r The current request
607 * @return The current port
609 AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
611 /** @see ap_bucket_type_error */
612 typedef struct ap_bucket_error ap_bucket_error;
615 * @struct ap_bucket_error
616 * @brief A bucket referring to an HTTP error
618 * This bucket can be passed down the filter stack to indicate that an
619 * HTTP error occurred while running a filter. In order for this bucket
620 * to be used successfully, it MUST be sent as the first bucket in the
621 * first brigade to be sent from a given filter.
623 struct ap_bucket_error {
624 /** Number of buckets using this memory */
625 apr_bucket_refcount refcount;
626 /** The error code */
628 /** The error string */
632 /** @see ap_bucket_type_error */
633 AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
636 * Determine if a bucket is an error bucket
637 * @param e The bucket to inspect
638 * @return true or false
640 #define AP_BUCKET_IS_ERROR(e) (e->type == &ap_bucket_type_error)
643 * Make the bucket passed in an error bucket
644 * @param b The bucket to make into an error bucket
645 * @param error The HTTP error code to put in the bucket.
646 * @param buf An optional error string to put in the bucket.
647 * @param p A pool to allocate out of.
648 * @return The new bucket, or NULL if allocation failed
650 AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
651 const char *buf, apr_pool_t *p);
654 * Create a bucket referring to an HTTP error.
655 * @param error The HTTP error code to put in the bucket.
656 * @param buf An optional error string to put in the bucket.
657 * @param p A pool to allocate the error string out of.
658 * @param list The bucket allocator from which to allocate the bucket
659 * @return The new bucket, or NULL if allocation failed
661 AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error, const char *buf,
663 apr_bucket_alloc_t *list);
665 AP_DECLARE_NONSTD(apr_status_t) ap_byterange_filter(ap_filter_t *f, apr_bucket_brigade *b);
666 AP_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b);
667 AP_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(ap_filter_t *,
668 apr_bucket_brigade *);
669 AP_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(ap_filter_t *f, apr_bucket_brigade *b);
672 * Sett up the protocol fields for subsidiary requests
673 * @param rnew New Sub Request
674 * @param r current request
676 AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
679 * A wrapup function to keep the internal accounting straight.
680 * Indicates that there is no more content coming.
681 * @param sub_r Subrequest that is now compete
683 AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub_r);
686 * Send an interim (HTTP 1xx) response immediately.
687 * @param r The request
688 * @param send_headers Whether to send&clear headers in r->headers_out
690 AP_DECLARE(void) ap_send_interim_response(request_rec *r, int send_headers);
696 #endif /* !APACHE_HTTP_PROTOCOL_H */