--- parser3/src/main/pa_http.C 2020/10/10 09:05:42 1.85 +++ 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.85 2020/10/10 09:05:42 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 @@ -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,6 +990,24 @@ 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() { @@ -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();