summaryrefslogtreecommitdiffabout
path: root/src/cgi_gateway.cc
Unidiff
Diffstat (limited to 'src/cgi_gateway.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--src/cgi_gateway.cc175
1 files changed, 164 insertions, 11 deletions
diff --git a/src/cgi_gateway.cc b/src/cgi_gateway.cc
index eae7a03..30410f2 100644
--- a/src/cgi_gateway.cc
+++ b/src/cgi_gateway.cc
@@ -1,19 +1,23 @@
1#include <errno.h>
2#include <ctype.h>
1#include "kingate/cgi_gateway.h" 3#include "kingate/cgi_gateway.h"
2#include "kingate/util.h" 4#include "kingate/util.h"
3#include "kingate/exception.h" 5#include "kingate/exception.h"
4 6
5namespace kingate { 7namespace kingate {
6 8
9 static string empty_string;
10
7 cgi_gateway::cgi_gateway(cgi_interface& ci) 11 cgi_gateway::cgi_gateway(cgi_interface& ci)
8 : iface(ci), b_parsed_content(false) { 12 : iface(ci), b_parsed_content(false) {
9 // Fetch GET content 13 // Fetch GET content
10 if(iface.has_meta("QUERY_STRING")) { 14 try {
11 string qs = iface.get_meta("QUERY_STRING"); 15 string qs = get_meta("QUERY_STRING");
12 parse_query(qs,get); 16 parse_query(qs,get);
13 } 17 }catch(exception_notfound& enf) { }
14 // Fetch POST content 18 // Fetch POST content
15 if(!strcasecmp(get_content_type().c_str(),"application/x-www-form-urlencoded")) { 19 if(!strcasecmp(content_type().c_str(),"application/x-www-form-urlencoded")) {
16 unsigned long cl = get_content_length(); 20 unsigned long cl = content_length();
17 if(cl) { 21 if(cl) {
18 char * tmp = new char[cl]; 22 char * tmp = new char[cl];
19 iface.in().read(tmp,cl); 23 iface.in().read(tmp,cl);
@@ -28,7 +32,7 @@ namespace kingate {
28 bool cgi_gateway::has_GET(const string& n) const { 32 bool cgi_gateway::has_GET(const string& n) const {
29 return get.find(n) != get.end(); 33 return get.find(n) != get.end();
30 } 34 }
31 string cgi_gateway::get_GET(const string& n) const { 35 const string& cgi_gateway::get_GET(const string& n) const {
32 params_t::const_iterator i = get.find(n); 36 params_t::const_iterator i = get.find(n);
33 if(i==get.end()) 37 if(i==get.end())
34 throw exception_notfound(CODEPOINT,"no such parameter"); 38 throw exception_notfound(CODEPOINT,"no such parameter");
@@ -37,7 +41,7 @@ namespace kingate {
37 bool cgi_gateway::has_POST(const string& n) const { 41 bool cgi_gateway::has_POST(const string& n) const {
38 return post.find(n) != post.end(); 42 return post.find(n) != post.end();
39 } 43 }
40 string cgi_gateway::get_POST(const string& n) const { 44 const string& cgi_gateway::get_POST(const string& n) const {
41 params_t::const_iterator i = post.find(n); 45 params_t::const_iterator i = post.find(n);
42 if(i==post.end()) 46 if(i==post.end())
43 throw exception_notfound(CODEPOINT,"no such parameter"); 47 throw exception_notfound(CODEPOINT,"no such parameter");
@@ -46,7 +50,7 @@ namespace kingate {
46 bool cgi_gateway::has_param(const string& n) const { 50 bool cgi_gateway::has_param(const string& n) const {
47 return has_GET(n) || has_POST(n); 51 return has_GET(n) || has_POST(n);
48 } 52 }
49 string cgi_gateway::get_param(const string& n) const { 53 const string& cgi_gateway::get_param(const string& n) const {
50 params_t::const_iterator i = get.find(n); 54 params_t::const_iterator i = get.find(n);
51 if(i!=get.end()) 55 if(i!=get.end())
52 return i->second; 56 return i->second;
@@ -56,9 +60,12 @@ namespace kingate {
56 throw exception_notfound(CODEPOINT,"no such parameter"); 60 throw exception_notfound(CODEPOINT,"no such parameter");
57 } 61 }
58 62
63 /*
64 * deprecated stuff.
65 */
59 const string& cgi_gateway::get_content_type() const { 66 const string& cgi_gateway::get_content_type() const {
60 if(!has_meta("CONTENT_TYPE")) 67 if(!has_meta("CONTENT_TYPE"))
61 return ""; // XXX: 68 return empty_string;
62 return get_meta("CONTENT_TYPE"); 69 return get_meta("CONTENT_TYPE");
63 } 70 }
64 unsigned long cgi_gateway::get_content_length() const { 71 unsigned long cgi_gateway::get_content_length() const {
@@ -67,6 +74,152 @@ namespace kingate {
67 string cl = get_meta("CONTENT_LENGTH"); 74 string cl = get_meta("CONTENT_LENGTH");
68 return strtol(cl.c_str(),NULL,10); 75 return strtol(cl.c_str(),NULL,10);
69 } 76 }
77 /*
78 *
79 */
80
81 const string& cgi_gateway::http_request_header(const string& hn) const {
82 string mvn = "HTTP_";
83 for(const char* p=hn.c_str();*p;p++) {
84 if(*p=='-')
85 mvn += '_';
86 else
87 mvn += toupper(*p);
88 }
89 return get_meta(mvn);
90 }
91
92 const string& cgi_gateway::auth_type() const {
93 try {
94 return get_meta("AUTH_TYPE");
95 }catch(exception_notfound& enf) {
96 return empty_string;
97 }
98 }
99 unsigned long cgi_gateway::content_length() const {
100 try {
101 const string& cl = get_meta("CONTENT_LENGTH");
102 errno = 0;
103 const char *clp = cl.c_str();
104 unsigned long rv = strtol(clp,(char**)&clp,10);
105 if(errno || *clp)
106 throw server_error(CODEPOINT,"Invalid CONTENT_LENGTH value passed from server");
107 return rv;
108 }catch(exception_notfound& enf) {
109 return 0;
110 }
111 }
112 const string& cgi_gateway::content_type() const {
113 try {
114 return get_meta("CONTENT_TYPE");
115 }catch(exception_notfound& enf) {
116 return empty_string;
117 }
118 }
119 const string& cgi_gateway::gateway_interface() const {
120 try {
121 return get_meta("GATEWAY_INTERFACE");
122 }catch(exception_notfound& enf) {
123 return empty_string;
124 }
125 }
126 const string& cgi_gateway::path_info() const {
127 try {
128 return get_meta("PATH_INFO");
129 }catch(exception_notfound& enf) {
130 return empty_string;
131 }
132 }
133 const string& cgi_gateway::path_translated() const {
134 try {
135 return get_meta("PATH_TRANSLATED");
136 }catch(exception_notfound& enf) {
137 return empty_string;
138 }
139 }
140 const string& cgi_gateway::query_string() const {
141 try {
142 return get_meta("QUERY_STRING");
143 }catch(exception_notfound& enf) {
144 return empty_string;
145 }
146 }
147 const string& cgi_gateway::remote_addr() const {
148 try {
149 return get_meta("REMOTE_ADDR");
150 }catch(exception_notfound& enf) {
151 return empty_string;
152 }
153 }
154 const string& cgi_gateway::remote_host() const {
155 try {
156 return get_meta("REMOTE_HOST");
157 }catch(exception_notfound& enf) {
158 return remote_addr();
159 }
160 }
161 const string& cgi_gateway::remote_ident() const {
162 try {
163 return get_meta("REMOTE_IDENT");
164 }catch(exception_notfound& enf) {
165 return empty_string;
166 }
167 }
168 const string& cgi_gateway::remote_user() const {
169 try {
170 return get_meta("REMOTE_USER");
171 }catch(exception_notfound& enf) {
172 return empty_string;
173 }
174 }
175 const string& cgi_gateway::request_method() const {
176 try {
177 return get_meta("REQUEST_METHOD");
178 }catch(exception_notfound& enf) {
179 throw server_error(CODEPOINT,"No REQUEST_METHOD passed from server");
180 }
181 }
182 const string& cgi_gateway::script_name() const {
183 try {
184 return get_meta("SCRIPT_NAME");
185 }catch(exception_notfound& enf) {
186 throw server_error(CODEPOINT,"No SCRIPT_NAME passed from server");
187 }
188 }
189 const string& cgi_gateway::server_name() const {
190 try {
191 return get_meta("SERVER_NAME");
192 }catch(exception_notfound& enf) {
193 throw server_error(CODEPOINT,"No SERVER_NAME passed from server");
194 }
195 }
196 unsigned int cgi_gateway::server_port() const {
197 try {
198 const string& sp = get_meta("SERVER_PORT");
199 errno = 0;
200 const char *spp = sp.c_str();
201 unsigned int rv = strtol(spp,(char**)&spp,10);
202 if(errno || *spp)
203 throw server_error(CODEPOINT,"Invalid SERVER_PORT value passed from server");
204 return rv;
205 }catch(exception_notfound& enf) {
206 throw server_error(CODEPOINT,"No SERVER_PORT passed from server");
207 }
208 }
209 const string& cgi_gateway::server_protocol() const {
210 try {
211 return get_meta("SERVER_PROTOCOL");
212 }catch(exception_notfound& enf) {
213 throw server_error(CODEPOINT,"No SERVER_PROTOCOL passed from server");
214 }
215 }
216 const string& cgi_gateway::server_software() const {
217 try {
218 return get_meta("SERVER_SOFTWARE");
219 }catch(exception_notfound& enf) {
220 throw server_error(CODEPOINT,"No SERVER_SOFTWARE passed from server");
221 }
222 }
70 223
71 void cgi_gateway::parse_query(string& q,params_t& p) { 224 void cgi_gateway::parse_query(string& q,params_t& p) {
72 while(!q.empty()) { 225 while(!q.empty()) {
@@ -78,9 +231,9 @@ namespace kingate {
78 q.erase(0,amp+1); 231 q.erase(0,amp+1);
79 string::size_type eq = pp.find('='); 232 string::size_type eq = pp.find('=');
80 if(eq == string::npos) { 233 if(eq == string::npos) {
81 p.insert(params_t::value_type("",url_unescape(pp))); 234 p.insert(params_t::value_type("",url_decode(pp)));
82 }else{ 235 }else{
83 p.insert(params_t::value_type(url_unescape(pp.substr(0,eq)),url_unescape(pp.substr(eq+1)))); 236 p.insert(params_t::value_type(url_decode(pp.substr(0,eq)),url_decode(pp.substr(eq+1))));
84 } 237 }
85 } 238 }
86 } 239 }