Annotation of parser3/src/targets/apache/mod_parser3_core.C, revision 1.20
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.16 moko 4: Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.1 moko 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
1.20 ! moko 8: volatile const char * IDENT_MOD_PARSER3_CORE_C="$Id: mod_parser3_core.C,v 1.19 2019/12/25 22:22:07 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;
29:
1.1 moko 30: void pa_setup_module_cells() {
31: if(globals_inited)
32: return;
33: globals_inited=true;
34:
35: /// no trying to __try here [yet]
36: try {
1.18 moko 37: // init libraries
1.1 moko 38: pa_globals_init();
39: } catch(const Exception& e) { // global problem
40: SAPI::abort("setup_module_cells failed: %s", e.comment());
41: }
42: }
43:
44: void pa_destroy_module_cells() {
1.7 moko 45: if(!globals_inited)
46: return;
47:
1.1 moko 48: pa_globals_done();
49: }
50:
51:
52: //@{
53: /// SAPI func decl
54:
55: class SAPI_Info {
56: public:
57: pa_request_rec* r;
58: };
59:
60: void SAPI::log(SAPI_Info& SAPI_info, const char* fmt, ...) {
61: va_list args;
62: va_start(args,fmt);
63: char buf[MAX_LOG_STRING];
64: size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args);
65: size=remove_crlf(buf, buf+size);
66: pa_ap_log_rerror(0, 0, PA_APLOG_ERR | PA_APLOG_NOERRNO, SAPI_info.r, "%s", buf);
67: va_end(args);
68: }
69:
70: static void die_or_abort(const char* fmt, va_list args, bool write_core) {
71: char buf[MAX_LOG_STRING];
72: size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args);
73: size=remove_crlf(buf, buf+size);
74: pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_EMERG, 0, "%s", buf);
75:
76: // exit & try to produce core dump
77: if(write_core)
78: abort();
79: else
80: exit(1);
81: }
82:
83: void SAPI::die(const char* fmt, ...) {
84: va_list args;
85: va_start(args, fmt);
86: die_or_abort(fmt, args, false/*write core?*/);
87: va_end(args);
88: }
89:
90: void SAPI::abort(const char* fmt, ...) {
91: va_list args;
92: va_start(args, fmt);
93: die_or_abort(fmt, args, true/*write core?*/);
94: va_end(args);
95: }
96:
1.11 moko 97: char* SAPI::Env::get(SAPI_Info& SAPI_info, const char* name) {
1.1 moko 98: const char* dont_return_me=pa_ap_table_get(SAPI_info.r->subprocess_env, name);
99: return dont_return_me?pa_strdup(dont_return_me):0;
100: }
101:
102: #ifndef DOXYGEN
103: struct SAPI_environment_append_info {
104: const char** cur;
105: };
106: #endif
107: static const char* mk_env_pair(const char* key, const char* value) {
108: char *result=new(PointerFreeGC) char[strlen(key)+1/*=*/+strlen(value)+1/*0*/];
109: strcpy(result, key); strcat(result, "="); strcat(result, value);
110: return result;
111: }
112: static int SAPI_environment_append(void *d, const char* k, const char* val) {
113: if( k && val ) {
114: SAPI_environment_append_info& info=
115: *static_cast<SAPI_environment_append_info *>(d);
116: *info.cur++=mk_env_pair(k, val);
117: }
118: return 1/*true*/;
119: }
1.11 moko 120: const char* const* SAPI::Env::get(SAPI_Info& SAPI_info) {
1.1 moko 121: const pa_table *t=SAPI_info.r->subprocess_env;
1.12 moko 122: const char** result=new const char*[pa_ap_table_size(t)+1/*0*/];
1.1 moko 123: SAPI_environment_append_info info={result};
124: pa_ap_table_do(SAPI_environment_append, &info, t, 0); *info.cur=0; // mark EOE
125: return result;
126: }
127:
128: size_t SAPI::read_post(SAPI_Info& SAPI_info, char *buf, size_t max_bytes) {
129: /* pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_DEBUG, SAPI_info.r->server,
130: "mod_parser3: SAPI::read_post(max=%u)", max_bytes);
131: */
132: int retval;
133: if((retval = pa_ap_setup_client_block(SAPI_info.r, PA_REQUEST_CHUNKED_ERROR)))
134: return 0;
135: if(!pa_ap_should_client_block(SAPI_info.r))
136: return 0;
137:
138: uint total_read_bytes=0;
139: void (*handler)(int)=pa_signal(PA_SIGPIPE, PA_SIG_IGN);
140: while (total_read_bytes<max_bytes) {
141: pa_ap_hard_timeout("Read POST information", SAPI_info.r); /* start timeout timer */
142: uint read_bytes=
143: pa_ap_get_client_block(SAPI_info.r, buf+total_read_bytes, max_bytes-total_read_bytes);
144: pa_ap_reset_timeout(SAPI_info.r);
145: if (read_bytes<=0)
146: break;
147: total_read_bytes+=read_bytes;
148: }
149: pa_signal(PA_SIGPIPE, handler);
150: return total_read_bytes;
151: }
152:
153: /// @test location provide with protocol. think about internal redirects
154: void SAPI::add_header_attribute(SAPI_Info& SAPI_info,
155: const char* dont_store_key, const char* dont_store_value) {
156: if(strcasecmp(dont_store_key, "location")==0)
157: *SAPI_info.r->status=302;
158:
159: if(strcasecmp(dont_store_key, HTTP_CONTENT_TYPE)==0) {
160: /* r->content_type, *not* r->headers_out("Content-type"). If you don't
161: * set it, it will be filled in with the server's default type (typically
162: * "text/plain"). You *must* also ensure that r->content_type is lower
163: * case.
164: */
165: *SAPI_info.r->content_type = pa_ap_pstrdup(SAPI_info.r->pool, dont_store_value);
166: } else if(strcasecmp(dont_store_key, HTTP_STATUS)==0)
167: *SAPI_info.r->status=atoi(dont_store_value);
168: else
169: pa_ap_table_addn(SAPI_info.r->headers_out,
170: pa_ap_pstrdup(SAPI_info.r->pool, capitalize(dont_store_key)),
171: pa_ap_pstrdup(SAPI_info.r->pool, dont_store_value));
172: }
173:
174: void SAPI::send_header(SAPI_Info& SAPI_info) {
175: pa_ap_hard_timeout("Send header", SAPI_info.r);
176: pa_ap_send_http_header(SAPI_info.r);
177: pa_ap_kill_timeout(SAPI_info.r);
178: }
179:
180: size_t SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) {
181: pa_ap_hard_timeout("Send body", SAPI_info.r);
182: size = (size_t)pa_ap_rwrite(buf, size, SAPI_info.r);
183: pa_ap_kill_timeout(SAPI_info.r);
184: return size;
185: }
186:
187: //@}
188:
1.8 moko 189: #ifndef PA_DEBUG_DISABLE_GC
1.10 moko 190: #ifndef _MSC_VER
1.8 moko 191: extern long GC_large_alloc_warn_suppressed;
1.1 moko 192: #endif
1.10 moko 193: #endif
1.1 moko 194:
195: /**
196: main workhorse
197:
198: @todo intelligent cache-control
199: */
200: static void real_parser_handler(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
201: // collect garbage from prev request
1.8 moko 202: #ifndef PA_DEBUG_DISABLE_GC
203: GC_dont_gc=0;
204: GC_gcollect();
205: GC_dont_gc=1;
1.10 moko 206: #endif
1.1 moko 207:
208: // populate env
209: pa_ap_add_common_vars(SAPI_info.r);
210: pa_ap_add_cgi_vars(SAPI_info.r);
211:
212: // Request info
213: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
214:
1.11 moko 215: request_info.document_root=SAPI::Env::get(SAPI_info, "DOCUMENT_ROOT");
1.1 moko 216: request_info.path_translated=SAPI_info.r->filename;
217: request_info.method=SAPI_info.r->method;
218: request_info.query_string=SAPI_info.r->args;
1.15 moko 219: request_info.uri=request_info.strip_absolute_uri(SAPI::Env::get(SAPI_info, "REQUEST_URI"));
1.11 moko 220: request_info.content_type=SAPI::Env::get(SAPI_info, "CONTENT_TYPE");
221: const char* content_length=SAPI::Env::get(SAPI_info, "CONTENT_LENGTH");
1.1 moko 222: request_info.content_length=content_length?atoi(content_length):0;
1.11 moko 223: request_info.cookie=SAPI::Env::get(SAPI_info, "HTTP_COOKIE");
1.1 moko 224: request_info.mail_received=false;
225:
226: // prepare to process request
227: Request request(
228: SAPI_info,
229: request_info,
1.3 moko 230: String::Language(String::L_HTML|String::L_OPTIMIZE_BIT)
1.1 moko 231: );
232:
233: // process the request
234: request.core(
235: dcfg->parser_config_filespec, true, // /path/to/config
236: SAPI_info.r->header_only!=0);
237: }
238:
239: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
1.20 ! moko 240: static const Exception call_real_parser_handler__do_PEH_return_it(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
1.1 moko 241: try {
242: real_parser_handler(SAPI_info, dcfg);
243: } catch(const Exception& e) {
244: return e;
245: }
246:
247: return Exception();
248: }
1.20 ! moko 249:
! 250: static void call_real_parser_handler__supress_system_exception(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
1.1 moko 251: Exception parser_exception;
252: LPEXCEPTION_POINTERS system_exception=0;
253:
254: __try {
1.20 ! moko 255: parser_exception=call_real_parser_handler__do_PEH_return_it(SAPI_info, dcfg);
! 256: } __except ( (system_exception=GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) {
1.1 moko 257: if(system_exception)
258: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.20 ! moko 259: throw Exception("system", 0, "0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
1.1 moko 260: else
1.20 ! moko 261: throw Exception("system", 0, "<no exception record>");
1.1 moko 262: else
1.20 ! moko 263: throw Exception("system", 0, "<no exception information>");
1.1 moko 264: }
265:
266: if(parser_exception)
267: throw Exception(parser_exception);
268: }
269: #endif
270:
271: /// @test r->finfo.st_mode check seems to work only on win32
272: int pa_parser_handler(pa_request_rec *r, Parser_module_config *dcfg) {
273: // SAPI info
274: SAPI_Info SAPI_info; SAPI_info.r=r;
275:
1.2 moko 276: if(r->file_not_found )
1.1 moko 277: return PA_HTTP_NOT_FOUND;
278:
279: try { // global try
280: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
281: call_real_parser_handler__supress_system_exception(
282: #else
283: real_parser_handler(
284: #endif
285: SAPI_info, dcfg);
286:
287: // successful finish
288: } catch(const Exception& e) { // global problem
289: // don't allocate anything on pool here:
290: // possible pool' exception not catch-ed now
291: // and there could be out-of-memory exception
292: char buf[MAX_STRING];
293: snprintf(buf, MAX_STRING, "Unhandled exception %s",
294: e.comment());
295: // log it
296: SAPI::log(SAPI_info, "%s", buf);
297:
298: //
299: int content_length=strlen(buf);
300:
301: // prepare header
302: // capitalized headers are used for preventing malloc during capitalization
303: SAPI::add_header_attribute(SAPI_info, HTTP_CONTENT_TYPE_CAPITALIZED, "text/plain");
304: // don't use 'format' function because it calls malloc
305: char content_length_cstr[MAX_NUMBER];
306: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
307: SAPI::add_header_attribute(SAPI_info, HTTP_CONTENT_LENGTH_CAPITALIZED, content_length_cstr);
308:
309: // send header
310: SAPI::send_header(SAPI_info);
311:
312: // send body
313: if(!r->header_only)
314: SAPI::send_body(SAPI_info, buf, content_length);
315:
316: // unsuccessful finish
317: }
318:
319: /*
320: * We did what we wanted to do, so tell the rest of the server we
321: * succeeded.
322: */
323: return PA_OK;
324: }
E-mail: