Annotation of parser3/src/targets/cgi/parser3.C, revision 1.143
1.27 paf 1: /** @file
2: Parser: scripting and CGI main.
3:
1.43 paf 4: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.128 paf 5: Author: Alexander Petrosyan <paf@design.ru>(http://paf.design.ru)
1.27 paf 6:
1.143 ! paf 7: $Id: parser3.C,v 1.142 2001/11/21 08:34:43 paf Exp $
1.1 paf 8: */
9:
1.40 paf 10: #include "pa_config_includes.h"
1.3 paf 11:
12: #ifdef WIN32
13: # include <windows.h>
1.139 paf 14: #endif
15:
16: #if _MSC_VER
1.131 paf 17: # include <new.h>
1.3 paf 18: #endif
1.27 paf 19:
1.37 paf 20: #include "pa_sapi.h"
1.76 paf 21: #include "classes.h"
1.24 paf 22: #include "pa_common.h"
1.2 paf 23: #include "pa_request.h"
1.57 paf 24: #include "pa_socks.h"
1.68 paf 25: #include "pa_version.h"
1.125 parser 26: #include "pool_storage.h"
1.69 paf 27:
1.120 parser 28: #ifdef XML
29: #include <XalanTransformer/XalanCAPI.h>
30: #endif
31:
1.127 paf 32: //#define DEBUG_POOL_MALLOC
1.84 parser 33:
1.109 parser 34: // consts
1.113 parser 35:
36: extern const char *main_RCSIds[];
1.116 parser 37: #ifdef USE_SMTP
1.113 parser 38: extern const char *smtp_RCSIds[];
1.114 parser 39: #endif
1.113 parser 40: extern const char *gd_RCSIds[];
41: extern const char *classes_RCSIds[];
42: extern const char *types_RCSIds[];
1.115 parser 43: extern const char *parser3_RCSIds[];
1.119 parser 44: #ifdef XML
45: extern const char *xalan_patched_RCSIds[];
46: #endif
1.113 parser 47: const char **RCSIds[]={
48: main_RCSIds,
1.116 parser 49: #ifdef USE_SMTP
1.113 parser 50: smtp_RCSIds,
1.114 parser 51: #endif
1.113 parser 52: gd_RCSIds,
53: classes_RCSIds,
54: types_RCSIds,
1.115 parser 55: parser3_RCSIds,
1.118 parser 56: #ifdef XML
57: xalan_patched_RCSIds,
58: #endif
1.113 parser 59: 0
60: };
1.84 parser 61:
1.42 paf 62: /// IIS refuses to read bigger chunks
63: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M
64:
1.45 paf 65: const char *argv0;
1.125 parser 66: Pool_storage pool_storage;
67: Pool pool(&pool_storage); // global pool [dont describe to doxygen: it confuses it with param names]
1.27 paf 68: bool cgi; ///< we were started as CGI?
1.5 paf 69:
1.46 paf 70: // SAPI
1.86 parser 71:
1.124 parser 72: static void log(const char *fmt, va_list args) {
1.61 paf 73: bool opened;
74: FILE *f=0;
75:
76: if(argv0) {
77: // beside by binary
78: char file_spec[MAX_STRING];
1.98 parser 79: strncpy(file_spec, argv0, MAX_STRING-1); file_spec[MAX_STRING-1]=0; // filespec of my binary
1.61 paf 80: rsplit(file_spec, '/'); rsplit(file_spec, '\\');// strip filename
81: strcat(file_spec, "/parser3.log");
82: f=fopen(file_spec, "at");
83: }
84: opened=f!=0;
85: if(!opened)
86: f=stderr;
87:
88: // prefix
89: time_t t=time(0);
90: const char *stamp=ctime(&t);
91: fprintf(f, "[%.*s] ", strlen(stamp)-1, stamp);
92: // message
1.117 parser 93:
94: char buf[MAX_STRING];
95: size_t size=vsnprintf(buf, MAX_STRING, fmt, args);
96: remove_crlf(buf, buf+size);
97:
98: fwrite(buf, size, 1, f);
1.61 paf 99: // newline
100: fprintf(f, "\n");
101:
102: if(opened)
103: fclose(f);
1.85 parser 104: else
105: fflush(f);
1.124 parser 106: }
107:
108: // appends to parser3.log located beside my binary if openable, to stderr otherwize
109: void SAPI::log(Pool& , const char *fmt, ...) {
110: va_list args;
111: va_start(args,fmt);
112: ::log(fmt, args);
113: va_end(args);
114: }
115:
116: void SAPI::die(const char *fmt, ...) {
1.137 paf 117: #ifdef DEBUG_POOL_MALLOC
118: extern void log_pool_stats(Pool& pool);
119: log_pool_stats(pool);
120: #endif
121:
1.138 paf 122: // log
123:
124: // logging is more important than user
125: // she can cancel download, we'd get SIG_PIPE,
126: // nothing would be logged then
1.124 parser 127: va_list args;
128: va_start(args,fmt);
129: ::log(fmt, args);
130: va_end(args);
131:
1.138 paf 132: // inform user
133:
1.134 paf 134: char body[MAX_STRING];
1.138 paf 135: int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134 paf 136:
137: // prepare header
138: // let's be honest, that's bad we couldn't produce valid output
139: SAPI::add_header_attribute(pool, "status", "500");
140: SAPI::add_header_attribute(pool, "content-type", "text/plain");
141: char content_length_cstr[MAX_NUMBER];
142: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
143: SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
144:
145: // send header
146: SAPI::send_header(pool);
147:
148: // body
149: SAPI::send_body(pool, body, content_length);
150:
1.124 parser 151: exit(1);
1.61 paf 152: }
153:
1.122 parser 154: const char *SAPI::get_env(Pool& , const char *name) {
1.109 parser 155: return getenv(name);
1.28 paf 156: }
157:
1.122 parser 158: size_t SAPI::read_post(Pool& , char *buf, size_t max_bytes) {
1.59 paf 159: size_t read_size=0;
1.12 paf 160: do {
1.36 paf 161: int chunk_size=read(fileno(stdin),
1.42 paf 162: buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129 paf 163: if(chunk_size<=0)
1.12 paf 164: break;
165: read_size+=chunk_size;
166: } while(read_size<max_bytes);
167:
168: return read_size;
1.10 paf 169: }
170:
1.122 parser 171: void SAPI::add_header_attribute(Pool& , const char *key, const char *value) {
1.68 paf 172: if(cgi)
1.20 paf 173: printf("%s: %s\n", key, value);
1.19 paf 174: }
175:
1.56 paf 176: /// @todo intelligent cache-control
1.122 parser 177: void SAPI::send_header(Pool& ) {
1.33 paf 178: if(cgi) {
1.55 paf 179: puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33 paf 180:
181: // header | body delimiter
1.20 paf 182: puts("");
1.33 paf 183: }
1.30 paf 184: }
1.20 paf 185:
1.122 parser 186: void SAPI::send_body(Pool& , const void *buf, size_t size) {
1.19 paf 187: stdout_write(buf, size);
1.58 paf 188: }
189:
1.97 parser 190: //
191:
192: char *full_file_spec(char *file_name) {
1.108 parser 193: if(file_name && !strchr(file_name, '/')) {
1.97 parser 194: static char cwd[MAX_STRING]; getcwd(cwd, MAX_STRING);
195: static char buf[MAX_STRING];
196: snprintf(buf, MAX_STRING, "%s/%s", cwd, file_name);
197: return buf;
198: }
199: return file_name;
200: }
201:
1.40 paf 202: /**
1.122 parser 203: main workhorse
1.19 paf 204:
1.122 parser 205: @todo
1.40 paf 206: IIS: remove trailing default-document[index.html] from $request.uri.
207: to do that we need to consult metabase,
208: wich is tested but seems slow.
209: */
1.122 parser 210: void real_parser_handler(
211: const char *filespec_to_process,
212: const char *request_method, bool header_only) {
213: // init socks
214: init_socks(pool);
215:
216: #ifdef XML
217: /**
218: * Initialize Xerces and Xalan.
219: *
220: * Should be called only once per process before making
221: * any other API calls.
222: */
223: //_asm int 3;
224: XalanInitialize();
225: #endif
226:
227: // init global classes
228: init_methoded_array(pool);
229: // init global variables
230: pa_globals_init(pool);
231:
232: if(!filespec_to_process)
233: throw Exception(0, 0,
234: 0,
235: "Parser/%s", PARSER_VERSION);
236:
237: // Request info
238: Request::Info request_info;
239: if(cgi) {
240: if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
241: request_info.document_root=env_document_root;
242: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
243: // IIS
244: size_t len=strlen(filespec_to_process)-strlen(path_info);
245: char *buf=(char *)pool.malloc(len+1);
246: memcpy(buf, filespec_to_process, len); buf[len]=0;
247: request_info.document_root=buf;
248: } else
249: throw Exception(0, 0,
250: 0,
251: "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
252: } else {
253: static char buf[MAX_STRING];
254: strncpy(buf, filespec_to_process, MAX_STRING-1); buf[MAX_STRING-1]=0;
255: if(rsplit(buf, '/') || rsplit(buf, '\\')) // strip filename
256: request_info.document_root=buf;
257: else
258: request_info.document_root="";
259: }
260: request_info.path_translated=filespec_to_process;
261: request_info.method=request_method ? request_method : "GET";
262: const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
263: request_info.query_string=query_string;
264: if(cgi) {
265: if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
266: request_info.uri=env_request_uri;
267: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
268: if(query_string) {
269: char *reconstructed_uri=(char *)pool.malloc(
270: strlen(path_info)+1/*'?'*/+
271: strlen(query_string)+1/*0*/);
272: strcpy(reconstructed_uri, path_info);
273: strcat(reconstructed_uri, "?");
274: strcat(reconstructed_uri, query_string);
275: request_info.uri=reconstructed_uri;
276: } else
277: request_info.uri=path_info;
278: else
279: throw Exception(0, 0,
280: 0,
281: "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
282:
283: if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
284: size_t script_name_len=strlen(script_name);
285: size_t uri_len=strlen(request_info.uri);
286: if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
287: script_name_len != uri_len) // under IIS they are the same
288: throw Exception(0, 0,
289: 0,
290: "CGI: illegal call");
291: }
292: } else
293: request_info.uri=0;
294:
295: request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
296: const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
297: request_info.content_length=(content_length?atoi(content_length):0);
298: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
299: request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
300:
301: // prepare to process request
302: Request request(pool,
303: request_info,
1.143 ! paf 304: #ifdef _DEBUG
! 305: String::UL_HTML|String::UL_OPTIMIZE_BIT
! 306: #else
! 307: cgi ? String::UL_HTML|String::UL_OPTIMIZE_BIT : String::UL_AS_IS
! 308: #endif
! 309: ,
1.130 paf 310: true /* status_allowed */);
1.122 parser 311:
312: // some root-controlled location
313: #ifdef SYSCONFDIR
314: const char *root_config_filespec=SYSCONFDIR "/" CONFIG_FILE_NAME;
315: #else
316: # ifdef WIN32
317: // c:\windows
318: char root_config_path[MAX_STRING];
319: GetWindowsDirectory(root_config_path, MAX_STRING);
320:
321: char root_config_filespec[MAX_STRING];
322: snprintf(root_config_filespec, MAX_STRING,
323: "%s/%s",
324: root_config_path, CONFIG_FILE_NAME);
325: # else
326: #error must be compiled either configure/make or MSVC++
327: # endif
328: #endif
329:
330: // beside by binary
331: // @todo full path, not ./!
332: static char site_config_path[MAX_STRING];
333: strncpy(site_config_path, argv0, MAX_STRING-1); site_config_path[MAX_STRING-1]=0; // filespec of my binary
334: if(!(
335: rsplit(site_config_path, '/') ||
336: rsplit(site_config_path, '\\'))) { // strip filename
337: // no path, just filename
338: site_config_path[0]='.'; site_config_path[1]=0;
339: }
340:
341: char site_config_filespec[MAX_STRING];
342: snprintf(site_config_filespec, MAX_STRING,
343: "%s/%s",
344: site_config_path, CONFIG_FILE_NAME);
345:
346: // process the request
347: request.core(
348: root_config_filespec, false,
349: site_config_filespec, false,
350: header_only);
351:
352: //
353: done_socks();
354:
355: #ifdef DEBUG_POOL_MALLOC
356: extern void log_pool_stats(Pool& pool);
357: log_pool_stats(pool);
358: #endif
359: }
360:
361: void call_real_parser_handler__do_SEH(
362: const char *filespec_to_process,
363: const char *request_method, bool header_only) {
1.133 paf 364: #if _MSC_VER && !defined(_DEBUG)
1.122 parser 365: LPEXCEPTION_POINTERS system_exception=0;
366: __try {
367: #endif
368: real_parser_handler(
369: filespec_to_process,
370: request_method, header_only);
371:
1.133 paf 372: #if _MSC_VER && !defined(_DEBUG)
1.122 parser 373: } __except (
374: (system_exception=GetExceptionInformation()),
375: EXCEPTION_EXECUTE_HANDLER) {
376:
377: if(system_exception)
378: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
379: throw Exception(0, 0,
380: 0,
381: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
382: else
383: throw Exception(0, 0, 0, "Exception <no exception record>");
384: else
385: throw Exception(0, 0, 0, "Exception <no exception information>");
386: }
387: #endif
388: }
389:
1.139 paf 390: #if _MSC_VER
1.135 paf 391: int failed_new(size_t size) {
392: SAPI::die("out of memory in 'new', failed to allocated %u bytes", size);
393: return 0; // not reached
1.131 paf 394: }
1.135 paf 395: #endif
1.131 paf 396:
1.135 paf 397: #ifdef HAVE_SET_NEW_HANDLER
1.134 paf 398: void failed_new() {
1.135 paf 399: SAPI::die("out of memory in 'new'");
1.131 paf 400: }
401: #endif
402:
1.5 paf 403: int main(int argc, char *argv[]) {
1.45 paf 404: argv0=argv[0];
405:
1.32 paf 406: umask(2);
407:
1.3 paf 408: // were we started as CGI?
1.20 paf 409: cgi=
1.109 parser 410: getenv("SERVER_SOFTWARE") ||
411: getenv("SERVER_NAME") ||
412: getenv("GATEWAY_INTERFACE") ||
413: getenv("REQUEST_METHOD");
1.5 paf 414:
1.10 paf 415: if(!cgi) {
416: if(argc<2) {
1.69 paf 417: printf(
1.100 parser 418: "Parser/%s Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)\n"
1.128 paf 419: "Author: Alexander Petrosyan <paf@design.ru>(http://paf.design.ru)\n"
1.100 parser 420: "\n"
421: "Usage: %s <file>\n",
1.69 paf 422: PARSER_VERSION,
423: argv0?argv0:"parser3");
1.67 paf 424: return 1;
1.10 paf 425: }
426: }
427:
1.100 parser 428: #ifdef WIN32
429: setmode(fileno(stdin), _O_BINARY);
430: setmode(fileno(stdout), _O_BINARY);
431: setmode(fileno(stderr), _O_BINARY);
432: #endif
433:
1.139 paf 434: #if _MSC_VER
1.138 paf 435: _set_new_handler(failed_new);
436: #endif
437:
438: #ifdef HAVE_SET_NEW_HANDLER
439: std::set_new_handler(failed_new);
440: #endif
441:
1.109 parser 442: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36 paf 443: #ifdef WIN32
1.43 paf 444: back_slashes_to_slashes(filespec_to_process);
1.36 paf 445: #endif
1.97 parser 446: filespec_to_process=full_file_spec(filespec_to_process);
1.10 paf 447:
1.109 parser 448: const char *request_method=getenv("REQUEST_METHOD");
1.35 paf 449: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131 paf 450:
1.122 parser 451: try { // global try
452: call_real_parser_handler__do_SEH(
453: filespec_to_process,
454: request_method, header_only);
455: } catch(const Exception& e) { // global problem
1.44 paf 456: // don't allocate anything on pool here:
457: // possible pool' exception not catch-ed now
458: // and there could be out-of-memory exception
1.43 paf 459:
1.134 paf 460: SAPI::die("exception in request exception handler: ", e.comment());
461: #ifndef _DEBUG
1.131 paf 462: } catch(...) {
1.134 paf 463: SAPI::die("<unknown exception>");
1.133 paf 464: #endif
1.16 paf 465: }
1.122 parser 466:
1.109 parser 467:
468: #ifndef WIN32
469: //
470: if(!cgi)
471: SAPI::send_body(pool, "\n", 1);
472: #endif
1.134 paf 473: return 0;
1.1 paf 474: }
E-mail: