Skip to content

Commit

Permalink
API CHANGE Removed old HTTP::sendRequest() and HTTP::sendPostRequest()
Browse files Browse the repository at this point in the history
functions which are sparsely used, and not maintained or tested. Use custom code instead.
  • Loading branch information
Sean Harvey committed May 10, 2012
1 parent 7f4c6e3 commit 8a46e38
Showing 1 changed file with 5 additions and 71 deletions.
76 changes: 5 additions & 71 deletions control/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
*/
class HTTP {

static $userName;
static $password;
protected static $cache_age = 0;

protected static $modification_date = null;

protected static $etag = null;

/**
* Turns a local system filename into a URL by comparing it to the script filename
Expand Down Expand Up @@ -224,75 +227,6 @@ static function loadMimeTypes() {
return $mimeData;
}

/**
* Send an HTTP request to the host.
*
* @return String Response text
*/
static function sendRequest( $host, $path, $query, $port = 80 ) {
$socket = fsockopen( $host, $port, $errno, $error );

if( !$socket )
return $error;

if( $query )
$query = '?' . $query;

if( self::$userName && self::$password ) {
$auth = "Authorization: Basic " . base64_encode( self::$userName . ':' . self::$password ) . "\r\n";
} else {
$auth = '';
}

$request = "GET {$path}{$query} HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n{$auth}\r\n";

fwrite( $socket, $request );
$response = stream_get_contents( $socket );

return $response;
}

/**
* Send a HTTP POST request through fsockopen().
*
* @param string $host Absolute URI without path, e.g. http://silverstripe.com
* @param string $path Path with leading slash
* @param array|string $data Payload for the request
* @param string $name Parametername for the payload (only if passed as a string)
* @param string $query
* @param string $port
* @return string Raw HTTP-result including headers
*/
static function sendPostRequest($host, $path, $data, $name = null, $query = '', $port = 80, $getResponse = true) {
$socket = fsockopen($host, $port, $errno, $error);

if(!$socket)
return $error;

if(self::$userName && self::$password)
$auth = "Authorization: Basic " . base64_encode(self::$userName . ':' . self::$password) . "\r\n";
else
$auth = '';

if($query)
$query = '?' . $query;

$dataStr = (is_array($data)) ? http_build_query($data) : $name . '=' . urlencode($data);
$request = "POST {$path}{$query} HTTP/1.1\r\nHost: $host\r\n{$auth}Content-Type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($dataStr) . "\r\n\r\n";
$request .= $dataStr . "\r\n\r\n";

fwrite($socket, $request);

if($getResponse){
$response = stream_get_contents($socket);
return $response;
}

}

protected static $cache_age = 0, $modification_date = null;
protected static $etag = null;

/**
* Set the maximum age of this page in web caches, in seconds
*/
Expand Down

0 comments on commit 8a46e38

Please sign in to comment.