Annotation of parser3/src/targets/cgi/parser3.C, revision 1.138
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.138 ! paf 7: $Id: parser3.C,v 1.137 2001/11/16 16:00:59 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, ...) {
1.137 paf 114: #ifdef DEBUG_POOL_MALLOC
115: extern void log_pool_stats(Pool& pool);
116: log_pool_stats(pool);
117: #endif
118:
1.138 ! paf 119: // log
! 120:
! 121: // logging is more important than user
! 122: // she can cancel download, we'd get SIG_PIPE,
! 123: // nothing would be logged then
1.124 parser 124: va_list args;
125: va_start(args,fmt);
126: ::log(fmt, args);
127: va_end(args);
128:
1.138 ! paf 129: // inform user
! 130:
1.134 paf 131: char body[MAX_STRING];
1.138 ! paf 132: int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134 paf 133:
134: // prepare header
135: // let's be honest, that's bad we couldn't produce valid output
136: SAPI::add_header_attribute(pool, "status", "500");
137: SAPI::add_header_attribute(pool, "content-type", "text/plain");
138: char content_length_cstr[MAX_NUMBER];
139: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
140: SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
141:
142: // send header
143: SAPI::send_header(pool);
144:
145: // body
146: SAPI::send_body(pool, body, content_length);
147:
1.124 parser 148: exit(1);
1.61 paf 149: }
150:
1.122 parser 151: const char *SAPI::get_env(Pool& , const char *name) {
1.109 parser 152: return getenv(name);
1.28 paf 153: }
154:
1.122 parser 155: size_t SAPI::read_post(Pool& , char *buf, size_t max_bytes) {
1.59 paf 156: size_t read_size=0;
1.12 paf 157: do {
1.36 paf 158: int chunk_size=read(fileno(stdin),
1.42 paf 159: buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129 paf 160: if(chunk_size<=0)
1.12 paf 161: break;
162: read_size+=chunk_size;
163: } while(read_size<max_bytes);
164:
165: return read_size;
1.10 paf 166: }
167:
1.122 parser 168: void SAPI::add_header_attribute(Pool& , const char *key, const char *value) {
1.68 paf 169: if(cgi)
1.20 paf 170: printf("%s: %s\n", key, value);
1.19 paf 171: }
172:
1.56 paf 173: /// @todo intelligent cache-control
1.122 parser 174: void SAPI::send_header(Pool& ) {
1.33 paf 175: if(cgi) {
1.55 paf 176: puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33 paf 177:
178: // header | body delimiter
1.20 paf 179: puts("");
1.33 paf 180: }
1.30 paf 181: }
1.20 paf 182:
1.122 parser 183: void SAPI::send_body(Pool& , const void *buf, size_t size) {
1.19 paf 184: stdout_write(buf, size);
1.58 paf 185: }
186:
1.97 parser 187: //
188:
189: char *full_file_spec(char *file_name) {
1.108 parser 190: if(file_name && !strchr(file_name, '/')) {
1.97 parser 191: static char cwd[MAX_STRING]; getcwd(cwd, MAX_STRING);
192: static char buf[MAX_STRING];
193: snprintf(buf, MAX_STRING, "%s/%s", cwd, file_name);
194: return buf;
195: }
196: return file_name;
197: }
198:
1.40 paf 199: /**
1.122 parser 200: main workhorse
1.19 paf 201:
1.122 parser 202: @todo
1.40 paf 203: IIS: remove trailing default-document[index.html] from $request.uri.
204: to do that we need to consult metabase,
205: wich is tested but seems slow.
206: */
1.122 parser 207: void real_parser_handler(
208: const char *filespec_to_process,
209: const char *request_method, bool header_only) {
210: // init socks
211: init_socks(pool);
212:
213: #ifdef XML
214: /**
215: * Initialize Xerces and Xalan.
216: *
217: * Should be called only once per process before making
218: * any other API calls.
219: */
220: //_asm int 3;
221: XalanInitialize();
222: #endif
223:
224: // init global classes
225: init_methoded_array(pool);
226: // init global variables
227: pa_globals_init(pool);
228:
229: if(!filespec_to_process)
230: throw Exception(0, 0,
231: 0,
232: "Parser/%s", PARSER_VERSION);
233:
234: // Request info
235: Request::Info request_info;
236: if(cgi) {
237: if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
238: request_info.document_root=env_document_root;
239: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
240: // IIS
241: size_t len=strlen(filespec_to_process)-strlen(path_info);
242: char *buf=(char *)pool.malloc(len+1);
243: memcpy(buf, filespec_to_process, len); buf[len]=0;
244: request_info.document_root=buf;
245: } else
246: throw Exception(0, 0,
247: 0,
248: "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
249: } else {
250: static char buf[MAX_STRING];
251: strncpy(buf, filespec_to_process, MAX_STRING-1); buf[MAX_STRING-1]=0;
252: if(rsplit(buf, '/') || rsplit(buf, '\\')) // strip filename
253: request_info.document_root=buf;
254: else
255: request_info.document_root="";
256: }
257: request_info.path_translated=filespec_to_process;
258: request_info.method=request_method ? request_method : "GET";
259: const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
260: request_info.query_string=query_string;
261: if(cgi) {
262: if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
263: request_info.uri=env_request_uri;
264: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
265: if(query_string) {
266: char *reconstructed_uri=(char *)pool.malloc(
267: strlen(path_info)+1/*'?'*/+
268: strlen(query_string)+1/*0*/);
269: strcpy(reconstructed_uri, path_info);
270: strcat(reconstructed_uri, "?");
271: strcat(reconstructed_uri, query_string);
272: request_info.uri=reconstructed_uri;
273: } else
274: request_info.uri=path_info;
275: else
276: throw Exception(0, 0,
277: 0,
278: "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
279:
280: if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
281: size_t script_name_len=strlen(script_name);
282: size_t uri_len=strlen(request_info.uri);
283: if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
284: script_name_len != uri_len) // under IIS they are the same
285: throw Exception(0, 0,
286: 0,
287: "CGI: illegal call");
288: }
289: } else
290: request_info.uri=0;
291:
292: request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
293: const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
294: request_info.content_length=(content_length?atoi(content_length):0);
295: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
296: request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
297:
298: // prepare to process request
299: Request request(pool,
300: request_info,
1.136 paf 301: cgi ? String::UL_OPTIMIZED_HTML : String::UL_AS_IS,
1.130 paf 302: true /* status_allowed */);
1.122 parser 303:
304: // some root-controlled location
305: #ifdef SYSCONFDIR
306: const char *root_config_filespec=SYSCONFDIR "/" CONFIG_FILE_NAME;
307: #else
308: # ifdef WIN32
309: // c:\windows
310: char root_config_path[MAX_STRING];
311: GetWindowsDirectory(root_config_path, MAX_STRING);
312:
313: char root_config_filespec[MAX_STRING];
314: snprintf(root_config_filespec, MAX_STRING,
315: "%s/%s",
316: root_config_path, CONFIG_FILE_NAME);
317: # else
318: #error must be compiled either configure/make or MSVC++
319: # endif
320: #endif
321:
322: // beside by binary
323: // @todo full path, not ./!
324: static char site_config_path[MAX_STRING];
325: strncpy(site_config_path, argv0, MAX_STRING-1); site_config_path[MAX_STRING-1]=0; // filespec of my binary
326: if(!(
327: rsplit(site_config_path, '/') ||
328: rsplit(site_config_path, '\\'))) { // strip filename
329: // no path, just filename
330: site_config_path[0]='.'; site_config_path[1]=0;
331: }
332:
333: char site_config_filespec[MAX_STRING];
334: snprintf(site_config_filespec, MAX_STRING,
335: "%s/%s",
336: site_config_path, CONFIG_FILE_NAME);
337:
338: // process the request
339: request.core(
340: root_config_filespec, false,
341: site_config_filespec, false,
342: header_only);
343:
344: //
345: done_socks();
346:
347: #ifdef DEBUG_POOL_MALLOC
348: extern void log_pool_stats(Pool& pool);
349: log_pool_stats(pool);
350: #endif
351: }
352:
353: void call_real_parser_handler__do_SEH(
354: const char *filespec_to_process,
355: const char *request_method, bool header_only) {
1.133 paf 356: #if _MSC_VER && !defined(_DEBUG)
1.122 parser 357: LPEXCEPTION_POINTERS system_exception=0;
358: __try {
359: #endif
360: real_parser_handler(
361: filespec_to_process,
362: request_method, header_only);
363:
1.133 paf 364: #if _MSC_VER && !defined(_DEBUG)
1.122 parser 365: } __except (
366: (system_exception=GetExceptionInformation()),
367: EXCEPTION_EXECUTE_HANDLER) {
368:
369: if(system_exception)
370: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
371: throw Exception(0, 0,
372: 0,
373: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
374: else
375: throw Exception(0, 0, 0, "Exception <no exception record>");
376: else
377: throw Exception(0, 0, 0, "Exception <no exception information>");
378: }
379: #endif
380: }
381:
1.135 paf 382: #ifdef WIN32
383: int failed_new(size_t size) {
384: SAPI::die("out of memory in 'new', failed to allocated %u bytes", size);
385: return 0; // not reached
1.131 paf 386: }
1.135 paf 387: #endif
1.131 paf 388:
1.135 paf 389: #ifdef HAVE_SET_NEW_HANDLER
1.134 paf 390: void failed_new() {
1.135 paf 391: SAPI::die("out of memory in 'new'");
1.131 paf 392: }
393: #endif
394:
1.5 paf 395: int main(int argc, char *argv[]) {
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.138 ! paf 426: #ifdef WIN32
! 427: _set_new_handler(failed_new);
! 428: #endif
! 429:
! 430: #ifdef HAVE_SET_NEW_HANDLER
! 431: std::set_new_handler(failed_new);
! 432: #endif
! 433:
1.109 parser 434: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36 paf 435: #ifdef WIN32
1.43 paf 436: back_slashes_to_slashes(filespec_to_process);
1.36 paf 437: #endif
1.97 parser 438: filespec_to_process=full_file_spec(filespec_to_process);
1.10 paf 439:
1.109 parser 440: const char *request_method=getenv("REQUEST_METHOD");
1.35 paf 441: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131 paf 442:
1.122 parser 443: try { // global try
444: call_real_parser_handler__do_SEH(
445: filespec_to_process,
446: request_method, header_only);
447: } catch(const Exception& e) { // global problem
1.44 paf 448: // don't allocate anything on pool here:
449: // possible pool' exception not catch-ed now
450: // and there could be out-of-memory exception
1.43 paf 451:
1.134 paf 452: SAPI::die("exception in request exception handler: ", e.comment());
453: #ifndef _DEBUG
1.131 paf 454: } catch(...) {
1.134 paf 455: SAPI::die("<unknown exception>");
1.133 paf 456: #endif
1.16 paf 457: }
1.122 parser 458:
1.109 parser 459:
460: #ifndef WIN32
461: //
462: if(!cgi)
463: SAPI::send_body(pool, "\n", 1);
464: #endif
1.134 paf 465: return 0;
1.1 paf 466: }
E-mail: