Annotation of parser3/src/targets/cgi/pa_sapi_info.h, revision 1.15
1.1 moko 1: #ifndef PA_SAPI_INFO_H
2: #define PA_SAPI_INFO_H
3:
1.15 ! moko 4: #define IDENT_PA_SAPI_INFO_H "$Id: pa_sapi_info.h,v 1.14 2020/12/17 11:47:17 moko Exp $"
1.1 moko 5:
6: #include "pa_sapi.h"
7: #include "pa_http.h"
8:
9: /// IIS refuses to read bigger chunks
10: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M
11:
1.12 moko 12: // for signal handlers and cgi console detection
13: static Request *request=0;
14:
15:
1.1 moko 16: class SAPI_Info : public PA_Allocated {
17: public:
18: int http_response_code;
19:
1.7 moko 20: SAPI_Info() : http_response_code(200) {}
1.1 moko 21:
22: virtual char* get_env(const char* name) {
23: if(char *local=getenv(name))
24: return pa_strdup(local);
25: else
26: return 0;
27: }
28:
1.10 moko 29: virtual bool set_env(const char*, const char*) {
1.8 moko 30: return false;
31: }
32:
1.1 moko 33: virtual const char* const *get_env() {
34: #ifdef _MSC_VER
35: extern char **_environ;
36: return _environ;
37: #else
38: extern char **environ;
39: return environ;
40: #endif
41: }
42:
43: virtual size_t read_post(char *, size_t) {
44: return 0;
45: }
46:
47: virtual void add_header_attribute(const char* dont_store_key, const char* dont_store_value) {
48: if(strcasecmp(dont_store_key, HTTP_STATUS)==0)
49: http_response_code=atoi(dont_store_value);
50: }
51:
52: virtual void send_header() {}
53:
54: virtual size_t send_body(const void *buf, size_t size) {
55: return stdout_write(buf, size);
56: }
57:
58: } *sapiInfo = NULL;
59:
60: class SAPI_Info_CGI : public SAPI_Info {
61: public:
62:
63: virtual size_t read_post(char *buf, size_t max_bytes) {
64: size_t read_size=0;
65: do {
66: ssize_t chunk_size=read(fileno(stdin), buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
67: if(chunk_size<=0)
68: break;
69: read_size+=chunk_size;
70: } while(read_size<max_bytes);
71: return read_size;
72: }
73:
74: virtual void add_header_attribute(const char* dont_store_key, const char* dont_store_value) {
75: SAPI_Info::add_header_attribute(dont_store_key, dont_store_value);
1.12 moko 76: if(!request || !request->console.was_used())
1.1 moko 77: printf("%s: %s\n", capitalize(dont_store_key), dont_store_value);
78: }
79:
80: virtual void send_header() {
81: puts("");
82: }
83:
84:
85: };
86:
1.3 moko 87: char* replace_char(char* str, char from, char to){
88: for(char *pos = strchr(str,from); pos; pos=strchr(pos,from)) {
89: *pos = to;
90: }
91: return str;
92: }
93:
1.1 moko 94: class SAPI_Info_HTTPD : public SAPI_Info {
95: public:
96:
97: HTTPD_Connection &connection;
1.14 moko 98: String::Body output;
1.3 moko 99: HashStringString env;
1.1 moko 100:
101: SAPI_Info_HTTPD(HTTPD_Connection &aconnection) : connection(aconnection) {}
102:
1.3 moko 103: void populate_env() {
104: String::Body host("localhost");
1.2 moko 105: for(Array_iterator<HTTP_Headers::Header> i(connection.headers()); i.has_next(); ){
106: HTTP_Headers::Header header=i.next();
1.3 moko 107: String name("HTTP_");
108: name << replace_char(header.name.cstrm(), '-', '_');
109: String::Body value=header.value;
110:
111: if(header.name == "HOST"){
112: size_t port=value.pos(':');
113: if(port != STRING_NOT_FOUND)
114: value=value.mid(0, port);
115: host=value;
116: }
117: env.put(name, value);
1.1 moko 118: }
1.3 moko 119:
120: env.put("REQUEST_METHOD", connection.method());
121: env.put("REQUEST_URI", connection.uri());
122: env.put("QUERY_STRING", connection.query());
123:
124: env.put("SERVER_NAME", host);
1.15 ! moko 125: env.put("SERVER_PORT", HTTPD_Server::port);
1.3 moko 126: env.put("REMOTE_ADDR", connection.remote_addr);
127:
1.1 moko 128: }
129:
1.3 moko 130: virtual char* get_env(const char* name) {
131: String::Body value = env.get(name);
132: return !value ? NULL : value.cstrm();
1.1 moko 133: }
134:
1.8 moko 135: virtual bool set_env(const char* name, const char* value) {
136: env.put(name, *new String(value));
137: return true;
138: }
139:
1.1 moko 140: virtual const char* const *get_env() {
1.3 moko 141: const char** result=new(PointerGC) const char*[env.count()+1/*0*/];
1.1 moko 142: const char** cur=result;
1.3 moko 143: for(HashStringString::Iterator i(env); i; i.next()){
144: String pair;
145: pair << i.key() << "=" << i.value();
146: *cur++=pair.cstr();
1.1 moko 147: }
148: *cur=NULL;
149: return result;
150: }
151:
1.4 moko 152: virtual size_t read_post(char *buf, size_t max_bytes) {
153: return connection.read_post(buf, max_bytes);
1.1 moko 154: }
155:
156: virtual void add_header_attribute(const char* dont_store_key, const char* dont_store_value) {
157: if(strcasecmp(dont_store_key, "location")==0)
158: http_response_code=302;
159: if(strcasecmp(dont_store_key, HTTP_STATUS)==0)
160: http_response_code=atoi(dont_store_value);
161: else
162: output << capitalize(dont_store_key) << ": " << pa_strdup(dont_store_value) << "\r\n";
163: }
164:
1.14 moko 165: void clear_response_headers() {
166: http_response_code=200;
167: output.clear();
168: }
169:
1.6 moko 170: static const char *exception_http_status(const char *type) {
171: struct Lookup {
172: const char *code;
173: const char *type;
174: } static lookup[] = {
1.13 moko 175: { "", "httpd.write"},
1.6 moko 176: {"400", "httpd.request"},
1.9 moko 177: {"400", "http.response"},
1.6 moko 178: {"404", "file.missing"},
179: {"408", "httpd.timeout"},
1.13 moko 180: {"408", "httpd.read"},
1.6 moko 181: {"501", "httpd.method"},
182: { NULL, ""}
183: };
184: Lookup *cur = lookup;
185: for(; cur->code; cur++)
186: if(!strcmp(type, cur->type))
187: return cur->code;
188: return "500";
189: }
190:
191: static const char *status_message(int code) {
1.1 moko 192: struct Lookup {
193: int code;
194: const char *message;
195: } static lookup[] = {
196: {200, "OK"},
1.11 moko 197: {204, "No Content"},
1.1 moko 198: {206, "Partial Content"},
199: {301, "Moved Permanently"},
200: {302, "Found"},
1.6 moko 201: {304, "Not Modified"},
1.1 moko 202: {400, "Bad Request"},
203: {401, "Unauthorized"},
204: {403, "Forbidden"},
205: {404, "Not Found"},
1.6 moko 206: {408, "Request Timeout"},
1.11 moko 207: {416, "Range Not Satisfiable"},
1.1 moko 208: {500, "Internal Server Error"},
209: {501, "Not Implemented"},
210: {502, "Bad Gateway"},
211: {504, "Gateway Timeout"},
212: { 0, "Undescribed"}
213: };
214: Lookup *cur = lookup;
215: for(; cur->code; cur++)
216: if(code == cur->code)
217: return cur->message;
218: return cur->message;
219: }
220:
221: virtual void send_header() {
222: String result("HTTP/1.0 ");
1.6 moko 223: result << String::Body::Format(http_response_code) << " " << status_message(http_response_code) << "\r\n" << output << "\r\n";
1.1 moko 224: send_body(result.cstr(), result.length());
225: }
226:
227: virtual size_t send_body(const void *buf, size_t size) {
1.5 moko 228: return connection.send_body(buf, size);
1.1 moko 229: }
230:
231: };
232:
233: #endif
E-mail: