diff --git "a/class-network\\Exception.php" "b/class-network\\Exception.php" new file mode 100644 index 0000000..227e6eb --- /dev/null +++ "b/class-network\\Exception.php" @@ -0,0 +1,21 @@ +. + +# Network +namespace network; + +class Exception extends \Exception {} diff --git "a/class-network\\HTTPRequest.php" "b/class-network\\HTTPRequest.php" index 072898c..2ab1fac 100644 --- "a/class-network\\HTTPRequest.php" +++ "b/class-network\\HTTPRequest.php" @@ -1,410 +1,452 @@ . # Network namespace network; use cli\Log; +use InvalidArgumentException; /** * HTTP request handler for GET and POST requests. */ class HTTPRequest { /** * Class version * * To be incremented every time do you notice this number. * * @var string */ const VERSION = 0.4; /** * Source code URL * * @var string */ const REPO = 'https://github.com/valerio-bozzolan/boz-mw'; /** * Wait seconds before each GET request. */ static $WAIT = 0.2; /** * Wait seconds before each POST request * * @var float */ static $WAIT_POST = 0.2; /** * Wait seconds before each lag * * @var float */ const WAIT_DOS = 5; /** * Full HTTP URL to the API endpoint * * @var string */ private $api; /** * HTTP GET/POST data * * @var array */ private $data; /** * Internal arguments * * @param array */ private $args; /** * Latest HTTP response headers * * @var array */ private $latestHttpResponseHeaders = []; + /** + * Latest HTTP error status code + * + * @var Status + */ + private $latestHttpResponseStatus; + /** * HTTP cookies * * @var array */ private $cookies = []; /** * Constructor. * * @param $api string API endpoint * @param $args array Internal arguments */ public function __construct( $api, $args = [] ) { $this->api = $api; $this->setArgs( $args ); } /** * Statical constructor. * * @return network\HTTPRequest */ public static function factory( $api, $args = [] ) { return new self( $api, $args ); } /** * Get internal arguments. * * @return array */ public function getArgs() { return $this->args; } /** * Set internal arguments * * method: GET, POST etc. * user-agent: HTTP user agent * sensitive: flag to indicate if the data is sensitive and should not be printed as usual in the log * wait: microseconds to wait after every GET request * wait-post: microseconds to wait after every POST request * headers: HTTP headers * * @param $args array Internal arguments * @return self */ public function setArgs( $args ) { $this->args = array_replace( [ 'method' => 'GET', 'user-agent' => sprintf( 'boz-mw HTTPRequest.php/%s %s', self::VERSION, self::REPO ), 'sensitive' => false, 'wait' => self::$WAIT, 'wait-post' => self::$WAIT_POST, 'headers' => [], ], $args ); return $this; } /** * Make an HTTP GET query * * @param $data array GET data * @param $args array Internal arguments * @return mixed Response */ public function fetch( $data = [], $args = [] ) { $args = array_replace( $this->args, $args ); // Eventually post-process the data before using $data = static::onDataReady( $data ); // HTTP query using file_get_contents() $url = $this->api; $query = http_build_query( $data ); $context = [ 'http' => [] ]; $context['http']['method'] = $args['method']; if( $args['user-agent'] ) { $args['headers'][] = self::header( 'User-Agent', $args['user-agent'] ); } if( $this->haveCookies() ) { $args['headers'][] = $this->getCookieHeader(); } if( $query ) { if( 'POST' === $args['method'] ) { $args['headers'][] = self::header('Content-Type', 'application/x-www-form-urlencoded' ); $context['http']['content'] = $query; } else { $url .= "?$query"; } } if( 'GET' === $args['method'] ) { Log::debug( "GET $url" ); } else { Log::sensitive( "{$args['method']} $url $query", "{$args['method']} $url" ); } if( $args['headers'] ) { $context['http']['header'] = self::implodeHTTPHeaders( $args['headers'] ); } if( $args['wait'] ) { Log::debug( sprintf( "Waiting %.2f s.", $args['wait'] ) ); usleep( $args['wait'] * 1000000 ); } $stream_context = stream_context_create( $context ); $response = file_get_contents( $url, false, $stream_context ); + // Here $http_response_header exists magically (PHP merda!) if( ! isset( $http_response_header ) ) { throw new Exception( 'undefined http_response_header variable: wrong request?' ); } - - var_dump( $http_response_header );exit; - - // Here $http_response_header exists magically (PHP merda!) $this->loadHTTPResponseHeaders( $http_response_header ); - if( isset( $this->latestHttpResponseHeaders['HTTP/1.1 500 Internal Server Error'] ) ) { - Log::warn( "500 Internal Server Error!" ); - $args = array_replace( [ - 'wait' => self::WAIT_DOS - ], $args ); - return $this->fetch( $data , $args ); + // Check the HTTP status + $status = $this->getLatestHTTPResponseStatus(); + if( ! $status->isOK() ) { + if( $status->isClientError() ) { + Log::warn( sprintf( + "waiting because of HTTP status %s: %s", + $status->getCode(), + $status->getMessage() + ) ); + $args = array_replace( [ + 'wait' => self::WAIT_DOS + ], $args ); + return $this->fetch( $data, $args ); + } + throw new NotOKException( $status ); } - Log::debug( "Fetched" ); + Log::debug( $status->getHeader() ); return static::onFetched( $response, $data ); } /** * Effectuate an HTTP POST. * * @param $data array POST data * @param $args Internal arguments * @return mixed Response */ public function post( $data = [], $args = [] ) { $args = array_replace( [ 'wait' => $this->args['wait-post'] ], $args, [ 'method' => 'POST' ] ); return $this->fetch( $data, $args ); } /** * Get latest HTTP response headers. * * @return array */ public function getLatestHTTPResponseHeaders() { return $this->latestHttpResponseHeaders; } + /** + * Get latest HTTP status + * + * @return Status + */ + private function getLatestHTTPResponseStatus() { + return $this->latestHttpResponseStatus; + } + /** * Check if cookies are set * * @return bool */ public function haveCookies() { return isset( $this->cookies ); } /** * Get cookies * * @return array */ public function getCookies() { return $this->cookies; } /** * Set an HTTP cookie name => value pair. * * @param $name string Cookie name * @param $value string Cookie value * @return self */ public function setCookie( $name, $value ) { $this->cookies[ $name ] = $value; return $this; } public function getCookieHeader() { $cookies = []; foreach( $this->getCookies() as $name => $value ) { $cookies[] = urlencode( $name ) . '=' . urlencode( $value ); } return self::headerRaw( 'Cookie', implode( '; ', $cookies ) ); } public function getHTTPCookies() { return $this->cookies; } /** * Set an HTTP cookie in its raw form. * * @param $cookie string Cookie text */ private function setRawCookie( $cookie ) { $parts = explode(';', $cookie); if( isset( $parts[0] ) ) { $name_value = explode('=', $parts[0] ); if( 2 === count( $name_value ) ) { list( $name, $value ) = $name_value; $this->setCookie( $name, $value ); } } } /** * Load HTTP response headers, filling cookies. */ private function loadHTTPResponseHeaders( $http_response_headers ) { - $this->latestHttpResponseHeaders = self::parseHTTPResponseHeaders( $http_response_headers ); + list( $this->latestHttpResponseHeaders, $this->latestHttpResponseStatus ) + = self::parseHTTPResponseHeaders( $http_response_headers ); + if( isset( $this->latestHttpResponseHeaders['Set-Cookie'] ) ) { foreach( $this->latestHttpResponseHeaders['Set-Cookie'] as $cookie ) { $this->setRawCookie( $cookie ); } } } + /** + * Implode HTTP headers + * + * @param $headers array + * @return string + */ private function implodeHTTPHeaders( $headers ) { if( ! $headers ) { return null; } $s = ''; foreach( $headers as $header ) { $s .= "$header\r\n"; } return $s; } /** - * Group HTTP headers by keys. + * Group HTTP headers by keys and get the HTTP Status. * - * @return array + * @param $http_response_headers array + * @return array The first element contains an associative array of header name and value(s). The second one contains the Status. */ private static function parseHTTPResponseHeaders( $http_response_headers ) { + $status = null; $headers = []; foreach( $http_response_headers as $header ) { // Check if it's an header like 'Foo: bar' $header_parts = explode(':', $header, 2); if( 2 === count( $header_parts ) ) { list( $name, $value ) = $header_parts; if( ! isset( $headers[ $name ] ) ) { $headers[ $name ] = []; } $headers[ $name ][] = ltrim( $value ); } else { - $headers[ $header ] = true; + try { + $status = Status::createFromHeader( $header ); + } catch( InvalidArgumentException $e ) { + $headers[ $header ] = true; + } } } - return $headers; + + if( null === $status ) { + throw new \Exception( 'HTTP response without an HTTP status code' ); + } + + return [ $headers, $status ]; } /** * Make an HTTP header raw string from a name => value pair. * * The value will be URL-encoded. * * @param $name string HTTP header name * @param $value string HTTP header value * @return string HTTP header */ private static function header( $name, $value ) { return self::headerRaw( $name, urlencode( $value ) ); } /** * Make an HTTP header raw string from a name => value pair. * * The value is not URL-encoded. * */ private static function headerRaw( $name, $value ) { return sprintf( '%s: %s', $name, $value ); } /** * Can be overrided to post-process the data before its use * * @param $data GET/POST data * @return array GET/POST data post-processed */ protected function onDataReady( $data ) { return $data; } /** * Callback to be overloaded * * This is called every time something is fetched. * * @param $response mixed Response * @param $request_data mixed GET/POST data * @return mixed Response */ protected function onFetched( $response, $request_data ) { return $response; } } diff --git "a/class-network\\NotOKException.php" "b/class-network\\NotOKException.php" new file mode 100644 index 0000000..48a661b --- /dev/null +++ "b/class-network\\NotOKException.php" @@ -0,0 +1,53 @@ +. + +namespace network; + +/** + * A class for whatever HTTP status that is not 200 + */ +class NotOKException extends Exception { + + /** + * HTTP status + * + * @var Status + */ + private $httpStatus; + + /** + * Constructor + * + * @param $http_status object HTTP status + * @param $code int + * @param $previous object + */ + public function __construct( $http_status, $code = 0, Exception $previous = null ) { + $this->httpStatus = $http_status; + parent::__construct( $http_status->getHeader(), $code, $previous ); + } + + /** + * Get the HTTP status object + * + * @return Status + */ + public function getHTTPStatus() { + return $this->httpStatus; + } + +} diff --git "a/class-network\\Status.php" "b/class-network\\Status.php" new file mode 100644 index 0000000..c8f5354 --- /dev/null +++ "b/class-network\\Status.php" @@ -0,0 +1,161 @@ +. + +# Network +namespace network; + +use InvalidArgumentException; + +/** + * HTTP status + */ +class Status { + + /** + * HTTP status code + * + * @var string + */ + private $code; + + /** + * HTTP status message + * + * @var string + */ + private $message; + + /** + * HTTP status class + * + * @var string + */ + private $class; + + /** + * HTTP status class + * + * @var string + */ + private $header; + + /** + * Constructor + * + * @param $code string e.g. '200' + * @param $message string e.g. 'OK' + * @param $class string e.g. '2' + * @param $header string e.g. 'HTTP/1.1 200 OK' + */ + public function __construct( $code, $message, $class, $header ) { + $this->code = $code; + $this->message = $message; + $this->class = $class; + $this->header = $header; + } + + /** + * Create an HTTP status object from an header + * + * @param $header string e.g. 'HTTP/1.1 200 OK' + * @return self + */ + public static function createFromHeader( $header ) { + preg_match( '/^HTTP\/[0-9.]* (([0-9])[0-9]+) (.*)$/', $header, $matches ); + if( 4 === count( $matches ) ) { + list( , $code, $class, $message ) = $matches; + } else { + throw new InvalidArgumentException( 'not an HTTP status' ); + } + return new self( $code, $message, $class, $header ); + } + + /** + * Get the HTTP status code + * + * @return string + */ + public function getCode() { + return $this->code; + } + + /** + * Get the HTTP status message + * + * @return string + */ + public function getMessage() { + return $this->message; + } + + /** + * Get the HTTP status class + * + * @return string + */ + public function getClass() { + return $this->class; + } + + /** + * Get the HTTP status header + * + * @return string + */ + public function getHeader() { + return $this->header; + } + + /** + * Is this HTTP status code? + * + * @param $code string|int HTTP status code + * @return bool + */ + public function isCode( $code ) { + return $this->getCode() == $code; + } + + /** + * Is this HTTP status class? + * + * @param $class string|int HTTP status class + * @return bool + */ + public function isClass( $class ) { + return $this->getClass() == $class; + } + + /** + * Is the HTTP status 200 OK? + * + * @return boolean + */ + public function isOK() { + return $this->isCode( 200 ); + } + + /** + * Is the HTTP class 5? + * + * @return boolean + */ + public function isClientError() { + return $this->isClass( 5 ); + } + +}