static int random_chunk_handler()

in c-modules/random_chunk/mod_random_chunk.c [103:180]


static int random_chunk_handler(request_rec *r)
{
    apr_size_t seed = 0;
    apr_size_t count = 0;
    int i;
    char buf[MAX_SEGMENT + 1];
    unsigned int len;
    apr_size_t total = 0;

    if (strcmp(r->handler, "random_chunk")) {
        return DECLINED;
    }

    if (r->proto_num < HTTP_VERSION(1,1)) {
        return DECLINED;
    }

    r->allowed |= (AP_METHOD_BIT << M_GET);

    if (r->method_number != M_GET) {
        return DECLINED;
    }

    r->content_type = "text/html";              

#ifdef APACHE1
    ap_send_http_header(r);
#endif
    if (r->header_only) {
        return OK;
    }

    httpd_test_split_qs_numbers(r, &seed, &count, NULL);

    if (!count) {
        ap_rputs("Must include args! ... "
                 "of the form <code>?seed,count</code>", r);
        return 0;
    }

#ifdef WIN32
    srand(seed); /* XXX: apr-ize */
#else
    srandom(seed); /* XXX: apr-ize */
#endif

    for (i = 0; i < count; ++i) {
#ifdef WIN32
        len = rand() % (MAX_SEGMENT + ONE_WEIGHT);
#else
        len = random() % (MAX_SEGMENT + ONE_WEIGHT);
#endif

        if (len >= MAX_SEGMENT) {
            ap_rputc((i & 1) ? '0' : '1', r);
            total += 1;
        }
        else if (len == 0) {
            /* 1.x version used to do this; but chunk_filter does now */
#if 0
            ap_bsetflag(r->connection->client, B_CHUNK, 0);
            ap_bsetflag(r->connection->client, B_CHUNK, 1);
#endif
        }
        else {
            memset(buf, '2' + len, len);
            buf[len] = 0;
            total += ap_rputs(buf, r);
        }
    }

    ap_rprintf(r, "__END__:%" APR_SIZE_T_FMT, total);

    fprintf(stderr, "[mod_random_chunk] sent %" APR_SIZE_T_FMT "bytes\n", 
            total);

    return 0;
}