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