Annotation of parser3/src/targets/cgi/parser3.C, revision 1.31
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.31 ! paf 8: $Id: parser3.C,v 1.30 2001/03/22 11:19:15 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.30 paf 98: static void send_header(const char *buf, size_t size) {
1.20 paf 99: if(cgi) // header | body delimiter
100: puts("");
1.30 paf 101: }
1.20 paf 102:
1.30 paf 103: static void send_body(const char *buf, size_t size) {
1.19 paf 104: stdout_write(buf, size);
1.17 paf 105: }
106:
1.30 paf 107: /// Service funcs
108: Service_funcs service_funcs={
109: get_env,
110: read_post,
111: add_header_attribute,
112: send_header,
113: send_body
114: };
115:
116:
1.19 paf 117: // main
118:
1.5 paf 119: int main(int argc, char *argv[]) {
1.23 paf 120: // TODO:umask(2);
1.27 paf 121: //\#ifdef WIN32
122: setmode(fileno(stdin), _O_BINARY);
123: setmode(fileno(stdout), _O_BINARY);
124: setmode(fileno(stderr), _O_BINARY);
125: //\#endif
1.12 paf 126:
1.3 paf 127: // were we started as CGI?
1.20 paf 128: cgi=
1.2 paf 129: getenv("SERVER_SOFTWARE") ||
130: getenv("SERVER_NAME") ||
131: getenv("GATEWAY_INTERFACE") ||
132: getenv("REQUEST_METHOD");
1.5 paf 133:
1.10 paf 134: if(!cgi) {
135: if(argc<2) {
136: char *binary=argv[0];
1.13 paf 137: printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10 paf 138: exit(1);
139: }
140: }
141:
1.26 paf 142: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.27 paf 143: //\#ifdef WIN32
1.26 paf 144: fix_slashes(filespec_to_process);
1.27 paf 145: //\#endif
1.10 paf 146:
1.5 paf 147: PTRY { // global try
148: // must be first in PTRY{}PCATCH
149: #ifdef WIN32
1.14 paf 150: # if _MSC_VER
1.5 paf 151: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
152: //TODO: initSocks();
1.8 paf 153: # endif
1.5 paf 154: #endif
1.2 paf 155:
1.10 paf 156: // init global variables
1.9 paf 157: globals_init(pool);
1.10 paf 158:
1.13 paf 159: if(!filespec_to_process)
160: PTHROW(0, 0,
161: 0,
162: "no file to process");
163:
1.10 paf 164: // Request info
165: Request::Info request_info;
1.13 paf 166: const char *document_root=getenv("DOCUMENT_ROOT");
167: if(!document_root) {
168: static char fake_document_root[MAX_STRING];
169: strncpy(fake_document_root, filespec_to_process, MAX_STRING);
170: rsplit(fake_document_root, '/'); rsplit(fake_document_root, '\\');// strip filename
171: document_root=fake_document_root;
172: }
173: request_info.document_root=document_root;
1.10 paf 174: request_info.path_translated=filespec_to_process;
1.15 paf 175: request_info.method=getenv("REQUEST_METHOD");
1.10 paf 176: request_info.query_string=getenv("QUERY_STRING");
1.15 paf 177: request_info.uri=getenv("REQUEST_URI");
1.10 paf 178: request_info.content_type=getenv("CONTENT_TYPE");
179: const char *content_length=getenv("CONTENT_LENGTH");
180: request_info.content_length=(content_length?atoi(content_length):0);
1.21 paf 181: request_info.cookie=getenv("HTTP_COOKIE");
1.10 paf 182:
1.5 paf 183: // prepare to process request
1.27 paf 184: Pool request_pool;
185: Request request(request_pool,
1.10 paf 186: request_info,
1.27 paf 187: cgi ? String::UL_HTML_TYPO : String::UL_NO
1.5 paf 188: );
189:
190: // some root-controlled location
1.30 paf 191: char *root_auto_path;
1.3 paf 192: #ifdef WIN32
1.10 paf 193: // c:\windows
1.30 paf 194: root_auto_path=(char *)pool.malloc(MAX_STRING);
195: GetWindowsDirectory(root_auto_path, MAX_STRING);
196: strcat(root_auto_path, "/");
1.3 paf 197: #else
1.10 paf 198: // ~nobody
1.30 paf 199: root_auto_path=getenv("HOME");
1.5 paf 200: #endif
201:
202: // beside by binary
1.30 paf 203: char *site_auto_path=(char *)pool.malloc(MAX_STRING);
204: strncpy(site_auto_path, argv[0], MAX_STRING); // filespec of my binary
205: rsplit(site_auto_path, '/'); rsplit(site_auto_path, '\\');// strip filename
206: strcat(site_auto_path, "/");
1.5 paf 207:
208: // process the request
1.16 paf 209: request.core(pool.exception(),
1.30 paf 210: root_auto_path, false,
1.31 ! paf 211: site_auto_path, false,
! 212: strcasecmp(request_info.method, "HEAD")==0);
1.16 paf 213:
1.22 paf 214: // must be last in PTRY{}PCATCH
1.5 paf 215: #ifdef WIN32
1.14 paf 216: # if _MSC_VER
1.5 paf 217: SetUnhandledExceptionFilter(0);
1.8 paf 218: # endif
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.19 paf 226: // header
227: (*service_funcs.output_header_attribute)("content-type", "text/plain");
228: char content_length_cstr[MAX_NUMBER];
229: snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length);
230: (*service_funcs.output_header_attribute)("content-length",
231: content_length_cstr);
232:
233: // body
234: (*service_funcs.output_body)(body, content_length);
1.2 paf 235:
1.16 paf 236: // unsuccessful finish
237: return 1;
238: }
239: PEND_CATCH
1.1 paf 240: }
E-mail: