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