From 475157da0797cf154d0cf789bea64ee6c9bb274f Mon Sep 17 00:00:00 2001
From: Kevin Wojniak <kainjow@users.noreply.github.com>
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 t ype that reflects its use.
---
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 dacc2ac..faace73 100644
a
|
b
|
int32 PoorManServer::_Worker(void* data)
|
317 | 317 | /*true means the connection is already handled |
318 | 318 | *by the directory index generator in httpd_start_request(). |
319 | 319 | */ |
320 | | if (hc->file_address == (char*) 0) { |
321 | | static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->SetHits( |
322 | | static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->GetHits() + 1 |
323 | | ); |
324 | | hc->conn_fd = -1; |
| 320 | if (hc->processed_directory_index == 1) { |
| 321 | if (hc->method == METHOD_GET) { |
| 322 | static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->SetHits( |
| 323 | static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->GetHits() + 1 |
| 324 | ); |
| 325 | } |
325 | 326 | goto cleanup; |
326 | 327 | } |
327 | 328 | |
diff --git a/src/apps/poorman/libhttpd/libhttpd.c b/src/apps/poorman/libhttpd/libhttpd.c
index ce22eeb..ab52cc4 100644
a
|
b
|
httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc )
|
1767 | 1767 | hc->last_byte_index = -1; |
1768 | 1768 | hc->keep_alive = 0; |
1769 | 1769 | hc->should_linger = 0; |
1770 | | hc->file_address = (char*) 0; |
| 1770 | hc->processed_directory_index = 0; |
1771 | 1771 | return GC_OK; |
1772 | 1772 | } |
1773 | 1773 | |
… |
… |
httpd_close_conn( httpd_conn* hc, struct timeval* nowP )
|
2459 | 2459 | { |
2460 | 2460 | make_log_entry( hc, nowP ); |
2461 | 2461 | |
2462 | | if ( hc->file_address != (char*) 0 ) |
2463 | | { |
2464 | | //mmc_unmap( hc->file_address, &(hc->sb), nowP ); |
2465 | | hc->file_address = (char*) 0; |
2466 | | } |
2467 | 2462 | if ( hc->conn_fd >= 0 ) |
2468 | 2463 | { |
2469 | 2464 | (void) close( hc->conn_fd ); |
… |
… |
ls( httpd_conn* hc )
|
2713 | 2708 | send_mime( |
2714 | 2709 | hc, 200, ok200title, "", "", "text/html; charset=%s", (off_t) -1, |
2715 | 2710 | hc->sb.st_mtime ); |
| 2711 | httpd_write_response( hc ); |
2716 | 2712 | free(de); |
2717 | 2713 | } |
2718 | 2714 | else if ( hc->method == METHOD_GET ) |
… |
… |
ls( httpd_conn* hc )
|
2948 | 2944 | free(de); |
2949 | 2945 | return -1; |
2950 | 2946 | } |
| 2947 | hc->processed_directory_index = 1; |
2951 | 2948 | return 0; |
2952 | 2949 | } |
2953 | 2950 | |
… |
… |
if(hc->hs->do_list_dir){
|
3265 | 3262 | } |
3266 | 3263 | else |
3267 | 3264 | { |
3268 | | hc->file_address = (char*)1; |
3269 | 3265 | send_mime( |
3270 | 3266 | hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size, |
3271 | 3267 | hc->sb.st_mtime ); |
diff --git a/src/apps/poorman/libhttpd/libhttpd.h b/src/apps/poorman/libhttpd/libhttpd.h
index f820071..71ce5ce 100644
a
|
b
|
typedef struct {
|
145 | 145 | int should_linger; |
146 | 146 | struct stat sb; |
147 | 147 | int conn_fd; |
148 | | char* file_address; |
| 148 | int processed_directory_index; |
149 | 149 | } httpd_conn; |
150 | 150 | |
151 | 151 | /* Methods. */ |