Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions httpproxyserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/*
* HTTP proxy TFTPServer example
*
* Copyright (c) 2011 Business Technology Group http://www.btg.co.nz/
* Copyright (c) 2011 <mattias.wadman@gmail.com>
*
* MIT License:
Expand Down Expand Up @@ -63,21 +64,36 @@ 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;
}
}

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]))
Expand Down