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