\n"); return; } /* 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 field size. */ if (len > r->server->limit_req_fieldsize) { r->status = HTTP_BAD_REQUEST; apr_table_setn(r->notes, "error-notes", apr_pstrcat(r->pool, "Size of a request header field " "exceeds server limit.
\n" "
\n", ap_escape_html(r->pool, field), "\n", NULL)); return; } copy = apr_palloc(r->pool, len + 1); memcpy(copy, field, len + 1); if (!(value = strchr(copy, ':'))) { /* Find the colon separator */ r->status = HTTP_BAD_REQUEST; /* or abort the bad request */ apr_table_setn(r->notes, "error-notes", apr_pstrcat(r->pool, "Request header field is missing " "colon separator.
\n" "
\n", ap_escape_html(r->pool, copy), "\n", NULL)); return; } *value = '\0'; ++value; while (*value == ' ' || *value == '\t') { ++value; /* Skip to start of value */ } apr_table_addn(tmp_headers, copy, value); } apr_table_overlap(r->headers_in, tmp_headers, APR_OVERLAP_TABLES_MERGE); } request_rec *ap_read_request(conn_rec *conn) { request_rec *r; apr_pool_t *p; const char *expect; int access_status, keptalive; apr_pool_create(&p, conn->pool); r = apr_pcalloc(p, sizeof(request_rec)); r->pool = p; r->connection = conn; r->server = conn->base_server; keptalive = conn->keepalive == 1; conn->keepalive = 0; r->user = NULL; r->ap_auth_type = NULL; r->allowed_methods = ap_make_method_list(p, 2); r->headers_in = apr_table_make(r->pool, 50); r->subprocess_env = apr_table_make(r->pool, 50); r->headers_out = apr_table_make(r->pool, 12); r->err_headers_out = apr_table_make(r->pool, 5); r->notes = apr_table_make(r->pool, 5); r->request_config = ap_create_request_config(r->pool); ap_run_create_request(r); r->per_dir_config = r->server->lookup_defaults; r->sent_bodyct = 0; /* bytect isn't for body */ r->read_length = 0; r->read_body = REQUEST_NO_BODY; r->status = HTTP_REQUEST_TIME_OUT; /* Until we get a request */ r->the_request = NULL; r->output_filters = conn->output_filters; r->input_filters = conn->input_filters; apr_setsocketopt(conn->client_socket, APR_SO_TIMEOUT, (int)(keptalive ? r->server->keep_alive_timeout * APR_USEC_PER_SEC : r->server->timeout * APR_USEC_PER_SEC)); /* Get the request... */ if (!read_request_line(r)) { if (r->status == HTTP_REQUEST_URI_TOO_LARGE) { ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "request failed: URI too long"); ap_send_error_response(r, 0); ap_run_log_transaction(r); return r; } return NULL; } if (keptalive) { apr_setsocketopt(r->connection->client_socket, APR_SO_TIMEOUT, (int)(r->server->timeout * APR_USEC_PER_SEC)); } if (!r->assbackwards) { get_mime_headers(r); if (r->status != HTTP_REQUEST_TIME_OUT) { ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "request failed: error reading the headers"); ap_send_error_response(r, 0); ap_run_log_transaction(r); 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... */ ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "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); ap_run_log_transaction(r); return r; } } r->status = HTTP_OK; /* Until further notice. */ /* update what we think the virtual host is based on the headers we've * now read. may update status. */ ap_update_vhost_from_headers(r); /* we may have switched to another server */ r->per_dir_config = r->server->lookup_defaults; if ((!r->hostname && (r->proto_num >= HTTP_VERSION(1,1))) || ((r->proto_num == HTTP_VERSION(1,1)) && !apr_table_get(r->headers_in, "Host"))) { /* * 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; ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "client sent HTTP/1.1 request without hostname " "(see RFC2616 section 14.23): %s", r->uri); } if (r->status != HTTP_OK) { ap_send_error_response(r, 0); ap_run_log_transaction(r); return r; } if (((expect = apr_table_get(r->headers_in, "Expect")) != 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. */ if (strcasecmp(expect, "100-continue") == 0) { r->expecting_100 = 1; } else { r->status = HTTP_EXPECTATION_FAILED; ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, r, "client sent an unrecognized expectation value of " "Expect: %s", expect); ap_send_error_response(r, 0); (void) ap_discard_request_body(r); ap_run_log_transaction(r); return r; } } if ((access_status = ap_run_post_read_request(r))) { ap_die(access_status, r); ap_run_log_transaction(r); return NULL; } 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... */ void ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r) { rnew->the_request = r->the_request; /* Keep original request-line */ rnew->assbackwards = 1; /* Don't send headers from this. */ rnew->no_local_copy = 1; /* Don't try to send HTTP_NOT_MODIFIED for a * fragment. */ rnew->method = "GET"; rnew->method_number = M_GET; rnew->protocol = "INCLUDED"; rnew->status = HTTP_OK; rnew->headers_in = r->headers_in; rnew->subprocess_env = apr_table_copy(rnew->pool, r->subprocess_env); rnew->headers_out = apr_table_make(rnew->pool, 5); rnew->err_headers_out = apr_table_make(rnew->pool, 5); rnew->notes = apr_table_make(rnew->pool, 5); rnew->expecting_100 = r->expecting_100; rnew->read_length = r->read_length; rnew->read_body = REQUEST_NO_BODY; rnew->main = (request_rec *) r; } static void end_output_stream(request_rec *r) { apr_bucket_brigade *bb; apr_bucket *b; bb = apr_brigade_create(r->pool); b = apr_bucket_eos_create(); APR_BRIGADE_INSERT_TAIL(bb, b); ap_pass_brigade(r->output_filters, bb); } void ap_finalize_sub_req_protocol(request_rec *sub) { end_output_stream(sub); } /* 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. */ AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r) { while (r->next) { r = r->next; } /* tell the filter chain there is no more content coming */ if (!r->eos_sent) { end_output_stream(r); } } /* * Support for the Basic authentication protocol, and a bit for Digest. */ AP_DECLARE(void) ap_note_auth_failure(request_rec *r) { if (!strcasecmp(ap_auth_type(r), "Basic")) ap_note_basic_auth_failure(r); else if (!strcasecmp(ap_auth_type(r), "Digest")) ap_note_digest_auth_failure(r); } AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r) { if (strcasecmp(ap_auth_type(r), "Basic")) ap_note_auth_failure(r); else apr_table_setn(r->err_headers_out, (PROXYREQ_PROXY == r->proxyreq) ? "Proxy-Authenticate" : "WWW-Authenticate", apr_pstrcat(r->pool, "Basic realm=\"", ap_auth_name(r), "\"", NULL)); } AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r) { apr_table_setn(r->err_headers_out, (PROXYREQ_PROXY == r->proxyreq) ? "Proxy-Authenticate" : "WWW-Authenticate", apr_psprintf(r->pool, "Digest realm=\"%s\", nonce=\"%llx\"", ap_auth_name(r), r->request_time)); } AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw) { const char *auth_line = apr_table_get(r->headers_in, (PROXYREQ_PROXY == r->proxyreq) ? "Proxy-Authorization" : "Authorization"); const char *t; if (!(t = ap_auth_type(r)) || strcasecmp(t, "Basic")) return DECLINED; if (!ap_auth_name(r)) { ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "need AuthName: %s", r->uri); return HTTP_INTERNAL_SERVER_ERROR; } if (!auth_line) { ap_note_basic_auth_failure(r); return HTTP_UNAUTHORIZED; } if (strcasecmp(ap_getword(r->pool, &auth_line, ' '), "Basic")) { /* Client tried to authenticate using wrong auth scheme */ ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "client used wrong authentication scheme: %s", r->uri); ap_note_basic_auth_failure(r); return HTTP_UNAUTHORIZED; } while (*auth_line== ' ' || *auth_line== '\t') { auth_line++; } t = ap_pbase64decode(r->pool, auth_line); /* 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. */ r->user = ap_getword_nulls (r->pool, &t, ':'); r->ap_auth_type = "Basic"; *pw = t; return OK; } struct content_length_ctx { apr_bucket_brigade *saved; int compute_len; apr_size_t curr_len; }; /* 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_CORE_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(ap_filter_t *f, apr_bucket_brigade *b) { request_rec *r = f->r; struct content_length_ctx *ctx; apr_status_t rv; apr_bucket *e; int eos = 0, flush = 0, partial_send_okay = 0; apr_bucket_brigade *more, *split; apr_read_type_e eblock = APR_NONBLOCK_READ; ctx = f->ctx; if (!ctx) { /* first time through */ f->ctx = ctx = apr_pcalloc(r->pool, sizeof(struct content_length_ctx)); ctx->compute_len = 1; /* Assume we will compute the length */ } /* 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 * by a connection close * In both cases, we can send bytes to the client w/o needing to * compute content-length. * Todo: * We should be able to force connection close from this filter * when we see we are buffering too much. */ if ((r->proto_num >= HTTP_VERSION(1,1)) || (!f->r->connection->keepalive)) { partial_send_okay = 1; } more = b; while (more) { b = more; more = NULL; split = NULL; flush = 0; APR_BRIGADE_FOREACH(e, b) { const char *ignored; apr_size_t len; len = 0; if (APR_BUCKET_IS_EOS(e)) { eos = 1; } else if (APR_BUCKET_IS_FLUSH(e)) { if (partial_send_okay) { split = b; more = apr_brigade_split(b, APR_BUCKET_NEXT(e)); break; } } else if ((ctx->curr_len > 4*AP_MIN_BYTES_TO_WRITE)) { /* 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 (partial_send_okay) { split = b; more = apr_brigade_split(b, e); break; } } if (e->length == -1) { /* if length unknown */ rv = apr_bucket_read(e, &ignored, &len, eblock); if (rv == APR_SUCCESS) { /* Attempt a nonblocking read next time through */ eblock = APR_NONBLOCK_READ; } else if (rv == APR_EAGAIN) { /* Make the next read blocking. If the client supports chunked * encoding, flush the filter stack to the network. */ eblock = APR_BLOCK_READ; if (partial_send_okay) { split = b; more = apr_brigade_split(b, e); flush = 1; break; } } else if (rv != APR_EOF) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "ap_content_length_filter: apr_bucket_read() failed"); return rv; } } else { len = e->length; } ctx->curr_len += len; r->bytes_sent += len; } if (split) { ctx->compute_len = 0; /* Ooops, can't compute the length now */ ctx->curr_len = 0; if (ctx->saved) { APR_BRIGADE_CONCAT(ctx->saved, split); apr_brigade_destroy(split); split = ctx->saved; ctx->saved = NULL; } if (flush) { rv = ap_fflush(f->next, split); } else { rv = ap_pass_brigade(f->next, split); } if (rv != APR_SUCCESS) return rv; } } if ((ctx->curr_len < AP_MIN_BYTES_TO_WRITE) && !eos) { return ap_save_brigade(f, &ctx->saved, &b, (r->main) ? r->main->pool : r->pool); } if (ctx->compute_len) { /* save the brigade; we can't pass any data to the next * filter until we have the entire content length */ if (!eos) { return ap_save_brigade(f, &ctx->saved, &b, r->pool); } ap_set_content_length(r, r->bytes_sent); } if (ctx->saved) { APR_BRIGADE_CONCAT(ctx->saved, b); apr_brigade_destroy(b); b = ctx->saved; ctx->saved = NULL; } ctx->curr_len = 0; return ap_pass_brigade(f->next, b); } /* * Send the body of a response to the client. */ AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset, apr_size_t len, apr_size_t *nbytes) { apr_bucket_brigade *bb = NULL; apr_bucket *b; apr_status_t rv; bb = apr_brigade_create(r->pool); b = apr_bucket_file_create(fd, offset, len, r->pool); APR_BRIGADE_INSERT_TAIL(bb, b); rv = ap_pass_brigade(r->output_filters, bb); if (rv != APR_SUCCESS) { *nbytes = 0; /* no way to tell how many were actually sent */ } else { *nbytes = len; } return rv; } #if APR_HAS_MMAP /* send data from an in-memory buffer */ AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset, size_t length) { apr_bucket_brigade *bb = NULL; apr_bucket *b; bb = apr_brigade_create(r->pool); b = apr_bucket_mmap_create(mm, offset, length); APR_BRIGADE_INSERT_TAIL(bb, b); ap_pass_brigade(r->output_filters, bb); return mm->size; /* XXX - change API to report apr_status_t? */ } #endif /* APR_HAS_MMAP */ typedef struct { apr_bucket_brigade *bb; } old_write_filter_ctx; AP_CORE_DECLARE_NONSTD(apr_status_t) ap_old_write_filter( ap_filter_t *f, apr_bucket_brigade *bb) { old_write_filter_ctx *ctx = f->ctx; AP_DEBUG_ASSERT(ctx); if (ctx->bb != 0) { /* 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. */ APR_BRIGADE_CONCAT(ctx->bb, bb); } return ap_pass_brigade(f->next, ctx->bb); } static apr_status_t buffer_output(request_rec *r, const char *str, apr_size_t len) { ap_filter_t *f; old_write_filter_ctx *ctx; 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 */ for (f = r->output_filters; f != NULL; f = f->next) if (strcmp("OLD_WRITE", f->frec->name) == 0) break; if (f == NULL) { /* our filter hasn't been added yet */ ctx = apr_pcalloc(r->pool, sizeof(*ctx)); ap_add_output_filter("OLD_WRITE", ctx, r, r->connection); 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) { apr_bucket_brigade *bb = apr_brigade_create(r->pool); apr_bucket *b = apr_bucket_transient_create(str, len); APR_BRIGADE_INSERT_TAIL(bb, b); return ap_pass_brigade(r->output_filters, bb); } /* grab the context from our filter */ ctx = r->output_filters->ctx; if (ctx->bb == NULL) { ctx->bb = apr_brigade_create(r->pool); } ap_fwrite(f->next, ctx->bb, str, len); return APR_SUCCESS; } AP_DECLARE(int) ap_rputc(int c, request_rec *r) { char c2 = (char)c; if (r->connection->aborted) { return -1; } if (buffer_output(r, &c2, 1) != APR_SUCCESS) return -1; return c; } AP_DECLARE(int) ap_rputs(const char *str, request_rec *r) { apr_size_t len; if (r->connection->aborted) return -1; if (buffer_output(r, str, len = strlen(str)) != APR_SUCCESS) return -1; return len; } AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r) { if (r->connection->aborted) return -1; if (buffer_output(r, buf, nbyte) != APR_SUCCESS) return -1; return nbyte; } AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list va) { char buf[4096]; apr_size_t written; if (r->connection->aborted) return -1; /* ### fix this mechanism to allow more than 4K of output */ written = apr_vsnprintf(buf, sizeof(buf), fmt, va); if (buffer_output(r, buf, written) != APR_SUCCESS) return -1; return written; } AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt, ...) { va_list va; int n; if (r->connection->aborted) return -1; va_start(va, fmt); n = ap_vrprintf(r, fmt, va); va_end(va); return n; } AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r, ...) { va_list va; const char *s; apr_size_t len; 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 */ va_start(va, r); while (1) { s = va_arg(va, const char *); if (s == NULL) break; len = strlen(s); if (buffer_output(r, s, len) != APR_SUCCESS) { return -1; } written += len; } va_end(va); return written; } AP_DECLARE(int) ap_rflush(request_rec *r) { apr_bucket_brigade *bb; apr_bucket *b; bb = apr_brigade_create(r->pool); b = apr_bucket_flush_create(); APR_BRIGADE_INSERT_TAIL(bb, b); if (ap_pass_brigade(r->output_filters, bb) != APR_SUCCESS) 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. */ AP_DECLARE(void) ap_set_last_modified(request_rec *r) { apr_time_t mod_time = ap_rationalize_mtime(r, r->mtime); char *datestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN); apr_rfc822_date(datestr, mod_time); apr_table_setn(r->headers_out, "Last-Modified", datestr); } AP_IMPLEMENT_HOOK_RUN_ALL(int,post_read_request, (request_rec *r),(r),OK,DECLINED) AP_IMPLEMENT_HOOK_RUN_ALL(int,log_transaction, (request_rec *r),(r),OK,DECLINED) AP_IMPLEMENT_HOOK_RUN_FIRST(const char *,http_method, (const request_rec *r),(r),NULL) AP_IMPLEMENT_HOOK_RUN_FIRST(unsigned short,default_port, (const request_rec *r),(r),0)