Annotation of parser3/src/targets/cgi/pa_sapi_info.h, revision 1.13

1.1       moko        1: #ifndef PA_SAPI_INFO_H
                      2: #define PA_SAPI_INFO_H
                      3: 
1.13    ! moko        4: #define IDENT_PA_SAPI_INFO_H "$Id: pa_sapi_info.h,v 1.12 2020/12/15 17:23:56 moko Exp $"
1.1       moko        5: 
                      6: #include "pa_sapi.h"
                      7: #include "pa_http.h"
                      8: 
                      9: /// IIS refuses to read bigger chunks
                     10: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M
                     11: 
1.12      moko       12: // for signal handlers and cgi console detection
                     13: static Request *request=0;
                     14: 
                     15: 
1.1       moko       16: class SAPI_Info : public PA_Allocated {
                     17: public:
                     18:        int http_response_code;
                     19: 
1.7       moko       20:        SAPI_Info() : http_response_code(200) {}
1.1       moko       21: 
                     22:        virtual char* get_env(const char* name) {
                     23:                if(char *local=getenv(name))
                     24:                        return pa_strdup(local);
                     25:                else
                     26:                        return 0;
                     27:        }
                     28: 
1.10      moko       29:        virtual bool set_env(const char*, const char*) {
1.8       moko       30:                return false;
                     31:        }
                     32: 
1.1       moko       33:        virtual const char* const *get_env() {
                     34: #ifdef _MSC_VER
                     35:                extern char **_environ;
                     36:                return _environ;
                     37: #else
                     38:                extern char **environ;
                     39:                return environ;
                     40: #endif
                     41:        }
                     42: 
                     43:        virtual size_t read_post(char *, size_t) {
                     44:                return 0;
                     45:        }
                     46: 
                     47:        virtual void add_header_attribute(const char* dont_store_key, const char* dont_store_value) {
                     48:                if(strcasecmp(dont_store_key, HTTP_STATUS)==0)
                     49:                        http_response_code=atoi(dont_store_value);
                     50:        }
                     51: 
                     52:        virtual void send_header() {}
                     53: 
                     54:        virtual size_t send_body(const void *buf, size_t size) {
                     55:                return stdout_write(buf, size);
                     56:        }
                     57: 
                     58: } *sapiInfo = NULL;
                     59: 
                     60: class SAPI_Info_CGI : public SAPI_Info {
                     61: public:
                     62: 
                     63:        virtual size_t read_post(char *buf, size_t max_bytes) {
                     64:                size_t read_size=0;
                     65:                do {
                     66:                        ssize_t chunk_size=read(fileno(stdin), buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
                     67:                        if(chunk_size<=0)
                     68:                        break;
                     69:                        read_size+=chunk_size;
                     70:                } while(read_size<max_bytes);
                     71:                return read_size;
                     72:        }
                     73: 
                     74:        virtual void add_header_attribute(const char* dont_store_key, const char* dont_store_value) {
                     75:                SAPI_Info::add_header_attribute(dont_store_key, dont_store_value);
1.12      moko       76:                if(!request || !request->console.was_used())
1.1       moko       77:                        printf("%s: %s\n", capitalize(dont_store_key), dont_store_value);
                     78:        }
                     79: 
                     80:        virtual void send_header() {
                     81:                puts("");
                     82:        }
                     83: 
                     84: 
                     85: };
                     86: 
1.3       moko       87: char* replace_char(char* str, char from, char to){
                     88:     for(char *pos = strchr(str,from); pos; pos=strchr(pos,from)) {
                     89:         *pos = to;
                     90:     }
                     91:     return str;
                     92: }
                     93: 
1.1       moko       94: class SAPI_Info_HTTPD : public SAPI_Info {
                     95: public:
                     96: 
                     97:        HTTPD_Connection &connection;
                     98:        String output;
1.3       moko       99:        HashStringString env;
1.1       moko      100: 
                    101:        SAPI_Info_HTTPD(HTTPD_Connection &aconnection) : connection(aconnection) {}
                    102: 
1.3       moko      103:        void populate_env() {
                    104:                String::Body host("localhost");
1.2       moko      105:                for(Array_iterator<HTTP_Headers::Header> i(connection.headers()); i.has_next(); ){
                    106:                        HTTP_Headers::Header header=i.next();
1.3       moko      107:                        String name("HTTP_");
                    108:                        name << replace_char(header.name.cstrm(), '-', '_');
                    109:                        String::Body value=header.value;
                    110: 
                    111:                        if(header.name == "HOST"){
                    112:                                size_t port=value.pos(':');
                    113:                                if(port != STRING_NOT_FOUND)
                    114:                                        value=value.mid(0, port);
                    115:                                host=value;
                    116:                        }
                    117:                        env.put(name, value);
1.1       moko      118:                }
1.3       moko      119: 
                    120:                env.put("REQUEST_METHOD", connection.method());
                    121:                env.put("REQUEST_URI", connection.uri());
                    122:                env.put("QUERY_STRING", connection.query());
                    123: 
                    124:                env.put("SERVER_NAME", host);
                    125:                env.put("REMOTE_ADDR", connection.remote_addr);
                    126: 
1.1       moko      127:        }
                    128: 
1.3       moko      129:        virtual char* get_env(const char* name) {
                    130:                String::Body value = env.get(name);
                    131:                return !value ? NULL : value.cstrm();
1.1       moko      132:        }
                    133: 
1.8       moko      134:        virtual bool set_env(const char* name, const char* value) {
                    135:                env.put(name, *new String(value));
                    136:                return true;
                    137:        }
                    138: 
1.1       moko      139:        virtual const char* const *get_env() {
1.3       moko      140:                const char** result=new(PointerGC) const char*[env.count()+1/*0*/];
1.1       moko      141:                const char** cur=result;
1.3       moko      142:                for(HashStringString::Iterator i(env); i; i.next()){
                    143:                        String pair;
                    144:                        pair << i.key() << "=" << i.value();
                    145:                        *cur++=pair.cstr();
1.1       moko      146:                }
                    147:                *cur=NULL;
                    148:                return result;
                    149:        }
                    150: 
1.4       moko      151:        virtual size_t read_post(char *buf, size_t max_bytes) {
                    152:                return connection.read_post(buf, max_bytes);
1.1       moko      153:        }
                    154: 
                    155:        virtual void add_header_attribute(const char* dont_store_key, const char* dont_store_value) {
                    156:                if(strcasecmp(dont_store_key, "location")==0)
                    157:                        http_response_code=302;
                    158:                if(strcasecmp(dont_store_key, HTTP_STATUS)==0)
                    159:                        http_response_code=atoi(dont_store_value);
                    160:                else
                    161:                        output << capitalize(dont_store_key) << ": " << pa_strdup(dont_store_value) << "\r\n";
                    162:        }
                    163: 
1.6       moko      164:        static const char *exception_http_status(const char *type) {
                    165:                struct Lookup {
                    166:                        const char *code;
                    167:                        const char *type;
                    168:                } static lookup[] = {
1.13    ! moko      169:                        {  "",  "httpd.write"},
1.6       moko      170:                        {"400", "httpd.request"},
1.9       moko      171:                        {"400", "http.response"},
1.6       moko      172:                        {"404", "file.missing"},
                    173:                        {"408", "httpd.timeout"},
1.13    ! moko      174:                        {"408", "httpd.read"},
1.6       moko      175:                        {"501", "httpd.method"},
                    176:                        { NULL, ""}
                    177:                };
                    178:                Lookup *cur = lookup;
                    179:                for(; cur->code; cur++)
                    180:                        if(!strcmp(type, cur->type))
                    181:                                return cur->code;
                    182:                return "500";
                    183:        }
                    184: 
                    185:        static const char *status_message(int code) {
1.1       moko      186:                struct Lookup {
                    187:                        int code;
                    188:                        const char *message;
                    189:                } static lookup[] = {
                    190:                        {200, "OK"},
1.11      moko      191:                        {204, "No Content"},
1.1       moko      192:                        {206, "Partial Content"},
                    193:                        {301, "Moved Permanently"},
                    194:                        {302, "Found"},
1.6       moko      195:                        {304, "Not Modified"},
1.1       moko      196:                        {400, "Bad Request"},
                    197:                        {401, "Unauthorized"},
                    198:                        {403, "Forbidden"},
                    199:                        {404, "Not Found"},
1.6       moko      200:                        {408, "Request Timeout"},
1.11      moko      201:                        {416, "Range Not Satisfiable"},
1.1       moko      202:                        {500, "Internal Server Error"},
                    203:                        {501, "Not Implemented"},
                    204:                        {502, "Bad Gateway"},
                    205:                        {504, "Gateway Timeout"},
                    206:                        {  0, "Undescribed"}
                    207:                };
                    208:                Lookup *cur = lookup;
                    209:                for(; cur->code; cur++)
                    210:                        if(code == cur->code)
                    211:                                return cur->message;
                    212:                return cur->message;
                    213:        }
                    214: 
                    215:        virtual void send_header() {
                    216:                String result("HTTP/1.0 ");
1.6       moko      217:                result << String::Body::Format(http_response_code) << " " << status_message(http_response_code) << "\r\n" << output << "\r\n";
1.1       moko      218:                send_body(result.cstr(), result.length());
                    219:        }
                    220: 
                    221:        virtual size_t send_body(const void *buf, size_t size) {
1.5       moko      222:                return connection.send_body(buf, size);
1.1       moko      223:        }
                    224: 
                    225: };
                    226: 
                    227: #endif

E-mail: