Annotation of parser3/src/targets/cgi/parser3.C, revision 1.39
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.39 ! paf 8: $Id: parser3.C,v 1.38 2001/03/23 10:32:53 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.39 ! paf 41: #if _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.39 ! paf 111: #include <stdlib.h>
1.5 paf 112: int main(int argc, char *argv[]) {
1.32 paf 113: umask(2);
114:
115: #ifdef WIN32
1.27 paf 116: setmode(fileno(stdin), _O_BINARY);
117: setmode(fileno(stdout), _O_BINARY);
118: setmode(fileno(stderr), _O_BINARY);
1.32 paf 119: #endif
1.12 paf 120:
1.3 paf 121: // were we started as CGI?
1.20 paf 122: cgi=
1.2 paf 123: getenv("SERVER_SOFTWARE") ||
124: getenv("SERVER_NAME") ||
125: getenv("GATEWAY_INTERFACE") ||
126: getenv("REQUEST_METHOD");
1.5 paf 127:
1.10 paf 128: if(!cgi) {
129: if(argc<2) {
130: char *binary=argv[0];
1.13 paf 131: printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10 paf 132: exit(1);
133: }
134: }
135:
1.26 paf 136: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36 paf 137: #ifdef WIN32
1.26 paf 138: fix_slashes(filespec_to_process);
1.36 paf 139: #endif
1.10 paf 140:
1.35 paf 141: const char *request_method=getenv("REQUEST_METHOD");
142: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.5 paf 143: PTRY { // global try
144: // must be first in PTRY{}PCATCH
1.39 ! paf 145: #if _MSC_VER
1.5 paf 146: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
147: //TODO: initSocks();
148: #endif
1.2 paf 149:
1.10 paf 150: // init global variables
1.37 paf 151: pa_globals_init(pool);
1.10 paf 152:
1.13 paf 153: if(!filespec_to_process)
154: PTHROW(0, 0,
155: 0,
156: "no file to process");
157:
1.10 paf 158: // Request info
159: Request::Info request_info;
1.39 ! paf 160: if(cgi) {
! 161: if(const char *env_document_root=getenv("DOCUMENT_ROOT"))
! 162: request_info.document_root=env_document_root;
! 163: else if(const char *path_info=getenv("PATH_INFO")) {
! 164: // under IIS for sure
! 165: static char buf[MAX_STRING];
! 166: size_t len=strlen(filespec_to_process)-strlen(path_info);
! 167: strncpy(buf, filespec_to_process, min(MAX_STRING-1, len));
! 168: buf[len]=0;
! 169: request_info.document_root=buf;
! 170: } else
! 171: PTHROW(0, 0,
! 172: 0,
! 173: "CGI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
! 174: } else {
! 175: static char buf[MAX_STRING];
! 176: strncpy(buf, filespec_to_process, MAX_STRING);
! 177: rsplit(buf, '/'); rsplit(buf, '\\');// strip filename
! 178: request_info.document_root=buf;
1.13 paf 179: }
1.10 paf 180: request_info.path_translated=filespec_to_process;
1.35 paf 181: request_info.method=request_method;
1.10 paf 182: request_info.query_string=getenv("QUERY_STRING");
1.15 paf 183: request_info.uri=getenv("REQUEST_URI");
1.10 paf 184: request_info.content_type=getenv("CONTENT_TYPE");
185: const char *content_length=getenv("CONTENT_LENGTH");
186: request_info.content_length=(content_length?atoi(content_length):0);
1.21 paf 187: request_info.cookie=getenv("HTTP_COOKIE");
1.10 paf 188:
1.5 paf 189: // prepare to process request
1.38 paf 190: Request request(pool,
1.10 paf 191: request_info,
1.27 paf 192: cgi ? String::UL_HTML_TYPO : String::UL_NO
1.5 paf 193: );
194:
195: // some root-controlled location
1.3 paf 196: #ifdef WIN32
1.10 paf 197: // c:\windows
1.36 paf 198: static char root_auto_path[MAX_STRING];
1.30 paf 199: GetWindowsDirectory(root_auto_path, MAX_STRING);
1.3 paf 200: #else
1.10 paf 201: // ~nobody
1.36 paf 202: char *root_auto_path=getenv("HOME");
1.5 paf 203: #endif
204:
205: // beside by binary
1.36 paf 206: static char site_auto_path[MAX_STRING];
1.30 paf 207: strncpy(site_auto_path, argv[0], MAX_STRING); // filespec of my binary
208: rsplit(site_auto_path, '/'); rsplit(site_auto_path, '\\');// strip filename
1.5 paf 209:
210: // process the request
1.35 paf 211: request.core(
1.30 paf 212: root_auto_path, false,
1.31 paf 213: site_auto_path, false,
1.35 paf 214: header_only);
1.16 paf 215:
1.22 paf 216: // must be last in PTRY{}PCATCH
1.39 ! paf 217: #if _MSC_VER
1.5 paf 218: SetUnhandledExceptionFilter(0);
1.25 paf 219: #endif
1.16 paf 220: // successful finish
221: return 0;
222: } PCATCH(e) { // global problem
1.19 paf 223: const char *body=e.comment();
224: int content_length=strlen(body);
1.5 paf 225:
1.35 paf 226: // prepare header
1.37 paf 227: SAPI::add_header_attribute(pool, "content-type", "text/plain");
1.19 paf 228: char content_length_cstr[MAX_NUMBER];
1.34 paf 229: snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.37 paf 230: SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
1.35 paf 231:
232: // send header
1.37 paf 233: SAPI::send_header(pool);
1.19 paf 234:
235: // body
1.35 paf 236: if(!header_only)
1.37 paf 237: SAPI::send_body(pool, body, content_length);
1.2 paf 238:
1.16 paf 239: // unsuccessful finish
240: return 1;
241: }
242: PEND_CATCH
1.1 paf 243: }
E-mail: