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