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 * protocol.c --- routines which directly communicate with the client.
20 * Code originally by Rob McCool; much redone by Robert S. Thau
21 * and the Apache Software Foundation.
25 #include "apr_strings.h"
26 #include "apr_buckets.h"
28 #include "apr_signal.h"
29 #include "apr_strmatch.h"
31 #define APR_WANT_STDIO /* for sscanf */
32 #define APR_WANT_STRFUNC
33 #define APR_WANT_MEMFUNC
36 #include "util_filter.h"
37 #include "ap_config.h"
39 #include "http_config.h"
40 #include "http_core.h"
41 #include "http_protocol.h"
42 #include "http_main.h"
43 #include "http_request.h"
44 #include "http_vhost.h"
45 #include "http_log.h" /* For errors detected in basic auth common
48 #include "util_charset.h"
49 #include "util_ebcdic.h"
50 #include "scoreboard.h"
59 /* we know core's module_index is 0 */
60 #undef APLOG_MODULE_INDEX
61 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
64 APR_HOOK_LINK(pre_read_request)
65 APR_HOOK_LINK(post_read_request)
66 APR_HOOK_LINK(log_transaction)
67 APR_HOOK_LINK(http_scheme)
68 APR_HOOK_LINK(default_port)
69 APR_HOOK_LINK(note_auth_failure)
72 AP_DECLARE_DATA ap_filter_rec_t *ap_old_write_func = NULL;
75 /* Patterns to match in ap_make_content_type() */
76 static const char *needcset[] = {
81 static const apr_strmatch_pattern **needcset_patterns;
82 static const apr_strmatch_pattern *charset_pattern;
84 AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool)
87 for (i = 0; needcset[i]; i++) {
90 needcset_patterns = (const apr_strmatch_pattern **)
91 apr_palloc(pool, (i + 1) * sizeof(apr_strmatch_pattern *));
92 for (i = 0; needcset[i]; i++) {
93 needcset_patterns[i] = apr_strmatch_precompile(pool, needcset[i], 0);
95 needcset_patterns[i] = NULL;
96 charset_pattern = apr_strmatch_precompile(pool, "charset=", 0);
100 * Builds the content-type that should be sent to the client from the
101 * content-type specified. The following rules are followed:
102 * - if type is NULL or "", return NULL (do not set content-type).
103 * - if charset adding is disabled, stop processing and return type.
104 * - then, if there are no parameters on type, add the default charset
107 AP_DECLARE(const char *)ap_make_content_type(request_rec *r, const char *type)
109 const apr_strmatch_pattern **pcset;
110 core_dir_config *conf =
111 (core_dir_config *)ap_get_core_module_config(r->per_dir_config);
112 core_request_config *request_conf;
115 if (!type || *type == '\0') {
119 if (conf->add_default_charset != ADD_DEFAULT_CHARSET_ON) {
123 request_conf = ap_get_core_module_config(r->request_config);
124 if (request_conf->suppress_charset) {
128 type_len = strlen(type);
130 if (apr_strmatch(charset_pattern, type, type_len) != NULL) {
131 /* already has parameter, do nothing */
132 /* XXX we don't check the validity */
136 /* see if it makes sense to add the charset. At present,
137 * we only add it if the Content-type is one of needcset[]
139 for (pcset = needcset_patterns; *pcset ; pcset++) {
140 if (apr_strmatch(*pcset, type, type_len) != NULL) {
141 struct iovec concat[3];
142 concat[0].iov_base = (void *)type;
143 concat[0].iov_len = type_len;
144 concat[1].iov_base = (void *)"; charset=";
145 concat[1].iov_len = sizeof("; charset=") - 1;
146 concat[2].iov_base = (void *)(conf->add_default_charset_name);
147 concat[2].iov_len = strlen(conf->add_default_charset_name);
148 type = apr_pstrcatv(r->pool, concat, 3, NULL);
157 AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t clength)
159 r->clength = clength;
160 apr_table_setn(r->headers_out, "Content-Length",
161 apr_off_t_toa(r->pool, clength));
165 * Return the latest rational time from a request/mtime (modification time)
166 * pair. We return the mtime unless it's in the future, in which case we
167 * return the current time. We use the request time as a reference in order
168 * to limit the number of calls to time(). We don't check for futurosity
169 * unless the mtime is at least as new as the reference.
171 AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime)
175 /* For all static responses, it's almost certain that the file was
176 * last modified before the beginning of the request. So there's
177 * no reason to call time(NULL) again. But if the response has been
178 * created on demand, then it might be newer than the time the request
179 * started. In this event we really have to call time(NULL) again
180 * so that we can give the clients the most accurate Last-Modified. If we
181 * were given a time in the future, we return the current time - the
182 * Last-Modified can't be in the future.
184 now = (mtime < r->request_time) ? r->request_time : apr_time_now();
185 return (mtime > now) ? now : mtime;
188 /* Min # of bytes to allocate when reading a request line */
189 #define MIN_LINE_ALLOC 80
191 /* Get a line of protocol input, including any continuation lines
192 * caused by MIME folding (or broken clients) if fold != 0, and place it
193 * in the buffer s, of size n bytes, without the ending newline.
195 * If s is NULL, ap_rgetline_core will allocate necessary memory from r->pool.
197 * Returns APR_SUCCESS if there are no problems and sets *read to be
198 * the full length of s.
200 * APR_ENOSPC is returned if there is not enough buffer space.
201 * Other errors may be returned on other errors.
203 * The LF is *not* returned in the buffer. Therefore, a *read of 0
204 * indicates that an empty line was read.
206 * Notes: Because the buffer uses 1 char for NUL, the most we can return is
207 * (n - 1) actual characters.
209 * If no LF is detected on the last line due to a dropped connection
210 * or a full buffer, that's considered an error.
212 AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
213 apr_size_t *read, request_rec *r,
214 int fold, apr_bucket_brigade *bb)
218 apr_size_t bytes_handled = 0, current_alloc = 0;
219 char *pos, *last_char = *s;
220 int do_alloc = (*s == NULL), saw_eos = 0;
223 * Initialize last_char as otherwise a random value will be compared
224 * against APR_ASCII_LF at the end of the loop if bb only contains
225 * zero-length buckets.
231 apr_brigade_cleanup(bb);
232 rv = ap_get_brigade(r->input_filters, bb, AP_MODE_GETLINE,
234 if (rv != APR_SUCCESS) {
238 /* Something horribly wrong happened. Someone didn't block! */
239 if (APR_BRIGADE_EMPTY(bb)) {
243 for (e = APR_BRIGADE_FIRST(bb);
244 e != APR_BRIGADE_SENTINEL(bb);
245 e = APR_BUCKET_NEXT(e))
250 /* If we see an EOS, don't bother doing anything more. */
251 if (APR_BUCKET_IS_EOS(e)) {
256 rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
257 if (rv != APR_SUCCESS) {
262 /* no use attempting a zero-byte alloc (hurts when
263 * using --with-efence --enable-pool-debug) or
264 * doing any of the other logic either
269 /* Would this overrun our buffer? If so, we'll die. */
270 if (n < bytes_handled + len) {
271 *read = bytes_handled;
273 /* ensure this string is NUL terminated */
274 if (bytes_handled > 0) {
275 (*s)[bytes_handled-1] = '\0';
284 /* Do we have to handle the allocation ourselves? */
286 /* We'll assume the common case where one bucket is enough. */
289 if (current_alloc < MIN_LINE_ALLOC) {
290 current_alloc = MIN_LINE_ALLOC;
292 *s = apr_palloc(r->pool, current_alloc);
294 else if (bytes_handled + len > current_alloc) {
295 /* Increase the buffer size */
296 apr_size_t new_size = current_alloc * 2;
299 if (bytes_handled + len > new_size) {
300 new_size = (bytes_handled + len) * 2;
303 new_buffer = apr_palloc(r->pool, new_size);
305 /* Copy what we already had. */
306 memcpy(new_buffer, *s, bytes_handled);
307 current_alloc = new_size;
312 /* Just copy the rest of the data to the end of the old buffer. */
313 pos = *s + bytes_handled;
314 memcpy(pos, str, len);
315 last_char = pos + len - 1;
317 /* We've now processed that new data - update accordingly. */
318 bytes_handled += len;
321 /* If we got a full line of input, stop reading */
322 if (last_char && (*last_char == APR_ASCII_LF)) {
327 /* Now NUL-terminate the string at the end of the line;
328 * if the last-but-one character is a CR, terminate there */
329 if (last_char > *s && last_char[-1] == APR_ASCII_CR) {
333 bytes_handled = last_char - *s;
335 /* If we're folding, we have more work to do.
337 * Note that if an EOS was seen, we know we can't have another line.
339 if (fold && bytes_handled && !saw_eos) {
345 /* Clear the temp brigade for this filter read. */
346 apr_brigade_cleanup(bb);
348 /* We only care about the first byte. */
349 rv = ap_get_brigade(r->input_filters, bb, AP_MODE_SPECULATIVE,
351 if (rv != APR_SUCCESS) {
355 if (APR_BRIGADE_EMPTY(bb)) {
359 e = APR_BRIGADE_FIRST(bb);
361 /* If we see an EOS, don't bother doing anything more. */
362 if (APR_BUCKET_IS_EOS(e)) {
366 rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
367 if (rv != APR_SUCCESS) {
368 apr_brigade_cleanup(bb);
372 /* Found one, so call ourselves again to get the next line.
374 * FIXME: If the folding line is completely blank, should we
375 * stop folding? Does that require also looking at the next
378 /* When we call destroy, the buckets are deleted, so save that
379 * one character we need. This simplifies our execution paths
380 * at the cost of one character read.
383 if (c == APR_ASCII_BLANK || c == APR_ASCII_TAB) {
384 /* Do we have enough space? We may be full now. */
385 if (bytes_handled >= n) {
387 /* ensure this string is terminated */
392 apr_size_t next_size, next_len;
395 /* If we're doing the allocations for them, we have to
396 * give ourselves a NULL and copy it on return.
401 /* We're null terminated. */
405 next_size = n - bytes_handled;
407 rv = ap_rgetline_core(&tmp, next_size,
408 &next_len, r, 0, bb);
409 if (rv != APR_SUCCESS) {
413 if (do_alloc && next_len > 0) {
415 apr_size_t new_size = bytes_handled + next_len + 1;
417 /* we need to alloc an extra byte for a null */
418 new_buffer = apr_palloc(r->pool, new_size);
420 /* Copy what we already had. */
421 memcpy(new_buffer, *s, bytes_handled);
423 /* copy the new line, including the trailing null */
424 memcpy(new_buffer + bytes_handled, tmp, next_len + 1);
428 last_char += next_len;
429 bytes_handled += next_len;
432 else { /* next character is not tab or space */
437 *read = bytes_handled;
439 /* PR#43039: We shouldn't accept NULL bytes within the line */
440 if (strlen(*s) < bytes_handled) {
447 #if APR_CHARSET_EBCDIC
448 AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
449 apr_size_t *read, request_rec *r,
450 int fold, apr_bucket_brigade *bb)
452 /* on ASCII boxes, ap_rgetline is a macro which simply invokes
453 * ap_rgetline_core with the same parms
455 * on EBCDIC boxes, each complete http protocol input line needs to be
456 * translated into the code page used by the compiler. Since
457 * ap_rgetline_core uses recursion, we do the translation in a wrapper
458 * function to insure that each input character gets translated only once.
462 rv = ap_rgetline_core(s, n, read, r, fold, bb);
463 if (rv == APR_SUCCESS) {
464 ap_xlate_proto_from_ascii(*s, *read);
470 AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold)
475 apr_bucket_brigade *tmp_bb;
477 tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
478 rv = ap_rgetline(&tmp_s, n, &len, r, fold, tmp_bb);
479 apr_brigade_destroy(tmp_bb);
481 /* Map the out-of-space condition to the old API. */
482 if (rv == APR_ENOSPC) {
486 /* Anything else is just bad. */
487 if (rv != APR_SUCCESS) {
494 /* parse_uri: break apart the uri
496 * - sets r->args to rest after '?' (or NULL if no '?')
497 * - sets r->uri to request uri (without r->args part)
498 * - sets r->hostname (if not set already) from request (scheme://host:port)
500 AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri)
502 int status = HTTP_OK;
504 r->unparsed_uri = apr_pstrdup(r->pool, uri);
506 /* http://issues.apache.org/bugzilla/show_bug.cgi?id=31875
507 * http://issues.apache.org/bugzilla/show_bug.cgi?id=28450
509 * This is not in fact a URI, it's a path. That matters in the
510 * case of a leading double-slash. We need to resolve the issue
511 * by normalising that out before treating it as a URI.
513 while ((uri[0] == '/') && (uri[1] == '/')) {
516 if (r->method_number == M_CONNECT) {
517 status = apr_uri_parse_hostinfo(r->pool, uri, &r->parsed_uri);
520 /* Simple syntax Errors in URLs are trapped by
521 * parse_uri_components().
523 status = apr_uri_parse(r->pool, uri, &r->parsed_uri);
526 if (status == APR_SUCCESS) {
527 /* if it has a scheme we may need to do absoluteURI vhost stuff */
528 if (r->parsed_uri.scheme
529 && !strcasecmp(r->parsed_uri.scheme, ap_http_scheme(r))) {
530 r->hostname = r->parsed_uri.hostname;
532 else if (r->method_number == M_CONNECT) {
533 r->hostname = r->parsed_uri.hostname;
536 r->args = r->parsed_uri.query;
537 r->uri = r->parsed_uri.path ? r->parsed_uri.path
538 : apr_pstrdup(r->pool, "/");
540 #if defined(OS2) || defined(WIN32)
541 /* Handle path translations for OS/2 and plug security hole.
542 * This will prevent "http://www.wherever.com/..\..\/" from
543 * returning a directory for the root drive.
548 for (x = r->uri; (x = strchr(x, '\\')) != NULL; )
551 #endif /* OS2 || WIN32 */
556 r->status = HTTP_BAD_REQUEST; /* set error status */
557 r->uri = apr_pstrdup(r->pool, uri);
561 static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
568 conn_rec *conn = r->connection;
570 int major = 1, minor = 0; /* Assume HTTP/1.0 if non-"HTTP" protocol */
573 int num_blank_lines = 0;
574 int max_blank_lines = r->server->limit_req_fields;
576 if (max_blank_lines <= 0) {
577 max_blank_lines = DEFAULT_LIMIT_REQUEST_FIELDS;
580 /* Read past empty lines until we get a real request line,
581 * a read error, the connection closes (EOF), or we timeout.
583 * We skip empty lines because browsers have to tack a CRLF on to the end
584 * of POSTs to support old CERN webservers. But note that we may not
585 * have flushed any previous response completely to the client yet.
586 * We delay the flush as long as possible so that we can improve
587 * performance for clients that are pipelining requests. If a request
588 * is pipelined then we won't block during the (implicit) read() below.
589 * If the requests aren't pipelined, then the client is still waiting
590 * for the final buffer flush from us, and we will block in the implicit
591 * read(). B_SAFEREAD ensures that the BUFF layer flushes if it will
592 * have to block during a read.
598 /* insure ap_rgetline allocates memory each time thru the loop
599 * if there are empty lines
601 r->the_request = NULL;
602 rv = ap_rgetline(&(r->the_request), (apr_size_t)(r->server->limit_req_line + 2),
605 if (rv != APR_SUCCESS) {
606 r->request_time = apr_time_now();
608 /* ap_rgetline returns APR_ENOSPC if it fills up the
609 * buffer before finding the end-of-line. This is only going to
610 * happen if it exceeds the configured limit for a request-line.
612 if (APR_STATUS_IS_ENOSPC(rv)) {
613 r->status = HTTP_REQUEST_URI_TOO_LARGE;
614 r->proto_num = HTTP_VERSION(1,0);
615 r->protocol = apr_pstrdup(r->pool, "HTTP/1.0");
617 else if (APR_STATUS_IS_TIMEUP(rv)) {
618 r->status = HTTP_REQUEST_TIME_OUT;
620 else if (APR_STATUS_IS_EINVAL(rv)) {
621 r->status = HTTP_BAD_REQUEST;
625 } while ((len <= 0) && (++num_blank_lines < max_blank_lines));
627 if (APLOGrtrace5(r)) {
628 ap_log_rerror(APLOG_MARK, APLOG_TRACE5, 0, r,
629 "Request received from client: %s",
630 ap_escape_logitem(r->pool, r->the_request));
633 /* we've probably got something to do, ignore graceful restart requests */
635 r->request_time = apr_time_now();
637 r->method = ap_getword_white(r->pool, &ll);
640 /* XXX If we want to keep track of the Method, the protocol module should do
641 * it. That support isn't in the scoreboard yet. Hopefully next week
643 ap_update_connection_status(AP_CHILD_THREAD_FROM_ID(conn->id), "Method",
647 uri = ap_getword_white(r->pool, &ll);
649 /* Provide quick information about the request method as soon as known */
651 r->method_number = ap_method_number_of(r->method);
652 if (r->method_number == M_GET && r->method[0] == 'H') {
656 ap_parse_uri(r, uri);
659 * Request-URI = "*" | absoluteURI | abs_path | authority
661 * authority is a special case for CONNECT. If the request is not
662 * using CONNECT, and the parsed URI does not have scheme, and
663 * it does not begin with '/', and it is not '*', then, fail
664 * and give a 400 response. */
665 if (r->method_number != M_CONNECT
666 && !r->parsed_uri.scheme
668 && !(uri[0] == '*' && uri[1] == '\0')) {
669 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00559)
670 "invalid request-URI %s", uri);
673 r->status = HTTP_BAD_REQUEST;
674 r->uri = apr_pstrdup(r->pool, uri);
687 r->protocol = apr_pstrmemdup(r->pool, pro, len);
689 /* XXX ap_update_connection_status(conn->id, "Protocol", r->protocol); */
691 /* Avoid sscanf in the common case */
693 && pro[0] == 'H' && pro[1] == 'T' && pro[2] == 'T' && pro[3] == 'P'
694 && pro[4] == '/' && apr_isdigit(pro[5]) && pro[6] == '.'
695 && apr_isdigit(pro[7])) {
696 r->proto_num = HTTP_VERSION(pro[5] - '0', pro[7] - '0');
698 else if (3 == sscanf(r->protocol, "%4s/%u.%u", http, &major, &minor)
699 && (strcasecmp("http", http) == 0)
700 && (minor < HTTP_VERSION(1, 0)) ) /* don't allow HTTP/0.1000 */
701 r->proto_num = HTTP_VERSION(major, minor);
703 r->proto_num = HTTP_VERSION(1, 0);
708 static int table_do_fn_check_lengths(void *r_, const char *key,
712 if (value == NULL || r->server->limit_req_fieldsize >= strlen(value) )
715 r->status = HTTP_BAD_REQUEST;
716 apr_table_setn(r->notes, "error-notes",
717 apr_pstrcat(r->pool, "Size of a request header field "
718 "after merging exceeds server limit.<br />"
720 ap_escape_html(r->pool, key),
722 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00560) "Request header "
723 "exceeds LimitRequestFieldSize after merging: %s", key);
727 /* get the length of the field name for logging, but no more than 80 bytes */
728 #define LOG_NAME_MAX_LEN 80
729 static int field_name_len(const char *field)
731 const char *end = ap_strchr_c(field, ':');
732 if (end == NULL || end - field > LOG_NAME_MAX_LEN)
733 return LOG_NAME_MAX_LEN;
737 AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb)
739 char *last_field = NULL;
740 apr_size_t last_len = 0;
741 apr_size_t alloc_len = 0;
749 * Read header lines until we get the empty separator line, a read error,
750 * the connection closes (EOF), reach the server limit, or we timeout.
757 rv = ap_rgetline(&field, r->server->limit_req_fieldsize + 2,
760 if (rv != APR_SUCCESS) {
761 if (APR_STATUS_IS_TIMEUP(rv)) {
762 r->status = HTTP_REQUEST_TIME_OUT;
765 r->status = HTTP_BAD_REQUEST;
768 /* ap_rgetline returns APR_ENOSPC if it fills up the buffer before
769 * finding the end-of-line. This is only going to happen if it
770 * exceeds the configured limit for a field size.
772 if (rv == APR_ENOSPC && field) {
773 /* insure ap_escape_html will terminate correctly */
774 field[len - 1] = '\0';
775 apr_table_setn(r->notes, "error-notes",
777 "Size of a request header field "
778 "exceeds server limit.<br />\n"
780 ap_escape_html(r->pool, field),
782 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00561)
783 "Request header exceeds LimitRequestFieldSize: "
784 "%.*s", field_name_len(field), field);
789 if (last_field != NULL) {
790 if ((len > 0) && ((*field == '\t') || *field == ' ')) {
791 /* This line is a continuation of the preceding line(s),
792 * so append it to the line that we've set aside.
793 * Note: this uses a power-of-two allocator to avoid
794 * doing O(n) allocs and using O(n^2) space for
795 * continuations that span many many lines.
797 apr_size_t fold_len = last_len + len + 1; /* trailing null */
799 if (fold_len >= (apr_size_t)(r->server->limit_req_fieldsize)) {
800 r->status = HTTP_BAD_REQUEST;
801 /* report what we have accumulated so far before the
802 * overflow (last_field) as the field with the problem
804 apr_table_setn(r->notes, "error-notes",
806 "Size of a request header field "
808 "exceeds server limit.<br />\n"
810 ap_escape_html(r->pool, last_field),
812 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00562)
813 "Request header exceeds LimitRequestFieldSize "
814 "after folding: %.*s",
815 field_name_len(last_field), last_field);
819 if (fold_len > alloc_len) {
821 alloc_len += alloc_len;
822 if (fold_len > alloc_len) {
823 alloc_len = fold_len;
825 fold_buf = (char *)apr_palloc(r->pool, alloc_len);
826 memcpy(fold_buf, last_field, last_len);
827 last_field = fold_buf;
829 memcpy(last_field + last_len, field, len +1); /* +1 for nul */
833 else /* not a continuation line */ {
835 if (r->server->limit_req_fields
836 && (++fields_read > r->server->limit_req_fields)) {
837 r->status = HTTP_BAD_REQUEST;
838 apr_table_setn(r->notes, "error-notes",
839 "The number of request header fields "
840 "exceeds this server's limit.");
841 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00563)
842 "Number of request headers exceeds "
843 "LimitRequestFields");
847 if (!(value = strchr(last_field, ':'))) { /* Find ':' or */
848 r->status = HTTP_BAD_REQUEST; /* abort bad request */
849 apr_table_setn(r->notes, "error-notes",
851 "Request header field is "
852 "missing ':' separator.<br />\n"
854 ap_escape_html(r->pool,
857 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00564)
858 "Request header field is missing ':' "
859 "separator: %.*s", (int)LOG_NAME_MAX_LEN,
864 tmp_field = value - 1; /* last character of field-name */
866 *value++ = '\0'; /* NUL-terminate at colon */
868 while (*value == ' ' || *value == '\t') {
869 ++value; /* Skip to start of value */
872 /* Strip LWS after field-name: */
873 while (tmp_field > last_field
874 && (*tmp_field == ' ' || *tmp_field == '\t')) {
878 /* Strip LWS after field-value: */
879 tmp_field = last_field + last_len - 1;
880 while (tmp_field > value
881 && (*tmp_field == ' ' || *tmp_field == '\t')) {
885 apr_table_addn(r->headers_in, last_field, value);
887 /* reset the alloc_len so that we'll allocate a new
888 * buffer if we have to do any more folding: we can't
889 * use the previous buffer because its contents are
890 * now part of r->headers_in
894 } /* end if current line is not a continuation starting with tab */
897 /* Found a blank line, stop. */
902 /* Keep track of this line so that we can parse it on
903 * the next loop iteration. (In the folded case, last_field
904 * has been updated already.)
912 /* Combine multiple message-header fields with the same
913 * field-name, following RFC 2616, 4.2.
915 apr_table_compress(r->headers_in, APR_OVERLAP_TABLES_MERGE);
917 /* enforce LimitRequestFieldSize for merged headers */
918 apr_table_do(table_do_fn_check_lengths, r, r->headers_in, NULL);
921 AP_DECLARE(void) ap_get_mime_headers(request_rec *r)
923 apr_bucket_brigade *tmp_bb;
924 tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
925 ap_get_mime_headers_core(r, tmp_bb);
926 apr_brigade_destroy(tmp_bb);
929 request_rec *ap_read_request(conn_rec *conn)
935 apr_bucket_brigade *tmp_bb;
937 apr_interval_time_t cur_timeout;
940 apr_pool_create(&p, conn->pool);
941 apr_pool_tag(p, "request");
942 r = apr_pcalloc(p, sizeof(request_rec));
943 AP_READ_REQUEST_ENTRY((intptr_t)r, (uintptr_t)conn);
945 r->connection = conn;
946 r->server = conn->base_server;
949 r->ap_auth_type = NULL;
951 r->allowed_methods = ap_make_method_list(p, 2);
953 r->headers_in = apr_table_make(r->pool, 25);
954 r->subprocess_env = apr_table_make(r->pool, 25);
955 r->headers_out = apr_table_make(r->pool, 12);
956 r->err_headers_out = apr_table_make(r->pool, 5);
957 r->notes = apr_table_make(r->pool, 5);
959 r->request_config = ap_create_request_config(r->pool);
960 /* Must be set before we run create request hook */
962 r->proto_output_filters = conn->output_filters;
963 r->output_filters = r->proto_output_filters;
964 r->proto_input_filters = conn->input_filters;
965 r->input_filters = r->proto_input_filters;
966 ap_run_create_request(r);
967 r->per_dir_config = r->server->lookup_defaults;
969 r->sent_bodyct = 0; /* bytect isn't for body */
972 r->read_body = REQUEST_NO_BODY;
974 r->status = HTTP_OK; /* Until further notice */
975 r->the_request = NULL;
977 /* Begin by presuming any module can make its own path_info assumptions,
978 * until some module interjects and changes the value.
980 r->used_path_info = AP_REQ_DEFAULT_PATH_INFO;
982 r->useragent_addr = conn->client_addr;
983 r->useragent_ip = conn->client_ip;
985 tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
987 ap_run_pre_read_request(r, conn);
989 /* Get the request... */
990 if (!read_request_line(r, tmp_bb)) {
991 if (r->status == HTTP_REQUEST_URI_TOO_LARGE
992 || r->status == HTTP_BAD_REQUEST) {
993 if (r->status == HTTP_REQUEST_URI_TOO_LARGE) {
994 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00565)
995 "request failed: URI too long (longer than %d)",
996 r->server->limit_req_line);
998 else if (r->method == NULL) {
999 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00566)
1000 "request failed: invalid characters in URI");
1002 ap_send_error_response(r, 0);
1003 ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1004 ap_run_log_transaction(r);
1005 apr_brigade_destroy(tmp_bb);
1008 else if (r->status == HTTP_REQUEST_TIME_OUT) {
1009 ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1010 if (!r->connection->keepalives) {
1011 ap_run_log_transaction(r);
1013 apr_brigade_destroy(tmp_bb);
1017 apr_brigade_destroy(tmp_bb);
1022 /* We may have been in keep_alive_timeout mode, so toggle back
1023 * to the normal timeout mode as we fetch the header lines,
1026 csd = ap_get_conn_socket(conn);
1027 apr_socket_timeout_get(csd, &cur_timeout);
1028 if (cur_timeout != conn->base_server->timeout) {
1029 apr_socket_timeout_set(csd, conn->base_server->timeout);
1030 cur_timeout = conn->base_server->timeout;
1033 if (!r->assbackwards) {
1034 ap_get_mime_headers_core(r, tmp_bb);
1035 if (r->status != HTTP_OK) {
1036 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00567)
1037 "request failed: error reading the headers");
1038 ap_send_error_response(r, 0);
1039 ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1040 ap_run_log_transaction(r);
1041 apr_brigade_destroy(tmp_bb);
1045 if (apr_table_get(r->headers_in, "Transfer-Encoding")
1046 && apr_table_get(r->headers_in, "Content-Length")) {
1047 /* 2616 section 4.4, point 3: "if both Transfer-Encoding
1048 * and Content-Length are received, the latter MUST be
1049 * ignored"; so unset it here to prevent any confusion
1051 apr_table_unset(r->headers_in, "Content-Length");
1055 if (r->header_only) {
1057 * Client asked for headers only with HTTP/0.9, which doesn't send
1058 * headers! Have to dink things just to make sure the error message
1061 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00568)
1062 "client sent invalid HTTP/0.9 request: HEAD %s",
1065 r->status = HTTP_BAD_REQUEST;
1066 ap_send_error_response(r, 0);
1067 ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1068 ap_run_log_transaction(r);
1069 apr_brigade_destroy(tmp_bb);
1074 apr_brigade_destroy(tmp_bb);
1076 /* update what we think the virtual host is based on the headers we've
1077 * now read. may update status.
1079 ap_update_vhost_from_headers(r);
1081 /* Toggle to the Host:-based vhost's timeout mode to fetch the
1082 * request body and send the response body, if needed.
1084 if (cur_timeout != r->server->timeout) {
1085 apr_socket_timeout_set(csd, r->server->timeout);
1086 cur_timeout = r->server->timeout;
1089 /* we may have switched to another server */
1090 r->per_dir_config = r->server->lookup_defaults;
1092 if ((!r->hostname && (r->proto_num >= HTTP_VERSION(1, 1)))
1093 || ((r->proto_num == HTTP_VERSION(1, 1))
1094 && !apr_table_get(r->headers_in, "Host"))) {
1096 * Client sent us an HTTP/1.1 or later request without telling us the
1097 * hostname, either with a full URL or a Host: header. We therefore
1098 * need to (as per the 1.1 spec) send an error. As a special case,
1099 * HTTP/1.1 mentions twice (S9, S14.23) that a request MUST contain
1100 * a Host: header, and the server MUST respond with 400 if it doesn't.
1102 r->status = HTTP_BAD_REQUEST;
1103 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00569)
1104 "client sent HTTP/1.1 request without hostname "
1105 "(see RFC2616 section 14.23): %s", r->uri);
1109 * Add the HTTP_IN filter here to ensure that ap_discard_request_body
1110 * called by ap_die and by ap_send_error_response works correctly on
1111 * status codes that do not cause the connection to be dropped and
1112 * in situations where the connection should be kept alive.
1115 ap_add_input_filter_handle(ap_http_input_filter_handle,
1116 NULL, r, r->connection);
1118 if (r->status != HTTP_OK) {
1119 ap_send_error_response(r, 0);
1120 ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1121 ap_run_log_transaction(r);
1125 if ((access_status = ap_run_post_read_request(r))) {
1126 ap_die(access_status, r);
1127 ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1128 ap_run_log_transaction(r);
1133 if (((expect = apr_table_get(r->headers_in, "Expect")) != NULL)
1134 && (expect[0] != '\0')) {
1136 * The Expect header field was added to HTTP/1.1 after RFC 2068
1137 * as a means to signal when a 100 response is desired and,
1138 * unfortunately, to signal a poor man's mandatory extension that
1139 * the server must understand or return 417 Expectation Failed.
1141 if (strcasecmp(expect, "100-continue") == 0) {
1142 r->expecting_100 = 1;
1145 r->status = HTTP_EXPECTATION_FAILED;
1146 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00570)
1147 "client sent an unrecognized expectation value of "
1148 "Expect: %s", expect);
1149 ap_send_error_response(r, 0);
1150 ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
1151 ap_run_log_transaction(r);
1156 AP_READ_REQUEST_SUCCESS((uintptr_t)r, (char *)r->method, (char *)r->uri, (char *)r->server->defn_name, r->status);
1159 AP_READ_REQUEST_FAILURE((uintptr_t)r);
1163 /* if a request with a body creates a subrequest, remove original request's
1164 * input headers which pertain to the body which has already been read.
1165 * out-of-line helper function for ap_set_sub_req_protocol.
1168 static void strip_headers_request_body(request_rec *rnew)
1170 apr_table_unset(rnew->headers_in, "Content-Encoding");
1171 apr_table_unset(rnew->headers_in, "Content-Language");
1172 apr_table_unset(rnew->headers_in, "Content-Length");
1173 apr_table_unset(rnew->headers_in, "Content-Location");
1174 apr_table_unset(rnew->headers_in, "Content-MD5");
1175 apr_table_unset(rnew->headers_in, "Content-Range");
1176 apr_table_unset(rnew->headers_in, "Content-Type");
1177 apr_table_unset(rnew->headers_in, "Expires");
1178 apr_table_unset(rnew->headers_in, "Last-Modified");
1179 apr_table_unset(rnew->headers_in, "Transfer-Encoding");
1183 * A couple of other functions which initialize some of the fields of
1184 * a request structure, as appropriate for adjuncts of one kind or another
1185 * to a request in progress. Best here, rather than elsewhere, since
1186 * *someone* has to set the protocol-specific fields...
1189 AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew,
1190 const request_rec *r)
1192 rnew->the_request = r->the_request; /* Keep original request-line */
1194 rnew->assbackwards = 1; /* Don't send headers from this. */
1195 rnew->no_local_copy = 1; /* Don't try to send HTTP_NOT_MODIFIED for a
1197 rnew->method = "GET";
1198 rnew->method_number = M_GET;
1199 rnew->protocol = "INCLUDED";
1201 rnew->status = HTTP_OK;
1203 rnew->headers_in = apr_table_copy(rnew->pool, r->headers_in);
1205 /* did the original request have a body? (e.g. POST w/SSI tags)
1206 * if so, make sure the subrequest doesn't inherit body headers
1208 if (!r->kept_body && (apr_table_get(r->headers_in, "Content-Length")
1209 || apr_table_get(r->headers_in, "Transfer-Encoding"))) {
1210 strip_headers_request_body(rnew);
1212 rnew->subprocess_env = apr_table_copy(rnew->pool, r->subprocess_env);
1213 rnew->headers_out = apr_table_make(rnew->pool, 5);
1214 rnew->err_headers_out = apr_table_make(rnew->pool, 5);
1215 rnew->notes = apr_table_make(rnew->pool, 5);
1217 rnew->expecting_100 = r->expecting_100;
1218 rnew->read_length = r->read_length;
1219 rnew->read_body = REQUEST_NO_BODY;
1221 rnew->main = (request_rec *) r;
1224 static void end_output_stream(request_rec *r)
1226 conn_rec *c = r->connection;
1227 apr_bucket_brigade *bb;
1230 bb = apr_brigade_create(r->pool, c->bucket_alloc);
1231 b = apr_bucket_eos_create(c->bucket_alloc);
1232 APR_BRIGADE_INSERT_TAIL(bb, b);
1233 ap_pass_brigade(r->output_filters, bb);
1236 AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub)
1238 /* tell the filter chain there is no more content coming */
1239 if (!sub->eos_sent) {
1240 end_output_stream(sub);
1244 /* finalize_request_protocol is called at completion of sending the
1245 * response. Its sole purpose is to send the terminating protocol
1246 * information for any wrappers around the response message body
1247 * (i.e., transfer encodings). It should have been named finalize_response.
1249 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r)
1251 (void) ap_discard_request_body(r);
1253 /* tell the filter chain there is no more content coming */
1255 end_output_stream(r);
1260 * Support for the Basic authentication protocol, and a bit for Digest.
1262 AP_DECLARE(void) ap_note_auth_failure(request_rec *r)
1264 const char *type = ap_auth_type(r);
1266 ap_run_note_auth_failure(r, type);
1269 ap_log_rerror(APLOG_MARK, APLOG_ERR,
1270 0, r, APLOGNO(00571) "need AuthType to note auth failure: %s", r->uri);
1274 AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r)
1276 ap_note_auth_failure(r);
1279 AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r)
1281 ap_note_auth_failure(r);
1284 AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw)
1286 const char *auth_line = apr_table_get(r->headers_in,
1287 (PROXYREQ_PROXY == r->proxyreq)
1288 ? "Proxy-Authorization"
1292 if (!(t = ap_auth_type(r)) || strcasecmp(t, "Basic"))
1295 if (!ap_auth_name(r)) {
1296 ap_log_rerror(APLOG_MARK, APLOG_ERR,
1297 0, r, APLOGNO(00572) "need AuthName: %s", r->uri);
1298 return HTTP_INTERNAL_SERVER_ERROR;
1302 ap_note_auth_failure(r);
1303 return HTTP_UNAUTHORIZED;
1306 if (strcasecmp(ap_getword(r->pool, &auth_line, ' '), "Basic")) {
1307 /* Client tried to authenticate using wrong auth scheme */
1308 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00573)
1309 "client used wrong authentication scheme: %s", r->uri);
1310 ap_note_auth_failure(r);
1311 return HTTP_UNAUTHORIZED;
1314 while (*auth_line == ' ' || *auth_line == '\t') {
1318 t = ap_pbase64decode(r->pool, auth_line);
1319 r->user = ap_getword_nulls (r->pool, &t, ':');
1320 r->ap_auth_type = "Basic";
1327 struct content_length_ctx {
1328 int data_sent; /* true if the C-L filter has already sent at
1329 * least one bucket on to the next output filter
1332 apr_bucket_brigade *tmpbb;
1335 /* This filter computes the content length, but it also computes the number
1336 * of bytes sent to the client. This means that this filter will always run
1337 * through all of the buckets in all brigades
1339 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(
1341 apr_bucket_brigade *b)
1343 request_rec *r = f->r;
1344 struct content_length_ctx *ctx;
1347 apr_read_type_e eblock = APR_NONBLOCK_READ;
1351 f->ctx = ctx = apr_palloc(r->pool, sizeof(*ctx));
1353 ctx->tmpbb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1356 /* Loop through this set of buckets to compute their length
1358 e = APR_BRIGADE_FIRST(b);
1359 while (e != APR_BRIGADE_SENTINEL(b)) {
1360 if (APR_BUCKET_IS_EOS(e)) {
1364 if (e->length == (apr_size_t)-1) {
1366 const char *ignored;
1369 /* This is probably a pipe bucket. Send everything
1370 * prior to this, and then read the data for this bucket.
1372 rv = apr_bucket_read(e, &ignored, &len, eblock);
1373 if (rv == APR_SUCCESS) {
1374 /* Attempt a nonblocking read next time through */
1375 eblock = APR_NONBLOCK_READ;
1376 r->bytes_sent += len;
1378 else if (APR_STATUS_IS_EAGAIN(rv)) {
1379 /* Output everything prior to this bucket, and then
1380 * do a blocking read on the next batch.
1382 if (e != APR_BRIGADE_FIRST(b)) {
1384 apr_brigade_split_ex(b, e, ctx->tmpbb);
1385 flush = apr_bucket_flush_create(r->connection->bucket_alloc);
1387 APR_BRIGADE_INSERT_TAIL(b, flush);
1388 rv = ap_pass_brigade(f->next, b);
1389 if (rv != APR_SUCCESS || f->c->aborted) {
1392 apr_brigade_cleanup(b);
1393 APR_BRIGADE_CONCAT(b, ctx->tmpbb);
1394 e = APR_BRIGADE_FIRST(b);
1398 eblock = APR_BLOCK_READ;
1402 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00574)
1403 "ap_content_length_filter: "
1404 "apr_bucket_read() failed");
1409 r->bytes_sent += e->length;
1411 e = APR_BUCKET_NEXT(e);
1414 /* If we've now seen the entire response and it's otherwise
1415 * okay to set the C-L in the response header, then do so now.
1417 * We can only set a C-L in the response header if we haven't already
1418 * sent any buckets on to the next output filter for this request.
1420 if (ctx->data_sent == 0 && eos &&
1421 /* don't whack the C-L if it has already been set for a HEAD
1422 * by something like proxy. the brigade only has an EOS bucket
1423 * in this case, making r->bytes_sent zero.
1425 * if r->bytes_sent > 0 we have a (temporary) body whose length may
1426 * have been changed by a filter. the C-L header might not have been
1427 * updated so we do it here. long term it would be cleaner to have
1428 * such filters update or remove the C-L header, and just use it
1431 !(r->header_only && r->bytes_sent == 0 &&
1432 apr_table_get(r->headers_out, "Content-Length"))) {
1433 ap_set_content_length(r, r->bytes_sent);
1437 return ap_pass_brigade(f->next, b);
1441 * Send the body of a response to the client.
1443 AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r,
1444 apr_off_t offset, apr_size_t len,
1447 conn_rec *c = r->connection;
1448 apr_bucket_brigade *bb = NULL;
1451 bb = apr_brigade_create(r->pool, c->bucket_alloc);
1453 apr_brigade_insert_file(bb, fd, offset, len, r->pool);
1455 rv = ap_pass_brigade(r->output_filters, bb);
1456 if (rv != APR_SUCCESS) {
1457 *nbytes = 0; /* no way to tell how many were actually sent */
1467 /* send data from an in-memory buffer */
1468 AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
1471 conn_rec *c = r->connection;
1472 apr_bucket_brigade *bb = NULL;
1475 bb = apr_brigade_create(r->pool, c->bucket_alloc);
1476 b = apr_bucket_mmap_create(mm, offset, length, c->bucket_alloc);
1477 APR_BRIGADE_INSERT_TAIL(bb, b);
1478 ap_pass_brigade(r->output_filters, bb);
1480 return mm->size; /* XXX - change API to report apr_status_t? */
1482 #endif /* APR_HAS_MMAP */
1485 apr_bucket_brigade *bb;
1486 apr_bucket_brigade *tmpbb;
1487 } old_write_filter_ctx;
1489 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(
1490 ap_filter_t *f, apr_bucket_brigade *bb)
1492 old_write_filter_ctx *ctx = f->ctx;
1494 AP_DEBUG_ASSERT(ctx);
1496 if (ctx->bb != NULL) {
1497 /* whatever is coming down the pipe (we don't care), we
1498 * can simply insert our buffered data at the front and
1499 * pass the whole bundle down the chain.
1501 APR_BRIGADE_PREPEND(bb, ctx->bb);
1504 return ap_pass_brigade(f->next, bb);
1507 static ap_filter_t *insert_old_write_filter(request_rec *r)
1510 old_write_filter_ctx *ctx;
1512 /* future optimization: record some flags in the request_rec to
1513 * say whether we've added our filter, and whether it is first.
1516 /* this will typically exit on the first test */
1517 for (f = r->output_filters; f != NULL; f = f->next) {
1518 if (ap_old_write_func == f->frec)
1523 /* our filter hasn't been added yet */
1524 ctx = apr_pcalloc(r->pool, sizeof(*ctx));
1525 ctx->tmpbb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1527 ap_add_output_filter("OLD_WRITE", ctx, r, r->connection);
1528 f = r->output_filters;
1534 static apr_status_t buffer_output(request_rec *r,
1535 const char *str, apr_size_t len)
1537 conn_rec *c = r->connection;
1539 old_write_filter_ctx *ctx;
1544 f = insert_old_write_filter(r);
1547 /* if the first filter is not our buffering filter, then we have to
1548 * deliver the content through the normal filter chain
1550 if (f != r->output_filters) {
1552 apr_bucket *b = apr_bucket_transient_create(str, len, c->bucket_alloc);
1553 APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, b);
1555 rv = ap_pass_brigade(r->output_filters, ctx->tmpbb);
1556 apr_brigade_cleanup(ctx->tmpbb);
1560 if (ctx->bb == NULL) {
1561 ctx->bb = apr_brigade_create(r->pool, c->bucket_alloc);
1564 return ap_fwrite(f->next, ctx->bb, str, len);
1567 AP_DECLARE(int) ap_rputc(int c, request_rec *r)
1571 if (r->connection->aborted) {
1575 if (buffer_output(r, &c2, 1) != APR_SUCCESS)
1581 AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r)
1583 if (r->connection->aborted)
1586 if (buffer_output(r, buf, nbyte) != APR_SUCCESS)
1592 struct ap_vrprintf_data {
1593 apr_vformatter_buff_t vbuff;
1598 static apr_status_t r_flush(apr_vformatter_buff_t *buff)
1600 /* callback function passed to ap_vformatter to be called when
1601 * vformatter needs to write into buff and buff.curpos > buff.endpos */
1603 /* ap_vrprintf_data passed as a apr_vformatter_buff_t, which is then
1604 * "downcast" to an ap_vrprintf_data */
1605 struct ap_vrprintf_data *vd = (struct ap_vrprintf_data*)buff;
1607 if (vd->r->connection->aborted)
1610 /* r_flush is called when vbuff is completely full */
1611 if (buffer_output(vd->r, vd->buff, AP_IOBUFSIZE)) {
1615 /* reset the buffer position */
1616 vd->vbuff.curpos = vd->buff;
1617 vd->vbuff.endpos = vd->buff + AP_IOBUFSIZE;
1622 AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list va)
1625 struct ap_vrprintf_data vd;
1626 char vrprintf_buf[AP_IOBUFSIZE];
1628 vd.vbuff.curpos = vrprintf_buf;
1629 vd.vbuff.endpos = vrprintf_buf + AP_IOBUFSIZE;
1631 vd.buff = vrprintf_buf;
1633 if (r->connection->aborted)
1636 written = apr_vformatter(r_flush, &vd.vbuff, fmt, va);
1638 if (written != -1) {
1639 int n = vd.vbuff.curpos - vrprintf_buf;
1641 /* last call to buffer_output, to finish clearing the buffer */
1642 if (buffer_output(r, vrprintf_buf,n) != APR_SUCCESS)
1651 AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt, ...)
1656 if (r->connection->aborted)
1660 n = ap_vrprintf(r, fmt, va);
1666 AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r, ...)
1671 apr_size_t written = 0;
1673 if (r->connection->aborted)
1676 /* ### TODO: if the total output is large, put all the strings
1677 * ### into a single brigade, rather than flushing each time we
1678 * ### fill the buffer
1682 s = va_arg(va, const char *);
1687 if (buffer_output(r, s, len) != APR_SUCCESS) {
1698 AP_DECLARE(int) ap_rflush(request_rec *r)
1700 conn_rec *c = r->connection;
1703 old_write_filter_ctx *ctx;
1706 f = insert_old_write_filter(r);
1709 b = apr_bucket_flush_create(c->bucket_alloc);
1710 APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, b);
1712 rv = ap_pass_brigade(r->output_filters, ctx->tmpbb);
1713 apr_brigade_cleanup(ctx->tmpbb);
1714 if (rv != APR_SUCCESS)
1721 * This function sets the Last-Modified output header field to the value
1722 * of the mtime field in the request structure - rationalized to keep it from
1723 * being in the future.
1725 AP_DECLARE(void) ap_set_last_modified(request_rec *r)
1727 if (!r->assbackwards) {
1728 apr_time_t mod_time = ap_rationalize_mtime(r, r->mtime);
1729 char *datestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
1731 apr_rfc822_date(datestr, mod_time);
1732 apr_table_setn(r->headers_out, "Last-Modified", datestr);
1736 typedef struct hdr_ptr {
1738 apr_bucket_brigade *bb;
1740 static int send_header(void *data, const char *key, const char *val)
1742 ap_fputstrs(((hdr_ptr*)data)->f, ((hdr_ptr*)data)->bb,
1743 key, ": ", val, CRLF, NULL);
1746 AP_DECLARE(void) ap_send_interim_response(request_rec *r, int send_headers)
1749 char *status_line = NULL;
1752 if (r->proto_num < 1001) {
1753 /* don't send interim response to HTTP/1.0 Client */
1756 if (!ap_is_HTTP_INFO(r->status)) {
1757 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00575)
1758 "Status is %d - not sending interim response", r->status);
1761 if ((r->status == HTTP_CONTINUE) && !r->expecting_100) {
1763 * Don't send 100-Continue when there was no Expect: 100-continue
1764 * in the request headers. For origin servers this is a SHOULD NOT
1765 * for proxies it is a MUST NOT according to RFC 2616 8.2.3
1770 /* if we send an interim response, we're no longer in a state of
1771 * expecting one. Also, this could feasibly be in a subrequest,
1772 * so we need to propagate the fact that we responded.
1774 for (rr = r; rr != NULL; rr = rr->main) {
1775 rr->expecting_100 = 0;
1778 status_line = apr_pstrcat(r->pool, AP_SERVER_PROTOCOL, " ", r->status_line, CRLF, NULL);
1779 ap_xlate_proto_to_ascii(status_line, strlen(status_line));
1781 x.f = r->connection->output_filters;
1782 x.bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1784 ap_fputs(x.f, x.bb, status_line);
1786 apr_table_do(send_header, &x, r->headers_out, NULL);
1787 apr_table_clear(r->headers_out);
1789 ap_fputs(x.f, x.bb, CRLF_ASCII);
1790 ap_fflush(x.f, x.bb);
1791 apr_brigade_destroy(x.bb);
1795 AP_IMPLEMENT_HOOK_VOID(pre_read_request,
1796 (request_rec *r, conn_rec *c),
1798 AP_IMPLEMENT_HOOK_RUN_ALL(int,post_read_request,
1799 (request_rec *r), (r), OK, DECLINED)
1800 AP_IMPLEMENT_HOOK_RUN_ALL(int,log_transaction,
1801 (request_rec *r), (r), OK, DECLINED)
1802 AP_IMPLEMENT_HOOK_RUN_FIRST(const char *,http_scheme,
1803 (const request_rec *r), (r), NULL)
1804 AP_IMPLEMENT_HOOK_RUN_FIRST(unsigned short,default_port,
1805 (const request_rec *r), (r), 0)
1806 AP_IMPLEMENT_HOOK_RUN_FIRST(int, note_auth_failure,
1807 (request_rec *r, const char *auth_type),
1808 (r, auth_type), DECLINED)