protocol.c revision ebe7da316894e2b93b4a905fccd2496d0ed1bc78
* the documentation and/or other materials provided with the * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * ==================================================================== * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. * Code originally by Rob McCool; much redone by Robert S. Thau * and the Apache Software Foundation. #
include "http_log.h" /* For errors detected in basic auth common * 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, type is set to ap_default_type(r) * - 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. * If s is NULL, ap_rgetline 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. /* 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. */ /* Do we have to handle the allocation ourselves? */ /* We'll assume the common case where one bucket is enough. */ /* We resize to the next power of 2. */ /* 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. */ /* We no longer need the returned brigade. */ /* We likely aborted early before reading anything or we read no * data. Technically, this might be success condition. But, * probably means something is horribly wrong. For now, we'll * treat this as APR_SUCCESS, but it may be worth re-examining. /* If we didn't get a full line of input, try again. */ /* Do we have enough space? We may be full now. */ /* If we're doing the allocations for them, we have to * give ourselves a NULL and copy it on return. /* We're not null terminated yet. */ /* Again we need to alloc an extra two bytes for LF, null */ /* Copy what we already had. */ /* We now go backwards over any CR (if present) or white spaces. * Trim any extra trailing spaces or tabs except for the first * space or tab at the beginning of a blank string. This makes * it much easier to check field values for exact matches, and * saves memory as well. Terminate string at end of line. /* Trim any extra trailing spaces or tabs except for the first * space or tab at the beginning of a blank string. This makes * it much easier to check field values for exact matches, and while (
pos > ((*s) +
1) &&
/* Since we want to remove the LF from the line, we'll go ahead * and set this last character to be the term NULL and reset * bytes_handled accordingly. /* 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. /* Create a brigade for this filter read. */ /* We only care about the first byte. */ /* If we see an EOS, don't bother doing anything more. */ /* 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. /* We no longer need the returned brigade. */ /* 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 /* Do we have enough space? We may be full now. */ /* If we're doing the allocations for them, we have to * give ourselves a NULL and copy it on return. /* We're null terminated. */ /* Again we need to alloc an extra two bytes for LF, null */ /* Copy what we already had. */ /* FIXME: Can we optimize this at all by placing it a different layer? */ /* 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) /* Simple syntax Errors in URLs are trapped by parse_uri_components(). */ /* 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. /* insure ap_rgetline allocates memory each time thru the loop * if there are empty lines /* we've probably got something to do, ignore graceful restart requests */ /* XXX If we want to keep track of the Method, the protocol module should do * it. That support isn't in the scoreboard yet. Hopefully next week /* Provide quick information about the request method as soon as known */ /* ap_getline returns (size of max buffer - 1) 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. /* XXX ap_update_connection_status(conn->id, "Protocol", r->protocol); */ /* Avoid sscanf in the common case */ pro[0] ==
'H' &&
pro[
1] ==
'T' &&
pro[
2] ==
'T' &&
pro[
3] ==
'P' &&
/* We'll use apr_table_overlap later to merge these into r->headers_in. */ * 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. "Size of a request header field " "exceeds server limit.<br />\n" /* Found a blank line, stop. */ "The number of request header fields exceeds " "Request header field is missing " "colon separator.<br />\n" ++
value;
/* Skip to start of value */ /* Must be set before we run create request hook */ "request failed: URI too long");
"request failed: error reading the headers");
* 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. /* 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);
* 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 of " * 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... /* 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. 0, r,
"need AuthType to note auth failure: %s", r->
uri);
/* if there is no AuthType configure or it is something other than * Basic, let ap_note_auth_failure() deal with it 0, r,
"need AuthName: %s", r->
uri);
/* Client tried to authenticate using wrong auth scheme */ "client used wrong authentication scheme: %s", r->
uri);
/* Note that this allocation has to be made from r->connection->pool * because it has the lifetime of the connection. The other allocations * are temporary and can be tossed away any time. /* 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 if (!
ctx) {
/* first time through */ /* Humm, is this check the best it can be? * - protocol >= HTTP/1.1 implies support for chunking * - non-keepalive implies the end of byte stream will be signaled * In both cases, we can send bytes to the client w/o needing to * compute content-length. * We should be able to force connection close from this filter * when we see we are buffering too much. /* If we've accumulated more than 4xAP_MIN_BYTES_TO_WRITE and * the client supports chunked encoding, send what we have * and come back for more. if (e->
length == -
1) {
/* if length unknown */ /* Attempt a nonblocking read next time through */ /* Make the next read blocking. If the client supports chunked * encoding, flush the filter stack to the network. "ap_content_length_filter: apr_bucket_read() failed");
/* save the brigade; we can't pass any data to the next * filter until we have the entire content length * 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 */ /* grab the context from our filter */ /* 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 */ /* tack on null terminator on remaining string */ /* 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