summaryrefslogtreecommitdiffabout
path: root/src/cgi_gateway.cc
authorMichael Krelin <hacker@klever.net>2005-01-29 20:14:37 (UTC)
committer Michael Krelin <hacker@klever.net>2005-01-29 20:14:37 (UTC)
commitff4b919683537625f693eedf53006364d0f8444d (patch) (unidiff)
tree4c19e38c0832b16b4ca98ae5af6542d932373eb1 /src/cgi_gateway.cc
parentf9a64a67c89a7566e63ed66c3a69c359abea4dfd (diff)
downloadkingate-0.0.zip
kingate-0.0.tar.gz
kingate-0.0.tar.bz2
initial commit into repository0.0
Diffstat (limited to 'src/cgi_gateway.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--src/cgi_gateway.cc88
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
5namespace 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}