summaryrefslogtreecommitdiff
path: root/agen5/agCgi.c
blob: 641988db872aefa1f69b7fa35fd4f5f60de67990 (plain)
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

/**
 * @file agCgi.c
 *
 *  Time-stamp:        "2012-03-04 19:19:12 bkorb"
 *
 *  This is a CGI wrapper for AutoGen.  It will take POST-method
 *  name-value pairs and emit AutoGen definitions to a spawned
 *  AutoGen process.
 *
 *  This file is part of AutoGen.
 *  AutoGen Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
 *
 * AutoGen is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * AutoGen is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

typedef struct {
    char const*  pzName;
    char*        pzValue;
} tNameMap;

#define ENV_TABLE \
    _ET_(SERVER_SOFTWARE) \
    _ET_(SERVER_NAME) \
    _ET_(GATEWAY_INTERFACE) \
    _ET_(SERVER_PROTOCOL) \
    _ET_(SERVER_PORT) \
    _ET_(REQUEST_METHOD) \
    _ET_(PATH_INFO) \
    _ET_(PATH_TRANSLATED) \
    _ET_(SCRIPT_NAME) \
    _ET_(QUERY_STRING) \
    _ET_(REMOTE_HOST) \
    _ET_(REMOTE_ADDR) \
    _ET_(AUTH_TYPE) \
    _ET_(REMOTE_USER) \
    _ET_(REMOTE_IDENT) \
    _ET_(CONTENT_TYPE) \
    _ET_(CONTENT_LENGTH) \
    _ET_(HTTP_ACCEPT) \
    _ET_(HTTP_USER_AGENT) \
    _ET_(HTTP_REFERER)

static tNameMap nameValueMap[] = {
#define _ET_(n) { #n, NULL },
    ENV_TABLE
#undef _ET_
    { NULL, NULL }
};

typedef enum {
#define _ET_(n) n ## _IDX,
    ENV_TABLE
#undef _ET_
    NAME_CT
} tNameIdx;

#define pzCgiMethod nameValueMap[ REQUEST_METHOD_IDX ].pzValue
#define pzCgiQuery  nameValueMap[ QUERY_STRING_IDX   ].pzValue
#define pzCgiLength nameValueMap[ CONTENT_LENGTH_IDX ].pzValue

/* = = = START-STATIC-FORWARD = = = */
static char*
parseInput(char* pzSrc, int len);
/* = = = END-STATIC-FORWARD = = = */

LOCAL void
loadCgi(void)
{
    /*
     *  Redirect stderr to a file.  If it gets used, we must trap it
     *  and emit the content-type: preamble before actually emitting it.
     *  First, tho, do a simple stderr->stdout redirection just in case
     *  we stumble before we're done with this.
     */
    dup2(STDOUT_FILENO, STDERR_FILENO);
    (void)fdopen(STDERR_FILENO, "w" FOPEN_BINARY_FLAG);
    oops_pfx = CGI_ERR_MSG_FMT;
    {
        int tmpfd;
        AGDUPSTR(cgi_stderr, CGI_TEMP_ERR_FILE_STR, "stderr file");
        tmpfd = mkstemp(cgi_stderr);
        if (tmpfd < 0)
            AG_ABEND(aprf(MKSTEMP_FAIL_FMT, cgi_stderr));
        dup2(tmpfd, STDERR_FILENO);
        close(tmpfd);
    }

    /*
     *  Pull the CGI-relevant environment variables.  Anything we don't find
     *  gets an empty string default.
     */
    {
        tNameMap* pNM = nameValueMap;
        tNameIdx  ix  = (tNameIdx)0;

        do  {
            pNM->pzValue = getenv(pNM->pzName);
            if (pNM->pzValue == NULL)
                pNM->pzValue = (char*)zNil;
        } while (pNM++, ++ix < NAME_CT);
    }

    base_ctx = (scan_ctx_t*)AGALOC(sizeof(scan_ctx_t), "CGI ctx");
    memset((void*)base_ctx, 0, sizeof(scan_ctx_t));

    {
        size_t textLen = strtoul(pzCgiLength, NULL, 0);
        char*  pzText;

        if (strcasecmp(pzCgiMethod, "POST") == 0) {
            if (textLen == 0)
                AG_ABEND(LOAD_CGI_NO_DATA_MSG);

            pzText  = AGALOC(textLen + 1, "CGI POST");
            if (fread(pzText, (size_t)1, textLen, stdin) != textLen)
                AG_CANT(LOAD_CGI_READ_NAME, LOAD_CGI_READ_WHAT);

            pzText[ textLen ] = NUL;

            base_ctx->scx_data = parseInput(pzText, (int)textLen);
            AGFREE(pzText);

        } else if (strcasecmp(pzCgiMethod, LOAD_CGI_GET_NAME) == 0) {
            if (textLen == 0)
                textLen = strlen(pzCgiQuery);
            base_ctx->scx_data = parseInput(pzCgiQuery, (int)textLen);

        } else {
            AG_ABEND(aprf(LOAD_CGI_INVAL_REQ_FMT, pzCgiMethod));
            /* NOTREACHED */
#ifdef  WARNING_WATCH
            pzText = NULL;
#endif
        }
    }

    base_ctx->scx_line  = 1;
    base_ctx->scx_fname = LOAD_CGI_DEFS_MARKER;
    base_ctx->scx_scan  = base_ctx->scx_data;
}


static char*
parseInput(char* pzSrc, int len)
{
#   define defLen   (sizeof("Autogen Definitions cgi;\n") - 1)
    char*  pzRes  = AGALOC((len * 2) + defLen + 1, "CGI Definitions");

    memcpy(pzRes, PARSE_INPUT_AG_DEF_STR, defLen);
    (void)cgi_run_fsm(pzSrc, len, pzRes + defLen, len*2);
    return AGREALOC(pzRes, strlen(pzRes)+1, "CGI input");
}

/*
 * Local Variables:
 * mode: C
 * c-file-style: "stroustrup"
 * indent-tabs-mode: nil
 * End:
 * end of agen5/agCgi.c */