Annotation of parser3/src/targets/cgi/parser3.C, revision 1.22
1.1 paf 1: /*
2: Parser
3: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
4: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
5:
1.22 ! paf 6: $Id: parser3.C,v 1.21 2001/03/18 20:31:29 paf Exp $
1.1 paf 7: */
8:
1.5 paf 9: #ifdef HAVE_CONFIG_H
10: # include "pa_config.h"
11: #endif
12:
1.3 paf 13:
14: #ifdef WIN32
15: # include <windows.h>
16: # include <io.h>
17: #endif
18: #include <stdlib.h>
1.4 paf 19: #include <stdio.h>
20: #include <string.h>
21: #include <fcntl.h>
1.3 paf 22:
1.5 paf 23: #include "pa_globals.h"
1.2 paf 24: #include "pa_request.h"
25: #include "pa_common.h"
1.1 paf 26:
1.5 paf 27: Pool pool; // global pool
1.20 paf 28: bool cgi;
1.5 paf 29:
1.3 paf 30: #ifdef WIN32
1.14 paf 31: # if _MSC_VER
1.5 paf 32: // intercept global system errors
33: LONG WINAPI TopLevelExceptionFilter (
34: struct _EXCEPTION_POINTERS *ExceptionInfo
35: ) {
36: char buf[MAX_STRING];
37: if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
38: struct _EXCEPTION_RECORD *r=ExceptionInfo->ExceptionRecord;
39:
40: int printed=0;
41: printed+=snprintf(buf+printed, MAX_STRING-printed, "Exception 0x%X at 0x%p",
42: r->ExceptionCode,
43: r->ExceptionAddress);
44: for(unsigned int i=0; i<r->NumberParameters; i++)
45: printed+=snprintf(buf+printed, MAX_STRING-printed, ", 0x%X",
46: r->ExceptionInformation[i]);
47: } else
48: strcpy(buf, "Exception <unknown>");
49:
50: PTHROW(0, 0,
51: 0,
52: buf);
53:
54: return EXCEPTION_EXECUTE_HANDLER; // never reached
55: }
1.8 paf 56: # endif
1.3 paf 57: #endif
58:
1.19 paf 59: // service funcs
60:
1.12 paf 61: int read_post(char *buf, int max_bytes) {
62: int read_size=0;
63: do {
64: int chunk_size=read
65: (fileno(stdin), buf+read_size, min(0x400*0x400, max_bytes-read_size));
66: if(chunk_size<0)
67: break;
68: read_size+=chunk_size;
69: } while(read_size<max_bytes);
70:
71: return read_size;
1.10 paf 72: }
73:
1.19 paf 74: void output_header_attribute(const char *key, const char *value) {
1.20 paf 75: if(cgi)
76: printf("%s: %s\n", key, value);
1.19 paf 77: }
78:
79: void output_body(const char *buf, size_t size) {
1.20 paf 80: if(cgi) // header | body delimiter
81: puts("");
82:
1.19 paf 83: stdout_write(buf, size);
1.17 paf 84: }
85:
1.19 paf 86: // main
87:
1.5 paf 88: int main(int argc, char *argv[]) {
1.12 paf 89: //TODO: umask(2);
90: #ifdef WIN32
91: _setmode(fileno(stdin), _O_BINARY);
92: _setmode(fileno(stdout), _O_BINARY);
93: _setmode(fileno(stderr), _O_BINARY);
94: #endif
95:
1.10 paf 96: // Service funcs
97: service_funcs.read_post=read_post;
1.19 paf 98: service_funcs.output_header_attribute=output_header_attribute;
99: service_funcs.output_body=output_body;
1.10 paf 100:
1.3 paf 101: // were we started as CGI?
1.20 paf 102: cgi=
1.2 paf 103: getenv("SERVER_SOFTWARE") ||
104: getenv("SERVER_NAME") ||
105: getenv("GATEWAY_INTERFACE") ||
106: getenv("REQUEST_METHOD");
1.5 paf 107:
1.10 paf 108: if(!cgi) {
109: if(argc<2) {
110: char *binary=argv[0];
1.13 paf 111: printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10 paf 112: exit(1);
113: }
114: }
115:
116: const char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
117:
1.5 paf 118: PTRY { // global try
119: // must be first in PTRY{}PCATCH
120: #ifdef WIN32
1.14 paf 121: # if _MSC_VER
1.5 paf 122: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
123: //TODO: initSocks();
1.8 paf 124: # endif
1.5 paf 125: #endif
1.2 paf 126:
1.10 paf 127: // init global variables
1.9 paf 128: globals_init(pool);
1.10 paf 129:
1.13 paf 130: if(!filespec_to_process)
131: PTHROW(0, 0,
132: 0,
133: "no file to process");
134:
1.10 paf 135: // Request info
1.5 paf 136: // TODO: ifdef WIN32 flip \\ to /
1.10 paf 137: Request::Info request_info;
1.13 paf 138: const char *document_root=getenv("DOCUMENT_ROOT");
139: if(!document_root) {
140: static char fake_document_root[MAX_STRING];
141: strncpy(fake_document_root, filespec_to_process, MAX_STRING);
142: rsplit(fake_document_root, '/'); rsplit(fake_document_root, '\\');// strip filename
143: document_root=fake_document_root;
144: }
145: request_info.document_root=document_root;
1.10 paf 146: request_info.path_translated=filespec_to_process;
1.15 paf 147: request_info.method=getenv("REQUEST_METHOD");
1.10 paf 148: request_info.query_string=getenv("QUERY_STRING");
1.15 paf 149: request_info.uri=getenv("REQUEST_URI");
1.10 paf 150: request_info.content_type=getenv("CONTENT_TYPE");
151: const char *content_length=getenv("CONTENT_LENGTH");
152: request_info.content_length=(content_length?atoi(content_length):0);
1.21 paf 153: request_info.cookie=getenv("HTTP_COOKIE");
1.10 paf 154:
1.5 paf 155: // prepare to process request
1.6 paf 156: Request request(Pool(),
1.10 paf 157: request_info,
1.20 paf 158: cgi ? String::Untaint_lang::HTML_TYPO : String::Untaint_lang::NO
1.5 paf 159: );
160:
161: // some root-controlled location
162: char *sys_auto_path1;
1.3 paf 163: #ifdef WIN32
1.10 paf 164: // c:\windows
1.5 paf 165: sys_auto_path1=(char *)pool.malloc(MAX_STRING);
1.7 paf 166: GetWindowsDirectory(sys_auto_path1, MAX_STRING);
1.10 paf 167: strcat(sys_auto_path1, PATH_DELIMITER_STRING);
1.3 paf 168: #else
1.10 paf 169: // ~nobody
1.5 paf 170: sys_auto_path1=getenv("HOME");
171: #endif
172:
173: // beside by binary
174: char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
1.6 paf 175: strncpy(sys_auto_path2, argv[0], MAX_STRING); // filespec of my binary
1.13 paf 176: rsplit(sys_auto_path2, '/'); rsplit(sys_auto_path2, '\\');// strip filename
177: strcat(sys_auto_path2, PATH_DELIMITER_STRING);
1.5 paf 178:
179: // process the request
1.16 paf 180: request.core(pool.exception(),
1.5 paf 181: sys_auto_path1,
182: sys_auto_path2);
1.19 paf 183: // no actions with request' data past this point
184: // request.exception not not handled here, but all
185: // request' data are associated with it's pool=exception
1.16 paf 186:
1.22 ! paf 187: // must be last in PTRY{}PCATCH
1.5 paf 188: #ifdef WIN32
1.14 paf 189: # if _MSC_VER
1.5 paf 190: SetUnhandledExceptionFilter(0);
1.8 paf 191: # endif
1.16 paf 192: // successful finish
193: return 0;
1.3 paf 194: #endif
1.16 paf 195: } PCATCH(e) { // global problem
1.19 paf 196: const char *body=e.comment();
197: int content_length=strlen(body);
1.5 paf 198:
1.19 paf 199: // header
200: (*service_funcs.output_header_attribute)("content-type", "text/plain");
201: char content_length_cstr[MAX_NUMBER];
202: snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length);
203: (*service_funcs.output_header_attribute)("content-length",
204: content_length_cstr);
205:
206: // body
207: (*service_funcs.output_body)(body, content_length);
1.2 paf 208:
1.16 paf 209: // unsuccessful finish
210: return 1;
211: }
212: PEND_CATCH
1.1 paf 213: }
E-mail: