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