Annotation of parser3/src/main/pa_http.C, revision 1.135

1.1       paf         1: /** @file
                      2:        Parser: http support functions.
                      3: 
1.135   ! moko        4:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.121     moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       paf         6:  */
                      7: 
                      8: #include "pa_http.h"
                      9: #include "pa_common.h"
1.81      moko       10: #include "pa_base64.h"
1.1       paf        11: #include "pa_charsets.h"
                     12: #include "pa_request_charsets.h"
1.22      misha      13: #include "pa_request.h"
                     14: #include "pa_vfile.h"
                     15: #include "pa_random.h"
1.1       paf        16: 
1.135   ! moko       17: volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.134 2025/05/31 13:23:22 moko Exp $" IDENT_PA_HTTP_H; 
1.53      moko       18: 
1.1       paf        19: // defines
                     20: 
1.19      misha      21: #define HTTP_METHOD_NAME       "method"
                     22: #define HTTP_FORM_NAME "form"
                     23: #define HTTP_BODY_NAME "body"
                     24: #define HTTP_TIMEOUT_NAME      "timeout"
                     25: #define HTTP_HEADERS_NAME      "headers"
1.22      misha      26: #define HTTP_FORM_ENCTYPE_NAME "enctype"
1.19      misha      27: #define HTTP_ANY_STATUS_NAME   "any-status"
1.59      moko       28: #define HTTP_OMIT_POST_CHARSET_NAME    "omit-post-charset"     // ^file::load[...;http://...;$.method[post]] by default adds charset to content-type
1.12      misha      29: 
1.1       paf        30: #define HTTP_USER "user"
                     31: #define HTTP_PASSWORD "password"
                     32: 
1.70      moko       33: #define HTTP_USER_AGENT "user-agent"
1.1       paf        34: #define DEFAULT_USER_AGENT "parser3"
                     35: 
1.59      moko       36: #ifndef INADDR_NONE
                     37: #define INADDR_NONE ((ulong) -1)
                     38: #endif
1.1       paf        39: 
                     40: #undef CRLF
                     41: #define CRLF "\r\n"
                     42: 
1.54      misha      43: // helpers
1.56      misha      44: 
1.130     moko       45: #ifdef HTTPD_DEBUG
                     46: void pa_log(const char* fmt, ...);
                     47: #define LOG(action) action
                     48: #else
                     49: #define LOG(action)
                     50: #endif
                     51: 
                     52: ssize_t pa_send(int sock, const char *buffer, size_t len){
                     53:        size_t total_sent = 0;
                     54:        while (total_sent < len) {
                     55:                ssize_t bytes_sent=send(sock, buffer + total_sent, len - total_sent, 0);
                     56:                if (bytes_sent < 0) {
                     57:                        return bytes_sent;
                     58:                } else if (bytes_sent == 0) {
                     59:                        // Connection closed by the remote peer?
                     60:                        break;
                     61:                }
                     62:                LOG(
                     63:                        if(bytes_sent != len - total_sent)
                     64:                                pa_log("httpd [%d] partial send %d of (%d)", sock, bytes_sent, len - total_sent)
                     65:                );
                     66:                total_sent += bytes_sent;
                     67:        }
                     68:        return total_sent;
                     69: }
                     70: 
1.85      moko       71: bool HTTP_Headers::add_header(const char *line){
1.78      moko       72:        const char *value=strchr(line, ':');
                     73: 
                     74:        if(value && value != line){ // we need only headers, not the response code
1.134     moko       75:                Header header = Header(String::Body(str_upper(line, value-line)), String::Body(value+1).trim(String::TRIM_BOTH, " \t\n\r"));
1.78      moko       76: 
                     77:                if(header.name == String::Body(HTTP_CONTENT_TYPE_UPPER) && content_type.is_empty())
                     78:                        content_type=header.value;
                     79: 
                     80:                if(header.name == String::Body("CONTENT-LENGTH") && content_length==0)
1.95      moko       81:                        ALTER_EXCEPTION_COMMENT(content_length=pa_atoul(header.value.cstr()), " for content-length");
1.78      moko       82: 
                     83:                headers+=header;
                     84: 
                     85:                return true;
                     86:        }
                     87:        return false;
                     88: }
                     89: 
1.54      misha      90: class Cookies_table_template_columns: public ArrayString {
                     91: public:
                     92:        Cookies_table_template_columns() {
                     93:                *this+=new String("name");
                     94:                *this+=new String("value");
                     95:                *this+=new String("expires");
                     96:                *this+=new String("max-age");
                     97:                *this+=new String("domain");
                     98:                *this+=new String("path");
                     99:                *this+=new String("httponly");
                    100:                *this+=new String("secure");
                    101:        }
                    102: };
                    103: 
                    104: 
1.1       paf       105: static bool set_addr(struct sockaddr_in *addr, const char* host, const short port){
1.22      misha     106:        memset(addr, 0, sizeof(*addr)); 
                    107:        addr->sin_family=AF_INET;
                    108:        addr->sin_port=htons(port); 
                    109:        if(host) {
1.65      moko      110:                struct hostent *hostIP=gethostbyname(host);
                    111:                if(hostIP && hostIP->h_addrtype == AF_INET){
                    112:                        memcpy(&addr->sin_addr, hostIP->h_addr, hostIP->h_length);
                    113:                        return true;
                    114:                }
                    115:        }
                    116:        return false;
1.1       paf       117: }
                    118: 
1.84      moko      119: class HTTP_response : public PA_Allocated {
1.78      moko      120: public:
                    121:        char *buf;
                    122:        size_t length;
                    123:        size_t buf_size;
                    124:        size_t body_offset;
                    125: 
1.85      moko      126:        HTTP_Headers headers;
1.78      moko      127: 
1.97      moko      128:        HTTP_response() : buf(NULL), length(0), buf_size(0), body_offset(0){}
1.78      moko      129: 
                    130:        void resize(size_t size){
                    131:                buf_size=size;
                    132:                buf=(char *)pa_realloc(buf, size + 1);
                    133:        }
                    134: 
1.120     moko      135:        bool read(SOCKET sock, size_t size){
1.103     moko      136:                if(length + size > buf_size)
                    137:                        resize(buf_size * 2 + size);
1.78      moko      138:                ssize_t received_size=recv(sock, buf + length, size, 0);
1.103     moko      139:                if(received_size == 0)
1.78      moko      140:                        return false;
1.103     moko      141:                if(received_size < 0) {
                    142:                        if(int no = pa_socks_errno())
1.102     moko      143:                                throw Exception("http.timeout", 0, "error receiving response: %s (%d)", pa_socks_strerr(no), no);
1.78      moko      144:                        return false;
                    145:                }
                    146:                length+=received_size;
                    147:                buf[length]='\0';
                    148:                return true;
                    149:        }
                    150: 
1.83      moko      151:        size_t first_line(){
1.89      moko      152:                char *header=strchr(buf, '\n');
                    153:                if(!header)
1.78      moko      154:                        return false;
                    155: 
1.89      moko      156:                return header-buf;
1.78      moko      157:        }
                    158: 
                    159:        const char *status_code(char *status_line, int &result){
                    160:                char* status_start = strchr(status_line, ' ');
                    161: 
                    162:                if(!(status_start++))
                    163:                        return status_line;
                    164: 
                    165:                char* status_end=strchr(status_start, ' ');
                    166: 
                    167:                if(!status_end)
                    168:                        return status_line;
                    169: 
                    170:                if(status_end==status_start)
                    171:                        return status_line;
1.1       paf       172: 
1.78      moko      173:                const char *result_str=pa_strdup(status_start, status_end-status_start);
1.95      moko      174:                ALTER_EXCEPTION_COMMENT(result=pa_atoui(result_str), " for HTTP status");
1.78      moko      175:                return result_str;
                    176:        }
1.2       paf       177: 
1.78      moko      178:        bool body_start(){
                    179:                char *p=buf;
                    180:                while((p=strchr(p, '\n'))) {
                    181:                        if(p[1]=='\r' && p[2]=='\n'){  // \r\n\r\n
                    182:                                *p='\0';
                    183:                                body_offset=p-buf+3;
                    184:                                return true;
                    185:                        }
                    186:                        if(p[1]=='\n') { // \n\n
                    187:                                *p='\0';
                    188:                                body_offset=p-buf+2;
                    189:                                return true;
                    190:                        }
                    191:                        p++;
                    192:                }
                    193:                return false;
1.2       paf       194:        }
1.78      moko      195: 
                    196:        void parse_headers(){
                    197:                const String header_block(buf, String::L_TAINTED);
                    198:                
                    199:                ArrayString aheaders;
                    200:                header_block.split(aheaders, 0, "\n");
                    201: 
1.124     moko      202:                ArrayString::Iterator i(aheaders);
1.78      moko      203:                i.next(); // skipping status
1.125     moko      204:                for(;i;){
1.78      moko      205:                        const char *line=i.next()->cstr();
                    206:                        if(!headers.add_header(line))
1.97      moko      207:                                throw Exception("http.response", 0, "bad response from host - bad header \"%s\"", line);
1.78      moko      208:                }
1.1       paf       209:        }
                    210: 
1.120     moko      211:        int read_response(SOCKET sock, bool fail_on_status_ne_200);
1.78      moko      212: };
                    213: 
                    214: enum HTTP_response_state {
                    215:        HTTP_STATUS_CODE,
                    216:        HTTP_HEADERS,
                    217:        HTTP_BODY
                    218: };
                    219: 
1.120     moko      220: int HTTP_response::read_response(SOCKET sock, bool fail_on_status_ne_200) {
1.78      moko      221:        HTTP_response_state state=HTTP_STATUS_CODE;
                    222:        int result=0;
                    223: 
                    224:        size_t chunk_size=0x400*16;
1.88      moko      225:        resize(2*chunk_size);
1.78      moko      226: 
1.88      moko      227:        while(read(sock, chunk_size)){
1.78      moko      228:                switch(state){
                    229:                        case HTTP_STATUS_CODE: {
1.88      moko      230:                                size_t status_size=first_line();
1.78      moko      231:                                if(!status_size)
                    232:                                        break;
                    233: 
1.88      moko      234:                                const char *status=status_code(pa_strdup(buf, status_size), result);
1.78      moko      235: 
                    236:                                if(!result || fail_on_status_ne_200 && result!=200)
                    237:                                        throw Exception("http.status", status ? new String(status) : &String::Empty, "invalid HTTP response status");
                    238: 
                    239:                                state=HTTP_HEADERS;
                    240:                        }
                    241: 
                    242:                        case HTTP_HEADERS: {
1.88      moko      243:                                if(!body_start())
1.78      moko      244:                                        break;
                    245: 
1.88      moko      246:                                parse_headers();
1.78      moko      247: 
1.97      moko      248:                                size_t content_length=check_file_size(headers.content_length, 0);
1.88      moko      249:                                if(content_length>0 && (content_length + body_offset) > length){
                    250:                                        resize(content_length + body_offset + 0x400*64);
1.78      moko      251:                                }
                    252: 
                    253:                                state=HTTP_BODY;
1.1       paf       254:                                break;
                    255:                        }
1.78      moko      256: 
                    257:                        case HTTP_BODY: {
                    258:                                chunk_size=0x400*64;
1.1       paf       259:                                break;
                    260:                        }
                    261:                }
                    262:        }
1.78      moko      263: 
                    264:        if(state==HTTP_STATUS_CODE)
1.97      moko      265:                throw Exception("http.response", 0, "bad response from host - no status found (size=%u)", length);
1.78      moko      266: 
                    267:        if(state==HTTP_HEADERS){
1.88      moko      268:                parse_headers();
                    269:                body_offset=length;
1.1       paf       270:        }
1.78      moko      271: 
                    272:        return result;
1.1       paf       273: }
                    274: 
                    275: /* ********************** request *************************** */
                    276: 
                    277: #if defined(SIGALRM) && defined(HAVE_SIGSETJMP) && defined(HAVE_SIGLONGJMP)
                    278: #      define PA_USE_ALARM
                    279: #endif
                    280: 
                    281: #ifdef PA_USE_ALARM
                    282: static sigjmp_buf timeout_env;
                    283: static void timeout_handler(int /*sig*/){
1.101     moko      284:        siglongjmp(timeout_env, 1);
1.1       paf       285: }
1.118     moko      286: 
                    287: #define PA_NO_THREADS (HTTPD_Server::mode != HTTPD_Server::MULTITHREADED)
                    288: 
                    289: #define ALARM(value) if(PA_NO_THREADS) alarm(value)
1.101     moko      290: #else
                    291: #define ALARM(value)
1.1       paf       292: #endif
                    293: 
1.78      moko      294: static int http_request(HTTP_response& response, const char* host, short port, const char* request, size_t request_size, int timeout_secs, bool fail_on_status_ne_200) {
1.1       paf       295:        if(!host)
1.73      moko      296:                throw Exception("http.host", 0, "zero hostname");  //never
1.1       paf       297: 
1.120     moko      298:        volatile SOCKET sock=INVALID_SOCKET; // to prevent makeing it register variable, because it will be clobbered by longjmp [thanks gcc warning]
1.101     moko      299:                
1.1       paf       300: #ifdef PA_USE_ALARM
1.118     moko      301:        if(PA_NO_THREADS) signal(SIGALRM, timeout_handler);
                    302:        if(PA_NO_THREADS && sigsetjmp(timeout_env, 1)) {
1.101     moko      303:                // duplicating closesocket to make code more simple for old compilers
1.120     moko      304:                if(sock != INVALID_SOCKET)
1.101     moko      305:                        closesocket(sock);
                    306:                throw Exception("http.timeout", 0, "timeout occurred while retrieving document");
1.1       paf       307:                return 0; // never
1.101     moko      308:        } else
1.1       paf       309: #endif
1.101     moko      310:        {
                    311:                ALARM(timeout_secs);
1.1       paf       312:                try {
                    313:                        int result;
                    314:                        struct sockaddr_in dest;
                    315:                
                    316:                        if(!set_addr(&dest, host, port))
1.127     moko      317:                                throw Exception("http.host", 0, "cannot resolve hostname \"%s\"", host); 
1.1       paf       318:                        
1.120     moko      319:                        if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/)) == INVALID_SOCKET) {
1.1       paf       320:                                int no=pa_socks_errno();
1.127     moko      321:                                throw Exception("http.connect", 0, "cannot make socket: %s (%d)", pa_socks_strerr(no), no); 
1.1       paf       322:                        }
                    323: 
                    324:                        // To enable SO_DONTLINGER (that is, disable SO_LINGER) 
                    325:                        // l_onoff should be set to zero and setsockopt should be called
                    326:                        linger dont_linger={0,0};
                    327:                        setsockopt(sock, SOL_SOCKET, SO_LINGER, (const char *)&dont_linger, sizeof(dont_linger));
                    328: 
                    329: #ifdef WIN32
                    330: // SO_*TIMEO can be defined in .h but not implemlemented in protocol,
                    331: // failing subsequently with Option not supported by protocol (99) message
                    332: // could not suppress that, so leaving this only for win32
                    333:                        int timeout_ms=timeout_secs*1000;
                    334:                        setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout_ms, sizeof(timeout_ms));
                    335:                        setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout_ms, sizeof(timeout_ms));
                    336: #endif
                    337: 
                    338:                        if(connect(sock, (struct sockaddr *)&dest, sizeof(dest))) {
                    339:                                int no=pa_socks_errno();
1.127     moko      340:                                throw Exception("http.connect", 0, "cannot connect to host \"%s\": %s (%d)", host, pa_socks_strerr(no), no);
1.1       paf       341:                        }
1.22      misha     342: 
1.130     moko      343:                        if(pa_send(sock, request, request_size) < 0) {
1.1       paf       344:                                int no=pa_socks_errno();
1.78      moko      345:                                throw Exception("http.timeout", 0, "error sending request: %s (%d)", pa_socks_strerr(no), no);
1.1       paf       346:                        }
                    347: 
1.88      moko      348:                        result=response.read_response(sock, fail_on_status_ne_200);
1.78      moko      349:                        closesocket(sock);
1.101     moko      350:                        ALARM(0);
1.1       paf       351:                        return result;
                    352:                } catch(...) {
1.101     moko      353:                        ALARM(0);
1.120     moko      354:                        if(sock != INVALID_SOCKET)
1.78      moko      355:                                closesocket(sock);
1.1       paf       356:                        rethrow;
                    357:                }
                    358:        }
                    359: }
                    360: 
                    361: #ifndef DOXYGEN
                    362: struct Http_pass_header_info {
                    363:        Request_charsets* charsets;
                    364:        String* request;
1.35      misha     365:        bool* user_agent_specified;
                    366:        bool* content_type_specified;
                    367:        bool* content_type_url_encoded;
1.1       paf       368: };
                    369: #endif
1.50      moko      370: 
                    371: char *pa_http_safe_header_name(const char *name) {
                    372:        char *result=pa_strdup(name);
                    373:        char *n=result;
1.52      misha     374:        if(!pa_isalpha((unsigned char)*n))
1.50      moko      375:                *n++ = '_';
                    376:        for(; *n; ++n) {
1.52      misha     377:                if (!pa_isalnum((unsigned char)*n) && *n != '-' && *n != '_')
1.50      moko      378:                        *n = '_';
                    379:        }
                    380:        return result;
                    381: }
                    382: 
1.101     moko      383: static void http_pass_header(HashStringValue::key_type aname, HashStringValue::value_type avalue, Http_pass_header_info *info) {
1.9       misha     384: 
1.41      misha     385:        const char* name_cstr=aname.cstr();
                    386: 
1.38      misha     387:        if(strcasecmp(name_cstr, HTTP_CONTENT_LENGTH)==0)
                    388:                return;
                    389: 
1.50      moko      390:        String name=String(pa_http_safe_header_name(capitalize(name_cstr)), String::L_AS_IS);
                    391:        String value=attributed_meaning_to_string(*avalue, String::L_HTTP_HEADER, true);
1.9       misha     392: 
1.35      misha     393:        *info->request << name << ": " << value << CRLF;
1.1       paf       394:        
1.38      misha     395:        if(strcasecmp(name_cstr, HTTP_USER_AGENT)==0)
1.35      misha     396:                *info->user_agent_specified=true;
1.38      misha     397:        if(strcasecmp(name_cstr, HTTP_CONTENT_TYPE)==0){
1.35      misha     398:                *info->content_type_specified=true;
1.62      moko      399:                *info->content_type_url_encoded=pa_strncasecmp(value.cstr(), HTTP_CONTENT_TYPE_FORM_URLENCODED)==0;
1.35      misha     400:        }
1.1       paf       401: }
                    402: 
1.10      misha     403: static void http_pass_cookie(HashStringValue::key_type name, 
1.20      misha     404:                                HashStringValue::value_type value, 
                    405:                                Http_pass_header_info *info) {
1.10      misha     406:        
1.17      misha     407:        *info->request << String(name, String::L_HTTP_COOKIE) << "="
1.31      misha     408:                << attributed_meaning_to_string(*value, String::L_HTTP_COOKIE, true)
1.10      misha     409:                << "; "; 
                    410: 
                    411: }
1.1       paf       412: 
                    413: static const String* basic_authorization_field(const char* user, const char* pass) {
                    414:        if(!user&& !pass)
                    415:                return 0;
                    416: 
                    417:        String combined;  
                    418:        if(user)
                    419:                combined<<user;
                    420:        combined<<":";
                    421:        if(pass)
                    422:                combined<<pass;
                    423:        
1.20      misha     424:        String* result=new String("Basic ");
1.82      moko      425:        *result<<pa_base64_encode(combined.cstr(), combined.length(), Base64Options(false /*no wrap*/));
1.1       paf       426:        return result;
                    427: }
                    428: 
1.73      moko      429: static void form_string_value2string(HashStringValue::key_type key, const String& value, String& result) {
1.30      misha     430:        result << String(key, String::L_URI) << "=" << String(value, String::L_URI) << "&";
1.1       paf       431: }
1.20      misha     432: 
1.1       paf       433: #ifndef DOXYGEN
                    434: struct Form_table_value2string_info {
                    435:        HashStringValue::key_type key;
                    436:        String& result;
                    437: 
                    438:        Form_table_value2string_info(HashStringValue::key_type akey, String& aresult): 
                    439:                key(akey), result(aresult) {}
                    440: };
                    441: #endif
                    442: static void form_table_value2string(Table::element_type row, Form_table_value2string_info* info) {
                    443:        form_string_value2string(info->key, *row->get(0), info->result);
                    444: }
1.73      moko      445: 
                    446: static void form_value2string(HashStringValue::key_type key, HashStringValue::value_type value, String* result) {
1.1       paf       447:        if(const String* svalue=value->get_string())
                    448:                form_string_value2string(key, *svalue, *result);
                    449:        else if(Table* tvalue=value->get_table()) {
                    450:                Form_table_value2string_info info(key, *result);
                    451:                tvalue->for_each(form_table_value2string, &info);
                    452:        } else
1.73      moko      453:                throw Exception(PARSER_RUNTIME, new String(key, String::L_TAINTED),
1.63      moko      454:                        "is %s, " HTTP_FORM_NAME " option value can be string or table only (file is allowed for $." HTTP_METHOD_NAME "[POST] + $." HTTP_FORM_ENCTYPE_NAME "[" HTTP_CONTENT_TYPE_MULTIPART_FORMDATA "])", value->type());
1.1       paf       455: }
1.20      misha     456: 
1.5       misha     457: const char* pa_form2string(HashStringValue& form, Request_charsets& charsets) {
1.1       paf       458:        String string;
1.3       paf       459:        form.for_each<String*>(form_value2string, &string);
1.44      misha     460:        return string.untaint_and_transcode_cstr(String::L_URI, &charsets);
1.1       paf       461: }
1.22      misha     462: 
                    463: struct FormPart {
                    464:        Request* r;
                    465:        const char* boundary;
1.48      moko      466:        String* string;
1.22      misha     467:        Form_table_value2string_info* info;
1.48      moko      468: 
                    469:        struct BinaryBlock{
                    470:                const char* ptr;
                    471:                size_t length;
                    472: 
                    473:                BinaryBlock(String* astring, Request* r): ptr(astring->untaint_and_transcode_cstr(String::L_AS_IS, &r->charsets)), length(strlen(ptr)){}
                    474:                BinaryBlock(const char* aptr, size_t alength): ptr(aptr), length(alength){}
                    475:        };
                    476: 
                    477:        Array<BinaryBlock> blocks;
                    478: 
                    479:        FormPart(Request* ar, const char* aboundary): r(ar), boundary(aboundary), string(new String()){}
                    480: 
                    481:        const char *post(size_t &length){
                    482:                if(blocks.count()){
                    483:                        blocks+=BinaryBlock(string, r);
                    484: 
                    485:                        length=0;
                    486:                        for(size_t i=0; i<blocks.count(); i++)
                    487:                                length+=blocks[i].length;
                    488: 
                    489:                        char *result=(char *)pa_malloc_atomic(length);
                    490:                        char *ptr=result;
                    491: 
                    492:                        for(size_t i=0; i<blocks.count(); i++){
                    493:                                memcpy(ptr, blocks[i].ptr, blocks[i].length);
                    494:                                ptr+=blocks[i].length;
                    495:                        }
                    496: 
                    497:                        return result;
                    498:                } else {
                    499:                        BinaryBlock result(string, r);
                    500:                        length=result.length;
                    501:                        return result.ptr;
                    502:                }
                    503:        }
1.22      misha     504: };
                    505: 
1.73      moko      506: static void form_part_boundary_header(FormPart& part, String::Body name, const char* file_name=0) {
                    507:        *part.string << "--" << part.boundary << CRLF CONTENT_DISPOSITION_CAPITALIZED ": form-data; name=\"" << name << "\"";
1.22      misha     508:        if(file_name){
                    509:                if(strcmp(file_name, NONAME_DAT)!=0)
1.48      moko      510:                        *part.string << "; filename=\"" << file_name << "\"";
                    511:                *part.string << CRLF HTTP_CONTENT_TYPE_CAPITALIZED ": " << part.r->mime_type_of(file_name);
1.22      misha     512:        }
1.48      moko      513:        *part.string << CRLF CRLF;
1.22      misha     514: }
                    515: 
1.73      moko      516: static void form_string_value2part(HashStringValue::key_type key, const String& value, FormPart& part) {
1.28      misha     517:        form_part_boundary_header(part, key);
1.48      moko      518:        *part.string << value << CRLF;
1.22      misha     519: }
                    520: 
1.73      moko      521: static void form_file_value2part(HashStringValue::key_type key, VFile& vfile, FormPart& part) {
1.28      misha     522:        form_part_boundary_header(part, key, vfile.fields().get(name_name)->as_string().cstr());
1.48      moko      523:        part.blocks+=FormPart::BinaryBlock(part.string, part.r);
                    524:        part.blocks+=FormPart::BinaryBlock(vfile.value_ptr(), vfile.value_size());
                    525:        part.string=new String();
                    526:        *part.string << CRLF;
1.22      misha     527: }
                    528: 
                    529: static void form_table_value2part(Table::element_type row, FormPart* part) {
                    530:        form_string_value2part(part->info->key, *row->get(0), *part);
                    531: }
                    532: 
1.73      moko      533: static void form_value2part(HashStringValue::key_type key, HashStringValue::value_type value, FormPart& part) {
1.22      misha     534:        if(const String* svalue=value->get_string())
                    535:                form_string_value2part(key, *svalue, part);
                    536:        else if(Table* tvalue=value->get_table()) {
1.48      moko      537:                Form_table_value2string_info info(key, *part.string);
1.22      misha     538:                part.info = &info;
                    539:                tvalue->for_each(form_table_value2part, &part);
1.126     moko      540:        } else if(VFile* vfile=dynamic_cast<VFile *>(value)){
1.22      misha     541:                form_file_value2part(key, *vfile, part);
                    542:        } else
1.73      moko      543:                throw Exception(PARSER_RUNTIME, new String(key, String::L_TAINTED), "is %s, " HTTP_FORM_NAME " option value can be string, table or file only", value->type());
1.22      misha     544: }
                    545: 
                    546: const char* pa_form2string_multipart(HashStringValue& form, Request& r, const char* boundary, size_t& post_size){
1.48      moko      547:        FormPart formpart(&r, boundary);
1.22      misha     548:        form.for_each<FormPart&>(form_value2part, formpart);
1.48      moko      549:        *formpart.string << "--" << boundary << "--";
                    550:        // @todo: return binary blocks here to save memory in pa_internal_file_read_http
                    551:        return formpart.post(post_size);
1.22      misha     552: }
                    553: 
1.54      misha     554: // Set-Cookie: name=value; Domain=docs.foo.com; Path=/accounts; Expires=Wed, 13-Jan-2021 22:23:01 GMT; Secure; HttpOnly
                    555: static ArrayString* parse_cookie(Request& r, const String& cookie) {
1.64      moko      556:        char *current=pa_strdup(cookie.cstr());
1.54      misha     557:        
                    558:        const String* name=0;
1.55      moko      559:        const String* value=&String::Empty;
                    560:        const String* expires=&String::Empty;
                    561:        const String* max_age=&String::Empty;
                    562:        const String* path=&String::Empty;
                    563:        const String* domain=&String::Empty;
                    564:        const String* httponly=&String::Empty;
                    565:        const String* secure=&String::Empty;
1.54      misha     566: 
                    567:        bool first_pair=true;
                    568: 
                    569:        do {
                    570:                if(char *meaning=search_stop(current, ';'))
                    571:                        if(char *attribute=search_stop(meaning, '=')) {
                    572:                                const String* sname=new String(unescape_chars(attribute, strlen(attribute), &r.charsets.source(), true/*don't convert '"' to space*/), String::L_TAINTED);
                    573:                                const String* smeaning=0;
                    574:                                if(meaning)
                    575:                                        smeaning=new String(unescape_chars(meaning, strlen(meaning), &r.charsets.source(), true/*don't convert '"' to space*/), String::L_TAINTED);
                    576: 
                    577:                                if(first_pair) {
                    578:                                        // name + value
                    579:                                        name=sname;
1.122     moko      580:                                        if(smeaning)
                    581:                                                value=smeaning;
1.54      misha     582:                                        first_pair=false;
                    583:                                } else {
                    584:                                        const String& slower=sname->change_case(r.charsets.source(), String::CC_LOWER);
                    585: 
                    586:                                        if(slower == "expires")
                    587:                                                expires=smeaning;
                    588:                                        else if(slower == "max-age")
                    589:                                                max_age=smeaning;
                    590:                                        else if(slower == "domain")
                    591:                                                domain=smeaning;
                    592:                                        else if(slower == "path")
                    593:                                                path=smeaning;
                    594:                                        else if(slower == "httponly")
                    595:                                                httponly=new String("1", String::L_CLEAN);
                    596:                                        else if(slower == "secure")
                    597:                                                secure=new String("1", String::L_CLEAN);
                    598:                                        else {
                    599:                                                // todo@ ?
                    600:                                        }
                    601:                                }
                    602:                        }
                    603:        } while(current);
                    604: 
                    605:        if(!name)
                    606:                return 0;
                    607: 
                    608:        ArrayString* result=new ArrayString(8);
                    609:        *result+=name;
                    610:        *result+=value;
                    611:        *result+=expires;
                    612:        *result+=max_age;
                    613:        *result+=domain;
                    614:        *result+=path;
                    615:        *result+=httponly;
                    616:        *result+=secure;
                    617: 
                    618:        return result;
                    619: }
                    620: 
1.56      misha     621: Table* parse_cookies(Request& r, Table *cookies){
                    622:        Table& result=*new Table(new Cookies_table_template_columns);
                    623: 
1.125     moko      624:        for(Array_iterator<Table::element_type> i(*cookies); i; )
1.56      misha     625:                if(ArrayString* row=parse_cookie(r, *i.next()->get(0)))
                    626:                        result+=row;
                    627: 
                    628:        return &result;
                    629: }
                    630: 
1.75      moko      631: void tables_update(HashStringValue& tables, const String::Body name, const String& value){
1.72      moko      632:        Table *table;
                    633:        if(Value *valready=tables.get(name)) {
                    634:                // second+ appearence
                    635:                table=valready->get_table();
                    636:        } else {
                    637:                // first appearence
                    638:                Table::columns_type columns=new ArrayString(1);
                    639:                *columns+=new String("value");
                    640:                table=new Table(columns);
                    641:                tables.put(name, new VTable(table));
                    642:        }
                    643:        // this string becomes next row
                    644:        ArrayString& row=*new ArrayString(1);
                    645:        row+=&value;
                    646:        *table+=&row;
                    647: }
                    648: 
1.1       paf       649: /// @todo build .cookies field. use ^file.tables.SET-COOKIES.menu{ for now
1.72      moko      650: File_read_http_result pa_internal_file_read_http(Request& r, const String& file_spec, bool as_text, HashStringValue *options, bool transcode_text_result) {
1.1       paf       651:        File_read_http_result result;
1.20      misha     652:        char host[MAX_STRING];
1.66      moko      653:        const char *idna_host;
1.1       paf       654:        const char* uri; 
1.49      moko      655:        short port=80;
1.10      misha     656:        const char* method="GET";
1.21      misha     657:        bool method_is_get=true;
1.1       paf       658:        HashStringValue* form=0;
                    659:        int timeout_secs=2;
                    660:        bool fail_on_status_ne_200=true;
1.12      misha     661:        bool omit_post_charset=false;
1.1       paf       662:        Value* vheaders=0;
1.10      misha     663:        Value* vcookies=0;
1.11      misha     664:        Value* vbody=0;
1.72      moko      665:        Charset* asked_remote_charset=0;
1.58      moko      666:        Charset* real_remote_charset=0;
1.1       paf       667:        const char* user_cstr=0;
                    668:        const char* password_cstr=0;
1.22      misha     669:        const char* encode=0;
                    670:        bool multipart=false;
1.1       paf       671: 
                    672:        if(options) {
                    673:                int valid_options=pa_get_valid_file_options_count(*options);
                    674: 
                    675:                if(Value* vmethod=options->get(HTTP_METHOD_NAME)) {
                    676:                        valid_options++;
1.21      misha     677:                        method=vmethod->as_string().change_case(r.charsets.source(), String::CC_UPPER).cstr();
                    678:                        method_is_get=strcmp(method, "GET")==0;
1.1       paf       679:                }
1.22      misha     680:                if(Value* vencode=options->get(HTTP_FORM_ENCTYPE_NAME)) {
                    681:                        valid_options++;
                    682:                        encode=vencode->as_string().cstr();
                    683:                }
1.1       paf       684:                if(Value* vform=options->get(HTTP_FORM_NAME)) {
                    685:                        valid_options++;
                    686:                        form=vform->get_hash(); 
                    687:                } 
1.11      misha     688:                if(vbody=options->get(HTTP_BODY_NAME)) {
1.1       paf       689:                        valid_options++;
                    690:                } 
                    691:                if(Value* vtimeout=options->get(HTTP_TIMEOUT_NAME)) {
                    692:                        valid_options++;
                    693:                        timeout_secs=vtimeout->as_int(); 
                    694:                } 
1.11      misha     695:                if(vheaders=options->get(HTTP_HEADERS_NAME)) {
1.1       paf       696:                        valid_options++;
                    697:                } 
1.11      misha     698:                if(vcookies=options->get(HTTP_COOKIES_NAME)) {
1.10      misha     699:                        valid_options++;
                    700:                } 
1.1       paf       701:                if(Value* vany_status=options->get(HTTP_ANY_STATUS_NAME)) {
                    702:                        valid_options++;
                    703:                        fail_on_status_ne_200=!vany_status->as_bool(); 
1.12      misha     704:                }
1.20      misha     705:                if(Value* vomit_post_charset=options->get(HTTP_OMIT_POST_CHARSET_NAME)){
1.12      misha     706:                        valid_options++;
                    707:                        omit_post_charset=vomit_post_charset->as_bool();
                    708:                }
1.6       misha     709:                if(Value* vcharset_name=options->get(PA_CHARSET_NAME)) {
1.77      moko      710:                        asked_remote_charset=&pa_charsets.get(vcharset_name->as_string());
1.58      moko      711:                } 
                    712:                if(Value* vresponse_charset_name=options->get(PA_RESPONSE_CHARSET_NAME)) {
1.61      moko      713:                        valid_options++;
1.77      moko      714:                        real_remote_charset=&pa_charsets.get(vresponse_charset_name->as_string());
1.1       paf       715:                } 
                    716:                if(Value* vuser=options->get(HTTP_USER)) {
                    717:                        valid_options++;
                    718:                        user_cstr=vuser->as_string().cstr();
                    719:                } 
                    720:                if(Value* vpassword=options->get(HTTP_PASSWORD)) {
                    721:                        valid_options++;
                    722:                        password_cstr=vpassword->as_string().cstr();
                    723:                }
                    724: 
                    725:                if(valid_options!=options->count())
1.46      misha     726:                        throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.1       paf       727:        }
                    728:        if(!asked_remote_charset) // defaulting to $request:charset
1.22      misha     729:                asked_remote_charset=&(r.charsets).source();
                    730: 
                    731:        if(encode){
                    732:                if(method_is_get)
1.127     moko      733:                        throw Exception(PARSER_RUNTIME, 0, "you cannot use $." HTTP_FORM_ENCTYPE_NAME " option with method GET");
1.22      misha     734: 
                    735:                multipart=strcasecmp(encode, HTTP_CONTENT_TYPE_MULTIPART_FORMDATA)==0;
                    736: 
                    737:                if(!multipart && strcasecmp(encode, HTTP_CONTENT_TYPE_FORM_URLENCODED)!=0)
1.72      moko      738:                        throw Exception(PARSER_RUNTIME, 0, "$." HTTP_FORM_ENCTYPE_NAME " option value can be " HTTP_CONTENT_TYPE_FORM_URLENCODED " or " HTTP_CONTENT_TYPE_MULTIPART_FORMDATA " only");
1.22      misha     739:        }
1.1       paf       740: 
1.11      misha     741:        if(vbody){
                    742:                if(method_is_get)
1.127     moko      743:                        throw Exception(PARSER_RUNTIME, 0, "you cannot use $." HTTP_BODY_NAME " option with method GET");
1.11      misha     744: 
                    745:                if(form)
1.127     moko      746:                        throw Exception(PARSER_RUNTIME, 0, "you cannot use options $." HTTP_BODY_NAME " and $." HTTP_FORM_NAME " together");
1.11      misha     747:        }
1.1       paf       748: 
                    749:        //preparing request
1.29      misha     750:        String& connect_string=*new String(file_spec);
1.1       paf       751: 
1.48      moko      752:        const char* request;
                    753:        size_t request_size;
1.1       paf       754:        {
                    755:                // influence URLencoding of tainted pieces to String::L_URI lang
1.22      misha     756:                Temp_client_charset temp(r.charsets, *asked_remote_charset);
1.1       paf       757: 
1.44      misha     758:                const char* connect_string_cstr=connect_string.untaint_and_transcode_cstr(String::L_URI, &(r.charsets));
1.1       paf       759: 
                    760:                const char* current=connect_string_cstr;
                    761:                if(strncmp(current, "http://", 7)!=0)
1.72      moko      762:                        throw Exception(PARSER_RUNTIME, &connect_string, "does not start with http://"); //never
1.1       paf       763:                current+=7;
                    764: 
1.119     moko      765:                pa_strncpy(host, current, sizeof(host));
1.34      misha     766:                char* host_uri=lsplit(host, '/');
                    767:                uri=host_uri?current+(host_uri-1-host):"/";
                    768:                char* port_cstr=lsplit(host, ':');
1.49      moko      769:                
                    770:                if (port_cstr){
                    771:                        char* error_pos=0;
                    772:                        port=(short)strtol(port_cstr, &error_pos, 10);
                    773:                        if(port==0 || *error_pos)
                    774:                                throw Exception(PARSER_RUNTIME, &connect_string, "invalid port number '%s'", port_cstr);
                    775:                }
1.1       paf       776: 
1.66      moko      777:                idna_host=pa_idna_encode(host, r.charsets.source());
                    778: 
1.11      misha     779:                // making request head
1.1       paf       780:                String head;
1.11      misha     781:                head << method << " " << uri;
1.28      misha     782:                if(method_is_get && form)
                    783:                        head << (strchr(uri, '?')!=0?"&":"?") << pa_form2string(*form, r.charsets);
1.11      misha     784: 
1.66      moko      785:                head <<" HTTP/1.0" CRLF "Host: "<< idna_host;
1.49      moko      786:                if (port != 80)
                    787:                        head << ":" << port_cstr;
                    788:                head << CRLF;
1.11      misha     789: 
1.71      moko      790:                char* boundary= multipart ? get_uuid_boundary() : 0;
1.22      misha     791: 
1.35      misha     792:                String user_headers;
                    793:                bool user_agent_specified=false;
                    794:                bool content_type_specified=false;
                    795:                bool content_type_url_encoded=false;
                    796:                if(vheaders && !vheaders->is_string()) { // allow empty
                    797:                        if(HashStringValue *headers=vheaders->get_hash()) {
                    798:                                Http_pass_header_info info={
                    799:                                        &(r.charsets),
                    800:                                        &user_headers,
                    801:                                        &user_agent_specified,
                    802:                                        &content_type_specified,
                    803:                                        &content_type_url_encoded};
                    804:                                headers->for_each<Http_pass_header_info*>(http_pass_header, &info); 
                    805:                        } else
1.72      moko      806:                                throw Exception(PARSER_RUNTIME, 0, "headers param must be hash"); 
1.35      misha     807:                };
                    808: 
1.48      moko      809:                const char* request_body=0;
1.22      misha     810:                size_t post_size=0;
                    811:                if(form && !method_is_get) {
1.38      misha     812:                        head << "Content-Type: " << (multipart ? HTTP_CONTENT_TYPE_MULTIPART_FORMDATA : HTTP_CONTENT_TYPE_FORM_URLENCODED);
1.28      misha     813: 
                    814:                        if(!omit_post_charset)
                    815:                                head << "; charset=" << asked_remote_charset->NAME_CSTR();
                    816: 
1.22      misha     817:                        if(multipart) {
1.28      misha     818:                                head << "; boundary=" << boundary;
1.48      moko      819:                                request_body=pa_form2string_multipart(*form, r/*charsets & mime_type needed*/, boundary, post_size/*correct post_size returned here*/);
1.22      misha     820:                        } else {
1.48      moko      821:                                request_body=pa_form2string(*form, r.charsets);
                    822:                                post_size=strlen(request_body);
1.22      misha     823:                        }
1.28      misha     824:                        head << CRLF;
1.35      misha     825:                } else if(vbody) {
1.38      misha     826:                        // $.body was specified
1.35      misha     827:                        if(content_type_url_encoded){
1.36      misha     828:                                // transcode + url-encode
1.48      moko      829:                                request_body=vbody->as_string().untaint_and_transcode_cstr(String::L_URI, &(r.charsets));
1.35      misha     830:                        } else {
1.36      misha     831:                                // content-type != application/x-www-form-urlencoded -> transcode only, don't url-encode!
1.72      moko      832:                                const String &sbody=vbody->as_string();
                    833:                                request_body=Charset::transcode(String::C(sbody.cstr(), sbody.length()), r.charsets.source(), *asked_remote_charset).str;
1.35      misha     834:                        }
1.48      moko      835:                        post_size=strlen(request_body);
1.1       paf       836:                }
                    837: 
                    838:                // http://www.ietf.org/rfc/rfc2617.txt
                    839:                if(const String* authorization_field_value=basic_authorization_field(user_cstr, password_cstr))
1.38      misha     840:                        head << "Authorization: " << *authorization_field_value << CRLF;
1.1       paf       841: 
1.35      misha     842:                head << user_headers;
                    843: 
1.1       paf       844:                if(!user_agent_specified) // defaulting
1.38      misha     845:                        head << "User-Agent: " DEFAULT_USER_AGENT CRLF;
1.1       paf       846: 
1.12      misha     847:                if(form && !method_is_get && content_type_specified) // POST + form + content-type was specified
1.72      moko      848:                        throw Exception(PARSER_RUNTIME, 0, "$.content-type can't be specified with method POST"); 
1.12      misha     849: 
1.11      misha     850:                if(vcookies && !vcookies->is_string()){ // allow empty
1.10      misha     851:                        if(HashStringValue* cookies=vcookies->get_hash()) {
1.37      misha     852:                                head << "Cookie: ";
1.35      misha     853:                                Http_pass_header_info info={&(r.charsets), &head, 0, 0, 0};
1.10      misha     854:                                cookies->for_each<Http_pass_header_info*>(http_pass_cookie, &info); 
                    855:                                head << CRLF;
                    856:                        } else
1.72      moko      857:                                throw Exception(PARSER_RUNTIME, 0, "cookies param must be hash");
1.10      misha     858:                }
                    859: 
1.48      moko      860:                if(request_body)
1.128     moko      861:                        head << "Content-Length: " << pa_uitoa(post_size) << CRLF;
1.48      moko      862:                
                    863:                head << CRLF;
                    864:                
                    865:                const char *request_head=head.untaint_and_transcode_cstr(String::L_URI, &(r.charsets));
1.1       paf       866: 
1.48      moko      867:                if(request_body){
                    868:                        size_t head_size = strlen(request_head);
                    869:                        request_size=post_size + head_size;
                    870:                        char *ptr=(char *)pa_malloc_atomic(request_size);
                    871:                        memcpy(ptr, request_head, head_size);
                    872:                        memcpy(ptr+head_size, request_body, post_size);
                    873:                        request=ptr;
                    874:                } else {
                    875:                        request_size=strlen(request_head);
                    876:                        request=request_head;
                    877:                }
1.1       paf       878:        }
                    879:        
1.78      moko      880: 
1.97      moko      881:        HTTP_response response;
1.22      misha     882: 
1.28      misha     883:        // sending request
1.95      moko      884:        int status_code;
                    885:        ALTER_EXCEPTION_SOURCE(status_code=http_request(response, idna_host, port, request, request_size, timeout_secs, fail_on_status_ne_200), &connect_string);
1.78      moko      886: 
1.72      moko      887:        // processing results
1.78      moko      888:        char* raw_body=response.buf + response.body_offset;
                    889:        size_t raw_body_size=response.length - response.body_offset;
                    890: 
1.1       paf       891:        result.headers=new HashStringValue;
                    892:        VHash* vtables=new VHash;
1.133     moko      893:        HASH_PUT_CSTR(*result.headers, "tables", vtables);
1.72      moko      894: 
1.78      moko      895:        if (!real_remote_charset && !response.headers.content_type.is_empty())
                    896:                real_remote_charset=detect_charset(response.headers.content_type.cstr());
1.1       paf       897: 
1.72      moko      898:        if(as_text)
1.77      moko      899:                real_remote_charset=pa_charsets.checkBOM(raw_body, raw_body_size, real_remote_charset);
1.72      moko      900: 
                    901:        if (!real_remote_charset)
                    902:                real_remote_charset=asked_remote_charset; // never null
                    903: 
1.125     moko      904:        for(Array_iterator<HTTP_Headers::Header> i(response.headers.headers); i; ){
1.85      moko      905:                HTTP_Headers::Header header=i.next();
1.72      moko      906: 
                    907:                header.transcode(*real_remote_charset, r.charsets.source());
                    908: 
                    909:                String &header_value=*new String(header.value, String::L_TAINTED);
                    910: 
                    911:                tables_update(vtables->hash(), header.name, header_value);
                    912:                result.headers->put(header.name, new VString(header_value));
1.16      misha     913:        }
                    914: 
1.72      moko      915:        // filling $.cookies
1.89      moko      916:        if(vcookies=vtables->hash().get("SET-COOKIE"))
1.133     moko      917:                HASH_PUT_CSTR(*result.headers, HTTP_COOKIES_NAME, new VTable(parse_cookies(r, vcookies->get_table())));
1.72      moko      918: 
1.1       paf       919:        // output response
                    920:        String::C real_body=String::C(raw_body, raw_body_size);
1.16      misha     921: 
                    922:        if(as_text && transcode_text_result && raw_body_size) { // raw_body_size must be checked because transcode returns CONST string in case length==0, which contradicts hacking few lines below
1.22      misha     923:                real_body=Charset::transcode(real_body, *real_remote_charset, r.charsets.source());
1.1       paf       924:        }
                    925: 
                    926:        result.str=const_cast<char *>(real_body.str); // hacking a little
                    927:        result.length=real_body.length;
1.16      misha     928: 
1.22      misha     929:        if(as_text && result.length)
                    930:                fix_line_breaks(result.str, result.length);
                    931: 
1.1       paf       932:        result.headers->put(file_status_name, new VInt(status_code));
1.16      misha     933: 
1.1       paf       934:        return result;
                    935: }
1.84      moko      936: 
                    937: /* ********************** httpd *************************** */
                    938: 
1.100     moko      939: enum EscapeState {
                    940:         Initial,
                    941:         Default,
                    942:         EscapeFirst,
                    943:         EscapeSecond
                    944: };
                    945: 
                    946: static bool check_uri(const char *uri){
                    947:        EscapeState state=Initial;
1.120     moko      948:        uint escapedValue=0;
1.100     moko      949: 
                    950:        const char *pattern="/../";
                    951:        const char *pos=pattern;
                    952: 
                    953:        while(*uri){
                    954:                uchar c=(uchar)*(uri++);
                    955:                switch(state) {
                    956:                        case Initial:
                    957:                                if(c!='/')
                    958:                                        return false;
                    959:                                state=Default;
                    960:                                break;
                    961:                        case Default:
                    962:                                if(c=='%'){
                    963:                                        state=EscapeFirst;
                    964:                                        continue;
                    965:                                }
                    966:                                if(c=='?')
                    967:                                        return true;
                    968:                                break;
                    969:                        case EscapeFirst:
                    970:                                if(isxdigit(c)){
                    971:                                        state=EscapeSecond;
                    972:                                        escapedValue=hex_value[c] << 4;
                    973:                                        continue;
                    974:                                }
                    975:                                return false;
                    976:                        case EscapeSecond:
                    977:                                if(isxdigit(c)){
                    978:                                        state=Default;
1.105     moko      979:                                        c=(uchar)(escapedValue + hex_value[c]);
1.100     moko      980: 
                    981:                                        // implementing Apache AllowEncodedSlashes Off just in case
                    982:                                        if(c=='/' || c=='\\')
                    983:                                                return false;
                    984: 
                    985:                                        break;
                    986:                                }
                    987:                                return false;
                    988:                }
                    989: 
                    990:                if(c==*pos || c=='\\' && *pos=='/'){
                    991:                        if(!*(++pos))
                    992:                                return false;
                    993:                } else {
                    994:                        pos=pattern;
                    995:                }
                    996:        }
                    997:        return true;
                    998: }
                    999: 
1.84      moko     1000: class HTTPD_request : public HTTP_response {
                   1001: public:
                   1002:        const char *method;
                   1003:        const char *uri;
                   1004: 
1.97      moko     1005:        HTTPD_request() : HTTP_response(), method(NULL), uri(NULL){};
1.84      moko     1006: 
1.120     moko     1007:        ssize_t pa_recv(SOCKET sockfd, char *buf, size_t len);
1.103     moko     1008: 
1.120     moko     1009:        bool read(SOCKET sock, size_t size){
1.103     moko     1010:                if(length + size > buf_size)
                   1011:                        resize(buf_size * 2 + size);
                   1012:                ssize_t received_size=pa_recv(sock, buf + length, size);
                   1013:                if(received_size == 0)
                   1014:                        return false;
                   1015:                if(received_size < 0) {
                   1016:                        if(int no = pa_socks_errno())
1.111     moko     1017:                                throw Exception("httpd.read", 0, "error receiving request: %s (%d)", pa_socks_strerr(no), no);
1.103     moko     1018:                        return false;
                   1019:                }
                   1020:                length+=received_size;
                   1021:                buf[length]='\0';
                   1022:                return true;
                   1023:        }
                   1024: 
1.84      moko     1025:        const char *extract_method(char *method_line){
                   1026:                char* uri_start = strchr(method_line, ' ');
                   1027: 
                   1028:                if(!uri_start || uri_start == method_line)
                   1029:                        return NULL;
                   1030: 
                   1031:                char* uri_end=strchr(uri_start+1, ' ');
                   1032: 
                   1033:                if(!uri_end || uri_end == uri_start+1)
                   1034:                        return NULL;
                   1035: 
                   1036:                uri=pa_strdup(uri_start+1, uri_end-uri_start-1);
1.100     moko     1037:                if(!check_uri(uri))
                   1038:                        throw Exception("httpd.request", 0, "invalid uri '%s'", uri);
                   1039: 
1.84      moko     1040:                return str_upper(method_line, uri_start-method_line);
                   1041:        }
                   1042: 
1.103     moko     1043: 
1.120     moko     1044:        bool read_header(SOCKET);
                   1045:        size_t read_post(SOCKET, char *, size_t);
1.84      moko     1046: };
                   1047: 
                   1048: enum HTTPD_request_state {
                   1049:        HTTPD_METHOD,
                   1050:        HTTPD_HEADERS
                   1051: };
                   1052: 
1.120     moko     1053: ssize_t HTTPD_request::pa_recv(SOCKET sockfd, char *buffer, size_t len){
1.111     moko     1054:        LOG(pa_log("httpd [%d] recv %d appending to %d ...", sockfd, len, length));
1.107     moko     1055: 
                   1056: #ifdef PA_USE_ALARM
1.118     moko     1057:        if(PA_NO_THREADS) signal(SIGALRM, timeout_handler);
                   1058:        if(PA_NO_THREADS && sigsetjmp(timeout_env, 1)) {
1.111     moko     1059:                LOG(pa_log("httpd [%d] recv got %d sec timeout", sockfd, pa_httpd_timeout));
                   1060:                if(length) // timeout on "void" connection is normal
                   1061:                        throw Exception("httpd.timeout", 0, "timeout occurred while receiving request");
                   1062:                return 0;
1.103     moko     1063:        } else
1.107     moko     1064: #endif
1.103     moko     1065:        {
1.107     moko     1066:                ALARM(pa_httpd_timeout);
1.104     moko     1067:                ssize_t result=recv(sockfd, buffer, len, 0);
1.107     moko     1068:                ALARM(0);
1.111     moko     1069:                LOG(pa_log("httpd [%d] recv got %d bytes", sockfd, result));
1.112     moko     1070:                LOG(pa_log("httpd [%d] %s", sockfd, buffer));
1.103     moko     1071:                return result;
                   1072:        }
                   1073: }
                   1074: 
1.115     moko     1075: static bool valid_http_method(const char * method){
                   1076:        return method && (
                   1077:                !strcmp(method, "GET") ||
                   1078:                !strcmp(method, "HEAD") ||
                   1079:                !strcmp(method, "POST") ||
                   1080:                !strcmp(method, "PUT") ||
                   1081:                !strcmp(method, "DELETE") ||
                   1082:                !strcmp(method, "CONNECT") ||
                   1083:                !strcmp(method, "OPTIONS") ||
                   1084:                !strcmp(method, "TRACE") ||
                   1085:                !strcmp(method, "PATCH")
                   1086:        );
                   1087: }
                   1088: 
1.120     moko     1089: bool HTTPD_request::read_header(SOCKET sock) {
1.84      moko     1090:        enum HTTPD_request_state state = HTTPD_METHOD;
                   1091: 
                   1092:        size_t chunk_size = 0x400*4;
                   1093:        resize(chunk_size);
                   1094: 
                   1095:        while(read(sock, chunk_size)){
                   1096:                switch(state){
                   1097:                        case HTTPD_METHOD: {
                   1098:                                size_t method_size = first_line();
                   1099:                                if(!method_size)
                   1100:                                        break;
                   1101: 
                   1102:                                char *method_line = pa_strdup(buf, method_size);
                   1103:                                method = extract_method(method_line);
                   1104: 
1.115     moko     1105:                                if(!valid_http_method(method))
1.84      moko     1106:                                        throw Exception("httpd.method", new String(method ? method : method_line), "invalid request method");
                   1107:                                state = HTTPD_HEADERS;
                   1108:                        }
                   1109: 
                   1110:                        case HTTPD_HEADERS: {
                   1111:                                if(!body_start())
                   1112:                                        break;
                   1113: 
                   1114:                                parse_headers();
1.110     moko     1115:                                return true;
1.84      moko     1116:                        }
                   1117:                }
                   1118:        }
                   1119: 
1.111     moko     1120:        if(!length){ // browsers open connections in advance and they will be empty unless user requests more pages
                   1121:                LOG(pa_log("httpd [%d] void request", sock));
1.110     moko     1122:                return false;
1.111     moko     1123:        }
1.110     moko     1124: 
1.84      moko     1125:        if(state == HTTPD_METHOD)
                   1126:                throw Exception("httpd.request", 0, "bad request from host - no method found (size=%u)", length);
                   1127: 
                   1128:        if(state == HTTPD_HEADERS){
                   1129:                parse_headers();
                   1130:                body_offset=length;
                   1131:        }
1.110     moko     1132: 
                   1133:        return true;
1.84      moko     1134: }
                   1135: 
1.120     moko     1136: size_t HTTPD_request::read_post(SOCKET sock, char *body, size_t max_bytes) {
1.87      moko     1137:        size_t total_read = min(length - body_offset, max_bytes);
1.98      moko     1138:        memcpy(body, buf + body_offset, total_read);
1.87      moko     1139: 
                   1140:        while (total_read < max_bytes){
1.103     moko     1141:                ssize_t received_size = pa_recv(sock, body + total_read, max_bytes - total_read);
1.87      moko     1142:                if(received_size == 0)
                   1143:                        return total_read;
                   1144:                if(received_size < 0) {
                   1145:                        if(int no = pa_socks_errno())
1.111     moko     1146:                                throw Exception("httpd.read", new String(uri), "error receiving request body: %s (%d)", pa_socks_strerr(no), no);
1.87      moko     1147:                        return total_read;
                   1148:                }
                   1149:                total_read += received_size;
                   1150:        }
                   1151:        return total_read;
                   1152: }
                   1153: 
1.84      moko     1154: /* ********************************************************** */
                   1155: 
1.85      moko     1156: Array<HTTP_Headers::Header> &HTTPD_Connection::headers() {
1.84      moko     1157:        return request->headers.headers;
                   1158: }
                   1159: 
                   1160: const char *HTTPD_Connection::method() {
                   1161:        return request->method;
                   1162: }
                   1163: 
                   1164: const char *HTTPD_Connection::uri() {
                   1165:        return request->uri;
                   1166: }
                   1167: 
                   1168: const char *HTTPD_Connection::content_type() {
                   1169:        return request->headers.content_type.cstr();
                   1170: }
                   1171: 
                   1172: uint64_t HTTPD_Connection::content_length(){
                   1173:        return request->headers.content_length;
                   1174: }
                   1175: 
1.110     moko     1176: bool HTTPD_Connection::read_header(){
1.84      moko     1177:        request = new HTTPD_request();
1.111     moko     1178:        bool result = request->read_header(sock);
                   1179:        LOG(if(result){
                   1180:                pa_log("httpd [%d] got %s \"%s\"", sock, method(), uri());
                   1181:        })
                   1182:        return result;
1.84      moko     1183: }
                   1184: 
1.87      moko     1185: size_t HTTPD_Connection::read_post(char *body, size_t max_bytes) {
                   1186:        return request->read_post(sock, body, max_bytes);
                   1187: }
                   1188: 
1.90      moko     1189: size_t HTTPD_Connection::send_body(const void *buf, size_t size) {
1.112     moko     1190:        LOG(pa_log("httpd [%d] response %d bytes", sock, size));
                   1191:        LOG(pa_log("httpd [%d] %s", sock, buf));
1.131     moko     1192:        ssize_t result=pa_send(sock, (const char*)buf, size);
                   1193:        if(result < 0) {
1.90      moko     1194:                int no=pa_socks_errno();
1.111     moko     1195:                throw Exception("httpd.write", 0, "error sending response: %s (%d)", pa_socks_strerr(no), no);
1.90      moko     1196:        }
1.131     moko     1197:        return result;
1.90      moko     1198: }
                   1199: 
1.93      moko     1200: HTTPD_Connection::~HTTPD_Connection(){
1.120     moko     1201:        if(sock != INVALID_SOCKET){
1.111     moko     1202:                LOG(pa_log("httpd [%d] closed", sock));
1.93      moko     1203:                closesocket(sock);
1.111     moko     1204:        }
1.93      moko     1205: }
                   1206: 
1.132     moko     1207: static int sock_ready(SOCKET fd, int timeout_value){
1.93      moko     1208:        struct timeval timeout = {0, timeout_value * 1000};
                   1209:        fd_set fds;
                   1210:        FD_ZERO(&fds);
                   1211:        FD_SET(fd, &fds);
1.120     moko     1212:        int nfds = (int)fd + 1; /* typecast as nfds is ignored in MSVC anyway */
1.132     moko     1213:        return select(nfds, &fds, NULL, NULL, &timeout)>0;  /* read */
1.93      moko     1214: }
                   1215: 
1.120     moko     1216: bool HTTPD_Connection::accept(SOCKET server_sock, int timeout_value) {
1.132     moko     1217:        int ready = sock_ready(server_sock, timeout_value);
1.93      moko     1218:        if (ready < 0) {
                   1219:                int no=pa_socks_errno();
                   1220:                if(no == EINTR)
                   1221:                        return false;
                   1222:                throw Exception("httpd.accept", 0, "error waiting for connection: %s (%d)", pa_socks_strerr(no), no);
                   1223:        }
                   1224:        if (ready == 0)
                   1225:                return false; /* Timeout */
                   1226: 
                   1227:        struct sockaddr_in addr;
                   1228:        socklen_t sock_addr_len = sizeof(struct sockaddr_in);
                   1229:        memset(&addr, 0, sock_addr_len);
                   1230: 
                   1231:        sock = ::accept(server_sock, (struct sockaddr *)&addr, &sock_addr_len);
1.120     moko     1232:        if(sock == INVALID_SOCKET){
1.93      moko     1233:                int no=pa_socks_errno();
                   1234:                throw Exception("httpd.accept", 0, "error accepting connection: %s (%d)", pa_socks_strerr(no), no);
                   1235:        }
                   1236: 
1.123     moko     1237: //     Has no positive performance effect, requires include <netinet/tcp.h>
                   1238: //     static int sock_on=1;
                   1239: //     setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&sock_on, sizeof(sock_on));
                   1240: 
1.93      moko     1241:        remote_addr = pa_strdup(inet_ntoa(addr.sin_addr));
1.111     moko     1242:        LOG(pa_log("httpd [%d] accepted from %s", sock, remote_addr));
1.93      moko     1243:        return true;
                   1244: }
1.84      moko     1245: 
1.107     moko     1246: HTTPD_Server::HTTPD_MODE HTTPD_Server::mode = HTTPD_Server::SEQUENTIAL;
1.114     moko     1247: const char *HTTPD_Server::port=NULL;
1.106     moko     1248: 
1.108     moko     1249: void HTTPD_Server::set_mode(const String &value){
                   1250:                 if(value == "sequental") mode = SEQUENTIAL;
1.113     moko     1251: #ifdef HAVE_TLS
1.108     moko     1252:                 else if (value == "threaded") mode = MULTITHREADED;
1.113     moko     1253: #endif
1.108     moko     1254: #ifdef _MSC_VER
1.117     moko     1255:                else throw Exception("httpd.mode", &value, "$MAIN:HTTPD.mode must be 'sequental' or 'threaded'");
1.108     moko     1256: #else
                   1257:                 else if (value == "parallel") mode = PARALLEL;
1.117     moko     1258:                else throw Exception("httpd.mode", &value, "$MAIN:HTTPD.mode must be 'sequental', 'parallel' or 'threaded'");
1.108     moko     1259: #endif
                   1260: }
                   1261: 
1.120     moko     1262: SOCKET HTTPD_Server::bind(const char *host_port){
1.84      moko     1263:        struct sockaddr_in me;
                   1264: 
1.114     moko     1265:        port = strchr(host_port, ':');
1.86      moko     1266:        const char *host = NULL;
1.116     moko     1267:        if(port){
1.114     moko     1268:                if(port > host_port)
                   1269:                        host = pa_strdup(host_port, port - host_port);
1.86      moko     1270:                port += 1;
                   1271:        } else {
                   1272:                port = host_port;
                   1273:        }
                   1274: 
1.105     moko     1275:        if(!set_addr(&me, host, (short)pa_atoui(port))){
1.84      moko     1276:                if (host)
1.127     moko     1277:                        throw Exception("httpd.bind", 0, "cannot resolve hostname \"%s\"", host);
1.84      moko     1278:                me.sin_addr.s_addr=INADDR_ANY;
                   1279:        }
                   1280: 
1.120     moko     1281:        SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/);
1.84      moko     1282: 
1.120     moko     1283:        if(sock == INVALID_SOCKET){
1.84      moko     1284:                int no=pa_socks_errno();
1.127     moko     1285:                throw Exception("httpd.bind", 0, "cannot make socket: %s (%d)", pa_socks_strerr(no), no);
1.84      moko     1286:        }
                   1287: 
1.93      moko     1288:        static int sock_on = 1;
                   1289: 
1.84      moko     1290:        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&sock_on, sizeof(sock_on)) ||
                   1291:            setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&sock_on, sizeof(sock_on)) ||
                   1292:            ::bind(sock, (struct sockaddr*)&me, sizeof(me)) ||
                   1293:            listen(sock, 16)) {
1.89      moko     1294:                closesocket(sock);
1.84      moko     1295:                int no = pa_socks_errno();
1.127     moko     1296:                throw Exception("httpd.bind", 0, "cannot bind socket: %s (%d)", pa_socks_strerr(no), no);
1.84      moko     1297:        }
                   1298:        return sock;
                   1299: }

E-mail: