--- parser3/src/main/pa_http.C 2020/10/10 06:08:36 1.84 +++ parser3/src/main/pa_http.C 2020/10/12 14:28:53 1.87 @@ -14,7 +14,7 @@ #include "pa_vfile.h" #include "pa_random.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; +volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.87 2020/10/12 14:28:53 moko Exp $" IDENT_PA_HTTP_H; #ifdef _MSC_VER #include @@ -48,7 +48,7 @@ volatile const char * IDENT_PA_HTTP_C="$ // helpers -bool ResponseHeaders::add_header(const char *line){ +bool HTTP_Headers::add_header(const char *line){ const char *value=strchr(line, ':'); if(value && value != line){ // we need only headers, not the response code @@ -103,7 +103,7 @@ public: size_t buf_size; size_t body_offset; - ResponseHeaders headers; + HTTP_Headers headers; const String &url; HTTP_response(const String& aurl) : buf(NULL), length(0), buf_size(0), body_offset(0), url(aurl){} @@ -884,8 +884,8 @@ File_read_http_result pa_internal_file_r if (!real_remote_charset) real_remote_charset=asked_remote_charset; // never null - for(Array_iterator i(response.headers.headers); i.has_next(); ){ - ResponseHeaders::Header header=i.next(); + for(Array_iterator i(response.headers.headers); i.has_next(); ){ + HTTP_Headers::Header header=i.next(); header.transcode(*real_remote_charset, r.charsets.source()); @@ -941,7 +941,8 @@ public: return str_upper(method_line, uri_start-method_line); } - void read_header(int sock); + void read_header(int); + size_t read_post(int, char *, size_t); }; enum HTTPD_request_state { @@ -989,9 +990,27 @@ void HTTPD_request::read_header(int sock } } +size_t HTTPD_request::read_post(int sock, char *body, size_t max_bytes) { + size_t total_read = min(length - body_offset, max_bytes); + memcpy(body, buf, total_read); + + while (total_read < max_bytes){ + ssize_t received_size = recv(sock, buf + total_read, max_bytes - total_read, 0); + if(received_size == 0) + return total_read; + if(received_size < 0) { + if(int no = pa_socks_errno()) + throw Exception("httpd.timeout", &url, "error receiving request body: %s (%d)", pa_socks_strerr(no), no); + return total_read; + } + total_read += received_size; + } + return total_read; +} + /* ********************************************************** */ -Array &HTTPD_Connection::headers() { +Array &HTTPD_Connection::headers() { return request->headers.headers; } @@ -1016,12 +1035,25 @@ void HTTPD_Connection::read_header(){ request->read_header(sock); } +size_t HTTPD_Connection::read_post(char *body, size_t max_bytes) { + return request->read_post(sock, body, max_bytes); +} + static int sock_on = 1; -int HTTPD_Server::bind(const char *host, int port){ +int HTTPD_Server::bind(const char *host_port){ struct sockaddr_in me; - if(!set_addr(&me, host, port)){ + const char *port = strchr(host_port, ':'); + const char *host = NULL; + if(port && port > host_port){ + host = pa_strdup(host_port, port - host_port); + port += 1; + } else { + port = host_port; + } + + if(!set_addr(&me, host, pa_atoui(port, 10))){ if (host) throw Exception("httpd.bind", 0, "can not resolve hostname \"%s\"", host); me.sin_addr.s_addr=INADDR_ANY; @@ -1057,9 +1089,7 @@ int ready(int fd,int operation,int timeo } } - 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();