apr_status_t round_robin_create_req()

in flood_round_robin.c [259:403]


apr_status_t round_robin_create_req(profile_t *profile, request_t *r)
{
    round_robin_profile_t *p;
    char *cookies, *path;
    char *enc_credtls, *credtls, *authz_hdr = NULL, *extra_hdr = NULL;
    cookie_t *cook;
   
    p = (round_robin_profile_t*)profile; 

    /* Do we want to save the entire response? */
    r->wantresponse = p->url[p->current_url].responsetemplate ? 1 : 0;

    /* FIXME: This algorithm sucks. */
    if (p->cookie)
    {
        cookies = apr_pstrdup(p->pool, "Cookie: ");
        cook = p->cookie;
        while (cook)
        {
            if (cook != p->cookie)
                cookies = apr_pstrcat(p->pool, cookies, ";", NULL);

            cookies = apr_pstrcat(p->pool, cookies, cook->name, "=", 
                                  cook->value, NULL);
            cook = cook->next; 
        }
        cookies = apr_pstrcat(p->pool, cookies, CRLF, NULL);
    }
    else
        cookies = "";

    if (p->url[p->current_url].user) {
        if (!p->url[p->current_url].password) {
            apr_file_printf(local_stderr,
                            "missing password for user '%s'\n",
                            p->url[p->current_url].user);
            return APR_EGENERAL;
        } else {
            int credlen;
            credtls = apr_pstrcat(r->pool, p->url[p->current_url].user,
                                  ":", p->url[p->current_url].password, NULL);
            credlen = strlen(credtls);
            enc_credtls = (char *) apr_palloc(r->pool,
                                              apr_base64_encode_len(credlen) + 1);
            apr_base64_encode(enc_credtls, credtls, credlen);
            authz_hdr = apr_pstrcat(r->pool, "Authorization: Basic ",
                                    enc_credtls, CRLF, NULL);
        }
    }

    if (p->url[p->current_url].extra_headers) {
        extra_hdr = p->url[p->current_url].extra_headers;
    }

    if (p->proxy_url != NULL) {
        path = apr_pstrcat(r->pool, r->parsed_uri->scheme, "://",
                                    r->parsed_uri->hostinfo,
                                    r->parsed_uri->path, NULL);
    }
    else {
        path = r->parsed_uri->path;
    }

    switch (r->method)
    {
    case GET:
    case HEAD:
        r->rbuf = apr_psprintf(r->pool, 
                               "%s %s%s%s HTTP/1.1" CRLF
                               "User-Agent: Flood/" FLOOD_VERSION CRLF
                               "Connection: %s" CRLF
                               "Host: %s" CRLF 
                               "%s" /* Extra headers */
                               "%s" /* Authz */
                               "%s" /* Cookies */
                               CRLF, /* End of HTTP request headers */
                               p->url[p->current_url].method_string,
                               path,
                               r->parsed_uri->query ? "?" : "",
                               r->parsed_uri->query ? r->parsed_uri->query : "",
                               r->keepalive ? "Keep-Alive" : "Close",
                               r->parsed_uri->hostinfo,
                               extra_hdr ? extra_hdr : "",
                               authz_hdr ? authz_hdr : "",
                               cookies);
        r->rbuftype = POOL;
        r->rbufsize = strlen(r->rbuf);
        break;
    case POST:
    case OTHER:
        if (r->payload) {
            r->rbuf = apr_psprintf(r->pool, 
                                   "%s %s%s%s HTTP/1.1" CRLF
                                   "User-Agent: Flood/" FLOOD_VERSION CRLF
                                   "Connection: %s" CRLF
                                   "Host: %s" CRLF
                                   "Content-Length: %d" CRLF 
                                   "Content-Type: %s" CRLF 
                                   "%s" /* Extra headers */
                                   "%s" /* Authz */
                                   "%s" /* Cookies */
                                   CRLF /* End of HTTP request headers */
                                   "%s" /* Body */,
                                   p->url[p->current_url].method_string,
                                   path, 
                                   r->parsed_uri->query ? "?" : "",
                                   r->parsed_uri->query ?
                                       r->parsed_uri->query : "",
                                   r->keepalive ? "Keep-Alive" : "Close",
                                   r->parsed_uri->hostinfo,
                                   r->payloadsize,
                                   r->contenttype ? r->contenttype :
                                       "application/x-www-form-urlencoded", 
                                   extra_hdr ? extra_hdr : "",
                                   authz_hdr ? authz_hdr : "",
                                   cookies,
                                   (char*)r->payload);
        } else { /* There is no payload, but it's still a POST */
            r->rbuf = apr_psprintf(r->pool, 
                                   "%s %s%s%s HTTP/1.1" CRLF
                                   "User-Agent: Flood/" FLOOD_VERSION CRLF
                                   "Connection: %s" CRLF
                                   "Host: %s" CRLF
                                   "%s" /* Extra headers */
                                   "%s" /* Authz */
                                   "%s" /* Cookies */
                                   CRLF /* End of HTTP request headers */
                                   "" /* No body */,
                                   p->url[p->current_url].method_string,
                                   path, 
                                   r->parsed_uri->query ? "?" : "",
                                   r->parsed_uri->query ? r->parsed_uri->query : "",
                                   r->keepalive ? "Keep-Alive" : "Close",
                                   r->parsed_uri->hostinfo,
                                   extra_hdr ? extra_hdr : "",
                                   authz_hdr ? authz_hdr : "",
                                   cookies);
        }
        r->rbuftype = POOL;
        r->rbufsize = strlen(r->rbuf);
        break;
    }

    return APR_SUCCESS;
}