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