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

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

E-mail: