-rw-r--r-- | src/cgi_gateway.cc | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/cgi_gateway.cc b/src/cgi_gateway.cc new file mode 100644 index 0000000..eae7a03 --- a/dev/null +++ b/src/cgi_gateway.cc | |||
@@ -0,0 +1,88 @@ | |||
1 | #include "kingate/cgi_gateway.h" | ||
2 | #include "kingate/util.h" | ||
3 | #include "kingate/exception.h" | ||
4 | |||
5 | namespace kingate { | ||
6 | |||
7 | cgi_gateway::cgi_gateway(cgi_interface& ci) | ||
8 | : iface(ci), b_parsed_content(false) { | ||
9 | // Fetch GET content | ||
10 | if(iface.has_meta("QUERY_STRING")) { | ||
11 | string qs = iface.get_meta("QUERY_STRING"); | ||
12 | parse_query(qs,get); | ||
13 | } | ||
14 | // Fetch POST content | ||
15 | if(!strcasecmp(get_content_type().c_str(),"application/x-www-form-urlencoded")) { | ||
16 | unsigned long cl = get_content_length(); | ||
17 | if(cl) { | ||
18 | char * tmp = new char[cl]; | ||
19 | iface.in().read(tmp,cl); | ||
20 | string qs(tmp,cl); | ||
21 | delete tmp; | ||
22 | parse_query(qs,post); | ||
23 | } | ||
24 | b_parsed_content = true; | ||
25 | } | ||
26 | } | ||
27 | |||
28 | bool cgi_gateway::has_GET(const string& n) const { | ||
29 | return get.find(n) != get.end(); | ||
30 | } | ||
31 | string cgi_gateway::get_GET(const string& n) const { | ||
32 | params_t::const_iterator i = get.find(n); | ||
33 | if(i==get.end()) | ||
34 | throw exception_notfound(CODEPOINT,"no such parameter"); | ||
35 | return i->second; | ||
36 | } | ||
37 | bool cgi_gateway::has_POST(const string& n) const { | ||
38 | return post.find(n) != post.end(); | ||
39 | } | ||
40 | string cgi_gateway::get_POST(const string& n) const { | ||
41 | params_t::const_iterator i = post.find(n); | ||
42 | if(i==post.end()) | ||
43 | throw exception_notfound(CODEPOINT,"no such parameter"); | ||
44 | return i->second; | ||
45 | } | ||
46 | bool cgi_gateway::has_param(const string& n) const { | ||
47 | return has_GET(n) || has_POST(n); | ||
48 | } | ||
49 | string cgi_gateway::get_param(const string& n) const { | ||
50 | params_t::const_iterator i = get.find(n); | ||
51 | if(i!=get.end()) | ||
52 | return i->second; | ||
53 | i = post.find(n); | ||
54 | if(i!=post.end()) | ||
55 | return i->second; | ||
56 | throw exception_notfound(CODEPOINT,"no such parameter"); | ||
57 | } | ||
58 | |||
59 | const string& cgi_gateway::get_content_type() const { | ||
60 | if(!has_meta("CONTENT_TYPE")) | ||
61 | return ""; // XXX: | ||
62 | return get_meta("CONTENT_TYPE"); | ||
63 | } | ||
64 | unsigned long cgi_gateway::get_content_length() const { | ||
65 | if(!has_meta("CONTENT_LENGTH")) | ||
66 | return 0; | ||
67 | string cl = get_meta("CONTENT_LENGTH"); | ||
68 | return strtol(cl.c_str(),NULL,10); | ||
69 | } | ||
70 | |||
71 | void cgi_gateway::parse_query(string& q,params_t& p) { | ||
72 | while(!q.empty()) { | ||
73 | string::size_type amp = q.find('&'); | ||
74 | string pp = (amp==string::npos)?q:q.substr(0,amp); | ||
75 | if(amp==string::npos) | ||
76 | q.clear(); | ||
77 | else | ||
78 | q.erase(0,amp+1); | ||
79 | string::size_type eq = pp.find('='); | ||
80 | if(eq == string::npos) { | ||
81 | p.insert(params_t::value_type("",url_unescape(pp))); | ||
82 | }else{ | ||
83 | p.insert(params_t::value_type(url_unescape(pp.substr(0,eq)),url_unescape(pp.substr(eq+1)))); | ||
84 | } | ||
85 | } | ||
86 | } | ||
87 | |||
88 | } | ||