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