Annotation of parser3/src/targets/cgi/parser3.C, revision 1.147
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.147 ! paf 7: $Id: parser3.C,v 1.146 2001/12/06 20:19:31 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.144 paf 122: va_list args;
123: va_start(args,fmt);
1.138 paf 124: // log
125:
126: // logging is more important than user
127: // she can cancel download, we'd get SIG_PIPE,
128: // nothing would be logged then
1.124 parser 129: ::log(fmt, args);
130:
1.138 paf 131: // inform user
132:
1.134 paf 133: char body[MAX_STRING];
1.138 paf 134: int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134 paf 135:
1.144 paf 136: va_end(args);
137:
1.134 paf 138: // prepare header
139: // let's be honest, that's bad we couldn't produce valid output
140: SAPI::add_header_attribute(pool, "status", "500");
141: SAPI::add_header_attribute(pool, "content-type", "text/plain");
142: char content_length_cstr[MAX_NUMBER];
143: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
144: SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
145:
146: // send header
147: SAPI::send_header(pool);
148:
149: // body
150: SAPI::send_body(pool, body, content_length);
151:
1.124 parser 152: exit(1);
1.61 paf 153: }
154:
1.122 parser 155: const char *SAPI::get_env(Pool& , const char *name) {
1.109 parser 156: return getenv(name);
1.28 paf 157: }
158:
1.122 parser 159: size_t SAPI::read_post(Pool& , char *buf, size_t max_bytes) {
1.59 paf 160: size_t read_size=0;
1.12 paf 161: do {
1.36 paf 162: int chunk_size=read(fileno(stdin),
1.42 paf 163: buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129 paf 164: if(chunk_size<=0)
1.12 paf 165: break;
166: read_size+=chunk_size;
167: } while(read_size<max_bytes);
168:
169: return read_size;
1.10 paf 170: }
171:
1.122 parser 172: void SAPI::add_header_attribute(Pool& , const char *key, const char *value) {
1.68 paf 173: if(cgi)
1.20 paf 174: printf("%s: %s\n", key, value);
1.19 paf 175: }
176:
1.56 paf 177: /// @todo intelligent cache-control
1.122 parser 178: void SAPI::send_header(Pool& ) {
1.33 paf 179: if(cgi) {
1.147 ! paf 180: // puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33 paf 181:
182: // header | body delimiter
1.20 paf 183: puts("");
1.33 paf 184: }
1.30 paf 185: }
1.20 paf 186:
1.122 parser 187: void SAPI::send_body(Pool& , const void *buf, size_t size) {
1.19 paf 188: stdout_write(buf, size);
1.58 paf 189: }
190:
1.97 parser 191: //
192:
193: char *full_file_spec(char *file_name) {
1.108 parser 194: if(file_name && !strchr(file_name, '/')) {
1.97 parser 195: static char cwd[MAX_STRING]; getcwd(cwd, MAX_STRING);
196: static char buf[MAX_STRING];
197: snprintf(buf, MAX_STRING, "%s/%s", cwd, file_name);
198: return buf;
199: }
200: return file_name;
201: }
202:
1.40 paf 203: /**
1.122 parser 204: main workhorse
1.19 paf 205:
1.122 parser 206: @todo
1.40 paf 207: IIS: remove trailing default-document[index.html] from $request.uri.
208: to do that we need to consult metabase,
209: wich is tested but seems slow.
1.144 paf 210: IIS5 todo find out proper 'illegal call' check
1.40 paf 211: */
1.122 parser 212: void real_parser_handler(
213: const char *filespec_to_process,
214: const char *request_method, bool header_only) {
215: // init socks
216: init_socks(pool);
217:
218: #ifdef XML
219: /**
220: * Initialize Xerces and Xalan.
221: *
222: * Should be called only once per process before making
223: * any other API calls.
224: */
225: //_asm int 3;
226: XalanInitialize();
227: #endif
228:
229: // init global classes
230: init_methoded_array(pool);
231: // init global variables
232: pa_globals_init(pool);
233:
234: if(!filespec_to_process)
1.144 paf 235: SAPI::die("Parser/%s", PARSER_VERSION);
1.122 parser 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)");
1.145 paf 282:
283: #ifndef WIN32
284: // they've changed this under IIS5.
1.122 parser 285: if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
286: size_t script_name_len=strlen(script_name);
287: size_t uri_len=strlen(request_info.uri);
288: if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
289: script_name_len != uri_len) // under IIS they are the same
1.144 paf 290: SAPI::die("CGI: illegal call");
1.122 parser 291: }
1.145 paf 292: #endif
1.122 parser 293: } else
294: request_info.uri=0;
295:
296: request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
297: const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
298: request_info.content_length=(content_length?atoi(content_length):0);
299: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
300: request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
301:
302: // prepare to process request
303: Request request(pool,
304: request_info,
1.143 paf 305: #ifdef _DEBUG
306: String::UL_HTML|String::UL_OPTIMIZE_BIT
307: #else
308: cgi ? String::UL_HTML|String::UL_OPTIMIZE_BIT : String::UL_AS_IS
309: #endif
310: ,
1.130 paf 311: true /* status_allowed */);
1.122 parser 312:
313: // some root-controlled location
314: #ifdef SYSCONFDIR
315: const char *root_config_filespec=SYSCONFDIR "/" CONFIG_FILE_NAME;
316: #else
317: # ifdef WIN32
318: // c:\windows
319: char root_config_path[MAX_STRING];
320: GetWindowsDirectory(root_config_path, MAX_STRING);
321:
322: char root_config_filespec[MAX_STRING];
323: snprintf(root_config_filespec, MAX_STRING,
324: "%s/%s",
325: root_config_path, CONFIG_FILE_NAME);
326: # else
327: #error must be compiled either configure/make or MSVC++
328: # endif
329: #endif
330:
331: // beside by binary
332: // @todo full path, not ./!
333: static char site_config_path[MAX_STRING];
334: strncpy(site_config_path, argv0, MAX_STRING-1); site_config_path[MAX_STRING-1]=0; // filespec of my binary
335: if(!(
336: rsplit(site_config_path, '/') ||
337: rsplit(site_config_path, '\\'))) { // strip filename
338: // no path, just filename
339: site_config_path[0]='.'; site_config_path[1]=0;
340: }
341:
342: char site_config_filespec[MAX_STRING];
343: snprintf(site_config_filespec, MAX_STRING,
344: "%s/%s",
345: site_config_path, CONFIG_FILE_NAME);
346:
347: // process the request
348: request.core(
349: root_config_filespec, false,
350: site_config_filespec, false,
351: header_only);
352:
353: //
354: done_socks();
355:
356: #ifdef DEBUG_POOL_MALLOC
357: extern void log_pool_stats(Pool& pool);
358: log_pool_stats(pool);
359: #endif
360: }
361:
362: void call_real_parser_handler__do_SEH(
363: const char *filespec_to_process,
364: const char *request_method, bool header_only) {
1.133 paf 365: #if _MSC_VER && !defined(_DEBUG)
1.122 parser 366: LPEXCEPTION_POINTERS system_exception=0;
367: __try {
368: #endif
369: real_parser_handler(
370: filespec_to_process,
371: request_method, header_only);
372:
1.133 paf 373: #if _MSC_VER && !defined(_DEBUG)
1.122 parser 374: } __except (
375: (system_exception=GetExceptionInformation()),
376: EXCEPTION_EXECUTE_HANDLER) {
377:
378: if(system_exception)
379: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
380: throw Exception(0, 0,
381: 0,
382: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
383: else
384: throw Exception(0, 0, 0, "Exception <no exception record>");
385: else
386: throw Exception(0, 0, 0, "Exception <no exception information>");
387: }
388: #endif
389: }
390:
1.139 paf 391: #if _MSC_VER
1.135 paf 392: int failed_new(size_t size) {
393: SAPI::die("out of memory in 'new', failed to allocated %u bytes", size);
394: return 0; // not reached
1.131 paf 395: }
1.135 paf 396: #endif
1.131 paf 397:
1.135 paf 398: #ifdef HAVE_SET_NEW_HANDLER
1.134 paf 399: void failed_new() {
1.135 paf 400: SAPI::die("out of memory in 'new'");
1.131 paf 401: }
402: #endif
403:
1.5 paf 404: int main(int argc, char *argv[]) {
1.144 paf 405: // _asm int 3;
1.45 paf 406: argv0=argv[0];
407:
1.32 paf 408: umask(2);
409:
1.3 paf 410: // were we started as CGI?
1.146 paf 411: cgi=
1.109 parser 412: getenv("SERVER_SOFTWARE") ||
413: getenv("SERVER_NAME") ||
414: getenv("GATEWAY_INTERFACE") ||
415: getenv("REQUEST_METHOD");
1.5 paf 416:
1.10 paf 417: if(!cgi) {
418: if(argc<2) {
1.69 paf 419: printf(
1.100 parser 420: "Parser/%s Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)\n"
1.128 paf 421: "Author: Alexander Petrosyan <paf@design.ru>(http://paf.design.ru)\n"
1.100 parser 422: "\n"
423: "Usage: %s <file>\n",
1.69 paf 424: PARSER_VERSION,
425: argv0?argv0:"parser3");
1.67 paf 426: return 1;
1.10 paf 427: }
428: }
429:
1.100 parser 430: #ifdef WIN32
431: setmode(fileno(stdin), _O_BINARY);
432: setmode(fileno(stdout), _O_BINARY);
433: setmode(fileno(stderr), _O_BINARY);
434: #endif
435:
1.139 paf 436: #if _MSC_VER
1.138 paf 437: _set_new_handler(failed_new);
438: #endif
439:
440: #ifdef HAVE_SET_NEW_HANDLER
441: std::set_new_handler(failed_new);
442: #endif
443:
1.109 parser 444: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36 paf 445: #ifdef WIN32
1.43 paf 446: back_slashes_to_slashes(filespec_to_process);
1.36 paf 447: #endif
1.97 parser 448: filespec_to_process=full_file_spec(filespec_to_process);
1.10 paf 449:
1.109 parser 450: const char *request_method=getenv("REQUEST_METHOD");
1.35 paf 451: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131 paf 452:
1.122 parser 453: try { // global try
454: call_real_parser_handler__do_SEH(
455: filespec_to_process,
456: request_method, header_only);
457: } catch(const Exception& e) { // global problem
1.44 paf 458: // don't allocate anything on pool here:
459: // possible pool' exception not catch-ed now
460: // and there could be out-of-memory exception
1.43 paf 461:
1.144 paf 462: SAPI::die("exception in request exception handler: %s", e.comment());
1.134 paf 463: #ifndef _DEBUG
1.131 paf 464: } catch(...) {
1.134 paf 465: SAPI::die("<unknown exception>");
1.133 paf 466: #endif
1.16 paf 467: }
1.122 parser 468:
1.109 parser 469:
470: #ifndef WIN32
471: //
472: if(!cgi)
473: SAPI::send_body(pool, "\n", 1);
474: #endif
1.134 paf 475: return 0;
1.1 paf 476: }
E-mail: