--- parser3/src/main/pa_http.C 2020/10/12 16:58:55 1.88 +++ parser3/src/main/pa_http.C 2020/10/14 00:07:42 1.92 @@ -14,10 +14,11 @@ #include "pa_vfile.h" #include "pa_random.h" -volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.88 2020/10/12 16:58:55 moko Exp $" IDENT_PA_HTTP_H; +volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.92 2020/10/14 00:07:42 moko Exp $" IDENT_PA_HTTP_H; #ifdef _MSC_VER #include +#define socklen_t int #else #define closesocket close #endif @@ -130,11 +131,11 @@ public: } size_t first_line(){ - char *headers=strchr(buf, '\n'); - if(!headers) + char *header=strchr(buf, '\n'); + if(!header) return false; - return headers-buf; + return header-buf; } const char *status_code(char *status_line, int &result){ @@ -897,7 +898,7 @@ File_read_http_result pa_internal_file_r } // filling $.cookies - if(Value *vcookies=vtables->hash().get("SET-COOKIE")) + if(vcookies=vtables->hash().get("SET-COOKIE")) result.headers->put(HTTP_COOKIES_NAME, new VTable(parse_cookies(r, vcookies->get_table()))); // output response @@ -967,7 +968,14 @@ void HTTPD_request::read_header(int sock char *method_line = pa_strdup(buf, method_size); method = extract_method(method_line); - if(!method || strcmp(method, "GET") && strcmp(method, "HEAD") && strcmp(method, "POST") && strcmp(method, "PUT") && strcmp(method, "DELETE")) + if(!method || + strcmp(method, "GET") && + strcmp(method, "HEAD") && + strcmp(method, "POST") && + strcmp(method, "PUT") && + strcmp(method, "DELETE") && + strcmp(method, "PATCH") + ) throw Exception("httpd.method", new String(method ? method : method_line), "invalid request method"); state = HTTPD_HEADERS; } @@ -1036,10 +1044,22 @@ void HTTPD_Connection::read_header(){ request->read_header(sock); } +void HTTPD_Connection::close(){ + ::closesocket(sock); +} + size_t HTTPD_Connection::read_post(char *body, size_t max_bytes) { return request->read_post(sock, body, max_bytes); } +size_t HTTPD_Connection::send_body(const void *buf, size_t size) { + if(send(sock, (const char*)buf, size, 0) != (ssize_t)size) { + int no=pa_socks_errno(); + throw Exception("httpd.timeout", 0, "error sending response: %s (%d)", pa_socks_strerr(no), no); + } + return size; +} + static int sock_on = 1; int HTTPD_Server::bind(const char *host_port){ @@ -1071,7 +1091,7 @@ int HTTPD_Server::bind(const char *host_ setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&sock_on, sizeof(sock_on)) || ::bind(sock, (struct sockaddr*)&me, sizeof(me)) || listen(sock, 16)) { - close(sock); + closesocket(sock); int no = pa_socks_errno(); throw Exception("httpd.bind", 0, "can not bind socket: %s (%d)", pa_socks_strerr(no), no); } @@ -1104,7 +1124,7 @@ HTTPD_Connection *HTTPD_Server::accept(i } struct sockaddr_in addr; - unsigned int sock_addr_len = sizeof(struct sockaddr_in); + socklen_t sock_addr_len = sizeof(struct sockaddr_in); memset(&addr, 0, sock_addr_len); int csock = ::accept(sock, (struct sockaddr *)&addr, &sock_addr_len);