Annotation of parser3/src/targets/cgi/parser3.C, revision 1.37
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.37 ! paf 8: $Id: parser3.C,v 1.36 2001/03/23 10:14:36 paf Exp $
1.1 paf 9: */
10:
1.5 paf 11: #ifdef HAVE_CONFIG_H
12: # include "pa_config.h"
13: #endif
14:
1.3 paf 15:
16: #ifdef WIN32
17: # include <windows.h>
18: # include <io.h>
1.27 paf 19: #else
20: # include <unistd.h>
1.3 paf 21: #endif
1.27 paf 22:
23: //\ifwin32
24: #include <io.h>
25: //#include <fcntl.h>
26: //\endifwin32
27:
1.3 paf 28: #include <stdlib.h>
1.4 paf 29: #include <stdio.h>
30: #include <string.h>
31: #include <fcntl.h>
1.3 paf 32:
1.37 ! paf 33: #include "pa_sapi.h"
1.24 paf 34: #include "pa_common.h"
1.5 paf 35: #include "pa_globals.h"
1.2 paf 36: #include "pa_request.h"
1.1 paf 37:
1.29 paf 38: Pool pool; // global pool
1.27 paf 39: bool cgi; ///< we were started as CGI?
1.5 paf 40:
1.36 paf 41: #if defined(WIN32) && _MSC_VER
1.5 paf 42: // intercept global system errors
1.36 paf 43: static LONG WINAPI TopLevelExceptionFilter (struct _EXCEPTION_POINTERS *ExceptionInfo) {
1.5 paf 44: char buf[MAX_STRING];
45: if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
1.36 paf 46: struct _EXCEPTION_RECORD *er=ExceptionInfo->ExceptionRecord;
1.31 paf 47: snprintf(buf, MAX_STRING, "Exception %#X at %p",
48: er->ExceptionCode,
49: er->ExceptionAddress);
1.5 paf 50: } else
51: strcpy(buf, "Exception <unknown>");
52:
53: PTHROW(0, 0,
54: 0,
55: buf);
56:
57: return EXCEPTION_EXECUTE_HANDLER; // never reached
58: }
1.27 paf 59: #endif
60:
61: //\if
1.30 paf 62: static void fix_slashes(char *s) {
1.27 paf 63: if(s)
64: for(; *s; s++)
65: if(*s=='\\')
66: *s='/';
1.26 paf 67: }
1.27 paf 68: //\endif
1.3 paf 69:
1.37 ! paf 70: //@{
! 71: /// SAPI funcs decl
! 72: const char *SAPI::get_env(Pool& pool, const char *name) {
1.28 paf 73: return getenv(name);
74: }
75:
1.37 ! paf 76: uint SAPI::read_post(Pool& pool, char *buf, uint max_bytes) {
1.36 paf 77: uint read_size=0;
1.12 paf 78: do {
1.36 paf 79: int chunk_size=read(fileno(stdin),
80: buf+read_size, min(0x400*0x400, max_bytes-read_size));
1.12 paf 81: if(chunk_size<0)
82: break;
83: read_size+=chunk_size;
84: } while(read_size<max_bytes);
85:
86: return read_size;
1.10 paf 87: }
88:
1.37 ! paf 89: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.20 paf 90: if(cgi)
91: printf("%s: %s\n", key, value);
1.19 paf 92: }
93:
1.33 paf 94: /// @todo intelligent cache-control
1.37 ! paf 95: void SAPI::send_header(Pool& pool) {
1.33 paf 96: if(cgi) {
1.36 paf 97: puts("Expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33 paf 98:
99: // header | body delimiter
1.20 paf 100: puts("");
1.33 paf 101: }
1.30 paf 102: }
1.20 paf 103:
1.37 ! paf 104: void SAPI::send_body(Pool& pool, const char *buf, size_t size) {
1.19 paf 105: stdout_write(buf, size);
1.17 paf 106: }
1.37 ! paf 107: //@}
1.30 paf 108:
1.19 paf 109: // main
110:
1.5 paf 111: int main(int argc, char *argv[]) {
1.32 paf 112: umask(2);
113:
114: #ifdef WIN32
1.27 paf 115: setmode(fileno(stdin), _O_BINARY);
116: setmode(fileno(stdout), _O_BINARY);
117: setmode(fileno(stderr), _O_BINARY);
1.32 paf 118: #endif
1.12 paf 119:
1.3 paf 120: // were we started as CGI?
1.20 paf 121: cgi=
1.2 paf 122: getenv("SERVER_SOFTWARE") ||
123: getenv("SERVER_NAME") ||
124: getenv("GATEWAY_INTERFACE") ||
125: getenv("REQUEST_METHOD");
1.5 paf 126:
1.10 paf 127: if(!cgi) {
128: if(argc<2) {
129: char *binary=argv[0];
1.13 paf 130: printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10 paf 131: exit(1);
132: }
133: }
134:
1.26 paf 135: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36 paf 136: #ifdef WIN32
1.26 paf 137: fix_slashes(filespec_to_process);
1.36 paf 138: #endif
1.10 paf 139:
1.35 paf 140: const char *request_method=getenv("REQUEST_METHOD");
141: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.5 paf 142: PTRY { // global try
143: // must be first in PTRY{}PCATCH
1.36 paf 144: #if defined(WIN32) && _MSC_VER
1.5 paf 145: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
146: //TODO: initSocks();
147: #endif
1.2 paf 148:
1.10 paf 149: // init global variables
1.37 ! paf 150: pa_globals_init(pool);
1.10 paf 151:
1.13 paf 152: if(!filespec_to_process)
153: PTHROW(0, 0,
154: 0,
155: "no file to process");
156:
1.10 paf 157: // Request info
158: Request::Info request_info;
1.36 paf 159: if(cgi)
160: request_info.document_root=getenv("DOCUMENT_ROOT");
161: else {
162: static char document_root[MAX_STRING];
163: strncpy(document_root, filespec_to_process, MAX_STRING);
164: rsplit(document_root, '/'); rsplit(document_root, '\\');// strip filename
165: request_info.document_root=document_root;
1.13 paf 166: }
1.10 paf 167: request_info.path_translated=filespec_to_process;
1.35 paf 168: request_info.method=request_method;
1.10 paf 169: request_info.query_string=getenv("QUERY_STRING");
1.15 paf 170: request_info.uri=getenv("REQUEST_URI");
1.10 paf 171: request_info.content_type=getenv("CONTENT_TYPE");
172: const char *content_length=getenv("CONTENT_LENGTH");
173: request_info.content_length=(content_length?atoi(content_length):0);
1.21 paf 174: request_info.cookie=getenv("HTTP_COOKIE");
1.10 paf 175:
1.5 paf 176: // prepare to process request
1.27 paf 177: Pool request_pool;
178: Request request(request_pool,
1.10 paf 179: request_info,
1.27 paf 180: cgi ? String::UL_HTML_TYPO : String::UL_NO
1.5 paf 181: );
182:
183: // some root-controlled location
1.3 paf 184: #ifdef WIN32
1.10 paf 185: // c:\windows
1.36 paf 186: static char root_auto_path[MAX_STRING];
1.30 paf 187: GetWindowsDirectory(root_auto_path, MAX_STRING);
1.3 paf 188: #else
1.10 paf 189: // ~nobody
1.36 paf 190: char *root_auto_path=getenv("HOME");
1.5 paf 191: #endif
192:
193: // beside by binary
1.36 paf 194: static char site_auto_path[MAX_STRING];
1.30 paf 195: strncpy(site_auto_path, argv[0], MAX_STRING); // filespec of my binary
196: rsplit(site_auto_path, '/'); rsplit(site_auto_path, '\\');// strip filename
1.5 paf 197:
198: // process the request
1.35 paf 199: request.core(
1.30 paf 200: root_auto_path, false,
1.31 paf 201: site_auto_path, false,
1.35 paf 202: header_only);
1.16 paf 203:
1.22 paf 204: // must be last in PTRY{}PCATCH
1.36 paf 205: #if defined(WIN32) && _MSC_VER
1.5 paf 206: SetUnhandledExceptionFilter(0);
1.25 paf 207: #endif
1.16 paf 208: // successful finish
209: return 0;
210: } PCATCH(e) { // global problem
1.19 paf 211: const char *body=e.comment();
212: int content_length=strlen(body);
1.5 paf 213:
1.35 paf 214: // prepare header
1.37 ! paf 215: SAPI::add_header_attribute(pool, "content-type", "text/plain");
1.19 paf 216: char content_length_cstr[MAX_NUMBER];
1.34 paf 217: snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.37 ! paf 218: SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
1.35 paf 219:
220: // send header
1.37 ! paf 221: SAPI::send_header(pool);
1.19 paf 222:
223: // body
1.35 paf 224: if(!header_only)
1.37 ! paf 225: SAPI::send_body(pool, body, content_length);
1.2 paf 226:
1.16 paf 227: // unsuccessful finish
228: return 1;
229: }
230: PEND_CATCH
1.1 paf 231: }
E-mail: