Annotation of parser3/src/targets/apache13core/mod_parser3_core.C, revision 1.4
1.1 paf 1: /** @file
2: Parser: apache 1.3 module, part, compiled by parser3project.
3:
1.3 paf 4: Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com)
1.1 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
1.4 ! paf 8: static const char * const IDENT_MOD_PARSER3_MAIN_C="$Date: 2004/02/11 15:33:17 $";
1.1 paf 9:
10: #include "pa_globals.h"
11:
12: #include "pa_httpd.h"
13:
14: #include "pa_common.h"
15: #include "pa_sapi.h"
16: #include "classes.h"
17: #include "pa_request.h"
18: #include "pa_version.h"
19: #include "pa_socks.h"
20:
21:
22: // generals
23:
24: const char* pa_version() {
25: return "Parser/"PARSER_VERSION;
26: }
27:
28: void pa_setup_module_cells() {
29: static bool globals_inited=false;
30: if(globals_inited)
31: return;
32: globals_inited=true;
33:
34: /// no trying to __try here [yet]
35: try {
36: // init socks
37: pa_init_socks();
38:
39: // init global variables
40: pa_globals_init();
41: } catch(const Exception& e) { // global problem
42: SAPI::abort("setup_module_cells failed: %s", e.comment());
43: }
44: }
45:
46: //@{
47: /// SAPI func decl
48:
49: class SAPI_Info {
50: public:
51: pa_request_rec* r;
52: };
53:
54: void SAPI::log(SAPI_Info& SAPI_info, const char* fmt, ...) {
55: va_list args;
56: va_start(args,fmt);
57: char buf[MAX_STRING];
58: size_t size=vsnprintf(buf, MAX_STRING, fmt, args);
59: remove_crlf(buf, buf+size);
60: pa_ap_log_rerror(0, 0, PA_APLOG_ERR | PA_APLOG_NOERRNO, SAPI_info.r, "%s", buf);
61: va_end(args);
62: }
63:
64: static void die_or_abort(const char* fmt, va_list args, bool write_core) {
65: char buf[MAX_STRING];
66: size_t size=vsnprintf(buf, MAX_STRING, fmt, args);
67: remove_crlf(buf, buf+size);
68: pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_EMERG, 0, "%s", buf);
69:
70: // exit & try to produce core dump
71: if(write_core)
72: abort();
73: else
74: exit(1);
75: }
76:
77: void SAPI::die(const char* fmt, ...) {
78: va_list args;
79: va_start(args, fmt);
80: die_or_abort(fmt, args, false/*write core?*/);
81: va_end(args);
82: }
83:
84: void SAPI::abort(const char* fmt, ...) {
85: va_list args;
86: va_start(args, fmt);
87: die_or_abort(fmt, args, true/*write core?*/);
88: va_end(args);
89: }
90:
91: char* SAPI::get_env(SAPI_Info& SAPI_info, const char* name) {
92: const char* dont_return_me=pa_ap_table_get(SAPI_info.r->subprocess_env, name);
93: return dont_return_me?pa_strdup(dont_return_me):0;
94: }
95:
96: #ifndef DOXYGEN
97: struct SAPI_environment_append_info {
98: const char** cur;
99: };
100: #endif
101: static const char* mk_env_pair(const char* key, const char* value) {
102: char *result=new(PointerFreeGC) char[strlen(key)+1/*=*/+strlen(value)+1/*0*/];
103: strcpy(result, key); strcat(result, "="); strcat(result, value);
104: return result;
105: }
106: static int SAPI_environment_append(void *d, const char* k, const char* val) {
107: if( k && val ) {
108: SAPI_environment_append_info& info=
109: *static_cast<SAPI_environment_append_info *>(d);
110: *info.cur++=mk_env_pair(k, val);
111: }
112: return 1/*true*/;
113: }
114: const char* const* SAPI::environment(SAPI_Info& SAPI_info) {
115: const pa_table *t=SAPI_info.r->subprocess_env;
1.2 paf 116: const char** result=new(UseGC) const char*[pa_ap_table_size(t)+1/*0*/];
1.1 paf 117: SAPI_environment_append_info info={result};
118: pa_ap_table_do(SAPI_environment_append, &info, t, 0); *info.cur=0; // mark EOE
119: return result;
120: }
121:
122: size_t SAPI::read_post(SAPI_Info& SAPI_info, char *buf, size_t max_bytes) {
123: /* pa_ap_log_error(PA_APLOG_MARK, PA_APLOG_DEBUG, SAPI_info.r->server,
124: "mod_parser3: SAPI::read_post(max=%u)", max_bytes);
125: */
126: int retval;
127: if((retval = pa_ap_setup_client_block(SAPI_info.r, PA_REQUEST_CHUNKED_ERROR)))
128: return 0;
129: if(!pa_ap_should_client_block(SAPI_info.r))
130: return 0;
131:
132: uint total_read_bytes=0;
133: void (*handler)(int)=pa_signal(PA_SIGPIPE, PA_SIG_IGN);
134: while (total_read_bytes<max_bytes) {
135: pa_ap_hard_timeout("Read POST information", SAPI_info.r); /* start timeout timer */
136: uint read_bytes=
137: pa_ap_get_client_block(SAPI_info.r, buf+total_read_bytes, max_bytes-total_read_bytes);
138: pa_ap_reset_timeout(SAPI_info.r);
139: if (read_bytes<=0)
140: break;
141: total_read_bytes+=read_bytes;
142: }
143: pa_signal(PA_SIGPIPE, handler);
144: return total_read_bytes;
145: }
146:
147: /// @test location provide with protocol. think about internal redirects
148: void SAPI::add_header_attribute(SAPI_Info& SAPI_info,
149: const char* dont_store_key, const char* dont_store_value) {
150: if(strcasecmp(dont_store_key, "location")==0)
151: *SAPI_info.r->status=302;
152:
153: if(strcasecmp(dont_store_key, "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, "status")==0)
161: *SAPI_info.r->status=atoi(dont_store_value);
162: else
163: pa_ap_table_addn(SAPI_info.r->headers_out,
164: pa_ap_pstrdup(SAPI_info.r->pool, dont_store_key),
165: pa_ap_pstrdup(SAPI_info.r->pool, dont_store_value));
166: }
167:
168: void SAPI::send_header(SAPI_Info& SAPI_info) {
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:
174: void SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) {
175: pa_ap_hard_timeout("Send body", SAPI_info.r);
176: pa_ap_rwrite(buf, size, SAPI_info.r);
177: pa_ap_kill_timeout(SAPI_info.r);
178: }
179:
180: //@}
181:
182: /**
183: main workhorse
184:
185: @todo intelligent cache-control
186: */
187: static void real_parser_handler(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
1.4 ! paf 188: // collect garbage from prev request
! 189: #ifndef PA_DEBUG_DISABLE_GC
! 190: {
! 191: int saved=GC_dont_gc;
! 192: GC_dont_gc=0;
! 193: GC_gcollect();
! 194: GC_dont_gc=saved;
! 195: }
! 196: #endif
! 197:
! 198: // populate env
1.1 paf 199: pa_ap_add_common_vars(SAPI_info.r);
200: pa_ap_add_cgi_vars(SAPI_info.r);
201:
202: // Request info
203: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
204:
205: request_info.document_root=SAPI::get_env(SAPI_info, "DOCUMENT_ROOT");
206: request_info.path_translated=SAPI_info.r->filename;
207: request_info.method=SAPI_info.r->method;
208: request_info.query_string=SAPI_info.r->args;
209: request_info.uri=SAPI::get_env(SAPI_info, "REQUEST_URI");
210: request_info.content_type=SAPI::get_env(SAPI_info, "CONTENT_TYPE");
211: const char* content_length=SAPI::get_env(SAPI_info, "CONTENT_LENGTH");
212: request_info.content_length=content_length?atoi(content_length):0;
213: request_info.cookie=SAPI::get_env(SAPI_info, "HTTP_COOKIE");
214: request_info.mail_received=false;
215:
216: //_asm int 3;
217: // prepare to process request
218: Request request(
219: SAPI_info,
220: request_info,
221: String::Language(String::L_HTML|String::L_OPTIMIZE_BIT),
222: dcfg->parser_status_allowed?true:false
223: );
224:
225: // process the request
226: request.core(
227: dcfg->parser_config_filespec, true, // /path/to/config
228: SAPI_info.r->header_only!=0);
229: }
230:
231: void call_real_parser_handler__do_SEH(SAPI_Info& SAPI_info, Parser_module_config *dcfg) {
232: #if _MSC_VER & !defined(_DEBUG)
233: LPEXCEPTION_POINTERS system_exception=0;
234: __try {
235: #endif
236: real_parser_handler(SAPI_info, dcfg);
237:
238: #if _MSC_VER & !defined(_DEBUG)
239: } __except (
240: (system_exception=GetExceptionInformation()),
241: EXCEPTION_EXECUTE_HANDLER) {
242:
243: if(system_exception)
244: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
245: throw Exception(0,
246: 0,
247: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
248: else
249: throw Exception(0, 0, "Exception <no exception record>");
250: else
251: throw Exception(0, 0, "Exception <no exception information>");
252: }
253: #endif
254: }
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) {
258: //_asm int 3;
259:
260: // SAPI info
261: SAPI_Info SAPI_info; SAPI_info.r=r;
262:
263: //_asm int 3;
264: if(r->finfo->st_mode == 0)
265: return PA_HTTP_NOT_FOUND;
266:
267: /* A flag which modules can set, to indicate that the data being
268: * returned is volatile, and clients should be told not to cache it.
269: */
270: // r->no_cache=1;
271:
272: try { // global try
273: call_real_parser_handler__do_SEH(SAPI_info, dcfg);
274: // successful finish
275: } catch(const Exception& e) { // global problem
276: // don't allocate anything on pool here:
277: // possible pool' exception not catch-ed now
278: // and there could be out-of-memory exception
279: const char* body=e.comment();
280: // log it
281: SAPI::log(SAPI_info, "exception in request exception handler: %s", body);
282:
283: //
284: int content_length=strlen(body);
285:
286: // prepare header
287: SAPI::add_header_attribute(SAPI_info, "content-type", "text/plain");
288: char content_length_cstr[MAX_NUMBER];
289: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
290: SAPI::add_header_attribute(SAPI_info, "content-length", content_length_cstr);
291:
292: // send header
293: SAPI::send_header(SAPI_info);
294:
295: // send body
296: if(!r->header_only)
297: SAPI::send_body(SAPI_info, body, content_length);
298:
299: // unsuccessful finish
300: }
301:
302: /*
303: * We did what we wanted to do, so tell the rest of the server we
304: * succeeded.
305: */
306: return PA_OK;
307: }
E-mail: