Blame modules/http/chunk_filter.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * chunk_filter.c --- HTTP/1.1 chunked transfer encoding filter.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_thread_proc.h"    /* for RLIMIT stuff */
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_connection.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_protocol.h"  /* For index_of_response().  Grump. */
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
Packit 90a5c9
#include "util_filter.h"
Packit 90a5c9
#include "util_ebcdic.h"
Packit 90a5c9
#include "ap_mpm.h"
Packit 90a5c9
#include "scoreboard.h"
Packit 90a5c9
Packit 90a5c9
#include "mod_core.h"
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * A pointer to this is used to memorize in the filter context that a bad
Packit 90a5c9
 * gateway error bucket had been seen. It is used as an invented unique pointer.
Packit 90a5c9
 */
Packit 90a5c9
static char bad_gateway_seen;
Packit 90a5c9
Packit 90a5c9
apr_status_t ap_http_chunk_filter(ap_filter_t *f, apr_bucket_brigade *b)
Packit 90a5c9
{
Packit 90a5c9
#define ASCII_CRLF  "\015\012"
Packit 90a5c9
#define ASCII_ZERO  "\060"
Packit 90a5c9
    conn_rec *c = f->r->connection;
Packit 90a5c9
    apr_bucket_brigade *more, *tmp;
Packit 90a5c9
    apr_bucket *e;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    for (more = tmp = NULL; b; b = more, more = NULL) {
Packit 90a5c9
        apr_off_t bytes = 0;
Packit 90a5c9
        apr_bucket *eos = NULL;
Packit 90a5c9
        apr_bucket *flush = NULL;
Packit 90a5c9
        /* XXX: chunk_hdr must remain at this scope since it is used in a
Packit 90a5c9
         *      transient bucket.
Packit 90a5c9
         */
Packit 90a5c9
        char chunk_hdr[20]; /* enough space for the snprintf below */
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
        for (e = APR_BRIGADE_FIRST(b);
Packit 90a5c9
             e != APR_BRIGADE_SENTINEL(b);
Packit 90a5c9
             e = APR_BUCKET_NEXT(e))
Packit 90a5c9
        {
Packit 90a5c9
            if (APR_BUCKET_IS_EOS(e)) {
Packit 90a5c9
                /* there shouldn't be anything after the eos */
Packit 90a5c9
                ap_remove_output_filter(f);
Packit 90a5c9
                eos = e;
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
            if (AP_BUCKET_IS_ERROR(e)
Packit 90a5c9
                && (((ap_bucket_error *)(e->data))->status
Packit 90a5c9
                    == HTTP_BAD_GATEWAY)) {
Packit 90a5c9
                /*
Packit 90a5c9
                 * We had a broken backend. Memorize this in the filter
Packit 90a5c9
                 * context.
Packit 90a5c9
                 */
Packit 90a5c9
                f->ctx = &bad_gateway_seen;
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
            if (APR_BUCKET_IS_FLUSH(e)) {
Packit 90a5c9
                flush = e;
Packit 90a5c9
                if (e != APR_BRIGADE_LAST(b)) {
Packit 90a5c9
                    more = apr_brigade_split_ex(b, APR_BUCKET_NEXT(e), tmp);
Packit 90a5c9
                }
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
            else if (e->length == (apr_size_t)-1) {
Packit 90a5c9
                /* unknown amount of data (e.g. a pipe) */
Packit 90a5c9
                const char *data;
Packit 90a5c9
                apr_size_t len;
Packit 90a5c9
Packit 90a5c9
                rv = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
Packit 90a5c9
                if (rv != APR_SUCCESS) {
Packit 90a5c9
                    return rv;
Packit 90a5c9
                }
Packit 90a5c9
                if (len > 0) {
Packit 90a5c9
                    /*
Packit 90a5c9
                     * There may be a new next bucket representing the
Packit 90a5c9
                     * rest of the data stream on which a read() may
Packit 90a5c9
                     * block so we pass down what we have so far.
Packit 90a5c9
                     */
Packit 90a5c9
                    bytes += len;
Packit 90a5c9
                    more = apr_brigade_split_ex(b, APR_BUCKET_NEXT(e), tmp);
Packit 90a5c9
                    break;
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    /* If there was nothing in this bucket then we can
Packit 90a5c9
                     * safely move on to the next one without pausing
Packit 90a5c9
                     * to pass down what we have counted up so far.
Packit 90a5c9
                     */
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                bytes += e->length;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * XXX: if there aren't very many bytes at this point it may
Packit 90a5c9
         * be a good idea to set them aside and return for more,
Packit 90a5c9
         * unless we haven't finished counting this brigade yet.
Packit 90a5c9
         */
Packit 90a5c9
        /* if there are content bytes, then wrap them in a chunk */
Packit 90a5c9
        if (bytes > 0) {
Packit 90a5c9
            apr_size_t hdr_len;
Packit 90a5c9
            /*
Packit 90a5c9
             * Insert the chunk header, specifying the number of bytes in
Packit 90a5c9
             * the chunk.
Packit 90a5c9
             */
Packit 90a5c9
            hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr),
Packit 90a5c9
                                   "%" APR_UINT64_T_HEX_FMT CRLF, (apr_uint64_t)bytes);
Packit 90a5c9
            ap_xlate_proto_to_ascii(chunk_hdr, hdr_len);
Packit 90a5c9
            e = apr_bucket_transient_create(chunk_hdr, hdr_len,
Packit 90a5c9
                                            c->bucket_alloc);
Packit 90a5c9
            APR_BRIGADE_INSERT_HEAD(b, e);
Packit 90a5c9
Packit 90a5c9
            /*
Packit 90a5c9
             * Insert the end-of-chunk CRLF before an EOS or
Packit 90a5c9
             * FLUSH bucket, or appended to the brigade
Packit 90a5c9
             */
Packit 90a5c9
            e = apr_bucket_immortal_create(ASCII_CRLF, 2, c->bucket_alloc);
Packit 90a5c9
            if (eos != NULL) {
Packit 90a5c9
                APR_BUCKET_INSERT_BEFORE(eos, e);
Packit 90a5c9
            }
Packit 90a5c9
            else if (flush != NULL) {
Packit 90a5c9
                APR_BUCKET_INSERT_BEFORE(flush, e);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                APR_BRIGADE_INSERT_TAIL(b, e);
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* RFC 2616, Section 3.6.1
Packit 90a5c9
         *
Packit 90a5c9
         * If there is an EOS bucket, then prefix it with:
Packit 90a5c9
         *   1) the last-chunk marker ("0" CRLF)
Packit 90a5c9
         *   2) the trailer
Packit 90a5c9
         *   3) the end-of-chunked body CRLF
Packit 90a5c9
         *
Packit 90a5c9
         * We only do this if we have not seen an error bucket with
Packit 90a5c9
         * status HTTP_BAD_GATEWAY. We have memorized an
Packit 90a5c9
         * error bucket that we had seen in the filter context.
Packit 90a5c9
         * The error bucket with status HTTP_BAD_GATEWAY indicates that the
Packit 90a5c9
         * connection to the backend (mod_proxy) broke in the middle of the
Packit 90a5c9
         * response. In order to signal the client that something went wrong
Packit 90a5c9
         * we do not create the last-chunk marker and set c->keepalive to
Packit 90a5c9
         * AP_CONN_CLOSE in the core output filter.
Packit 90a5c9
         *
Packit 90a5c9
         * XXX: it would be nice to combine this with the end-of-chunk
Packit 90a5c9
         * marker above, but this is a bit more straight-forward for
Packit 90a5c9
         * now.
Packit 90a5c9
         */
Packit 90a5c9
        if (eos && !f->ctx) {
Packit 90a5c9
            /* XXX: (2) trailers ... does not yet exist */
Packit 90a5c9
            e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF
Packit 90a5c9
                                           /* <trailers> */
Packit 90a5c9
                                           ASCII_CRLF, 5, c->bucket_alloc);
Packit 90a5c9
            APR_BUCKET_INSERT_BEFORE(eos, e);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* pass the brigade to the next filter. */
Packit 90a5c9
        rv = ap_pass_brigade(f->next, b);
Packit 90a5c9
        apr_brigade_cleanup(b);
Packit 90a5c9
        if (rv != APR_SUCCESS || eos != NULL) {
Packit 90a5c9
            return rv;
Packit 90a5c9
        }
Packit 90a5c9
        tmp = b;
Packit 90a5c9
    }
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}