--- parser3/src/main/pa_http.C 2017/01/23 09:33:02 1.78 +++ parser3/src/main/pa_http.C 2020/10/10 06:08:36 1.84 @@ -1,19 +1,20 @@ /** @file Parser: http support functions. - Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ #include "pa_http.h" #include "pa_common.h" +#include "pa_base64.h" #include "pa_charsets.h" #include "pa_request_charsets.h" #include "pa_request.h" #include "pa_vfile.h" #include "pa_random.h" -volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.78 2017/01/23 09:33:02 moko Exp $" IDENT_PA_HTTP_H; +volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.84 2020/10/10 06:08:36 moko Exp $" IDENT_PA_HTTP_H; #ifdef _MSC_VER #include @@ -95,7 +96,7 @@ static bool set_addr(struct sockaddr_in return false; } -class HTTP_response { +class HTTP_response : public PA_Allocated { public: char *buf; size_t length; @@ -103,9 +104,9 @@ public: size_t body_offset; ResponseHeaders headers; - String &url; + const String &url; - HTTP_response(String& aurl) : buf(NULL), length(0), buf_size(0), body_offset(0), url(aurl){} + HTTP_response(const String& aurl) : buf(NULL), length(0), buf_size(0), body_offset(0), url(aurl){} void resize(size_t size){ buf_size=size; @@ -128,7 +129,7 @@ public: return true; } - size_t status_size(){ + size_t first_line(){ char *headers=strchr(buf, '\n'); if(!headers) return false; @@ -151,7 +152,7 @@ public: return status_line; const char *result_str=pa_strdup(status_start, status_end-status_start); - result=atoi(result_str); + result=pa_atoui(result_str, 10); return result_str; } @@ -206,7 +207,7 @@ static int http_read_response(HTTP_respo while(response.read(sock, chunk_size)){ switch(state){ case HTTP_STATUS_CODE: { - size_t status_size=response.status_size(); + size_t status_size=response.first_line(); if(!status_size) break; @@ -280,7 +281,7 @@ static int http_request(HTTP_response& r // rewritten simplier [athough duplicating closesocket code] if(sock>=0) closesocket(sock); - throw Exception("http.timeout", 0, "timeout occured while retrieving document"); + throw Exception("http.timeout", 0, "timeout occurred while retrieving document"); return 0; // never } else { alarm(timeout_secs); @@ -406,7 +407,7 @@ static const String* basic_authorization combined< &HTTPD_Connection::headers() { + return request->headers.headers; +} + +const char *HTTPD_Connection::method() { + return request->method; +} + +const char *HTTPD_Connection::uri() { + return request->uri; +} + +const char *HTTPD_Connection::content_type() { + return request->headers.content_type.cstr(); +} + +uint64_t HTTPD_Connection::content_length(){ + return request->headers.content_length; +} + +void HTTPD_Connection::read_header(){ + request = new HTTPD_request(); + request->read_header(sock); +} + +static int sock_on = 1; + +int HTTPD_Server::bind(const char *host, int port){ + struct sockaddr_in me; + + if(!set_addr(&me, host, port)){ + if (host) + throw Exception("httpd.bind", 0, "can not resolve hostname \"%s\"", host); + me.sin_addr.s_addr=INADDR_ANY; + } + + int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/); + + if(sock < 0){ + int no=pa_socks_errno(); + throw Exception("httpd.bind", 0, "can not make socket: %s (%d)", pa_socks_strerr(no), no); + } + + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&sock_on, sizeof(sock_on)) || + setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&sock_on, sizeof(sock_on)) || + ::bind(sock, (struct sockaddr*)&me, sizeof(me)) || + listen(sock, 16)) { + close(sock); + int no = pa_socks_errno(); + throw Exception("httpd.bind", 0, "can not bind socket: %s (%d)", pa_socks_strerr(no), no); + } + return sock; +} + +int ready(int fd,int operation,int timeout_value){ + struct timeval timeout = {0, timeout_value * 1000}; + fd_set fds; + FD_ZERO(&fds); + FD_SET(fd, &fds); + switch (operation){ + case 0: return select(fd + 1, &fds, NULL, NULL, &timeout)>0; /* read */ + case 1: return select(fd + 1, NULL, &fds, NULL, &timeout)>0; /* write */ + default: return select(fd + 1, &fds, &fds, NULL, &timeout)>0; /* both */ + } +} + + +HTTPD_Connection *HTTPD_Server::accept(int sock, int timeout_value) { + + int ready = ::ready(sock, 0, timeout_value); + if (ready < 0) { + int no=pa_socks_errno(); + if(no == EINTR) + return NULL; + throw Exception("httpd.accept", 0, "error waiting for connection: %s (%d)", pa_socks_strerr(no), no); + } + if (ready == 0) { + /* Timeout */ + return NULL; + } + + struct sockaddr_in addr; + unsigned int sock_addr_len = sizeof(struct sockaddr_in); + memset(&addr, 0, sock_addr_len); + + int csock = ::accept(sock, (struct sockaddr *)&addr, &sock_addr_len); + if(csock == -1){ + int no=pa_socks_errno(); + throw Exception("httpd.accept", 0, "error accepting connection: %s (%d)", pa_socks_strerr(no), no); + } + + return new HTTPD_Connection(csock, pa_strdup(inet_ntoa(addr.sin_addr))); +} +