Annotation of parser3/src/targets/apache/mod_parser3_core.C, revision 1.50
1.1 moko 1: /** @file
1.8 moko 2: Parser: apache 1.3/2.X module, part, compiled by parser3project.
1.1 moko 3:
1.44 moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.36 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1 moko 6: */
7:
1.50 ! moko 8: volatile const char * IDENT_MOD_PARSER3_CORE_C="$Id: mod_parser3_core.C,v 1.49 2026/10/18 22:42:43 moko Exp $";
1.1 moko 9:
10: #include "pa_config_includes.h"
11:
12: #include "pa_globals.h"
13:
14: #include "pa_httpd.h"
15:
16: #include "pa_common.h"
17: #include "pa_sapi.h"
18: #include "classes.h"
19: #include "pa_request.h"
20:
1.17 moko 21: #if defined(_MSC_VER) && !defined(_DEBUG)
1.9 moko 22: # include <windows.h>
1.1 moko 23: # define PA_SUPPRESS_SYSTEM_EXCEPTION
24: #endif
25:
26: // generals
27:
1.7 moko 28: static bool globals_inited=false;
1.42 moko 29: const char* parser3_mode="apache"; // $status:mode
1.43 moko 30: const char *parser3_log_filespec(){ return ""; } // $status:log-filename
1.7 moko 31:
1.1 moko 32: void pa_setup_module_cells() {
33: if(globals_inited)
34: return;
35: globals_inited=true;
36:
37: /// no trying to __try here [yet]
38: try {
1.18 moko 39: // init libraries
1.1 moko 40: pa_globals_init();
41: } catch(const Exception& e) { // global problem
1.21 moko 42: SAPI::die("setup_module_cells failed: %s", e.comment());
1.1 moko 43: }
44: }
45:
46: void pa_destroy_module_cells() {
1.7 moko 47: if(!globals_inited)
48: return;
49:
1.1 moko 50: pa_globals_done();
51: }
52:
53:
54: //@{
55: /// SAPI func decl
56:
57: class SAPI_Info {
58: public:
59: pa_request_rec* r;
60: };
61:
62: void SAPI::log(SAPI_Info& SAPI_info, const char* fmt, ...) {
63: va_list args;
64: va_start(args,fmt);
65: char buf[MAX_LOG_STRING];
1.48 moko 66: size_t size=pa_vsnprintf(buf, MAX_LOG_STRING, fmt, args);
1.1 moko 67: size=remove_crlf(buf, buf+size);
68: pa_ap_log_rerror(0, 0, PA_APLOG_ERR | PA_APLOG_NOERRNO, SAPI_info.r, "%s", buf);
69: va_end(args);
70: }
71:
1.50 ! moko 72: void SAPI::set_response_timeout(SAPI_Info&, int) {} // not implemented
! 73:
1.21 moko 74: void SAPI::die(const char* fmt, ...) {
75: va_list args;
76: va_start(args, fmt);
1.1 moko 77: char buf[MAX_LOG_STRING];
1.48 moko 78: size_t size=pa_vsnprintf(buf, MAX_LOG_STRING, fmt, args);
1.1 moko 79: size=remove_crlf(buf, buf+size);
80: pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_EMERG, 0, "%s", buf);
1.21 moko 81: exit(1);
82: // va_end(args);
1.1 moko 83: }
84:
1.41 moko 85: void SAPI::send_error(SAPI_Info& SAPI_info, const char *exception_cstr, const char *status){
1.38 moko 86: // capitalized headers passed for preventing malloc during capitalization
1.41 moko 87: add_header_attribute(SAPI_info, HTTP_STATUS_CAPITALIZED, status);
88: add_header_attribute(SAPI_info, HTTP_CONTENT_TYPE_CAPITALIZED, "text/plain");
89: send_headers(SAPI_info);
90: send_body(SAPI_info, exception_cstr, strlen(exception_cstr));
1.38 moko 91: }
92:
1.11 moko 93: char* SAPI::Env::get(SAPI_Info& SAPI_info, const char* name) {
1.1 moko 94: const char* dont_return_me=pa_ap_table_get(SAPI_info.r->subprocess_env, name);
95: return dont_return_me?pa_strdup(dont_return_me):0;
96: }
97:
1.28 moko 98: bool SAPI::Env::set(SAPI_Info&, const char*, const char*) {
99: return false;
100: }
101:
1.1 moko 102: #ifndef DOXYGEN
103: struct SAPI_environment_append_info {
104: const char** cur;
105: };
106: #endif
1.22 moko 107:
1.1 moko 108: static int SAPI_environment_append(void *d, const char* k, const char* val) {
1.26 moko 109: if(k && val) {
110: SAPI_environment_append_info& info=*static_cast<SAPI_environment_append_info *>(d);
1.30 moko 111: *info.cur++=pa_strcat(k, "=", val);
1.1 moko 112: }
113: return 1/*true*/;
114: }
1.22 moko 115:
1.11 moko 116: const char* const* SAPI::Env::get(SAPI_Info& SAPI_info) {
1.1 moko 117: const pa_table *t=SAPI_info.r->subprocess_env;
1.35 moko 118: const char** result=new(PointerGC) const char*[pa_ap_table_size(t)+1/*0*/];
1.1 moko 119: SAPI_environment_append_info info={result};
120: pa_ap_table_do(SAPI_environment_append, &info, t, 0); *info.cur=0; // mark EOE
121: return result;
122: }
123:
124: size_t SAPI::read_post(SAPI_Info& SAPI_info, char *buf, size_t max_bytes) {
1.21 moko 125: // pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_DEBUG, SAPI_info.r->server, "mod_parser3: SAPI::read_post(max=%u)", max_bytes);
126:
1.1 moko 127: int retval;
128: if((retval = pa_ap_setup_client_block(SAPI_info.r, PA_REQUEST_CHUNKED_ERROR)))
129: return 0;
130: if(!pa_ap_should_client_block(SAPI_info.r))
131: return 0;
132:
133: uint total_read_bytes=0;
134: void (*handler)(int)=pa_signal(PA_SIGPIPE, PA_SIG_IGN);
135: while (total_read_bytes<max_bytes) {
136: pa_ap_hard_timeout("Read POST information", SAPI_info.r); /* start timeout timer */
137: uint read_bytes=
138: pa_ap_get_client_block(SAPI_info.r, buf+total_read_bytes, max_bytes-total_read_bytes);
139: pa_ap_reset_timeout(SAPI_info.r);
140: if (read_bytes<=0)
141: break;
142: total_read_bytes+=read_bytes;
143: }
144: pa_signal(PA_SIGPIPE, handler);
145: return total_read_bytes;
146: }
147:
148: /// @test location provide with protocol. think about internal redirects
1.22 moko 149: void SAPI::add_header_attribute(SAPI_Info& SAPI_info, const char* dont_store_key, const char* dont_store_value) {
1.1 moko 150: if(strcasecmp(dont_store_key, "location")==0)
151: *SAPI_info.r->status=302;
152:
153: if(strcasecmp(dont_store_key, HTTP_CONTENT_TYPE)==0) {
154: /* r->content_type, *not* r->headers_out("Content-type"). If you don't
155: * set it, it will be filled in with the server's default type (typically
156: * "text/plain"). You *must* also ensure that r->content_type is lower
157: * case.
158: */
159: *SAPI_info.r->content_type = pa_ap_pstrdup(SAPI_info.r->pool, dont_store_value);
160: } else if(strcasecmp(dont_store_key, HTTP_STATUS)==0)
1.25 moko 161: *SAPI_info.r->status=atoi(dont_store_value);
1.1 moko 162: else
163: pa_ap_table_addn(SAPI_info.r->headers_out,
164: pa_ap_pstrdup(SAPI_info.r->pool, capitalize(dont_store_key)),
165: pa_ap_pstrdup(SAPI_info.r->pool, dont_store_value));
166: }
167:
1.39 moko 168: void SAPI::send_headers(SAPI_Info& SAPI_info) {
1.1 moko 169: pa_ap_hard_timeout("Send header", SAPI_info.r);
170: pa_ap_send_http_header(SAPI_info.r);
171: pa_ap_kill_timeout(SAPI_info.r);
172: }
173:
1.41 moko 174: void SAPI::clear_headers(SAPI_Info&) {
1.39 moko 175: }
176:
1.1 moko 177: size_t SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) {
178: pa_ap_hard_timeout("Send body", SAPI_info.r);
179: size = (size_t)pa_ap_rwrite(buf, size, SAPI_info.r);
180: pa_ap_kill_timeout(SAPI_info.r);
181: return size;
182: }
183:
184: //@}
185:
186: /**
187: main workhorse
188: @todo intelligent cache-control
189: */
190: static void real_parser_handler(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
191: // collect garbage from prev request
1.34 moko 192: pa_gc_collect();
1.1 moko 193:
194: // populate env
195: pa_ap_add_common_vars(SAPI_info.r);
196: pa_ap_add_cgi_vars(SAPI_info.r);
197:
198: // Request info
199: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
200:
1.46 moko 201: request_info.set_document_root(SAPI::Env::get(SAPI_info, "DOCUMENT_ROOT"));
202: request_info.path_translated=backslashes_to_slashes(SAPI_info.r->filename);
1.1 moko 203: request_info.method=SAPI_info.r->method;
204: request_info.query_string=SAPI_info.r->args;
1.15 moko 205: request_info.uri=request_info.strip_absolute_uri(SAPI::Env::get(SAPI_info, "REQUEST_URI"));
1.11 moko 206: request_info.content_type=SAPI::Env::get(SAPI_info, "CONTENT_TYPE");
1.27 moko 207: request_info.content_length=pa_atoul(SAPI::Env::get(SAPI_info, "CONTENT_LENGTH"));
1.11 moko 208: request_info.cookie=SAPI::Env::get(SAPI_info, "HTTP_COOKIE");
1.1 moko 209: request_info.mail_received=false;
210:
211: // prepare to process request
1.29 moko 212: Request request(SAPI_info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
1.1 moko 213:
214: // process the request
1.31 moko 215: const char *config=dcfg->parser_config_filespec;
216: #ifdef SYSTEM_CONFIG_FILE
217: if(!config && entry_exists(SYSTEM_CONFIG_FILE)){
218: config=SYSTEM_CONFIG_FILE;
219: }
220: #endif
1.47 moko 221: request.core(config);
1.1 moko 222: }
223:
224: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
1.20 moko 225: static const Exception call_real_parser_handler__do_PEH_return_it(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
1.1 moko 226: try {
227: real_parser_handler(SAPI_info, dcfg);
228: } catch(const Exception& e) {
229: return e;
230: }
231:
232: return Exception();
233: }
1.20 moko 234:
235: static void call_real_parser_handler__supress_system_exception(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
1.1 moko 236: Exception parser_exception;
237: LPEXCEPTION_POINTERS system_exception=0;
238:
239: __try {
1.20 moko 240: parser_exception=call_real_parser_handler__do_PEH_return_it(SAPI_info, dcfg);
241: } __except ( (system_exception=GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) {
1.1 moko 242: if(system_exception)
243: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.45 moko 244: throw Exception("system", 0, "0x%08X at %p", er->ExceptionCode, er->ExceptionAddress);
1.1 moko 245: else
1.20 moko 246: throw Exception("system", 0, "<no exception record>");
1.1 moko 247: else
1.20 moko 248: throw Exception("system", 0, "<no exception information>");
1.1 moko 249: }
250:
251: if(parser_exception)
252: throw Exception(parser_exception);
253: }
254: #endif
255:
256: /// @test r->finfo.st_mode check seems to work only on win32
257: int pa_parser_handler(pa_request_rec *r, Parser_module_config *dcfg) {
1.49 moko 258: Temp_gc_references gc_references; // for pointers GC does not see
259:
1.1 moko 260: // SAPI info
261: SAPI_Info SAPI_info; SAPI_info.r=r;
262:
1.24 moko 263: if(r->file_not_found)
1.1 moko 264: return PA_HTTP_NOT_FOUND;
265:
266: try { // global try
267: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
268: call_real_parser_handler__supress_system_exception(
269: #else
270: real_parser_handler(
271: #endif
272: SAPI_info, dcfg);
273:
274: // successful finish
1.40 moko 275: } catch(const Exception& e) { // just in case
1.1 moko 276: // log it
1.24 moko 277: SAPI::log(SAPI_info, "%s", e.comment());
1.26 moko 278: SAPI::send_error(SAPI_info, e.comment());
1.1 moko 279: }
280:
281: /*
282: * We did what we wanted to do, so tell the rest of the server we
283: * succeeded.
284: */
285: return PA_OK;
286: }
E-mail: