mod_rndchunk.c revision 0f081398cf0eef8cc7c66a535d450110a92dc8ae
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen/* ====================================================================
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * Copyright (c) 1998-1999 The Apache Group. All rights reserved.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen *
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * Redistribution and use in source and binary forms, with or without
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * modification, are permitted provided that the following conditions
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * are met:
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen *
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * 1. Redistributions of source code must retain the above copyright
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * notice, this list of conditions and the following disclaimer.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen *
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * 2. Redistributions in binary form must reproduce the above copyright
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * notice, this list of conditions and the following disclaimer in
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * the documentation and/or other materials provided with the
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * distribution.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen *
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * 3. All advertising materials mentioning features or use of this
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * software must display the following acknowledgment:
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * "This product includes software developed by the Apache Group
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * for use in the Apache HTTP server project (http://www.apache.org/)."
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen *
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * 4. The names "Apache Server" and "Apache Group" must not be used to
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * endorse or promote products derived from this software without
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * prior written permission. For written permission, please contact
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * apache@apache.org.
f60e98b33646e7a3317553e13bac591a98ce41c0Lukasz Skalski *
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen * 5. Products derived from this software may not be called "Apache"
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * nor may "Apache" appear in their names without prior written
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering * permission of the Apache Group.
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering *
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering * 6. Redistributions of any form whatsoever must retain the following
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * acknowledgment:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * "This product includes software developed by the Apache Group
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen * for use in the Apache HTTP server project (http://www.apache.org/)."
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen *
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * OF THE POSSIBILITY OF SUCH DAMAGE.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * ====================================================================
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek *
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek * This software consists of voluntary contributions made by many
cc56fafeebf814ef035e549115cf1850e6473fa5WaLyong Cho * individuals on behalf of the Apache Group and was originally based
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek * on public domain software written at the National Center for
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek * Supercomputing Applications, University of Illinois, Urbana-Champaign.
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek * For more information on the Apache Group and the Apache HTTP server
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek * project, please see <http://www.apache.org/>.
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek *
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen */
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen/*
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * This module is intended to be used for testing chunked encoding. It
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen * generates a whole whack of output using ap_bputc() and ap_bputs(). It
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen * also exercises start_chunk() and end_chunk() in buff.c. To use it
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * you should use a tool like netcat and the src/test/check_chunked
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen * tool. Add something like this to your access.conf file:
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen *
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering * <Location /rndchunk>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * SetHandler rndchunk
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen * </Location>
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen *
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen * Then fake requests such as:
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * GET /rndchunk?0,1000000 HTTP/1.1
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * Host: localhost
b9e7a9d870ac41d4db954edd52a1f5dd7d153389Lennart Poettering *
b9e7a9d870ac41d4db954edd52a1f5dd7d153389Lennart Poettering * The first arg is the random seed, the second is the number of
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * "things" to do. You should try a few seeds.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen *
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * You should also edit main/buff.c and change DEFAULT_BUFSIZE (and
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * CHUNK_HEADER_SIZE). Small values are particularly useful for
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * finding bugs. Try a few different values.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * -djg
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering */
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen#include "httpd.h"
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering#include "http_protocol.h"
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering#include "http_config.h"
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering#include "http_main.h"
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering#define MAX_SEGMENT 32
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering#define ONE_WEIGHT (256-32)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int send_rndchunk(request_rec *r)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering{
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering const char *args;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering char *endptr;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen unsigned int seed;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen unsigned int count;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen int i;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen char buf[MAX_SEGMENT + 1];
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen unsigned int len;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen r->allowed |= (1 << M_GET);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (r->method_number != M_GET)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return DECLINED;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen r->content_type = "text/html";
96e6e394431dcc1db52847be311e2c8e61d7a9d6Lennart Poettering ap_send_http_header(r);
96e6e394431dcc1db52847be311e2c8e61d7a9d6Lennart Poettering if(r->header_only) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
af4ec4309e8f82aad87a8d574785c12f8763d5f8Lennart Poettering }
af4ec4309e8f82aad87a8d574785c12f8763d5f8Lennart Poettering ap_hard_timeout("send_rndchunk", r);
af4ec4309e8f82aad87a8d574785c12f8763d5f8Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (!r->chunked) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen ap_rputs("Not chunked!", r);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen ap_kill_timeout(r);
return 0;
}
args = r->args;
if (!args) {
error:
ap_rputs("Must include args! ... of the form <code>?seed,count</code>", r);
ap_kill_timeout(r);
return 0;
}
seed = strtol(args, &endptr, 0);
if (!endptr || *endptr != ',') {
goto error;
}
++endptr;
count = strtol(endptr, &endptr, 0);
srandom(seed);
for (i = 0; i < count; ++i) {
len = random() % (MAX_SEGMENT + ONE_WEIGHT);
if (len >= MAX_SEGMENT) {
ap_rputc((i & 1) ? '0' : '1', r);
}
else if (len == 0) {
/* not a really nice thing to do, but we need to test
* beginning/ending chunks as well
*/
ap_bsetflag(r->connection->client, B_CHUNK, 0);
ap_bsetflag(r->connection->client, B_CHUNK, 1);
}
else {
memset(buf, '2' + len, len);
buf[len] = 0;
ap_rputs(buf, r);
}
}
ap_kill_timeout(r);
return 0;
}
static const handler_rec rndchunk_handlers[] =
{
{"rndchunk", send_rndchunk},
{NULL}
};
module rndchunk_module = {
STANDARD_MODULE_STUFF,
NULL, /* initializer */
NULL, /* dir config creater */
NULL, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server config */
NULL, /* command table */
rndchunk_handlers, /* handlers */
NULL, /* filename translation */
NULL, /* check_user_id */
NULL, /* check auth */
NULL, /* check access */
NULL, /* type_checker */
NULL, /* fixups */
NULL, /* logger */
NULL /* header parser */
};