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) (side-by-side diff)
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) (show 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 @@
+#include "kingate/cgi_gateway.h"
+#include "kingate/util.h"
+#include "kingate/exception.h"
+
+namespace kingate {
+
+ cgi_gateway::cgi_gateway(cgi_interface& ci)
+ : iface(ci), b_parsed_content(false) {
+ // Fetch GET content
+ if(iface.has_meta("QUERY_STRING")) {
+ string qs = iface.get_meta("QUERY_STRING");
+ parse_query(qs,get);
+ }
+ // Fetch POST content
+ if(!strcasecmp(get_content_type().c_str(),"application/x-www-form-urlencoded")) {
+ unsigned long cl = get_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;
+ }
+ }
+
+ bool cgi_gateway::has_GET(const string& n) const {
+ return get.find(n) != get.end();
+ }
+ 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();
+ }
+ 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);
+ }
+ 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");
+ }
+
+ const string& cgi_gateway::get_content_type() const {
+ if(!has_meta("CONTENT_TYPE"))
+ return ""; // XXX:
+ 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);
+ }
+
+ 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_unescape(pp)));
+ }else{
+ p.insert(params_t::value_type(url_unescape(pp.substr(0,eq)),url_unescape(pp.substr(eq+1))));
+ }
+ }
+ }
+
+}