protocol.c revision 08cb74ca432a8c24e39f17dedce527e6a47b8001
/* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* http_protocol.c --- routines which directly communicate with the client.
*
* Code originally by Rob McCool; much redone by Robert S. Thau
* and the Apache Software Foundation.
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_buckets.h"
#include "apr_lib.h"
#include "apr_signal.h"
#include "apr_strmatch.h"
#define APR_WANT_STDIO /* for sscanf */
#define APR_WANT_STRFUNC
#define APR_WANT_MEMFUNC
#include "apr_want.h"
#define CORE_PRIVATE
#include "util_filter.h"
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_main.h"
#include "http_request.h"
#include "http_vhost.h"
#include "http_log.h" /* For errors detected in basic auth common
* support code... */
#include "mod_core.h"
#include "util_charset.h"
#include "util_ebcdic.h"
#include "scoreboard.h"
#include <stdarg.h>
#endif
#include <unistd.h>
#endif
)
/* Patterns to match in ap_make_content_type() */
static const char *needcset[] = {
};
static const apr_strmatch_pattern **needcset_patterns;
static const apr_strmatch_pattern *charset_pattern;
{
int i;
for (i = 0; needcset[i]; i++) {
continue;
}
needcset_patterns = (const apr_strmatch_pattern **)
for (i = 0; needcset[i]; i++) {
}
needcset_patterns[i] = NULL;
}
/*
* 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
* - return type
*/
{
const apr_strmatch_pattern **pcset;
&core_module);
if (!type) {
type = ap_default_type(r);
}
return type;
}
/* already has parameter, do nothing */
/* XXX we don't check the validity */
;
}
else {
/* see if it makes sense to add the charset. At present,
* we only add it if the Content-type is one of needcset[]
*/
break;
}
}
}
return type;
}
{
}
/*
* 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.
*/
}
/* Min # of bytes to allocate when reading a request line */
#define MIN_LINE_ALLOC 80
/* 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_core will allocate necessary memory from r->pool.
*
* Returns APR_SUCCESS if there are no problems and sets *read to be
* the full length of s.
*
* 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.
*/
{
apr_bucket *e;
for (;;) {
APR_BLOCK_READ, 0);
if (rv != APR_SUCCESS) {
return rv;
}
/* Something horribly wrong happened. Someone didn't block! */
if (APR_BRIGADE_EMPTY(bb)) {
return APR_EGENERAL;
}
for (e = APR_BRIGADE_FIRST(bb);
e != APR_BRIGADE_SENTINEL(bb);
e = APR_BUCKET_NEXT(e))
{
const char *str;
/* If we see an EOS, don't bother doing anything more. */
if (APR_BUCKET_IS_EOS(e)) {
saw_eos = 1;
break;
}
if (rv != APR_SUCCESS) {
return rv;
}
if (len == 0) {
/* no use attempting a zero-byte alloc (hurts when
* using --with-efence --enable-pool-debug) or
* doing any of the other logic either
*/
continue;
}
/* Would this overrun our buffer? If so, we'll die. */
if (n < bytes_handled + len) {
*read = bytes_handled;
if (*s) {
/* ensure this string is NUL terminated */
if (bytes_handled > 0) {
}
else {
(*s)[0] = '\0';
}
}
return APR_ENOSPC;
}
/* Do we have to handle the allocation ourselves? */
if (do_alloc) {
/* We'll assume the common case where one bucket is enough. */
if (!*s) {
current_alloc = len;
if (current_alloc < MIN_LINE_ALLOC) {
}
}
/* Increase the buffer size */
char *new_buffer;
}
/* Copy what we already had. */
*s = new_buffer;
}
}
/* Just copy the rest of the data to the end of the old buffer. */
pos = *s + bytes_handled;
/* We've now processed that new data - update accordingly. */
bytes_handled += len;
}
/* If we got a full line of input, stop reading */
break;
}
}
/* Now NUL-terminate the string at the end of the line;
* if the last-but-one character is a CR, terminate there */
last_char--;
}
*last_char = '\0';
bytes_handled = last_char - *s;
/* 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.
*/
for (;;) {
const char *str;
char c;
/* Clear the temp brigade for this filter read. */
/* We only care about the first byte. */
APR_BLOCK_READ, 1);
if (rv != APR_SUCCESS) {
return rv;
}
if (APR_BRIGADE_EMPTY(bb)) {
break;
}
e = APR_BRIGADE_FIRST(bb);
/* If we see an EOS, don't bother doing anything more. */
if (APR_BUCKET_IS_EOS(e)) {
break;
}
if (rv != APR_SUCCESS) {
return rv;
}
/* 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
* char?
*/
/* 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.
*/
c = *str;
if (c == APR_ASCII_BLANK || c == APR_ASCII_TAB) {
/* Do we have enough space? We may be full now. */
if (bytes_handled >= n) {
*read = n;
/* ensure this string is terminated */
(*s)[n-1] = '\0';
return APR_ENOSPC;
}
else {
char *tmp;
/* If we're doing the allocations for them, we have to
* give ourselves a NULL and copy it on return.
*/
if (do_alloc) {
} else {
/* We're null terminated. */
}
next_size = n - bytes_handled;
if (rv != APR_SUCCESS) {
return rv;
}
char *new_buffer;
/* we need to alloc an extra byte for a null */
/* Copy what we already had. */
/* copy the new line, including the trailing null */
*s = new_buffer;
}
}
}
else { /* next character is not tab or space */
break;
}
}
}
*read = bytes_handled;
return APR_SUCCESS;
}
{
/* 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 insure that each input character gets translated only once.
*/
if (rv == APR_SUCCESS) {
ap_xlate_proto_from_ascii(*s, *read);
}
return rv;
}
#endif
{
char *tmp_s = s;
/* Map the out-of-space condition to the old API. */
if (rv == APR_ENOSPC) {
return n;
}
/* Anything else is just bad. */
if (rv != APR_SUCCESS) {
return -1;
}
return (int)len;
}
/* parse_uri: break apart the uri
* Side Effects:
* - 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.
*/
++uri ;
}
if (r->method_number == M_CONNECT) {
}
else {
/* Simple syntax Errors in URLs are trapped by
* parse_uri_components().
*/
}
if (status == APR_SUCCESS) {
/* if it has a scheme we may need to do absoluteURI vhost stuff */
if (r->parsed_uri.scheme
}
else if (r->method_number == M_CONNECT) {
}
/* Handle path translations for OS/2 and plug security hole.
* This will prevent "http://www.wherever.com/..\..\/" from
* returning a directory for the root drive.
*/
{
char *x;
*x = '/';
}
#endif /* OS2 || WIN32 */
}
else {
}
}
{
const char *ll;
const char *uri;
const char *pro;
#if 0
#endif
char http[5];
int num_blank_lines = 0;
if (max_blank_lines <= 0) {
}
/* 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.
*/
do {
/* insure ap_rgetline allocates memory each time thru the loop
* if there are empty lines
*/
r->the_request = NULL;
if (rv != APR_SUCCESS) {
r->request_time = apr_time_now();
/* 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.
*/
if (rv == APR_ENOSPC) {
}
"Error while reading request line. (client didn't send a request?)");
}
return 0;
}
/* we've probably got something to do, ignore graceful restart requests */
r->request_time = apr_time_now();
ll = r->the_request;
#if 0
/* 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
* sometime. rbb */
r->method);
#endif
/* Provide quick information about the request method as soon as known */
r->header_only = 1;
}
ap_parse_uri(r, uri);
if (ll[0]) {
r->assbackwards = 0;
} else {
r->assbackwards = 1;
pro = "HTTP/0.9";
len = 8;
}
/* XXX ap_update_connection_status(conn->id, "Protocol", r->protocol); */
/* Avoid sscanf in the common case */
if (len == 8
}
else
return 1;
}
{
char *last_field = NULL;
apr_size_t last_len = 0;
apr_size_t alloc_len = 0;
char *field;
char *value;
int fields_read = 0;
char *tmp_field;
/*
* Read header lines until we get the empty separator line, a read error,
* the connection closes (EOF), reach the server limit, or we timeout.
*/
while(1) {
int folded = 0;
if (rv != APR_SUCCESS) {
r->status = HTTP_BAD_REQUEST;
/* 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.
*/
/* insure ap_escape_html will terminate correctly */
apr_pstrcat(r->pool,
"Size of a request header field "
"exceeds server limit.<br />\n"
"<pre>\n",
"</pre>\n", NULL));
}
return;
}
if (last_field != NULL) {
/* 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.
*/
r->status = HTTP_BAD_REQUEST;
/* report what we have accumulated so far before the
* overflow (last_field) as the field with the problem
*/
apr_pstrcat(r->pool,
"Size of a request header field "
"after folding "
"exceeds server limit.<br />\n"
"<pre>\n",
"</pre>\n", NULL));
return;
}
char *fold_buf;
}
}
folded = 1;
}
else /* not a continuation line */ {
if (r->server->limit_req_fields
r->status = HTTP_BAD_REQUEST;
"The number of request header fields "
"exceeds this server's limit.");
return;
}
apr_pstrcat(r->pool,
"Request header field is "
"missing ':' separator.<br />\n"
"<pre>\n",
ap_escape_html(r->pool,
"</pre>\n", NULL));
return;
}
++value; /* Skip to start of value */
}
/* Strip LWS after field-name: */
while (tmp_field > last_field
*tmp_field-- = '\0';
}
/* Strip LWS after field-value: */
*tmp_field-- = '\0';
}
/* 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
*/
alloc_len = 0;
} /* end if current line is not a continuation starting with tab */
}
/* Found a blank line, stop. */
if (len == 0) {
break;
}
/* 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.)
*/
if (!folded) {
last_field = field;
}
}
}
{
}
{
request_rec *r;
apr_pool_t *p;
const char *expect;
int access_status;
apr_pool_tag(p, "request");
r = apr_pcalloc(p, sizeof(request_rec));
r->pool = p;
r->connection = conn;
r->ap_auth_type = NULL;
/* Must be set before we run create request hook */
r->output_filters = r->proto_output_filters;
r->input_filters = r->proto_input_filters;
r->sent_bodyct = 0; /* bytect isn't for body */
r->read_length = 0;
r->read_body = REQUEST_NO_BODY;
r->the_request = NULL;
/* Get the request... */
if (!read_request_line(r, tmp_bb)) {
if (r->status == HTTP_REQUEST_URI_TOO_LARGE) {
ap_send_error_response(r, 0);
return r;
}
return NULL;
}
if (!r->assbackwards) {
if (r->status != HTTP_REQUEST_TIME_OUT) {
"request failed: error reading the headers");
ap_send_error_response(r, 0);
return r;
}
}
else {
if (r->header_only) {
/*
* 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
* comes through...
*/
"client sent invalid HTTP/0.9 request: HEAD %s",
r->uri);
r->header_only = 0;
r->status = HTTP_BAD_REQUEST;
ap_send_error_response(r, 0);
return r;
}
}
/* 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.
*/
r->status = HTTP_BAD_REQUEST;
"client sent HTTP/1.1 request without hostname "
"(see RFC2616 section 14.23): %s", r->uri);
}
ap_send_error_response(r, 0);
return r;
}
if ((access_status = ap_run_post_read_request(r))) {
ap_die(access_status, r);
return NULL;
}
&& (expect[0] != '\0')) {
/*
* 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.
*/
r->expecting_100 = 1;
}
else {
"client sent an unrecognized expectation value of "
"Expect: %s", expect);
ap_send_error_response(r, 0);
return r;
}
}
NULL, r, r->connection);
return r;
}
/*
* 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...
*/
const request_rec *r)
{
* fragment. */
}
static void end_output_stream(request_rec *r)
{
conn_rec *c = r->connection;
apr_bucket *b;
b = apr_bucket_eos_create(c->bucket_alloc);
}
{
/* 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.
*/
{
(void) ap_discard_request_body(r);
/* tell the filter chain there is no more content coming */
if (!r->eos_sent) {
}
}
/*
* Support for the Basic authentication protocol, and a bit for Digest.
*/
{
const char *type = ap_auth_type(r);
if (type) {
}
else {
0, r, "need AuthType to note auth failure: %s", r->uri);
}
}
{
const char *type = ap_auth_type(r);
/* if there is no AuthType configure or it is something other than
* Basic, let ap_note_auth_failure() deal with it
*/
else
: "WWW-Authenticate",
"\"", NULL));
}
{
: "WWW-Authenticate",
}
{
(PROXYREQ_PROXY == r->proxyreq)
? "Proxy-Authorization"
: "Authorization");
const char *t;
return DECLINED;
if (!ap_auth_name(r)) {
0, r, "need AuthName: %s", r->uri);
return HTTP_INTERNAL_SERVER_ERROR;
}
if (!auth_line) {
return HTTP_UNAUTHORIZED;
}
/* Client tried to authenticate using wrong auth scheme */
"client used wrong authentication scheme: %s", r->uri);
return HTTP_UNAUTHORIZED;
}
auth_line++;
}
r->ap_auth_type = "Basic";
*pw = t;
return OK;
}
struct content_length_ctx {
int data_sent; /* true if the C-L filter has already sent at
* least one bucket on to the next output filter
* for this request
*/
};
/* 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
*/
ap_filter_t *f,
{
request_rec *r = f->r;
struct content_length_ctx *ctx;
apr_bucket *e;
int eos = 0;
if (!ctx) {
}
/* Loop through this set of buckets to compute their length
*/
e = APR_BRIGADE_FIRST(b);
while (e != APR_BRIGADE_SENTINEL(b)) {
if (APR_BUCKET_IS_EOS(e)) {
eos = 1;
break;
}
const char *ignored;
/* This is probably a pipe bucket. Send everything
* prior to this, and then read the data for this bucket.
*/
if (rv == APR_SUCCESS) {
/* Attempt a nonblocking read next time through */
r->bytes_sent += len;
}
else if (APR_STATUS_IS_EAGAIN(rv)) {
/* Output everything prior to this bucket, and then
* do a blocking read on the next batch.
*/
if (e != APR_BRIGADE_FIRST(b)) {
return rv;
}
b = split;
e = APR_BRIGADE_FIRST(b);
}
continue;
}
else {
"ap_content_length_filter: "
"apr_bucket_read() failed");
return rv;
}
}
else {
r->bytes_sent += e->length;
}
e = APR_BUCKET_NEXT(e);
}
/* 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.
*/
ap_set_content_length(r, r->bytes_sent);
}
return ap_pass_brigade(f->next, b);
}
/*
* Send the body of a response to the client.
*/
{
conn_rec *c = r->connection;
apr_bucket *b;
if (rv != APR_SUCCESS) {
*nbytes = 0; /* no way to tell how many were actually sent */
}
else {
}
return rv;
}
#if APR_HAS_MMAP
/* send data from an in-memory buffer */
{
conn_rec *c = r->connection;
apr_bucket *b;
}
#endif /* APR_HAS_MMAP */
typedef struct {
{
/* 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.
*/
}
}
{
conn_rec *c = r->connection;
ap_filter_t *f;
if (len == 0)
return APR_SUCCESS;
/* 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 */
if (ap_old_write_func == f->frec)
break;
}
if (f == NULL) {
/* our filter hasn't been added yet */
f = r->output_filters;
}
/* if the first filter is not our buffering filter, then we have to
* deliver the content through the normal filter chain
*/
if (f != r->output_filters) {
}
/* grab the context from our filter */
}
}
{
char c2 = (char)c;
if (r->connection->aborted) {
return -1;
}
return -1;
return c;
}
{
if (r->connection->aborted)
return -1;
return -1;
return len;
}
{
if (r->connection->aborted)
return -1;
return -1;
return nbyte;
}
struct ap_vrprintf_data {
request_rec *r;
char *buff;
};
{
/* 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 */
return -1;
/* r_flush is called when vbuff is completely full */
return -1;
}
/* reset the buffer position */
return APR_SUCCESS;
}
{
struct ap_vrprintf_data vd;
char vrprintf_buf[AP_IOBUFSIZE];
vd.r = r;
if (r->connection->aborted)
return -1;
/* tack on null terminator on remaining string */
if (written != -1) {
/* last call to buffer_output, to finish clearing the buffer */
return -1;
written += n;
}
return written;
}
{
int n;
if (r->connection->aborted)
return -1;
return n;
}
{
const char *s;
apr_size_t written = 0;
if (r->connection->aborted)
return -1;
/* ### TODO: if the total output is large, put all the strings
* ### into a single brigade, rather than flushing each time we
* ### fill the buffer
*/
while (1) {
if (s == NULL)
break;
return -1;
}
}
return written;
}
{
conn_rec *c = r->connection;
apr_bucket *b;
b = apr_bucket_flush_create(c->bucket_alloc);
return -1;
return 0;
}
/*
* 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
* being in the future.
*/
{
if (!r->assbackwards) {
}
}
AP_IMPLEMENT_HOOK_RUN_FIRST(const char *,http_scheme,
(const request_rec *r), (r), NULL)
AP_IMPLEMENT_HOOK_RUN_FIRST(unsigned short,default_port,
(const request_rec *r), (r), 0)