From 29f03ae049441514296228f02eb15daf1cc093cc Mon Sep 17 00:00:00 2001 From: Jaybee- Date: Mon, 16 Jan 2012 08:23:39 +1300 Subject: [PATCH 1/3] When checking the result code, only look at the second token. This should be the numeric result code. This fixes a bug where an HTTP/1.0 server looks like it's failing all requests. --- httpserver.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httpserver.php b/httpserver.php index a5bb129..63f7591 100644 --- a/httpserver.php +++ b/httpserver.php @@ -92,7 +92,8 @@ public function exists($peer, $filename) } $this->log_warning($peer, 'HTTP Server Replied With: '.$http_response_header[0]); - if($http_response_header[0] != 'HTTP/1.1 200 OK') + $http_result=explode(" ",$http_response_header[0]); + if($http_result[1] != '200') return false; $this->contents = $contents; From 40debc2f100853fbea48b86230e4fdf9af1061e5 Mon Sep 17 00:00:00 2001 From: Jaybee- Date: Mon, 16 Jan 2012 09:31:52 +1300 Subject: [PATCH 2/3] Parts are copyright BTG. This project has nothing specific to do with bluebox; instructions on how to use it with bluebox are irrellevent in this project. --- httpproxyserver.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httpproxyserver.php b/httpproxyserver.php index 335cd2b..ae91d3c 100755 --- a/httpproxyserver.php +++ b/httpproxyserver.php @@ -3,6 +3,7 @@ /* * HTTP proxy TFTPServer example * + * Copyright (c) 2011 Business Technology Group http://www.btg.co.nz/ * Copyright (c) 2011 * * MIT License: @@ -77,7 +78,7 @@ public function get($peer, $filename, $mode) } if(count($_SERVER["argv"]) < 3) - die("Usage: {$_SERVER["argv"][0]} bind_ip http://webhost/bluebox/index.php/endpointmanager/config) [user] [debug] [foreground]\n"); + die("Usage: {$_SERVER["argv"][0]} bind_ip uri [user] [debug] [foreground]\n"); $debug = false; if(isset($_SERVER["argv"][4])) From c47c1d32414a3f2fd96076a4f1dd6cfc8c5ed89d Mon Sep 17 00:00:00 2001 From: Jaybee- Date: Tue, 17 Jan 2012 09:20:28 +1300 Subject: [PATCH 3/3] Better handling of http results (e.g. 302 redirect). Also log (debug) all headers. --- httpproxyserver.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/httpproxyserver.php b/httpproxyserver.php index ae91d3c..962fe7e 100755 --- a/httpproxyserver.php +++ b/httpproxyserver.php @@ -64,14 +64,29 @@ public function get($peer, $filename, $mode) $this->log_debug($peer, "Fetching URL $url"); $contents = @file_get_contents($url); + // note: $http_response_header is automatically populated. if($contents === false) { $this->log_warning($peer, "Failed to fetch $url"); return false; } - $this->log_debug($peer, "HTTP response line: {$http_response_header[0]}"); - if(!preg_match('/^HTTP\/1\.\d 200 .*$/', $http_response_header[0])) - return false; + foreach ($http_response_header AS $headerline) { + $this->log_debug($peer,"http header: $headerline"); + } + + // note: there may be more than one http response; e.g. a 302 first, then a 200. Get the last one. + $http_result=array_pop(preg_grep('/^HTTP\/\d+\.\d+ \d+ .*/',$http_response_header)); + if ($http_result===NULL) { + $this->log_error($peer,"HTTP did not contain a response: $http_response_header[0]"); + return false; + } + preg_match('/^HTTP\/\d+\.\d+ (\d+) .*/',$http_result,$result); + if (!in_array($result[1],array("200"))) { + $this->log_debug($peer,"HTTP response indicated failure: $result[0]"); + return false; + } + + $this->log_debug($peer, "HTTP response success: $result[0]"); return $contents; }