Annotation of parser3/src/targets/cgi/parser3.C, revision 1.77
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.27 paf 5:
1.43 paf 6: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1 paf 7:
1.77 ! parser 8: $Id: parser3.C,v 1.76 2001/04/28 15:22:47 paf Exp $
1.1 paf 9: */
10:
1.40 paf 11: #include "pa_config_includes.h"
1.3 paf 12:
13: #ifdef WIN32
14: # include <windows.h>
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.69 paf 23:
1.42 paf 24: /// IIS refuses to read bigger chunks
25: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M
26:
1.45 paf 27: const char *argv0;
1.42 paf 28: Pool pool(0); // global pool [dont describe to doxygen: it confuses it with param names]
1.27 paf 29: bool cgi; ///< we were started as CGI?
1.5 paf 30:
1.40 paf 31: #ifdef WIN32
32: /// global system errors into parser exceptions converter
1.43 paf 33: static LONG WINAPI TopLevelExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo) {
1.5 paf 34: char buf[MAX_STRING];
35: if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
1.36 paf 36: struct _EXCEPTION_RECORD *er=ExceptionInfo->ExceptionRecord;
1.46 paf 37: snprintf(buf, MAX_STRING, "Exception 0x%X at %p",
1.31 paf 38: er->ExceptionCode,
39: er->ExceptionAddress);
1.5 paf 40: } else
41: strcpy(buf, "Exception <unknown>");
42:
43: PTHROW(0, 0,
44: 0,
45: buf);
46:
47: return EXCEPTION_EXECUTE_HANDLER; // never reached
48: }
1.27 paf 49: #endif
50:
1.46 paf 51: // SAPI
1.61 paf 52: // appends to parser3.log located beside my binary
53: void SAPI::log(Pool& pool, const char *fmt, ...) {
54: bool opened;
55: FILE *f=0;
56:
57: if(argv0) {
58: // beside by binary
59: char file_spec[MAX_STRING];
60: strncpy(file_spec, argv0, MAX_STRING); // filespec of my binary
61: rsplit(file_spec, '/'); rsplit(file_spec, '\\');// strip filename
62: strcat(file_spec, "/parser3.log");
63: f=fopen(file_spec, "at");
64: }
65: opened=f!=0;
66: if(!opened)
67: f=stderr;
68:
69: // prefix
70: time_t t=time(0);
71: const char *stamp=ctime(&t);
72: fprintf(f, "[%.*s] ", strlen(stamp)-1, stamp);
73: // message
74: va_list args;
75: va_start(args,fmt);
76: vfprintf(f, fmt, args);
77: va_end(args);
78: // newline
79: fprintf(f, "\n");
80:
81: if(opened)
82: fclose(f);
83: }
84:
1.37 paf 85: const char *SAPI::get_env(Pool& pool, const char *name) {
1.28 paf 86: return getenv(name);
87: }
88:
1.59 paf 89: size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) {
90: size_t read_size=0;
1.12 paf 91: do {
1.36 paf 92: int chunk_size=read(fileno(stdin),
1.42 paf 93: buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.12 paf 94: if(chunk_size<0)
95: break;
96: read_size+=chunk_size;
97: } while(read_size<max_bytes);
98:
99: return read_size;
1.10 paf 100: }
101:
1.37 paf 102: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.68 paf 103: if(cgi)
1.20 paf 104: printf("%s: %s\n", key, value);
1.19 paf 105: }
106:
1.56 paf 107: /// @todo intelligent cache-control
1.37 paf 108: void SAPI::send_header(Pool& pool) {
1.33 paf 109: if(cgi) {
1.55 paf 110: puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33 paf 111:
112: // header | body delimiter
1.20 paf 113: puts("");
1.33 paf 114: }
1.30 paf 115: }
1.20 paf 116:
1.53 paf 117: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.19 paf 118: stdout_write(buf, size);
1.58 paf 119: }
120:
1.40 paf 121: /**
122: main workhorse
1.19 paf 123:
1.56 paf 124: @todo
1.40 paf 125: IIS: remove trailing default-document[index.html] from $request.uri.
126: to do that we need to consult metabase,
127: wich is tested but seems slow.
128: */
1.5 paf 129: int main(int argc, char *argv[]) {
1.45 paf 130: argv0=argv[0];
131:
1.32 paf 132: umask(2);
133:
134: #ifdef WIN32
1.27 paf 135: setmode(fileno(stdin), _O_BINARY);
136: setmode(fileno(stdout), _O_BINARY);
137: setmode(fileno(stderr), _O_BINARY);
1.32 paf 138: #endif
1.12 paf 139:
1.3 paf 140: // were we started as CGI?
1.20 paf 141: cgi=
1.2 paf 142: getenv("SERVER_SOFTWARE") ||
143: getenv("SERVER_NAME") ||
144: getenv("GATEWAY_INTERFACE") ||
145: getenv("REQUEST_METHOD");
1.5 paf 146:
1.10 paf 147: if(!cgi) {
148: if(argc<2) {
1.69 paf 149: printf(
150: "Parser/%s Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)\n"
151: "Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)\n"
152: "\n"
153: "Usage: %s <file>\n",
154: PARSER_VERSION,
155: argv0?argv0:"parser3");
1.67 paf 156: return 1;
1.10 paf 157: }
158: }
159:
1.26 paf 160: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36 paf 161: #ifdef WIN32
1.43 paf 162: back_slashes_to_slashes(filespec_to_process);
1.36 paf 163: #endif
1.10 paf 164:
1.35 paf 165: const char *request_method=getenv("REQUEST_METHOD");
166: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.5 paf 167: PTRY { // global try
168: // must be first in PTRY{}PCATCH
1.40 paf 169: #ifdef WIN32
1.5 paf 170: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
171: #endif
1.2 paf 172:
1.62 paf 173: // init socks
1.57 paf 174: init_socks(pool);
175:
1.73 paf 176: // init global classes
177: init_methoded_array(pool);
1.10 paf 178: // init global variables
1.37 paf 179: pa_globals_init(pool);
1.10 paf 180:
1.13 paf 181: if(!filespec_to_process)
182: PTHROW(0, 0,
183: 0,
1.68 paf 184: "Parser/%s", PARSER_VERSION);
1.13 paf 185:
1.10 paf 186: // Request info
187: Request::Info request_info;
1.39 paf 188: if(cgi) {
1.51 paf 189: if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
1.39 paf 190: request_info.document_root=env_document_root;
1.51 paf 191: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
1.40 paf 192: // IIS
1.39 paf 193: size_t len=strlen(filespec_to_process)-strlen(path_info);
1.40 paf 194: char *buf=(char *)pool.malloc(len+1);
195: strncpy(buf, filespec_to_process, len);
1.39 paf 196: buf[len]=0;
197: request_info.document_root=buf;
198: } else
199: PTHROW(0, 0,
200: 0,
1.43 paf 201: "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.39 paf 202: } else {
203: static char buf[MAX_STRING];
204: strncpy(buf, filespec_to_process, MAX_STRING);
1.70 paf 205: if(rsplit(buf, '/') || rsplit(buf, '\\')) // strip filename
206: request_info.document_root=buf;
207: else
208: request_info.document_root="";
1.13 paf 209: }
1.10 paf 210: request_info.path_translated=filespec_to_process;
1.35 paf 211: request_info.method=request_method;
1.51 paf 212: const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
1.40 paf 213: request_info.query_string=query_string;
1.68 paf 214: if(cgi) {
1.51 paf 215: if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
1.42 paf 216: request_info.uri=env_request_uri;
1.51 paf 217: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
1.42 paf 218: if(query_string) {
219: char *reconstructed_uri=(char *)malloc(
220: strlen(path_info)+1/*'?'*/+
221: strlen(query_string)+1/*0*/);
222: strcpy(reconstructed_uri, path_info);
223: strcat(reconstructed_uri, "?");
224: strcat(reconstructed_uri, query_string);
225: request_info.uri=reconstructed_uri;
226: } else
227: request_info.uri=path_info;
228: else
229: PTHROW(0, 0,
230: 0,
1.43 paf 231: "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
1.68 paf 232:
1.72 paf 233: if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
234: size_t script_name_len=strlen(script_name);
235: size_t uri_len=strlen(request_info.uri);
236: if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
237: script_name_len != uri_len) // under IIS they are the same
238: PTHROW(0, 0,
239: 0,
240: "CGI: illegal call");
241: }
1.68 paf 242: } else
1.42 paf 243: request_info.uri=0;
1.40 paf 244:
1.51 paf 245: request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
246: const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
1.10 paf 247: request_info.content_length=(content_length?atoi(content_length):0);
1.51 paf 248: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
249: request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
1.10 paf 250:
1.5 paf 251: // prepare to process request
1.38 paf 252: Request request(pool,
1.10 paf 253: request_info,
1.74 paf 254: cgi ? String::UL_USER_HTML : String::UL_AS_IS
1.5 paf 255: );
256:
257: // some root-controlled location
1.3 paf 258: #ifdef WIN32
1.10 paf 259: // c:\windows
1.36 paf 260: static char root_auto_path[MAX_STRING];
1.30 paf 261: GetWindowsDirectory(root_auto_path, MAX_STRING);
1.3 paf 262: #else
1.40 paf 263: // ~nobody todo: figure out a better place
1.64 paf 264: const char *root_auto_path=SAPI::get_env(pool, "HOME");
1.5 paf 265: #endif
266:
267: // beside by binary
1.36 paf 268: static char site_auto_path[MAX_STRING];
1.30 paf 269: strncpy(site_auto_path, argv[0], MAX_STRING); // filespec of my binary
270: rsplit(site_auto_path, '/'); rsplit(site_auto_path, '\\');// strip filename
1.5 paf 271:
272: // process the request
1.35 paf 273: request.core(
1.30 paf 274: root_auto_path, false,
1.31 paf 275: site_auto_path, false,
1.35 paf 276: header_only);
1.57 paf 277:
278: //
279: done_socks();
1.77 ! parser 280:
! 281: extern size_t malloc_total, calloc_total;
! 282: extern size_t malloc_times, calloc_times;
! 283: SAPI::log(pool,
! 284: "malloc %d/%d, calloc %d/%d",
! 285: malloc_total, malloc_times,
! 286: calloc_total, calloc_times);
1.16 paf 287:
1.22 paf 288: // must be last in PTRY{}PCATCH
1.40 paf 289: #ifdef WIN32
1.5 paf 290: SetUnhandledExceptionFilter(0);
1.25 paf 291: #endif
1.16 paf 292: // successful finish
293: return 0;
294: } PCATCH(e) { // global problem
1.43 paf 295: // must be first in PCATCH{}
296: #ifdef WIN32
297: SetUnhandledExceptionFilter(0);
298: #endif
1.44 paf 299: // don't allocate anything on pool here:
300: // possible pool' exception not catch-ed now
301: // and there could be out-of-memory exception
1.43 paf 302:
1.19 paf 303: const char *body=e.comment();
1.44 paf 304: // log it
305: SAPI::log(pool, "exception in request exception handler: %s", body);
306:
307: //
1.19 paf 308: int content_length=strlen(body);
1.5 paf 309:
1.35 paf 310: // prepare header
1.37 paf 311: SAPI::add_header_attribute(pool, "content-type", "text/plain");
1.19 paf 312: char content_length_cstr[MAX_NUMBER];
1.60 paf 313: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
1.37 paf 314: SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
1.35 paf 315:
316: // send header
1.37 paf 317: SAPI::send_header(pool);
1.19 paf 318:
319: // body
1.35 paf 320: if(!header_only)
1.37 paf 321: SAPI::send_body(pool, body, content_length);
1.2 paf 322:
1.16 paf 323: // unsuccessful finish
324: return 1;
325: }
326: PEND_CATCH
1.1 paf 327: }
E-mail: