From 4d8811742fa447ec05b4993a16a0931bc29aafab Mon Sep 17 00:00:00 2001 From: Kevin Wojniak Date: Sun, 26 Feb 2017 12:49:23 +0000 Subject: [PATCH] Fix client hang when HEAD request is sent to PoorMan If a HEAD request was sent to PoorMan, for example from curl ("curl --HEAD http://x.x.x.x") then the client would hang due to the connection never being closed. In PoorManServer::_Worker, after httpd_start_request() is called, a null file_address is used to detect when libhttpd has already sent a directory listing. In this situation, PoorMan assumes libhttpd already fully handled the request. However httpd_start_request() didn't properly set this flag for HEAD requests. In the if block for a null file_address, the file descriptor was incorrectly invalidated, which prevented the connection from closing. Fixing this revealed two more bugs. The first is libhttpd was not actually sending the http headers for HEAD directory listing requests. The second is PoorManServer would increment its hit count for HEAD directory listing requests. This change also refactors file_address to a more sensible name and type that reflects its use. Signed-off-by: Adrien Destugues Fixes #13347. --- src/apps/poorman/PoorManServer.cpp | 11 ++++++----- src/apps/poorman/libhttpd/libhttpd.c | 10 +++------- src/apps/poorman/libhttpd/libhttpd.h | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/apps/poorman/PoorManServer.cpp b/src/apps/poorman/PoorManServer.cpp index dacc2ac013..faace730e4 100644 --- a/src/apps/poorman/PoorManServer.cpp +++ b/src/apps/poorman/PoorManServer.cpp @@ -317,11 +317,12 @@ int32 PoorManServer::_Worker(void* data) /*true means the connection is already handled *by the directory index generator in httpd_start_request(). */ - if (hc->file_address == (char*) 0) { - static_cast(be_app)->GetPoorManWindow()->SetHits( - static_cast(be_app)->GetPoorManWindow()->GetHits() + 1 - ); - hc->conn_fd = -1; + if (hc->processed_directory_index == 1) { + if (hc->method == METHOD_GET) { + static_cast(be_app)->GetPoorManWindow()->SetHits( + static_cast(be_app)->GetPoorManWindow()->GetHits() + 1 + ); + } goto cleanup; } diff --git a/src/apps/poorman/libhttpd/libhttpd.c b/src/apps/poorman/libhttpd/libhttpd.c index ce22eebb10..ab52cc43b1 100644 --- a/src/apps/poorman/libhttpd/libhttpd.c +++ b/src/apps/poorman/libhttpd/libhttpd.c @@ -1767,7 +1767,7 @@ httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc ) hc->last_byte_index = -1; hc->keep_alive = 0; hc->should_linger = 0; - hc->file_address = (char*) 0; + hc->processed_directory_index = 0; return GC_OK; } @@ -2459,11 +2459,6 @@ httpd_close_conn( httpd_conn* hc, struct timeval* nowP ) { make_log_entry( hc, nowP ); - if ( hc->file_address != (char*) 0 ) - { - //mmc_unmap( hc->file_address, &(hc->sb), nowP ); - hc->file_address = (char*) 0; - } if ( hc->conn_fd >= 0 ) { (void) close( hc->conn_fd ); @@ -2713,6 +2708,7 @@ ls( httpd_conn* hc ) send_mime( hc, 200, ok200title, "", "", "text/html; charset=%s", (off_t) -1, hc->sb.st_mtime ); + httpd_write_response( hc ); free(de); } else if ( hc->method == METHOD_GET ) @@ -2948,6 +2944,7 @@ ls( httpd_conn* hc ) free(de); return -1; } + hc->processed_directory_index = 1; return 0; } @@ -3265,7 +3262,6 @@ if(hc->hs->do_list_dir){ } else { - hc->file_address = (char*)1; send_mime( hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size, hc->sb.st_mtime ); diff --git a/src/apps/poorman/libhttpd/libhttpd.h b/src/apps/poorman/libhttpd/libhttpd.h index f820071fd5..71ce5ce689 100644 --- a/src/apps/poorman/libhttpd/libhttpd.h +++ b/src/apps/poorman/libhttpd/libhttpd.h @@ -145,7 +145,7 @@ typedef struct { int should_linger; struct stat sb; int conn_fd; - char* file_address; + int processed_directory_index; } httpd_conn; /* Methods. */