Annotation of parser3/src/targets/cgi/parser3.C, revision 1.55

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.43      paf         4:        Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.27      paf         5: 
1.43      paf         6:        Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1       paf         7: 
1.55    ! paf         8:        $Id: parser3.C,v 1.54 2001/04/03 07:02:44 paf Exp $
1.1       paf         9: */
                     10: 
1.40      paf        11: #include "pa_config_includes.h"
1.3       paf        12: 
                     13: #ifdef WIN32
                     14: #      include <windows.h>
                     15: #endif
1.27      paf        16: 
                     17: #include <io.h>
1.3       paf        18: #include <stdlib.h>
1.4       paf        19: #include <stdio.h>
                     20: #include <fcntl.h>
1.45      paf        21: #include <time.h>
1.3       paf        22: 
1.37      paf        23: #include "pa_sapi.h"
1.24      paf        24: #include "pa_common.h"
1.5       paf        25: #include "pa_globals.h"
1.2       paf        26: #include "pa_request.h"
1.1       paf        27: 
1.42      paf        28: /// IIS refuses to read bigger chunks
                     29: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M 
                     30: 
1.45      paf        31: const char *argv0;
1.42      paf        32: Pool pool(0); // global pool [dont describe to doxygen: it confuses it with param names]
1.27      paf        33: bool cgi; ///< we were started as CGI?
1.5       paf        34: 
1.40      paf        35: #ifdef WIN32
                     36: /// global system errors into parser exceptions converter
1.43      paf        37: static LONG WINAPI TopLevelExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo) {
1.5       paf        38:        char buf[MAX_STRING];
                     39:        if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
1.36      paf        40:                struct _EXCEPTION_RECORD *er=ExceptionInfo->ExceptionRecord;
1.46      paf        41:                snprintf(buf, MAX_STRING, "Exception 0x%X at %p", 
1.31      paf        42:                        er->ExceptionCode, 
                     43:                        er->ExceptionAddress);
1.5       paf        44:        } else 
                     45:                strcpy(buf, "Exception <unknown>");
                     46:        
                     47:        PTHROW(0, 0,
                     48:                0,
                     49:                buf);
                     50: 
                     51:        return EXCEPTION_EXECUTE_HANDLER; // never reached
                     52: }
1.27      paf        53: #endif
                     54: 
1.46      paf        55: // SAPI
1.37      paf        56: const char *SAPI::get_env(Pool& pool, const char *name) {
1.28      paf        57:        return getenv(name);
                     58: }
                     59: 
1.37      paf        60: uint SAPI::read_post(Pool& pool, char *buf, uint max_bytes) {
1.36      paf        61:        uint read_size=0;
1.12      paf        62:        do {
1.36      paf        63:                int chunk_size=read(fileno(stdin), 
1.42      paf        64:                        buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.12      paf        65:                if(chunk_size<0)
                     66:                        break;
                     67:                read_size+=chunk_size;
                     68:        } while(read_size<max_bytes);
                     69: 
                     70:        return read_size;
1.10      paf        71: }
                     72: 
1.37      paf        73: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.20      paf        74:        if(cgi)
                     75:                printf("%s: %s\n", key, value);
1.19      paf        76: }
                     77: 
1.48      paf        78: /// @todo parser4: intelligent cache-control
1.37      paf        79: void SAPI::send_header(Pool& pool) {
1.33      paf        80:        if(cgi) {
1.55    ! paf        81:                puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf        82: 
                     83:                // header | body  delimiter
1.20      paf        84:                puts("");
1.33      paf        85:        }
1.30      paf        86: }
1.20      paf        87: 
1.53      paf        88: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.19      paf        89:        stdout_write(buf, size);
1.17      paf        90: }
1.44      paf        91: 
1.46      paf        92: // appends to parser3.log located beside my binary
1.44      paf        93: void SAPI::log(Pool& pool, const char *fmt, ...) {
1.45      paf        94:        bool opened;
                     95:        FILE *f=0;
                     96: 
                     97:        if(argv0) {
                     98:                // beside by binary
                     99:                char file_spec[MAX_STRING];
                    100:                strncpy(file_spec, argv0, MAX_STRING);  // filespec of my binary
                    101:                rsplit(file_spec, '/');  rsplit(file_spec, '\\');// strip filename
                    102:                strcat(file_spec, "/parser3.log");
                    103:                f=fopen(file_spec, "at");
                    104:        }
                    105:        opened=f!=0;
                    106:        if(!opened)
                    107:                f=stderr;
                    108: 
                    109:        // prefix
                    110:        time_t t=time(0);
                    111:        const char *stamp=ctime(&t);
                    112:        fprintf(f, "[%.*s] ", strlen(stamp)-1, stamp);
                    113:        // message
                    114:     va_list args;
                    115:        va_start(args,fmt);
                    116:        vfprintf(f, fmt, args);
                    117:        va_end(args);
                    118:        // newline
                    119:        fprintf(f, "\n");
                    120: 
                    121:        if(opened)
1.44      paf       122:                fclose(f);
                    123: }
1.30      paf       124: 
1.40      paf       125: /**
                    126:        main workhorse
1.19      paf       127: 
1.48      paf       128:        @todo parser4:
1.40      paf       129:                IIS: remove trailing default-document[index.html] from $request.uri.
                    130:                to do that we need to consult metabase,
                    131:                wich is tested but seems slow.
                    132: */
1.5       paf       133: int main(int argc, char *argv[]) {
1.45      paf       134:        argv0=argv[0];
                    135: 
1.32      paf       136:        umask(2);
                    137: 
                    138: #ifdef WIN32
1.27      paf       139:        setmode(fileno(stdin), _O_BINARY);
                    140:        setmode(fileno(stdout), _O_BINARY);
                    141:        setmode(fileno(stderr), _O_BINARY);
1.32      paf       142: #endif
1.12      paf       143: 
1.3       paf       144:        // were we started as CGI?
1.20      paf       145:        cgi=
1.2       paf       146:                getenv("SERVER_SOFTWARE") || 
                    147:                getenv("SERVER_NAME") || 
                    148:                getenv("GATEWAY_INTERFACE") || 
                    149:                getenv("REQUEST_METHOD");
1.5       paf       150:        
1.10      paf       151:        if(!cgi) {
                    152:                if(argc<2) {
1.45      paf       153:                        printf("Usage: %s <file>\n", argv0?argv0:"parser3");
1.10      paf       154:                        exit(1);
                    155:                }
                    156:        }
                    157: 
1.26      paf       158:        char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36      paf       159: #ifdef WIN32
1.43      paf       160:        back_slashes_to_slashes(filespec_to_process);
1.36      paf       161: #endif
1.10      paf       162: 
1.35      paf       163:        const char *request_method=getenv("REQUEST_METHOD");
                    164:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.5       paf       165:        PTRY { // global try
                    166:                // must be first in PTRY{}PCATCH
1.40      paf       167: #ifdef WIN32
1.5       paf       168:                SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
                    169:                //TODO: initSocks();
                    170: #endif
1.2       paf       171: 
1.10      paf       172:                // init global variables
1.37      paf       173:                pa_globals_init(pool);
1.10      paf       174: 
1.13      paf       175:                if(!filespec_to_process)
                    176:                        PTHROW(0, 0,
                    177:                                0,
                    178:                                "no file to process");
                    179: 
1.10      paf       180:                // Request info
                    181:                Request::Info request_info;
1.39      paf       182:                if(cgi) {
1.51      paf       183:                        if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
1.39      paf       184:                                request_info.document_root=env_document_root;
1.51      paf       185:                        else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
1.40      paf       186:                                // IIS
1.39      paf       187:                                size_t len=strlen(filespec_to_process)-strlen(path_info);
1.40      paf       188:                                char *buf=(char *)pool.malloc(len+1);
                    189:                                strncpy(buf, filespec_to_process, len);
1.39      paf       190:                                buf[len]=0;
                    191:                                request_info.document_root=buf;
                    192:                        } else
                    193:                                PTHROW(0, 0,
                    194:                                        0,
1.43      paf       195:                                        "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.39      paf       196:                } else {
                    197:                        static char buf[MAX_STRING];
                    198:                        strncpy(buf, filespec_to_process, MAX_STRING);
                    199:                        rsplit(buf, '/');  rsplit(buf, '\\');// strip filename
                    200:                        request_info.document_root=buf;
1.13      paf       201:                }
1.10      paf       202:                request_info.path_translated=filespec_to_process;
1.35      paf       203:                request_info.method=request_method;
1.51      paf       204:                const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
1.40      paf       205:                request_info.query_string=query_string;
1.42      paf       206:                if(cgi) 
1.51      paf       207:                        if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
1.42      paf       208:                                request_info.uri=env_request_uri;
1.51      paf       209:                        else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
1.42      paf       210:                                if(query_string) {
                    211:                                        char *reconstructed_uri=(char *)malloc(
                    212:                                                strlen(path_info)+1/*'?'*/+
                    213:                                                strlen(query_string)+1/*0*/);
                    214:                                        strcpy(reconstructed_uri, path_info);
                    215:                                        strcat(reconstructed_uri, "?");
                    216:                                        strcat(reconstructed_uri, query_string);
                    217:                                        request_info.uri=reconstructed_uri;
                    218:                                } else
                    219:                                        request_info.uri=path_info;
                    220:                        else
                    221:                                PTHROW(0, 0,
                    222:                                        0,
1.43      paf       223:                                        "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
1.42      paf       224:                else
                    225:                        request_info.uri=0;
1.40      paf       226: 
1.51      paf       227:                request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
                    228:                const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
1.10      paf       229:                request_info.content_length=(content_length?atoi(content_length):0);
1.51      paf       230:                request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
                    231:                request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
1.10      paf       232: 
1.5       paf       233:                // prepare to process request
1.38      paf       234:                Request request(pool,
1.10      paf       235:                        request_info,
1.54      paf       236:                        1||cgi ? String::UL_HTML_TYPO : String::UL_CLEAN
1.5       paf       237:                        );
                    238:                
                    239:                // some root-controlled location
1.3       paf       240: #ifdef WIN32
1.10      paf       241:                // c:\windows
1.36      paf       242:                static char root_auto_path[MAX_STRING];
1.30      paf       243:                GetWindowsDirectory(root_auto_path, MAX_STRING);
1.3       paf       244: #else
1.40      paf       245:                // ~nobody  todo: figure out a better place
1.51      paf       246:                char *root_auto_path=SAPI::get_env(pool, "HOME");
1.5       paf       247: #endif
                    248:                
                    249:                // beside by binary
1.36      paf       250:                static char site_auto_path[MAX_STRING];
1.30      paf       251:                strncpy(site_auto_path, argv[0], MAX_STRING);  // filespec of my binary
                    252:                rsplit(site_auto_path, '/');  rsplit(site_auto_path, '\\');// strip filename
1.5       paf       253:                
                    254:                // process the request
1.35      paf       255:                request.core(
1.30      paf       256:                        root_auto_path, false,
1.31      paf       257:                        site_auto_path, false,
1.35      paf       258:                        header_only);
1.16      paf       259: 
1.22      paf       260:                // must be last in PTRY{}PCATCH
1.40      paf       261: #ifdef WIN32
1.5       paf       262:                SetUnhandledExceptionFilter(0);
1.25      paf       263: #endif
1.16      paf       264:                // successful finish
                    265:                return 0;
                    266:        } PCATCH(e) { // global problem 
1.43      paf       267:                // must be first in PCATCH{}
                    268: #ifdef WIN32
                    269:                SetUnhandledExceptionFilter(0);
                    270: #endif
1.44      paf       271:                // don't allocate anything on pool here:
                    272:                //   possible pool' exception not catch-ed now
                    273:                //   and there could be out-of-memory exception
1.43      paf       274: 
1.19      paf       275:                const char *body=e.comment();
1.44      paf       276:                // log it
                    277:                SAPI::log(pool, "exception in request exception handler: %s", body);
                    278: 
                    279:                //
1.19      paf       280:                int content_length=strlen(body);
1.5       paf       281: 
1.35      paf       282:                // prepare header
1.37      paf       283:                SAPI::add_header_attribute(pool, "content-type", "text/plain");
1.19      paf       284:                char content_length_cstr[MAX_NUMBER];
1.34      paf       285:                snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.37      paf       286:                SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
1.35      paf       287: 
                    288:                // send header
1.37      paf       289:                SAPI::send_header(pool);
1.19      paf       290: 
                    291:                // body
1.35      paf       292:                if(!header_only)
1.37      paf       293:                        SAPI::send_body(pool, body, content_length);
1.2       paf       294: 
1.16      paf       295:                // unsuccessful finish
                    296:                return 1;
                    297:        }
                    298:        PEND_CATCH
1.1       paf       299: }

E-mail: