--- parser3/src/main/pa_http.C 2026/10/16 13:55:16 1.140 +++ parser3/src/main/pa_http.C 2026/10/26 19:06:51 1.145 @@ -12,12 +12,13 @@ #include "pa_request_charsets.h" #include "pa_vfile.h" #include "pa_random.h" +#include "pa_threads.h" #ifndef WIN32 #include // for TCP_NODELAY #endif -volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.140 2026/10/16 13:55:16 moko Exp $" IDENT_PA_HTTP_H; +volatile const char * IDENT_PA_HTTP_C="$Id: pa_http.C,v 1.145 2026/10/26 19:06:51 moko Exp $" IDENT_PA_HTTP_H; // defines @@ -57,7 +58,7 @@ void pa_log(const char* fmt, ...); ssize_t pa_send(int sock, const char *buffer, size_t len){ size_t total_sent = 0; while (total_sent < len) { - ssize_t bytes_sent=send(sock, buffer + total_sent, len - total_sent, 0); + ssize_t bytes_sent=pa_sock_send(sock, buffer + total_sent, len - total_sent); if (bytes_sent < 0) { return bytes_sent; } else if (bytes_sent == 0) { @@ -155,7 +156,7 @@ public: bool read(SOCKET sock, size_t size){ if(length + size > buf_size) resize(buf_size * 2 + size); - ssize_t received_size=recv(sock, buf + length, size, 0); + ssize_t received_size=pa_sock_recv(sock, buf + length, size); if(received_size == 0) return false; if(received_size < 0) { @@ -1110,7 +1111,7 @@ ssize_t HTTPD_request::pa_recv(SOCKET so #endif { ALARM(pa_httpd_timeout); - ssize_t result=recv(sockfd, buffer, len, 0); + ssize_t result=pa_sock_recv(sockfd, buffer, len); ALARM(0); if(result < 0 && sock_timed_out(pa_socks_errno())) { PA_RECV_TIMEOUT @@ -1161,6 +1162,11 @@ bool HTTPD_request::read_header(SOCKET s break; parse_headers(); + + if(length==body_offset){ // nothing but the headers (GET, websocket handshake), the buffer is no longer needed + buf=NULL; + length=body_offset=buf_size=0; + } return true; } } @@ -1184,7 +1190,8 @@ bool HTTPD_request::read_header(SOCKET s size_t HTTPD_request::read_post(SOCKET sock, char *body, size_t max_bytes) { size_t total_read = min(length - body_offset, max_bytes); - memcpy(body, buf + body_offset, total_read); + if(total_read) + memcpy(body, buf + body_offset, total_read); while (total_read < max_bytes){ ssize_t received_size = pa_recv(sock, body + total_read, max_bytes - total_read); @@ -1251,7 +1258,7 @@ String::C HTTPD_Connection::unprocessed( } size_t HTTPD_Connection::receive(char *dest, size_t max_bytes) { - ssize_t received_size=recv(sock, dest, max_bytes, 0); + ssize_t received_size=pa_sock_recv(sock, dest, max_bytes); if(received_size < 0) { int no=pa_socks_errno(); if(pa_socks_is_reset(no)) @@ -1274,6 +1281,7 @@ HTTPD_Connection::~HTTPD_Connection(){ if(sock != INVALID_SOCKET){ LOG(pa_log("httpd [%d] closed", sock)); closesocket(sock); + HTTPD_Server::track_connection(-1); } } @@ -1281,8 +1289,6 @@ bool HTTPD_Connection::accept(SOCKET ser int ready = pa_sock_ready(server_sock, false, timeout_value); if (ready < 0) { int no=pa_socks_errno(); - if(no == EINTR) - return false; throw Exception("httpd.accept", 0, "error waiting for connection: %s (%d)", pa_socks_strerr(no), no); } if (ready == 0) @@ -1297,6 +1303,7 @@ bool HTTPD_Connection::accept(SOCKET ser int no=pa_socks_errno(); throw Exception("httpd.accept", 0, "error accepting connection: %s (%d)", pa_socks_strerr(no), no); } + HTTPD_Server::track_connection(1); // taken back by the owner // Has no positive performance effect, requires include // static int sock_on=1; @@ -1312,6 +1319,13 @@ bool HTTPD_Connection::accept(SOCKET ser HTTPD_Server::HTTPD_MODE HTTPD_Server::mode = HTTPD_Server::SEQUENTIAL; const char *HTTPD_Server::port=NULL; +int HTTPD_Server::max_connections=INT_MAX; +int HTTPD_Server::connections=0; + +void HTTPD_Server::track_connection(int delta){ + SYNCHRONIZED; + connections+=delta; +} void HTTPD_Server::set_mode(const String &value){ if(value == "sequental") mode = SEQUENTIAL; @@ -1357,7 +1371,7 @@ SOCKET HTTPD_Server::bind(const char *ho 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)) { + listen(sock, 511)) { // same default as in apache/nginx closesocket(sock); int no = pa_socks_errno(); throw Exception("httpd.bind", 0, "cannot bind socket: %s (%d)", pa_socks_strerr(no), no);