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