Annotation of parser3/src/targets/cgi/parser3.C, revision 1.42
1.27 paf 1: /** @file
2: Parser: scripting and CGI main.
3:
1.1 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.27 paf 5:
1.1 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.42 ! paf 8: $Id: parser3.C,v 1.41 2001/03/23 14:03:28 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.36 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.40 paf 53: #ifdef WIN32
1.30 paf 54: static void fix_slashes(char *s) {
1.27 paf 55: if(s)
56: for(; *s; s++)
57: if(*s=='\\')
58: *s='/';
1.26 paf 59: }
1.40 paf 60: #endif
1.3 paf 61:
1.37 paf 62: //@{
63: /// SAPI funcs decl
64: const char *SAPI::get_env(Pool& pool, const char *name) {
1.28 paf 65: return getenv(name);
66: }
67:
1.37 paf 68: uint SAPI::read_post(Pool& pool, char *buf, uint max_bytes) {
1.36 paf 69: uint read_size=0;
1.12 paf 70: do {
1.36 paf 71: int chunk_size=read(fileno(stdin),
1.42 ! paf 72: buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.12 paf 73: if(chunk_size<0)
74: break;
75: read_size+=chunk_size;
76: } while(read_size<max_bytes);
77:
78: return read_size;
1.10 paf 79: }
80:
1.37 paf 81: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.20 paf 82: if(cgi)
83: printf("%s: %s\n", key, value);
1.19 paf 84: }
85:
1.33 paf 86: /// @todo intelligent cache-control
1.37 paf 87: void SAPI::send_header(Pool& pool) {
1.33 paf 88: if(cgi) {
1.41 paf 89: puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33 paf 90:
91: // header | body delimiter
1.20 paf 92: puts("");
1.33 paf 93: }
1.30 paf 94: }
1.20 paf 95:
1.37 paf 96: void SAPI::send_body(Pool& pool, const char *buf, size_t size) {
1.19 paf 97: stdout_write(buf, size);
1.17 paf 98: }
1.37 paf 99: //@}
1.30 paf 100:
1.40 paf 101: /**
102: main workhorse
1.19 paf 103:
1.40 paf 104: @todo
105: IIS: remove trailing default-document[index.html] from $request.uri.
106: to do that we need to consult metabase,
107: wich is tested but seems slow.
108: */
1.5 paf 109: int main(int argc, char *argv[]) {
1.32 paf 110: umask(2);
111:
112: #ifdef WIN32
1.27 paf 113: setmode(fileno(stdin), _O_BINARY);
114: setmode(fileno(stdout), _O_BINARY);
115: setmode(fileno(stderr), _O_BINARY);
1.32 paf 116: #endif
1.12 paf 117:
1.3 paf 118: // were we started as CGI?
1.20 paf 119: cgi=
1.2 paf 120: getenv("SERVER_SOFTWARE") ||
121: getenv("SERVER_NAME") ||
122: getenv("GATEWAY_INTERFACE") ||
123: getenv("REQUEST_METHOD");
1.5 paf 124:
1.10 paf 125: if(!cgi) {
126: if(argc<2) {
127: char *binary=argv[0];
1.13 paf 128: printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10 paf 129: exit(1);
130: }
131: }
132:
1.26 paf 133: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36 paf 134: #ifdef WIN32
1.26 paf 135: fix_slashes(filespec_to_process);
1.36 paf 136: #endif
1.10 paf 137:
1.35 paf 138: const char *request_method=getenv("REQUEST_METHOD");
139: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.5 paf 140: PTRY { // global try
141: // must be first in PTRY{}PCATCH
1.40 paf 142: #ifdef WIN32
1.5 paf 143: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
144: //TODO: initSocks();
145: #endif
1.2 paf 146:
1.10 paf 147: // init global variables
1.37 paf 148: pa_globals_init(pool);
1.10 paf 149:
1.13 paf 150: if(!filespec_to_process)
151: PTHROW(0, 0,
152: 0,
153: "no file to process");
154:
1.10 paf 155: // Request info
156: Request::Info request_info;
1.39 paf 157: if(cgi) {
158: if(const char *env_document_root=getenv("DOCUMENT_ROOT"))
159: request_info.document_root=env_document_root;
160: else if(const char *path_info=getenv("PATH_INFO")) {
1.40 paf 161: // IIS
1.39 paf 162: size_t len=strlen(filespec_to_process)-strlen(path_info);
1.40 paf 163: char *buf=(char *)pool.malloc(len+1);
164: strncpy(buf, filespec_to_process, len);
1.39 paf 165: buf[len]=0;
166: request_info.document_root=buf;
167: } else
168: PTHROW(0, 0,
169: 0,
170: "CGI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
171: } else {
172: static char buf[MAX_STRING];
173: strncpy(buf, filespec_to_process, MAX_STRING);
174: rsplit(buf, '/'); rsplit(buf, '\\');// strip filename
175: request_info.document_root=buf;
1.13 paf 176: }
1.10 paf 177: request_info.path_translated=filespec_to_process;
1.35 paf 178: request_info.method=request_method;
1.40 paf 179: const char *query_string=getenv("QUERY_STRING");
180: request_info.query_string=query_string;
1.42 ! paf 181: if(cgi)
! 182: if(const char *env_request_uri=getenv("REQUEST_URI"))
! 183: request_info.uri=env_request_uri;
! 184: else if (const char *path_info=getenv("PATH_INFO"))
! 185: if(query_string) {
! 186: char *reconstructed_uri=(char *)malloc(
! 187: strlen(path_info)+1/*'?'*/+
! 188: strlen(query_string)+1/*0*/);
! 189: strcpy(reconstructed_uri, path_info);
! 190: strcat(reconstructed_uri, "?");
! 191: strcat(reconstructed_uri, query_string);
! 192: request_info.uri=reconstructed_uri;
! 193: } else
! 194: request_info.uri=path_info;
! 195: else
! 196: PTHROW(0, 0,
! 197: 0,
! 198: "CGI: no PATH_INFO defined (in reinventing REQUEST_URI)");
! 199: else
! 200: request_info.uri=0;
1.40 paf 201:
1.10 paf 202: request_info.content_type=getenv("CONTENT_TYPE");
203: const char *content_length=getenv("CONTENT_LENGTH");
204: request_info.content_length=(content_length?atoi(content_length):0);
1.21 paf 205: request_info.cookie=getenv("HTTP_COOKIE");
1.10 paf 206:
1.5 paf 207: // prepare to process request
1.38 paf 208: Request request(pool,
1.10 paf 209: request_info,
1.27 paf 210: cgi ? String::UL_HTML_TYPO : String::UL_NO
1.5 paf 211: );
212:
213: // some root-controlled location
1.3 paf 214: #ifdef WIN32
1.10 paf 215: // c:\windows
1.36 paf 216: static char root_auto_path[MAX_STRING];
1.30 paf 217: GetWindowsDirectory(root_auto_path, MAX_STRING);
1.3 paf 218: #else
1.40 paf 219: // ~nobody todo: figure out a better place
1.36 paf 220: char *root_auto_path=getenv("HOME");
1.5 paf 221: #endif
222:
223: // beside by binary
1.36 paf 224: static char site_auto_path[MAX_STRING];
1.30 paf 225: strncpy(site_auto_path, argv[0], MAX_STRING); // filespec of my binary
226: rsplit(site_auto_path, '/'); rsplit(site_auto_path, '\\');// strip filename
1.5 paf 227:
228: // process the request
1.35 paf 229: request.core(
1.30 paf 230: root_auto_path, false,
1.31 paf 231: site_auto_path, false,
1.35 paf 232: header_only);
1.16 paf 233:
1.22 paf 234: // must be last in PTRY{}PCATCH
1.40 paf 235: #ifdef WIN32
1.5 paf 236: SetUnhandledExceptionFilter(0);
1.25 paf 237: #endif
1.16 paf 238: // successful finish
239: return 0;
240: } PCATCH(e) { // global problem
1.19 paf 241: const char *body=e.comment();
242: int content_length=strlen(body);
1.5 paf 243:
1.35 paf 244: // prepare header
1.37 paf 245: SAPI::add_header_attribute(pool, "content-type", "text/plain");
1.19 paf 246: char content_length_cstr[MAX_NUMBER];
1.34 paf 247: snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.37 paf 248: SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
1.35 paf 249:
250: // send header
1.37 paf 251: SAPI::send_header(pool);
1.19 paf 252:
253: // body
1.35 paf 254: if(!header_only)
1.37 paf 255: SAPI::send_body(pool, body, content_length);
1.2 paf 256:
1.16 paf 257: // unsuccessful finish
258: return 1;
259: }
260: PEND_CATCH
1.1 paf 261: }
E-mail: