--- parser3/src/targets/cgi/pa_sapi_info.h 2020/11/12 16:16:01 1.9 +++ parser3/src/targets/cgi/pa_sapi_info.h 2024/11/09 15:38:21 1.20 @@ -1,17 +1,23 @@ #ifndef PA_SAPI_INFO_H #define PA_SAPI_INFO_H -#define IDENT_PA_SAPI_INFO_H "$Id: pa_sapi_info.h,v 1.9 2020/11/12 16:16:01 moko Exp $" +#define IDENT_PA_SAPI_INFO_H "$Id: pa_sapi_info.h,v 1.20 2024/11/09 15:38:21 moko Exp $" #include "pa_sapi.h" #include "pa_http.h" /// IIS refuses to read bigger chunks const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M +const int HEADERS_SENT = 999; // can't send error message after headers have been sent + +// for signal handlers and cgi console detection +static Request *request=0; + class SAPI_Info : public PA_Allocated { public: int http_response_code; + String::Body headers; SAPI_Info() : http_response_code(200) {} @@ -22,7 +28,7 @@ public: return 0; } - virtual bool set_env(const char* name, const char* value) { + virtual bool set_env(const char*, const char*) { return false; } @@ -40,18 +46,24 @@ public: return 0; } - virtual void add_header_attribute(const char* dont_store_key, const char* dont_store_value) { + virtual void add_header(const char* dont_store_key, const char* dont_store_value) { + if(strcasecmp(dont_store_key, "location")==0) + http_response_code=302; if(strcasecmp(dont_store_key, HTTP_STATUS)==0) http_response_code=atoi(dont_store_value); } - virtual void send_header() {} + virtual void send_headers() {} virtual size_t send_body(const void *buf, size_t size) { return stdout_write(buf, size); } -} *sapiInfo = NULL; + virtual void send_error(const char *exception_cstr, const char *status){ + http_response_code=atoi(status); + send_body(exception_cstr, strlen(exception_cstr)); + } +}; class SAPI_Info_CGI : public SAPI_Info { public: @@ -67,20 +79,32 @@ public: return read_size; } - virtual void add_header_attribute(const char* dont_store_key, const char* dont_store_value) { - SAPI_Info::add_header_attribute(dont_store_key, dont_store_value); -// if(!request || !request->console.was_used()) - printf("%s: %s\n", capitalize(dont_store_key), dont_store_value); + virtual void add_header(const char* dont_store_key, const char* dont_store_value) { + headers << capitalize(dont_store_key) << ": " << pa_strdup(dont_store_value) << "\r\n"; } - virtual void send_header() { - puts(""); + virtual void send_headers() { + if(!request || !request->console.was_used()){ + headers << "\r\n"; + send_body(headers.cstr(), headers.length()); + http_response_code=HEADERS_SENT; + } } + virtual void send_error(const char *exception_cstr, const char *status){ + if (http_response_code==HEADERS_SENT) + return; + // memory allocation is not allowed + char buf[MAX_STRING]; + snprintf(buf, MAX_STRING, HTTP_STATUS_CAPITALIZED ": %s\r\n" + HTTP_CONTENT_TYPE_CAPITALIZED ": text/plain\r\n\r\n", status); + send_body(buf, strlen(buf)); + send_body(exception_cstr, strlen(exception_cstr)); + } }; -char* replace_char(char* str, char from, char to){ +static char* replace_char(char* str, char from, char to){ for(char *pos = strchr(str,from); pos; pos=strchr(pos,from)) { *pos = to; } @@ -91,14 +115,13 @@ class SAPI_Info_HTTPD : public SAPI_Info public: HTTPD_Connection &connection; - String output; HashStringString env; SAPI_Info_HTTPD(HTTPD_Connection &aconnection) : connection(aconnection) {} void populate_env() { String::Body host("localhost"); - for(Array_iterator i(connection.headers()); i.has_next(); ){ + for(Array_iterator i(connection.headers()); i; ){ HTTP_Headers::Header header=i.next(); String name("HTTP_"); name << replace_char(header.name.cstrm(), '-', '_'); @@ -118,13 +141,18 @@ public: env.put("QUERY_STRING", connection.query()); env.put("SERVER_NAME", host); + env.put("SERVER_PORT", HTTPD_Server::port); env.put("REMOTE_ADDR", connection.remote_addr); } virtual char* get_env(const char* name) { - String::Body value = env.get(name); - return !value ? NULL : value.cstrm(); + String::Body request_value = env.get(name); + if(!request_value.is_empty()) + return request_value.cstrm(); + if(char *server_value=getenv(name)) + return pa_strdup(server_value); + return NULL; } virtual bool set_env(const char* name, const char* value) { @@ -148,24 +176,17 @@ public: return connection.read_post(buf, max_bytes); } - virtual void add_header_attribute(const char* dont_store_key, const char* dont_store_value) { - if(strcasecmp(dont_store_key, "location")==0) - http_response_code=302; - if(strcasecmp(dont_store_key, HTTP_STATUS)==0) - http_response_code=atoi(dont_store_value); - else - output << capitalize(dont_store_key) << ": " << pa_strdup(dont_store_value) << "\r\n"; - } - static const char *exception_http_status(const char *type) { struct Lookup { const char *code; const char *type; } static lookup[] = { + { "", "httpd.write"}, {"400", "httpd.request"}, {"400", "http.response"}, {"404", "file.missing"}, {"408", "httpd.timeout"}, + {"408", "httpd.read"}, {"501", "httpd.method"}, { NULL, ""} }; @@ -182,6 +203,7 @@ public: const char *message; } static lookup[] = { {200, "OK"}, + {204, "No Content"}, {206, "Partial Content"}, {301, "Moved Permanently"}, {302, "Found"}, @@ -191,6 +213,7 @@ public: {403, "Forbidden"}, {404, "Not Found"}, {408, "Request Timeout"}, + {416, "Range Not Satisfiable"}, {500, "Internal Server Error"}, {501, "Not Implemented"}, {502, "Bad Gateway"}, @@ -204,16 +227,37 @@ public: return cur->message; } - virtual void send_header() { + virtual void add_header(const char* dont_store_key, const char* dont_store_value) { + if(strcasecmp(dont_store_key, "location")==0) + http_response_code=302; + if(strcasecmp(dont_store_key, HTTP_STATUS)==0) + http_response_code=atoi(dont_store_value); + else + headers << capitalize(dont_store_key) << ": " << pa_strdup(dont_store_value) << "\r\n"; + } + + virtual void send_headers() { String result("HTTP/1.0 "); - result << String::Body::Format(http_response_code) << " " << status_message(http_response_code) << "\r\n" << output << "\r\n"; + result << pa_uitoa(http_response_code) << " " << status_message(http_response_code) << "\r\n" << headers << "\r\n"; send_body(result.cstr(), result.length()); + http_response_code=HEADERS_SENT; } virtual size_t send_body(const void *buf, size_t size) { return connection.send_body(buf, size); } + virtual void send_error(const char *exception_cstr, const char *status){ + if (http_response_code==HEADERS_SENT) + return; + // memory allocation is not allowed + char buf[MAX_STRING]; + snprintf(buf, MAX_STRING, "HTTP/1.0 %s %s\r\n" + HTTP_CONTENT_TYPE_CAPITALIZED ": text/plain\r\n\r\n", status, status_message(atoi(status))); + send_body(buf, strlen(buf)); + send_body(exception_cstr, strlen(exception_cstr)); + } + }; #endif