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