protocol.c revision b2c17021012bc4f7cd8e374c6bd72b926737320f
278N/A/* Licensed to the Apache Software Foundation (ASF) under one or more 278N/A * contributor license agreements. See the NOTICE file distributed with 278N/A * this work for additional information regarding copyright ownership. 278N/A * The ASF licenses this file to You under the Apache License, Version 2.0 278N/A * (the "License"); you may not use this file except in compliance with 278N/A * the License. You may obtain a copy of the License at 278N/A * Unless required by applicable law or agreed to in writing, software 278N/A * distributed under the License is distributed on an "AS IS" BASIS, 278N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 278N/A * See the License for the specific language governing permissions and 278N/A * limitations under the License. 278N/A * Code originally by Rob McCool; much redone by Robert S. Thau 5680N/A * and the Apache Software Foundation. /* we know core's module_index is 0 */ /* Patterns to match in ap_make_content_type() */ * Builds the content-type that should be sent to the client from the * content-type specified. The following rules are followed: * - if type is NULL or "", return NULL (do not set content-type). * - if charset adding is disabled, stop processing and return type. * - then, if there are no parameters on type, add the default charset /* already has parameter, do nothing */ /* XXX we don't check the validity */ /* see if it makes sense to add the charset. At present, * we only add it if the Content-type is one of needcset[] * Return the latest rational time from a request/mtime (modification time) * pair. We return the mtime unless it's in the future, in which case we * return the current time. We use the request time as a reference in order * to limit the number of calls to time(). We don't check for futurosity * unless the mtime is at least as new as the reference. /* For all static responses, it's almost certain that the file was * last modified before the beginning of the request. So there's * no reason to call time(NULL) again. But if the response has been * created on demand, then it might be newer than the time the request * started. In this event we really have to call time(NULL) again * so that we can give the clients the most accurate Last-Modified. If we * were given a time in the future, we return the current time - the * Last-Modified can't be in the future. /* Get a line of protocol input, including any continuation lines * caused by MIME folding (or broken clients) if fold != 0, and place it * in the buffer s, of size n bytes, without the ending newline. * Pulls from r->proto_input_filters instead of r->input_filters for * stricter protocol adherence and better input filter behavior during * chunked trailer processing (for http). * If s is NULL, ap_rgetline_core will allocate necessary memory from r->pool. * Returns APR_SUCCESS if there are no problems and sets *read to be * APR_ENOSPC is returned if there is not enough buffer space. * Other errors may be returned on other errors. * The LF is *not* returned in the buffer. Therefore, a *read of 0 * indicates that an empty line was read. * Notes: Because the buffer uses 1 char for NUL, the most we can return is * (n - 1) actual characters. * If no LF is detected on the last line due to a dropped connection * or a full buffer, that's considered an error. * Initialize last_char as otherwise a random value will be compared * against APR_ASCII_LF at the end of the loop if bb only contains /* Something horribly wrong happened. Someone didn't block! */ /* If we see an EOS, don't bother doing anything more. */ /* no use attempting a zero-byte alloc (hurts when * using --with-efence --enable-pool-debug) or * doing any of the other logic either /* Would this overrun our buffer? If so, we'll die. */ /* ensure this string is NUL terminated */ /* Do we have to handle the allocation ourselves? */ /* We'll assume the common case where one bucket is enough. */ /* Increase the buffer size */ /* Copy what we already had. */ /* Just copy the rest of the data to the end of the old buffer. */ /* We've now processed that new data - update accordingly. */ /* If we got a full line of input, stop reading */ /* Now NUL-terminate the string at the end of the line; * if the last-but-one character is a CR, terminate there */ /* If we're folding, we have more work to do. * Note that if an EOS was seen, we know we can't have another line. /* Clear the temp brigade for this filter read. */ /* We only care about the first byte. */ /* If we see an EOS, don't bother doing anything more. */ /* Found one, so call ourselves again to get the next line. * FIXME: If the folding line is completely blank, should we * stop folding? Does that require also looking at the next /* When we call destroy, the buckets are deleted, so save that * one character we need. This simplifies our execution paths * at the cost of one character read. /* Do we have enough space? We may be full now. */ /* ensure this string is terminated */ /* If we're doing the allocations for them, we have to * give ourselves a NULL and copy it on return. /* We're null terminated. */ /* we need to alloc an extra byte for a null */ /* Copy what we already had. */ /* copy the new line, including the trailing null */ else {
/* next character is not tab or space */ /* PR#43039: We shouldn't accept NULL bytes within the line */ /* on ASCII boxes, ap_rgetline is a macro which simply invokes * ap_rgetline_core with the same parms * on EBCDIC boxes, each complete http protocol input line needs to be * translated into the code page used by the compiler. Since * ap_rgetline_core uses recursion, we do the translation in a wrapper * function to ensure that each input character gets translated only once. /* Map the out-of-space condition to the old API. */ /* Anything else is just bad. */ /* parse_uri: break apart the uri * - sets r->args to rest after '?' (or NULL if no '?') * - sets r->uri to request uri (without r->args part) * - sets r->hostname (if not set already) from request (scheme://host:port) * This is not in fact a URI, it's a path. That matters in the * case of a leading double-slash. We need to resolve the issue * by normalising that out before treating it as a URI. while ((
uri[0] ==
'/') && (
uri[
1] ==
'/')) {
/* if it has a scheme we may need to do absoluteURI vhost stuff */ /* Handle path translations for OS/2 and plug security hole. * returning a directory for the root drive. #
endif /* OS2 || WIN32 */ int major =
1,
minor = 0;
/* Assume HTTP/1.0 if non-"HTTP" protocol */ /* Read past empty lines until we get a real request line, * a read error, the connection closes (EOF), or we timeout. * We skip empty lines because browsers have to tack a CRLF on to the end * of POSTs to support old CERN webservers. But note that we may not * have flushed any previous response completely to the client yet. * We delay the flush as long as possible so that we can improve * performance for clients that are pipelining requests. If a request * is pipelined then we won't block during the (implicit) read() below. * If the requests aren't pipelined, then the client is still waiting * for the final buffer flush from us, and we will block in the implicit * read(). B_SAFEREAD ensures that the BUFF layer flushes if it will * have to block during a read. /* ensure ap_rgetline allocates memory each time thru the loop * if there are empty lines /* ap_rgetline returns APR_ENOSPC if it fills up the * buffer before finding the end-of-line. This is only going to * happen if it exceeds the configured limit for a request-line. "Request received from client: %s",
/* Provide quick information about the request method as soon as known */ /* If we deny 0.9, send error message with 1.x */ "HTTP/0.9 denied by server configuration");
/* Avoid sscanf in the common case */ &&
pro[0] ==
'H' &&
pro[
1] ==
'T' &&
pro[
2] ==
'T' &&
pro[
3] ==
'P' "Request line must not contain control characters");
/* RFC3986 3.5: no fragment */ "URI must not contain a fragment");
"Invalid HTTP method string: %s", r->
method);
"HTTP/0.x does not take a protocol");
"after merging exceeds server limit.<br />" "exceeds LimitRequestFieldSize after merging: %s",
key);
/* get the length of the field name for logging, but no more than 80 bytes */ * Read header lines until we get the empty separator line, a read error, * the connection closes (EOF), reach the server limit, or we timeout. /* ap_rgetline returns APR_ENOSPC if it fills up the buffer before * finding the end-of-line. This is only going to happen if it * exceeds the configured limit for a field size. /* ensure ap_escape_html will terminate correctly */ "Size of a request header field " "exceeds server limit.<br />\n" "Request header exceeds LimitRequestFieldSize%s" /* This line is a continuation of the preceding line(s), * so append it to the line that we've set aside. * Note: this uses a power-of-two allocator to avoid * doing O(n) allocs and using O(n^2) space for * continuations that span many many lines. /* report what we have accumulated so far before the * overflow (last_field) as the field with the problem "Size of a request header field " "exceeds server limit.<br />\n" "Request header exceeds LimitRequestFieldSize " else /* not a continuation line */ {
"The number of request header fields " "exceeds this server's limit.");
"Number of request headers exceeds " "Request header field is " "missing ':' separator.<br />\n" "Request header field is missing ':' " *
value++ =
'\0';
/* NUL-terminate at colon */ ++
value;
/* Skip to start of value */ /* Strip LWS after field-name: */ /* Strip LWS after field-value: */ "Empty request header field name not allowed");
"[HTTP strict] Request header field name contains " "control character: %.*s",
"Request header field '%.*s' contains " /* reset the alloc_len so that we'll allocate a new * buffer if we have to do any more folding: we can't * use the previous buffer because its contents are * now part of r->headers_in }
/* end if current line is not a continuation starting with tab */ /* Found a blank line, stop. */ /* Keep track of this line so that we can parse it on * the next loop iteration. (In the folded case, last_field * has been updated already.) /* Combine multiple message-header fields with the same * field-name, following RFC 2616, 4.2. /* enforce LimitRequestFieldSize for merged headers */ /* Must be set before we run create request hook */ /* Begin by presuming any module can make its own path_info assumptions, * until some module interjects and changes the value. "request failed: client's request-line exceeds LimitRequestLine (longer than %d)",
"request failed: invalid characters in URI");
/* We may have been in keep_alive_timeout mode, so toggle back * to the normal timeout mode as we fetch the header lines, "request failed: error reading the headers");
* Section 3.3.3.3: "If a Transfer-Encoding header field is * present in a request and the chunked transfer coding is not * the final encoding ...; the server MUST respond with the 400 * (Bad Request) status code and then close the connection". "client sent unknown Transfer-Encoding " * Section 3.3.3.3: "If a message is received with both a * Transfer-Encoding and a Content-Length header field, the * Transfer-Encoding overrides the Content-Length. ... A sender * MUST remove the received Content-Length field". * Client asked for headers only with HTTP/0.9, which doesn't send * headers! Have to dink things just to make sure the error message "client sent invalid HTTP/0.9 request: HEAD %s",
/* update what we think the virtual host is based on the headers we've * now read. may update status. /* Toggle to the Host:-based vhost's timeout mode to fetch the * request body and send the response body, if needed. /* we may have switched to another server */ * Client sent us an HTTP/1.1 or later request without telling us the * hostname, either with a full URL or a Host: header. We therefore * need to (as per the 1.1 spec) send an error. As a special case, * HTTP/1.1 mentions twice (S9, S14.23) that a request MUST contain * a Host: header, and the server MUST respond with 400 if it doesn't. "client sent HTTP/1.1 request without hostname " "(see RFC2616 section 14.23): %s", r->
uri);
* Add the HTTP_IN filter here to ensure that ap_discard_request_body * called by ap_die and by ap_send_error_response works correctly on * status codes that do not cause the connection to be dropped and * in situations where the connection should be kept alive. * The Expect header field was added to HTTP/1.1 after RFC 2068 * as a means to signal when a 100 response is desired and, * unfortunately, to signal a poor man's mandatory extension that * the server must understand or return 417 Expectation Failed. "client sent an unrecognized expectation value " "client sent an unrecognized expectation value " "of Expect (not fatal): %s",
expect);
/* if a request with a body creates a subrequest, remove original request's * input headers which pertain to the body which has already been read. * out-of-line helper function for ap_set_sub_req_protocol. * A couple of other functions which initialize some of the fields of * a request structure, as appropriate for adjuncts of one kind or another * to a request in progress. Best here, rather than elsewhere, since * *someone* has to set the protocol-specific fields... /* did the original request have a body? (e.g. POST w/SSI tags) * if so, make sure the subrequest doesn't inherit body headers /* tell the filter chain there is no more content coming */ /* finalize_request_protocol is called at completion of sending the * response. Its sole purpose is to send the terminating protocol * information for any wrappers around the response message body * (i.e., transfer encodings). It should have been named finalize_response. /* tell the filter chain there is no more content coming */ * Support for the Basic authentication protocol, and a bit for Digest. "need AuthType to note auth failure: %s", r->
uri);
"need AuthName: %s", r->
uri);
/* Client tried to authenticate using wrong auth scheme */ "client used wrong authentication scheme: %s", r->
uri);
int data_sent;
/* true if the C-L filter has already sent at * least one bucket on to the next output filter /* This filter computes the content length, but it also computes the number * of bytes sent to the client. This means that this filter will always run * through all of the buckets in all brigades /* Loop through this set of buckets to compute their length /* This is probably a pipe bucket. Send everything * prior to this, and then read the data for this bucket. /* Attempt a nonblocking read next time through */ /* Output everything prior to this bucket, and then * do a blocking read on the next batch. "ap_content_length_filter: " "apr_bucket_read() failed");
/* If we've now seen the entire response and it's otherwise * okay to set the C-L in the response header, then do so now. * We can only set a C-L in the response header if we haven't already * sent any buckets on to the next output filter for this request. /* don't whack the C-L if it has already been set for a HEAD * by something like proxy. the brigade only has an EOS bucket * in this case, making r->bytes_sent zero. * if r->bytes_sent > 0 we have a (temporary) body whose length may * have been changed by a filter. the C-L header might not have been * updated so we do it here. long term it would be cleaner to have * such filters update or remove the C-L header, and just use it * Send the body of a response to the client. *
nbytes = 0;
/* no way to tell how many were actually sent *//* send data from an in-memory buffer */ return mm->
size;
/* XXX - change API to report apr_status_t? */ #
endif /* APR_HAS_MMAP */ /* whatever is coming down the pipe (we don't care), we * can simply insert our buffered data at the front and * pass the whole bundle down the chain. /* future optimization: record some flags in the request_rec to * say whether we've added our filter, and whether it is first. /* this will typically exit on the first test */ /* our filter hasn't been added yet */ /* if the first filter is not our buffering filter, then we have to * deliver the content through the normal filter chain /* Flush callback for apr_vformatter; returns -1 on error. */ /* callback function passed to ap_vformatter to be called when * vformatter needs to write into buff and buff.curpos > buff.endpos */ /* ap_vrprintf_data passed as a apr_vformatter_buff_t, which is then * "downcast" to an ap_vrprintf_data */ /* r_flush is called when vbuff is completely full */ /* reset the buffer position */ /* last call to buffer_output, to finish clearing the buffer */ /* ### TODO: if the total output is large, put all the strings * ### into a single brigade, rather than flushing each time we * This function sets the Last-Modified output header field to the value * of the mtime field in the request structure - rationalized to keep it from /* don't send interim response to HTTP/1.0 Client */ "Status is %d - not sending interim response", r->
status);
* Don't send 100-Continue when there was no Expect: 100-continue * in the request headers. For origin servers this is a SHOULD NOT * for proxies it is a MUST NOT according to RFC 2616 8.2.3 /* if we send an interim response, we're no longer in a state of * expecting one. Also, this could feasibly be in a subrequest, * so we need to propagate the fact that we responded.