summaryrefslogtreecommitdiffabout
path: root/src/cgi_gateway.cc
blob: a2681aac3cfaff022e9526322e26047b96989a8f (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
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
#include <errno.h>
#include <ctype.h>
#include <sstream>
#include "kingate/cgi_gateway.h"
#include "kingate/util.h"
#include "kingate/exception.h"
#include "config.h"
#ifdef HAVE_MIMETIC
# include <mimetic/mimeentity.h>
# include <mimetic/parser/itparser.h>
#endif /* HAVE_MIMETIC */

namespace kingate {

#ifdef HAVE_MIMETIC
    using mimetic::MimeEntity;

    struct TornMimeEntity : public MimeEntity {
	typedef istreambuf_iterator<char> it_type;
	typedef it_type::iterator_category it_cat;
	struct IParser : public mimetic::IteratorParser<it_type,it_cat> {
	    typedef mimetic::IteratorParser<it_type,it_cat> BT;
	    IParser(MimeEntity& me)
		: BT::IteratorParser<it_type,it_cat>(me) { }
	    void loadHeader(it_type bit,it_type eit) {
		m_bit = bit; m_eit = eit;
		BT::loadHeader();
	    }
	    void loadBody(it_type bit,it_type eit) {
		m_bit = bit; m_eit = eit;
		BT::loadBody();
	    }
	};
	void load(istream& hs,istream& bs,int mask=0) {
	    IParser prs(*this);
	    prs.iMask(mask);
	    prs.loadHeader(it_type(hs),it_type());
	    prs.loadBody(it_type(bs),it_type());
	}
    };
#endif /* HAVE_MIMETIC */

    static string empty_string;

    cgi_gateway::basic_file_t::~basic_file_t() { }

    class string_file_t : public cgi_gateway::basic_file_t {
	public:
	    string _file_name;
	    string _content_type;
	    stringstream _content;

	    string_file_t(const string& fn,const string& ct,const string& s)
		: _file_name(fn), _content_type(ct), _content(s,ios::in) { }
	    const string& filename() const { return _file_name; }
	    const string& content_type() const { return _content_type; }
	    istream& content() { return _content; }
    };

    cgi_gateway::cgi_gateway(cgi_interface& ci,bool parsebody)
	: iface(ci), b_parsed_content(false) {
	    // Fetch GET content
	    try {
		string qs = get_meta("QUERY_STRING");
		parse_query(qs,get);
	    }catch(exception_notfound& enf) { }
	    if(parsebody)
		parse_request_body();
	    // Parse cookies
	    try {
		cookies.parse_cookies(get_meta("HTTP_COOKIE"));
	    }catch(exception_notfound& enf) { }
	}

    cgi_gateway::~cgi_gateway() throw() {
	for(files_t::iterator i=files.begin();i!=files.end();++i)
	    delete i->second;
	files.clear();
    }

    void cgi_gateway::parse_request_body() {
	if(b_parsed_content)
	    throw konforka::exception(CODEPOINT,"request body is already parsed");
	// Fetch POST content
	if(!strncasecmp(
		    content_type().c_str(),
		    "application/x-www-form-urlencoded",
		    sizeof("application/x-www-form-urlencoded")-1) ) {
	    unsigned long cl = content_length();
	    if(cl) {
		char * tmp = new char[cl];
		iface.in().read(tmp,cl);
		string qs(tmp,cl);
		delete tmp;
		parse_query(qs,post);
	    }
	    b_parsed_content = true;
	}
#ifdef HAVE_MIMETIC
	 else if(!strncasecmp(
		    content_type().c_str(),
		    "multipart/form-data",
		    sizeof("multipart/form-data")-1) ) {
	    stringstream h;
	    h
		<< "Content-Type: " << content_type() << "\r\n"
		<< "Content-Length: " << content_length() << "\r\n\n";
	    TornMimeEntity me;
	    me.load(h,iface.in(),0);
	    mimetic::MimeEntityList& parts = me.body().parts();
	    for(mimetic::MimeEntityList::iterator i=parts.begin();i!=parts.end();++i) {
		MimeEntity *p = *i;
		const mimetic::ContentDisposition& cd = p->header().contentDisposition();
		string n = cd.param("name");
		string fn = cd.param("filename");
		if(fn.empty()) {
		    post.insert(params_t::value_type(n,p->body()));
		}else{
		    const mimetic::ContentType& ct = p->header().contentType();
		    files.insert(files_t::value_type(n,new string_file_t(fn,ct.str(),p->body())));
		}
	    }
	    b_parsed_content = true;
	}
#endif /* HAVE_MIMETIC */
    }

    bool cgi_gateway::has_GET(const string& n) const {
	return get.find(n) != get.end();
    }
    const string& cgi_gateway::get_GET(const string& n) const {
	params_t::const_iterator i = get.find(n);
	if(i==get.end())
	    throw exception_notfound(CODEPOINT,"no such parameter");
	return i->second;
    }
    bool cgi_gateway::has_POST(const string& n) const {
	return post.find(n) != post.end();
    }
    const string& cgi_gateway::get_POST(const string& n) const {
	params_t::const_iterator i = post.find(n);
	if(i==post.end())
	    throw exception_notfound(CODEPOINT,"no such parameter");
	return i->second;
    }
    bool cgi_gateway::has_param(const string& n) const {
	return has_GET(n) || has_POST(n);
    }
    const string& cgi_gateway::get_param(const string& n) const {
	params_t::const_iterator i = get.find(n);
	if(i!=get.end())
	    return i->second;
	i = post.find(n);
	if(i!=post.end())
	    return i->second;
	throw exception_notfound(CODEPOINT,"no such parameter");
    }
    bool cgi_gateway::has_file(const string& n) const {
	return files.find(n) != files.end();
    }
    const cgi_gateway::file_t cgi_gateway::get_file(const string& n) const {
	files_t::const_iterator i = files.find(n);
	if(i==files.end())
	    throw exception_notfound(CODEPOINT,"no such parameter");
	return i->second;
    }
    cgi_gateway::file_t cgi_gateway::get_file(const string& n) {
	files_t::const_iterator i = files.find(n);
	if(i==files.end())
	    throw exception_notfound(CODEPOINT,"no such parameter");
	return i->second;
    }

    /*
     * deprecated stuff.
     */
    const string& cgi_gateway::get_content_type() const {
	if(!has_meta("CONTENT_TYPE"))
	    return empty_string;
	return get_meta("CONTENT_TYPE");
    }
    unsigned long cgi_gateway::get_content_length() const {
	if(!has_meta("CONTENT_LENGTH"))
	    return 0;
	string cl = get_meta("CONTENT_LENGTH");
	return strtol(cl.c_str(),NULL,10);
    }
    /*
     *
     */

    const string& cgi_gateway::http_request_header(const string& hn) const {
	string mvn = "HTTP_";
	for(const char* p=hn.c_str();*p;p++) {
	    if(*p=='-')
		mvn += '_';
	    else
		mvn += toupper(*p);
	}
	return get_meta(mvn);
    }

    const string& cgi_gateway::auth_type() const {
	try {
	    return get_meta("AUTH_TYPE");
	}catch(exception_notfound& enf) {
	    return empty_string;
	}
    }
    unsigned long cgi_gateway::content_length() const {
	try {
	    const string& cl = get_meta("CONTENT_LENGTH");
	    errno = 0;
	    const char *clp = cl.c_str();
	    unsigned long rv = strtol(clp,(char**)&clp,10);
	    if(errno || *clp)
		throw server_error(CODEPOINT,"Invalid CONTENT_LENGTH value passed from server");
	    return rv;
	}catch(exception_notfound& enf) {
	    return 0;
	}
    }
    const string& cgi_gateway::content_type() const {
	try {
	    return get_meta("CONTENT_TYPE");
	}catch(exception_notfound& enf) {
	    return empty_string;
	}
    }
    const string& cgi_gateway::gateway_interface() const {
	try {
	    return get_meta("GATEWAY_INTERFACE");
	}catch(exception_notfound& enf) {
	    return empty_string;
	}
    }
    const string& cgi_gateway::path_info() const {
	try {
	    return get_meta("PATH_INFO");
	}catch(exception_notfound& enf) {
	    return empty_string;
	}
    }
    const string& cgi_gateway::path_translated() const {
	try {
	    return get_meta("PATH_TRANSLATED");
	}catch(exception_notfound& enf) {
	    return empty_string;
	}
    }
    const string& cgi_gateway::query_string() const {
	try {
	    return get_meta("QUERY_STRING");
	}catch(exception_notfound& enf) {
	    return empty_string;
	}
    }
    const string& cgi_gateway::remote_addr() const {
	try {
	    return get_meta("REMOTE_ADDR");
	}catch(exception_notfound& enf) {
	    return empty_string;
	}
    }
    const string& cgi_gateway::remote_host() const {
	try {
	    return get_meta("REMOTE_HOST");
	}catch(exception_notfound& enf) {
	    return remote_addr();
	}
    }
    const string& cgi_gateway::remote_ident() const {
	try {
	    return get_meta("REMOTE_IDENT");
	}catch(exception_notfound& enf) {
	    return empty_string;
	}
    }
    const string& cgi_gateway::remote_user() const {
	try {
	    return get_meta("REMOTE_USER");
	}catch(exception_notfound& enf) {
	    return empty_string;
	}
    }
    const string& cgi_gateway::request_method() const {
	try {
	    return get_meta("REQUEST_METHOD");
	}catch(exception_notfound& enf) {
	    throw server_error(CODEPOINT,"No REQUEST_METHOD passed from server");
	}
    }
    const string& cgi_gateway::script_name() const {
	try {
	    return get_meta("SCRIPT_NAME");
	}catch(exception_notfound& enf) {
	    throw server_error(CODEPOINT,"No SCRIPT_NAME passed from server");
	}
    }
    const string& cgi_gateway::server_name() const {
	try {
	    return get_meta("SERVER_NAME");
	}catch(exception_notfound& enf) {
	    throw server_error(CODEPOINT,"No SERVER_NAME passed from server");
	}
    }
    unsigned int cgi_gateway::server_port() const {
	try {
	    const string& sp = get_meta("SERVER_PORT");
	    errno = 0;
	    const char *spp = sp.c_str();
	    unsigned int rv = strtol(spp,(char**)&spp,10);
	    if(errno || *spp)
		throw server_error(CODEPOINT,"Invalid SERVER_PORT value passed from server");
	    return rv;
	}catch(exception_notfound& enf) {
	    throw server_error(CODEPOINT,"No SERVER_PORT passed from server");
	}
    }
    const string& cgi_gateway::server_protocol() const {
	try {
	    return get_meta("SERVER_PROTOCOL");
	}catch(exception_notfound& enf) {
	    throw server_error(CODEPOINT,"No SERVER_PROTOCOL passed from server");
	}
    }
    const string& cgi_gateway::server_software() const {
	try {
	    return get_meta("SERVER_SOFTWARE");
	}catch(exception_notfound& enf) {
	    throw server_error(CODEPOINT,"No SERVER_SOFTWARE passed from server");
	}
    }

    void cgi_gateway::parse_query(string& q,params_t& p) {
	while(!q.empty()) {
	    string::size_type amp = q.find('&');
	    string pp = (amp==string::npos)?q:q.substr(0,amp);
	    if(amp==string::npos)
		q.clear();
	    else
		q.erase(0,amp+1);
	    string::size_type eq = pp.find('=');
	    if(eq == string::npos) {
		p.insert(params_t::value_type("",url_decode(pp)));
	    }else{
		p.insert(params_t::value_type(url_decode(pp.substr(0,eq)),url_decode(pp.substr(eq+1))));
	    }
	}
    }

}