Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e894bbe6f1
Fetching contributors…

Cannot retrieve contributors at this time

file 1011 lines (824 sloc) 30.245 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
/*
** Licensed to the Apache Software Foundation (ASF) under one or more
** contributor license agreements. See the NOTICE file distributed with
** this work for additional information regarding copyright ownership.
** The ASF licenses this file to You under the Apache License, Version 2.0
** (the "License"); you may not use this file except in compliance with
** the License. You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "apreq_module.h"
#include "apreq_error.h"
#include "apr_strings.h"
#include "apr_lib.h"
#include "apr_env.h"
#include "apreq_util.h"

#define USER_DATA_KEY "apreq"

/* Parroting APLOG_* ... */

#define CGILOG_EMERG 0 /* system is unusable */
#define CGILOG_ALERT 1 /* action must be taken immediately */
#define CGILOG_CRIT 2 /* critical conditions */
#define CGILOG_ERR 3 /* error conditions */
#define CGILOG_WARNING 4 /* warning conditions */
#define CGILOG_NOTICE 5 /* normal but significant condition */
#define CGILOG_INFO 6 /* informational */
#define CGILOG_DEBUG 7 /* debug-level messages */

#define CGILOG_LEVELMASK 7
#define CGILOG_MARK __FILE__, __LINE__

/** Interactive patch:
* TODO Don't use 65K buffer
* TODO Handle empty/non-existant parameters
* TODO Allow body elements to be files
* TODO When running body/get/cookies all at once, include previous cached
* values (and don't start at 0 in count)
* TODO What happens if user does apreq_param, but needs POST value - we'll
* never catch it now, as args param will match...
*/

struct cgi_handle {
    struct apreq_handle_t handle;

    apr_table_t *jar, *args, *body;
    apr_status_t jar_status,
                                 args_status,
                                 body_status;

    apreq_parser_t *parser;
    apreq_hook_t *hook_queue;
    apreq_hook_t *find_param;

    const char *temp_dir;
    apr_size_t brigade_limit;
    apr_uint64_t read_limit;
    apr_uint64_t bytes_read;

    apr_bucket_brigade *in;
    apr_bucket_brigade *tmpbb;

    int interactive_mode;
    const char *promptstr;
    apr_file_t *sout, *sin;
};

#define CRLF "\015\012"
static const char *nullstr = 0;
#define DEFAULT_PROMPT "([$t] )$n(\\($l\\))([$d]): "
#define MAX_PROMPT_NESTING_LEVELS 8
#define MAX_BUFFER_SIZE 65536

typedef struct {
    const char *t_name;
    int t_val;
} TRANS;

static const TRANS priorities[] = {
    {"emerg", CGILOG_EMERG},
    {"alert", CGILOG_ALERT},
    {"crit", CGILOG_CRIT},
    {"error", CGILOG_ERR},
    {"warn", CGILOG_WARNING},
    {"notice", CGILOG_NOTICE},
    {"info", CGILOG_INFO},
    {"debug", CGILOG_DEBUG},
    {NULL, -1},
};

static char* chomp(char* str) {
    apr_size_t p = strlen(str);
    while (--p >= 0) {
        switch ((char)(str[p])) {
        case '\015':
        case '\012':str[p]='\000';
                    break;
        default:return str;
        }
    }
    return str;
}

/** TODO: Support wide-characters */
/* prompt takes a apreq_handle and 2 strings - name and type - and prompts a
user for input via stdin/stdout. used in interactive mode.
name must be defined. type can be null.
we take the promptstring defined in the handle and interpolate variables as
follows:
$n - name of the variable we're asking for (param 2 to prompt())
$t - type of the variable we're asking for - like cookie, get, post, etc
(param 3 to prompt())
parentheses - if a variable is surrounded by parentheses, and interpolates
as null, then nothing else in the parentheses will be displayed
Useful if you want a string to only show up if a given variable
is available
These are planned for forward-compatibility, but the underlying features
need some love... I left these in here just as feature reminders, rather
than completely removing them from the code - at least they provide sanity
testing of the default prompt & parentheses - issac
$l - label for the param - the end-user-developer can provide a textual
description of the param (name) being requested (currently unused in
lib)
$d - default value for the param (currently unused in lib)
*/
static char *prompt(apreq_handle_t *handle, const char *name,
                    const char *type) {
    struct cgi_handle *req = (struct cgi_handle *)handle;
    const char *defval = nullstr;
    const char *label = NULL;
    const char *cprompt;
    char buf[MAX_PROMPT_NESTING_LEVELS][MAX_BUFFER_SIZE];
    /* Array of current arg for given p-level */
    char *start, curarg[MAX_PROMPT_NESTING_LEVELS] = "";
    /* Parenthesis level (for argument/text grouping) */
    int plevel;

    cprompt = req->promptstr - 1;
    *buf[0] = plevel = 0;
    start = buf[0];

    while (*(++cprompt) != 0) {
        switch (*cprompt) {
        case '$': /* interpolate argument; curarg[plevel] => 1 */
            cprompt++;
            switch (*cprompt) {
            case 't':
                if (type != NULL) {
                    strcpy(start, type);
                    start += strlen(type);
                    curarg[plevel] = 1;
                } else {
                    curarg[plevel] = curarg[plevel] | 0;
                }
                break;
            case 'n':
                /* Name can't be null :-) [If it can, we should
* immediately return NULL] */
                strcpy(start, name);
                start += strlen(name);
                curarg[plevel] = 1;
                break;
            case 'l':
                if (label != NULL) {
                    strcpy(start, label);
                    start += strlen(label);
                    curarg[plevel] = 1;
                } else {
                    curarg[plevel] = curarg[plevel] | 0;
                }
                break;
            case 'd':
                /* TODO: Once null defaults are available,
* remove if and use nullstr if defval == NULL */
                if (defval != NULL) {
                    strcpy(start, defval);
                    start += strlen(defval);
                    curarg[plevel] = 1;
                } else {
                    curarg[plevel] = curarg[plevel] | 0;
                }
                break;
            default:
                /* Handle this? */
                break;
            }
            break;

        case '(':
            if (plevel <= MAX_PROMPT_NESTING_LEVELS) {
                plevel++;
                curarg[plevel] = *buf[plevel] = 0;
                start = buf[plevel];
            }
            /* else? */
            break;

        case ')':
            if (plevel > 0) {
                *start = 0; /* Null terminate current string */
                
                /* Move pointer to end of string */
                plevel--;
                start = buf[plevel] + strlen(buf[plevel]);
                
                /* If old curarg was set, concat buffer with level down */
                if (curarg[plevel + 1]) {
                    strcpy(start, buf[plevel + 1]);
                    start += strlen(buf[plevel + 1]);
                }

                break;
            }
        case '\\': /* Check next character for escape sequence
* (just ignore it for now) */
            (void)*cprompt++;
            /* Fallthrough */

        default:
            *start++ = *cprompt;
        }
    }

    *start = 0; /* Null terminate the string */
    
    apr_file_printf(req->sout, "%s", buf[0]);
    apr_file_gets(buf[0], MAX_BUFFER_SIZE, req->sin);
    chomp(buf[0]);
    if (strcmp(buf[0], "")) {
/* if (strcmp(buf[0], nullstr)) */
            return apr_pstrdup(handle->pool, buf[0]);
/* return NULL; */
    }

    if (defval != nullstr)
        return apr_pstrdup(handle->pool, defval);

    return NULL;
}

static const char *cgi_header_in(apreq_handle_t *handle,
                                 const char *name)
{
    apr_pool_t *p = handle->pool;
    char *key = apr_pstrcat(p, "HTTP_", name, NULL);
    char *k, *value = NULL;
    for (k = key; *k; ++k) {
        if (*k == '-')
            *k = '_';
        else
            *k = apr_toupper(*k);
    }

    if (!strcmp(key, "HTTP_CONTENT_TYPE")
        || !strcmp(key, "HTTP_CONTENT_LENGTH"))
        {
            key += 5; /* strlen("HTTP_") */
        }

    apr_env_get(&value, key, p);

    return value;
}




static void cgi_log_error(const char *file, int line, int level,
                          apr_status_t status, apreq_handle_t *handle,
                          const char *fmt, ...)
{
    apr_pool_t *p = handle->pool;
    char buf[256];
    char *log_level_string, *ra;
    const char *remote_addr;
    unsigned log_level = CGILOG_WARNING;
    char date[APR_CTIME_LEN];
    va_list vp;
#ifndef WIN32
    apr_file_t *err;
#endif

    va_start(vp, fmt);

    if (apr_env_get(&log_level_string, "LOG_LEVEL", p) == APR_SUCCESS)
        log_level = (log_level_string[0] - '0');

    level &= CGILOG_LEVELMASK;

    if (level < (int)log_level) {

        if (apr_env_get(&ra, "REMOTE_ADDR", p) == APR_SUCCESS)
            remote_addr = ra;
        else
            remote_addr = "address unavailable";

        apr_ctime(date, apr_time_now());

#ifndef WIN32

        apr_file_open_stderr(&err, p);
        apr_file_printf(err, "[%s] [%s] [%s] %s(%d): %s: %s\n",
                        date, priorities[level].t_name, remote_addr, file, line,
                        apr_strerror(status,buf,255),apr_pvsprintf(p,fmt,vp));
        apr_file_flush(err);

#else
        fprintf(stderr, "[%s] [%s] [%s] %s(%d): %s: %s\n",
                date, priorities[level].t_name, remote_addr, file, line,
                apr_strerror(status,buf,255),apr_pvsprintf(p,fmt,vp));
#endif
    }

    va_end(vp);

}


APR_INLINE
static const char *cgi_query_string(apreq_handle_t *handle)
{
    char *value = NULL, qs[] = "QUERY_STRING";
    apr_env_get(&value, qs, handle->pool);
    return value;
}


static void init_body(apreq_handle_t *handle)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;
    const char *cl_header = cgi_header_in(handle, "Content-Length");
    apr_bucket_alloc_t *ba = handle->bucket_alloc;
    apr_pool_t *pool = handle->pool;
    apr_file_t *file;
    apr_bucket *eos, *pipe;

    if (cl_header != NULL) {
        char *dummy;
        apr_int64_t content_length = apr_strtoi64(cl_header, &dummy, 0);

        if (dummy == NULL || *dummy != 0) {
            req->body_status = APREQ_ERROR_BADHEADER;
            cgi_log_error(CGILOG_MARK, CGILOG_ERR, req->body_status, handle,
                          "Invalid Content-Length header (%s)", cl_header);
            return;
        }
        else if ((apr_uint64_t)content_length > req->read_limit) {
            req->body_status = APREQ_ERROR_OVERLIMIT;
            cgi_log_error(CGILOG_MARK, CGILOG_ERR, req->body_status, handle,
                          "Content-Length header (%s) exceeds configured "
                          "max_body limit (%" APR_UINT64_T_FMT ")",
                          cl_header, req->read_limit);
            return;
        }
    }

    if (req->parser == NULL) {
        const char *ct_header = cgi_header_in(handle, "Content-Type");

        if (ct_header != NULL) {
            apreq_parser_function_t pf = apreq_parser(ct_header);

            if (pf != NULL) {
                req->parser = apreq_parser_make(pool,
                                                ba,
                                                ct_header,
                                                pf,
                                                req->brigade_limit,
                                                req->temp_dir,
                                                req->hook_queue,
                                                NULL);
            }
            else {
                req->body_status = APREQ_ERROR_NOPARSER;
                return;
            }
        }
        else {
            req->body_status = APREQ_ERROR_NOHEADER;
            return;
        }
    }
    else {
        if (req->parser->brigade_limit > req->brigade_limit)
            req->parser->brigade_limit = req->brigade_limit;
        if (req->temp_dir != NULL)
            req->parser->temp_dir = req->temp_dir;
        if (req->hook_queue != NULL)
            apreq_parser_add_hook(req->parser, req->hook_queue);
    }

    req->hook_queue = NULL;
    req->in = apr_brigade_create(pool, ba);
    req->tmpbb = apr_brigade_create(pool, ba);

    apr_file_open_stdin(&file, pool); /* error status? */
    pipe = apr_bucket_pipe_create(file, ba);
    eos = apr_bucket_eos_create(ba);
    APR_BRIGADE_INSERT_HEAD(req->in, pipe);
    APR_BRIGADE_INSERT_TAIL(req->in, eos);

    req->body_status = APR_INCOMPLETE;

}

static apr_status_t cgi_read(apreq_handle_t *handle,
                             apr_off_t bytes)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;
    apr_bucket *e;
    apr_status_t s;

    if (req->body_status == APR_EINIT)
        init_body(handle);

    if (req->body_status != APR_INCOMPLETE)
        return req->body_status;


    switch (s = apr_brigade_partition(req->in, bytes, &e)) {
        apr_off_t len;

    case APR_SUCCESS:

        apreq_brigade_move(req->tmpbb, req->in, e);
        req->bytes_read += bytes;

        if (req->bytes_read > req->read_limit) {
            req->body_status = APREQ_ERROR_OVERLIMIT;
            cgi_log_error(CGILOG_MARK, CGILOG_ERR, req->body_status,
                          handle, "Bytes read (%" APR_UINT64_T_FMT
                          ") exceeds configured limit (%" APR_UINT64_T_FMT ")",
                          req->bytes_read, req->read_limit);
            break;
        }

        req->body_status =
            apreq_parser_run(req->parser, req->body, req->tmpbb);
        apr_brigade_cleanup(req->tmpbb);
        break;


    case APR_INCOMPLETE:

        apreq_brigade_move(req->tmpbb, req->in, e);
        s = apr_brigade_length(req->tmpbb, 1, &len);

        if (s != APR_SUCCESS) {
            req->body_status = s;
            break;
        }
        req->bytes_read += len;

        if (req->bytes_read > req->read_limit) {
            req->body_status = APREQ_ERROR_OVERLIMIT;
            cgi_log_error(CGILOG_MARK, CGILOG_ERR, req->body_status, handle,
                          "Bytes read (%" APR_UINT64_T_FMT
                          ") exceeds configured limit (%" APR_UINT64_T_FMT ")",
                          req->bytes_read, req->read_limit);

            break;
        }

        req->body_status =
            apreq_parser_run(req->parser, req->body, req->tmpbb);
        apr_brigade_cleanup(req->tmpbb);
        break;

    default:
        req->body_status = s;
    }

    return req->body_status;
}



static apr_status_t cgi_jar(apreq_handle_t *handle,
                            const apr_table_t **t)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;

    if (req->interactive_mode && req->jar_status != APR_SUCCESS) {
        char buf[65536];
        const char *name, *val;
        apreq_cookie_t *p;
        int i = 1;
        apr_file_printf(req->sout, "[CGI] Requested all cookies\n");
        while (1) {
            apr_file_printf(req->sout, "[CGI] Please enter a name for cookie %d (or just hit ENTER to end): ",
                     i++);
            apr_file_gets(buf, 65536, req->sin);
            chomp(buf);
            if (!strcmp(buf, "")) {
                break;
            }
            name = apr_pstrdup(handle->pool, buf);
            val = prompt(handle, name, "cookie");
            if (val == NULL)
                val = "";
            p = apreq_cookie_make(handle->pool, name, strlen(name), val, strlen(val));
            apreq_cookie_tainted_on(p);
            apreq_value_table_add(&p->v, req->jar);
        }
        req->jar_status = APR_SUCCESS;
    } /** Fallthrough */

    if (req->jar_status == APR_EINIT) {
        const char *cookies = cgi_header_in(handle, "Cookie");
        if (cookies != NULL) {
            req->jar_status =
                apreq_parse_cookie_header(handle->pool, req->jar, cookies);
        }
        else
            req->jar_status = APREQ_ERROR_NODATA;
    }

    *t = req->jar;
    return req->jar_status;
}

static apr_status_t cgi_args(apreq_handle_t *handle,
                             const apr_table_t **t)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;

    if (req->interactive_mode && req->args_status != APR_SUCCESS) {
        char buf[65536];
        const char *name, *val;
        apreq_param_t *p;
        int i = 1;
        apr_file_printf(req->sout, "[CGI] Requested all argument parameters\n");
        while (1) {
            apr_file_printf(req->sout, "[CGI] Please enter a name for parameter %d (or just hit ENTER to end): ",
                     i++);
            apr_file_gets(buf, 65536, req->sin);
            chomp(buf);
            if (!strcmp(buf, "")) {
                break;
            }
            name = apr_pstrdup(handle->pool, buf);
            val = prompt(handle, name, "parameter");
            if (val == NULL)
                val = "";
            p = apreq_param_make(handle->pool, name, strlen(name), val, strlen(val));
            apreq_param_tainted_on(p);
            apreq_value_table_add(&p->v, req->args);
            val = p->v.data;
        }
        req->args_status = APR_SUCCESS;
    } /** Fallthrough */

    if (req->args_status == APR_EINIT) {
        const char *qs = cgi_query_string(handle);
        if (qs != NULL) {
            req->args_status =
                apreq_parse_query_string(handle->pool, req->args, qs);
        }
        else
            req->args_status = APREQ_ERROR_NODATA;
    }

    *t = req->args;
    return req->args_status;
}




static apreq_cookie_t *cgi_jar_get(apreq_handle_t *handle,
                                   const char *name)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;
    const apr_table_t *t;
    const char *val = NULL;

    if (req->jar_status == APR_EINIT && !req->interactive_mode)
        cgi_jar(handle, &t);
    else
        t = req->jar;

    val = apr_table_get(t, name);
    if (val == NULL) {
        if (!req->interactive_mode) {
            return NULL;
        } else {
            apreq_cookie_t *p;
            val = prompt(handle, name, "cookie");
            if (val == NULL)
                return NULL;
            p = apreq_cookie_make(handle->pool, name, strlen(name), val, strlen(val));
            apreq_cookie_tainted_on(p);
            apreq_value_table_add(&p->v, req->jar);
            val = p->v.data;
        }
    }


    return apreq_value_to_cookie(val);
}

static apreq_param_t *cgi_args_get(apreq_handle_t *handle,
                                   const char *name)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;
    const apr_table_t *t;
    const char *val = NULL;

    if (req->args_status == APR_EINIT && !req->interactive_mode)
        cgi_args(handle, &t);
    else
        t = req->args;

    val = apr_table_get(t, name);
    if (val == NULL) {
        if (!req->interactive_mode) {
            return NULL;
        } else {
            apreq_param_t *p;
            val = prompt(handle, name, "parameter");
            if (val == NULL)
                return NULL;
            p = apreq_param_make(handle->pool, name, strlen(name), val, strlen(val));
            apreq_param_tainted_on(p);
            apreq_value_table_add(&p->v, req->args);
            val = p->v.data;
        }
    }


    return apreq_value_to_param(val);
}



static apr_status_t cgi_body(apreq_handle_t *handle,
                             const apr_table_t **t)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;

    if (req->interactive_mode && req->body_status != APR_SUCCESS) {
        const char *name, *val;
        apreq_param_t *p;
        int i = 1;
        apr_file_printf(req->sout, "[CGI] Requested all body parameters\n");
        while (1) {
            char buf[65536];
            apr_file_printf(req->sout, "[CGI] Please enter a name for parameter %d (or just hit ENTER to end): ",
                     i++);
            apr_file_gets(buf, 65536, req->sin);
            chomp(buf);
            if (!strcmp(buf, "")) {
                break;
            }
            name = apr_pstrdup(handle->pool, buf);
            val = prompt(handle, name, "parameter");
            if (val == NULL)
                val = "";
            p = apreq_param_make(handle->pool, name, strlen(name), val, strlen(val));
            apreq_param_tainted_on(p);
            apreq_value_table_add(&p->v, req->body);
            val = p->v.data;
        }
        req->body_status = APR_SUCCESS;
    } /** Fallthrough */
    
    switch (req->body_status) {

    case APR_EINIT:
        init_body(handle);
        if (req->body_status != APR_INCOMPLETE)
            break;

    case APR_INCOMPLETE:
        while (cgi_read(handle, APREQ_DEFAULT_READ_BLOCK_SIZE)
               == APR_INCOMPLETE)
            ; /*loop*/
    }

    *t = req->body;
    return req->body_status;
}

static apreq_param_t *cgi_body_get(apreq_handle_t *handle,
                                   const char *name)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;
    const char *val = NULL;
    apreq_hook_t *h;
    apreq_hook_find_param_ctx_t *hook_ctx;

    if (req->interactive_mode) {
        val = apr_table_get(req->body, name);
        if (val == NULL) {
            return NULL;
        } else {
            apreq_param_t *p;
            val = prompt(handle, name, "parameter");
            if (val == NULL)
                return NULL;
            p = apreq_param_make(handle->pool, name, strlen(name), val, strlen(val));
            apreq_param_tainted_on(p);
            apreq_value_table_add(&p->v, req->body);
            val = p->v.data;
            return apreq_value_to_param(val);
        }
    }


    switch (req->body_status) {

    case APR_SUCCESS:

        val = apr_table_get(req->body, name);
        if (val != NULL)
            return apreq_value_to_param(val);
        return NULL;


    case APR_EINIT:

        init_body(handle);
        if (req->body_status != APR_INCOMPLETE)
            return NULL;
        cgi_read(handle, APREQ_DEFAULT_READ_BLOCK_SIZE);


    case APR_INCOMPLETE:

        val = apr_table_get(req->body, name);
        if (val != NULL)
            return apreq_value_to_param(val);

        /* Not seen yet, so we need to scan for
param while prefetching the body */

        hook_ctx = apr_palloc(handle->pool, sizeof *hook_ctx);

        if (req->find_param == NULL)
            req->find_param = apreq_hook_make(handle->pool,
                                              apreq_hook_find_param,
                                              NULL, NULL);
        h = req->find_param;
        h->next = req->parser->hook;
        req->parser->hook = h;
        h->ctx = hook_ctx;
        hook_ctx->name = name;
        hook_ctx->param = NULL;
        hook_ctx->prev = req->parser->hook;

        do {
            cgi_read(handle, APREQ_DEFAULT_READ_BLOCK_SIZE);
            if (hook_ctx->param != NULL)
                return hook_ctx->param;
        } while (req->body_status == APR_INCOMPLETE);

        req->parser->hook = h->next;
        return NULL;


    default:

        if (req->body == NULL)
            return NULL;

        val = apr_table_get(req->body, name);
        if (val != NULL)
            return apreq_value_to_param(val);
        return NULL;
    }

    /* not reached */
    return NULL;
}

static apr_status_t cgi_parser_get(apreq_handle_t *handle,
                                   const apreq_parser_t **parser)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;

    *parser = req->parser;
    return APR_SUCCESS;
}

static apr_status_t cgi_parser_set(apreq_handle_t *handle,
                                   apreq_parser_t *parser)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;

    if (req->parser == NULL) {

        if (req->hook_queue != NULL) {
            apr_status_t s = apreq_parser_add_hook(parser, req->hook_queue);
            if (s != APR_SUCCESS)
                return s;
        }
        if (req->temp_dir != NULL) {
            parser->temp_dir = req->temp_dir;
        }
        if (req->brigade_limit < parser->brigade_limit) {
            parser->brigade_limit = req->brigade_limit;
        }

        req->hook_queue = NULL;
        req->parser = parser;
        return APR_SUCCESS;
    }
    else
        return APREQ_ERROR_MISMATCH;
}


static apr_status_t cgi_hook_add(apreq_handle_t *handle,
                                     apreq_hook_t *hook)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;

    if (req->parser != NULL) {
        return apreq_parser_add_hook(req->parser, hook);
    }
    else if (req->hook_queue != NULL) {
        apreq_hook_t *h = req->hook_queue;
        while (h->next != NULL)
            h = h->next;
        h->next = hook;
    }
    else {
        req->hook_queue = hook;
    }
    return APR_SUCCESS;

}

static apr_status_t cgi_brigade_limit_set(apreq_handle_t *handle,
                                          apr_size_t bytes)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;
    apr_size_t *limit = (req->parser == NULL)
                      ? &req->brigade_limit
                      : &req->parser->brigade_limit;

    if (*limit > bytes) {
        *limit = bytes;
        return APR_SUCCESS;
    }

    return APREQ_ERROR_MISMATCH;
}

static apr_status_t cgi_brigade_limit_get(apreq_handle_t *handle,
                                          apr_size_t *bytes)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;
    *bytes = (req->parser == NULL)
           ? req->brigade_limit
           : req->parser->brigade_limit;

    return APR_SUCCESS;
}

static apr_status_t cgi_read_limit_set(apreq_handle_t *handle,
                                       apr_uint64_t bytes)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;

    if (req->read_limit > bytes && req->bytes_read < bytes) {
        req->read_limit = bytes;
        return APR_SUCCESS;
    }

    return APREQ_ERROR_MISMATCH;
}


static apr_status_t cgi_read_limit_get(apreq_handle_t *handle,
                                       apr_uint64_t *bytes)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;
    *bytes = req->read_limit;
    return APR_SUCCESS;
}


static apr_status_t cgi_temp_dir_set(apreq_handle_t *handle,
                                     const char *path)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;
    const char **temp_dir = (req->parser == NULL)
                          ? &req->temp_dir
                          : &req->parser->temp_dir;


    if (*temp_dir == NULL && req->bytes_read == 0) {
        if (path != NULL)
            *temp_dir = apr_pstrdup(handle->pool, path);
        return APR_SUCCESS;
    }

    return APREQ_ERROR_MISMATCH;
}


static apr_status_t cgi_temp_dir_get(apreq_handle_t *handle,
                                     const char **path)
{
    struct cgi_handle *req = (struct cgi_handle *)handle;
    *path = (req->parser == NULL)
           ? req->temp_dir
           : req->parser->temp_dir;
    return APR_SUCCESS;
}



#ifdef APR_POOL_DEBUG
static apr_status_t ba_cleanup(void *data)
{
    apr_bucket_alloc_t *ba = data;
    apr_bucket_alloc_destroy(ba);
    return APR_SUCCESS;
}
#endif

/** Determine if we're interactive mode or not. Order is
QUERY_STRING ? NO : Interactive

I think we should just rely on GATEWAY_INTERFACE to set
non-interactive mode, and be interactive if it's not there

Behaviour change should really be:
Always check query_string before prompting user,
but rewrite body/cookies to get if interactive

Definately more work needed here...
*/
static int is_interactive_mode(apr_pool_t *pool) {
    char *value = NULL, qs[] = "GATEWAY_INTERFACE";
    apr_status_t rv;

    rv = apr_env_get(&value, qs, pool);
    if (rv != APR_SUCCESS)
        if (rv == APR_ENOENT)
            return 1;
        
        /** handle else? (!SUCCESS && !ENOENT) */
    return 0;
}

static APREQ_MODULE(cgi, 20090110);

APREQ_DECLARE(apreq_handle_t *)apreq_handle_cgi(apr_pool_t *pool)
{
    apr_bucket_alloc_t *ba;
    struct cgi_handle *req;
    void *data;

    apr_pool_userdata_get(&data, USER_DATA_KEY, pool);

    if (data != NULL)
        return data;

    req = apr_pcalloc(pool, sizeof *req);
    ba = apr_bucket_alloc_create(pool);

    /* check pool's userdata first. */

    req->handle.module = &cgi_module;
    req->handle.pool = pool;
    req->handle.bucket_alloc = ba;
    req->read_limit = (apr_uint64_t) -1;
    req->brigade_limit = APREQ_DEFAULT_BRIGADE_LIMIT;

    req->args = apr_table_make(pool, APREQ_DEFAULT_NELTS);
    req->body = apr_table_make(pool, APREQ_DEFAULT_NELTS);
    req->jar = apr_table_make(pool, APREQ_DEFAULT_NELTS);

    req->args_status =
        req->jar_status =
            req->body_status = APR_EINIT;

    if (is_interactive_mode(pool)) {
        req->interactive_mode = 1;
        apr_file_open_stdout(&(req->sout), pool);
        apr_file_open_stdin(&(req->sin), pool);
        req->promptstr=apr_pstrdup(pool, DEFAULT_PROMPT);
    }

    apr_pool_userdata_setn(&req->handle, USER_DATA_KEY, NULL, pool);

#ifdef APR_POOL_DEBUG
    apr_pool_cleanup_register(pool, ba, ba_cleanup, ba_cleanup);
#endif

    return &req->handle;
}
Something went wrong with that request. Please try again.