Annotation of parser3/src/targets/cgi/pa_sapi_info.h, revision 1.14
1.1 moko 1: #ifndef PA_SAPI_INFO_H
2: #define PA_SAPI_INFO_H
3:
1.14 ! moko 4: #define IDENT_PA_SAPI_INFO_H "$Id: pa_sapi_info.h,v 1.13 2020/12/16 14:51:26 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);
125: env.put("REMOTE_ADDR", connection.remote_addr);
126:
1.1 moko 127: }
128:
1.3 moko 129: virtual char* get_env(const char* name) {
130: String::Body value = env.get(name);
131: return !value ? NULL : value.cstrm();
1.1 moko 132: }
133:
1.8 moko 134: virtual bool set_env(const char* name, const char* value) {
135: env.put(name, *new String(value));
136: return true;
137: }
138:
1.1 moko 139: virtual const char* const *get_env() {
1.3 moko 140: const char** result=new(PointerGC) const char*[env.count()+1/*0*/];
1.1 moko 141: const char** cur=result;
1.3 moko 142: for(HashStringString::Iterator i(env); i; i.next()){
143: String pair;
144: pair << i.key() << "=" << i.value();
145: *cur++=pair.cstr();
1.1 moko 146: }
147: *cur=NULL;
148: return result;
149: }
150:
1.4 moko 151: virtual size_t read_post(char *buf, size_t max_bytes) {
152: return connection.read_post(buf, max_bytes);
1.1 moko 153: }
154:
155: virtual void add_header_attribute(const char* dont_store_key, const char* dont_store_value) {
156: if(strcasecmp(dont_store_key, "location")==0)
157: http_response_code=302;
158: if(strcasecmp(dont_store_key, HTTP_STATUS)==0)
159: http_response_code=atoi(dont_store_value);
160: else
161: output << capitalize(dont_store_key) << ": " << pa_strdup(dont_store_value) << "\r\n";
162: }
163:
1.14 ! moko 164: void clear_response_headers() {
! 165: http_response_code=200;
! 166: output.clear();
! 167: }
! 168:
1.6 moko 169: static const char *exception_http_status(const char *type) {
170: struct Lookup {
171: const char *code;
172: const char *type;
173: } static lookup[] = {
1.13 moko 174: { "", "httpd.write"},
1.6 moko 175: {"400", "httpd.request"},
1.9 moko 176: {"400", "http.response"},
1.6 moko 177: {"404", "file.missing"},
178: {"408", "httpd.timeout"},
1.13 moko 179: {"408", "httpd.read"},
1.6 moko 180: {"501", "httpd.method"},
181: { NULL, ""}
182: };
183: Lookup *cur = lookup;
184: for(; cur->code; cur++)
185: if(!strcmp(type, cur->type))
186: return cur->code;
187: return "500";
188: }
189:
190: static const char *status_message(int code) {
1.1 moko 191: struct Lookup {
192: int code;
193: const char *message;
194: } static lookup[] = {
195: {200, "OK"},
1.11 moko 196: {204, "No Content"},
1.1 moko 197: {206, "Partial Content"},
198: {301, "Moved Permanently"},
199: {302, "Found"},
1.6 moko 200: {304, "Not Modified"},
1.1 moko 201: {400, "Bad Request"},
202: {401, "Unauthorized"},
203: {403, "Forbidden"},
204: {404, "Not Found"},
1.6 moko 205: {408, "Request Timeout"},
1.11 moko 206: {416, "Range Not Satisfiable"},
1.1 moko 207: {500, "Internal Server Error"},
208: {501, "Not Implemented"},
209: {502, "Bad Gateway"},
210: {504, "Gateway Timeout"},
211: { 0, "Undescribed"}
212: };
213: Lookup *cur = lookup;
214: for(; cur->code; cur++)
215: if(code == cur->code)
216: return cur->message;
217: return cur->message;
218: }
219:
220: virtual void send_header() {
221: String result("HTTP/1.0 ");
1.6 moko 222: result << String::Body::Format(http_response_code) << " " << status_message(http_response_code) << "\r\n" << output << "\r\n";
1.1 moko 223: send_body(result.cstr(), result.length());
224: }
225:
226: virtual size_t send_body(const void *buf, size_t size) {
1.5 moko 227: return connection.send_body(buf, size);
1.1 moko 228: }
229:
230: };
231:
232: #endif
E-mail: