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