Class HttpSocket
Cake network socket connection class.
Core base class for HTTP network communication.
- Object
- CakeSocket
- HttpSocket
Copyright: Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
License: The MIT License
Location: http_socket.php
Properties summary
-
$config
publicarray
Default configuration settings for the HttpSocket -
$description
publicstring
Object description -
$lineBreak
publicstring
String that represents a line break. -
$quirksMode
publicboolean
When one activates the $quirksMode by setting it to true, all checks meant to enforce RFC 2616 (HTTP/1.1 specs) will be disabled and additional measures to deal with non-standard responses will be enabled.
-
$request
publicarray
The default values to use for a request -
$response
publicarray
The default structure for storing the response
Inherited Properties
Method Summary
-
__construct() public
Build an HTTP Socket using the specified configuration. -
__tokenEscapeChars() public
Gets escape chars according to RFC 2616 (HTTP 1.1 specs). -
buildCookies() public
undocumented function -
buildHeader() public
Builds the header. -
buildRequestLine() public
Builds a request line according to HTTP/1.1 specs. Activate quirks mode to work outside specs. -
buildUri() public
Takes a $uri array and turns it into a fully qualified URL string -
configUri() public
Parses and sets the specified URI into current request configuration. -
decodeBody() public
Generic function to decode a $body with a given $encoding. Returns either an array with the keys 'body' and 'header' or false on failure.
-
decodeChunkedBody() public
Decodes a chunked message $body and returns either an array with the keys 'body' and 'header' or false as a result.
-
delete() public
Issues a DELETE request to the specified URI, query, and request. -
escapeToken() public
Escapes a given $token according to RFC 2616 (HTTP 1.1 specs) -
get() public
Issues a GET request to the specified URI, query, and request. -
httpSerialize() public
Serializes an array for transport. -
loadCookies() public
undocumented function -
parseCookies() public
undocumented function -
parseHeader() public
Parses an array based header. -
parseQuery() public
This function can be thought of as a reverse to PHP5's http_build_query(). It takes a given query string and turns it into an array and supports nesting by using the php bracket syntax. So this menas you can parse queries like:
-
parseResponse() public
Parses the given message and breaks it down in parts. -
parseUri() public
Parses the given URI and breaks it down into pieces as an indexed array with elements such as 'scheme', 'port', 'query'.
-
post() public
Issues a POST request to the specified URI, query, and request. -
put() public
Issues a PUT request to the specified URI, query, and request. -
request() public
Issue the specified request. -
reset() public
Resets the state of this HttpSocket instance to it's initial state (before Object::__construct got executed) or does the same thing partially for the request and the response property only.
-
saveCookies() public
undocumented function -
unescapeToken() public
Unescapes a given $token according to RFC 2616 (HTTP 1.1 specs) -
url() public
undocumented function
Method Detail
__construct() public ¶
__construct( array $config = array() )
Build an HTTP Socket using the specified configuration.
Parameters
- array $config optional array()
- Configuration
Overrides
__tokenEscapeChars() public ¶
__tokenEscapeChars( boolean $hex = true , $chars = null )
Gets escape chars according to RFC 2616 (HTTP 1.1 specs).
Parameters
- boolean $hex optional true
- true to get them as HEX values, false otherwise
- $chars optional null
Returns
Escape chars
buildCookies() public ¶
buildCookies( unknown $cookies )
undocumented function
Parameters
- unknown $cookies
buildHeader() public ¶
buildHeader( array $header , $mode = 'standard' )
Builds the header.
Parameters
- array $header
- Header to build
- $mode optional 'standard'
Returns
Header built from array
buildRequestLine() public ¶
buildRequestLine( array $request = array() , string $versionToken = 'HTTP/1.1' )
Builds a request line according to HTTP/1.1 specs. Activate quirks mode to work outside specs.
Parameters
- array $request optional array()
- Needs to contain a 'uri' key. Should also contain a 'method' key, otherwise defaults to GET.
- string $versionToken optional 'HTTP/1.1'
- The version token to use, defaults to HTTP/1.1
Returns
Request line
buildUri() public ¶
buildUri( array $uri = array() , string $uriTemplate = '%scheme://%user:%pass@%host:%port/%path?%query#%fragment' )
Takes a $uri array and turns it into a fully qualified URL string
Parameters
- array $uri optional array()
- A $uri array, or uses $this->config if left empty
- string $uriTemplate optional '%scheme://%user:%pass@%host:%port/%path?%query#%fragment'
- The Uri template/format to use
Returns
A fully qualified URL formated according to $uriTemplate
configUri() public ¶
configUri( mixed $uri = null )
Parses and sets the specified URI into current request configuration.
Parameters
- mixed $uri optional null
- URI (see
HttpSocket::parseUri()
)
Returns
Current configuration settings
decodeBody() public ¶
decodeBody( string $body , mixed $encoding = 'chunked' )
Generic function to decode a $body with a given $encoding. Returns either an array with the keys 'body' and 'header' or false on failure.
Parameters
- string $body
- A string continaing the body to decode
- mixed $encoding optional 'chunked'
- Can be false in case no encoding is being used, or a string representing the encoding
Returns
Array or false
decodeChunkedBody() public ¶
decodeChunkedBody( string $body )
Decodes a chunked message $body and returns either an array with the keys 'body' and 'header' or false as a result.
Parameters
- string $body
- A string continaing the chunked body to decode
Returns
Array or false
delete() public ¶
delete( mixed $uri = null , array $data = array() , array $request = array() )
Issues a DELETE request to the specified URI, query, and request.
Parameters
- mixed $uri optional null
- URI to request (see
HttpSocket::parseUri()
) - array $data optional array()
- $query Query to append to URI
- array $request optional array()
- An indexed array with indexes such as 'method' or uri
Returns
Result of request
escapeToken() public ¶
escapeToken( string $token , $chars = null )
Escapes a given $token according to RFC 2616 (HTTP 1.1 specs)
Parameters
- string $token
- Token to escape
- $chars optional null
Returns
Escaped token
get() public ¶
get( mixed $uri = null , array $query = array() , array $request = array() )
Issues a GET request to the specified URI, query, and request.
Parameters
- mixed $uri optional null
- URI to request (see
HttpSocket::parseUri()
) - array $query optional array()
- Query to append to URI
- array $request optional array()
- An indexed array with indexes such as 'method' or uri
Returns
Result of request
httpSerialize() public ¶
httpSerialize( array $data = array() )
Serializes an array for transport.
Parameters
- array $data optional array()
- Data to serialize
Returns
Serialized variable
parseCookies() public ¶
parseCookies( unknown $header )
undocumented function
Parameters
- unknown $header
parseHeader() public ¶
parseHeader( array $header )
Parses an array based header.
Parameters
- array $header
- Header as an indexed array (field => value)
Returns
Parsed header
parseQuery() public ¶
parseQuery( mixed $query )
This function can be thought of as a reverse to PHP5's http_build_query(). It takes a given query string and turns it into an array and supports nesting by using the php bracket syntax. So this menas you can parse queries like:
- ?key[subKey]=value
- ?key[]=value1&key[]=value2
A leading '?' mark in $query is optional and does not effect the outcome of this function. For the complete capabilities of this implementation take a look at HttpSocketTest::testParseQuery()
Parameters
- mixed $query
- A query string to parse into an array or an array to return directly "as is"
Returns
The $query parsed into a possibly multi-level array. If an empty $query is given, an empty array is returned.
parseResponse() public ¶
parseResponse( string $message )
Parses the given message and breaks it down in parts.
Parameters
- string $message
- Message to parse
Returns
Parsed message (with indexed elements such as raw, status, header, body)
parseUri() public ¶
parseUri( string $uri = null , mixed $base = array() )
Parses the given URI and breaks it down into pieces as an indexed array with elements such as 'scheme', 'port', 'query'.
Parameters
- string $uri optional null
- URI to parse
- mixed $base optional array()
- If true use default URI config, otherwise indexed array to set 'scheme', 'host', 'port', etc.
Returns
Parsed URI
post() public ¶
post( mixed $uri = null , array $data = array() , array $request = array() )
Issues a POST request to the specified URI, query, and request.
Parameters
- mixed $uri optional null
- URI to request (see
HttpSocket::parseUri()
) - array $data optional array()
- $query Query to append to URI
- array $request optional array()
- An indexed array with indexes such as 'method' or uri
Returns
Result of request
put() public ¶
put( mixed $uri = null , array $data = array() , array $request = array() )
Issues a PUT request to the specified URI, query, and request.
Parameters
- mixed $uri optional null
- URI to request (see
HttpSocket::parseUri()
) - array $data optional array()
- $query Query to append to URI
- array $request optional array()
- An indexed array with indexes such as 'method' or uri
Returns
Result of request
request() public ¶
request( mixed $request = array() )
Issue the specified request.
Parameters
- mixed $request optional array()
- Either an URI string, or an array defining host/uri
Returns
false on error, request body on success
reset() public ¶
reset( boolean $full = true )
Resets the state of this HttpSocket instance to it's initial state (before Object::__construct got executed) or does the same thing partially for the request and the response property only.
Parameters
- boolean $full optional true
- If set to false only HttpSocket::response and HttpSocket::request are reseted
Returns
True on success
Overrides
unescapeToken() public ¶
unescapeToken( string $token , $chars = null )
Unescapes a given $token according to RFC 2616 (HTTP 1.1 specs)
Parameters
- string $token
- Token to unescape
- $chars optional null
Returns
Unescaped token
Methods inherited from CakeSocket
address() public ¶
address( )
Get the IP address of the current connection.
Returns
IP address
addresses() public ¶
addresses( )
Get all IP addresses associated with the current connection.
Returns
IP addresses
disconnect() public ¶
disconnect( )
Disconnect the socket from the current connection.
Returns
Success
read() public ¶
read( integer $length = 1024 )
Read data from the socket. Returns false if no data is available or no connection could be established.
Parameters
- integer $length optional 1024
- Optional buffer length to read; defaults to 1024
Returns
Socket data
setLastError() public ¶
setLastError( integer $errNum , string $errStr )
Set the last error.
Parameters
- integer $errNum
- Error code
- string $errStr
- Error string
Methods inherited from Object
Object() public ¶
Object( )
A hack to support __construct() on PHP 4 Hint: descendant classes have no PHP4 class_name() constructors, so this constructor gets called first and calls the top-layer __construct() which (if present) should call parent::__construct()
Returns
__openPersistent() public ¶
__openPersistent( string $name , string $type = null )
Open the persistent class file for reading Used by Object::_persist()
Parameters
- string $name
- Name of persisted class
- string $type optional null
- Type of persistance (e.g: registry)
_persist() public ¶
_persist( string $name , string $return , $object , $type = null )
Checks for a persistent class file, if found file is opened and true returned If file is not found a file is created and false returned If used in other locations of the model you should choose a unique name for the persistent file There are many uses for this method, see manual for examples
Parameters
- string $name
- name of the class to persist
- string $return
- $object the object to persist
- $object
- $type optional null
Returns
Success
_savePersistent() public ¶
_savePersistent( string $name , object $object )
You should choose a unique name for the persistent file
There are many uses for this method, see manual for examples
Parameters
- string $name
- name used for object to cache
- object $object
- the object to persist
Returns
true on save, throws error if file can not be created
_set() public ¶
_set( array $properties = array() )
Allows setting of multiple properties of the object in a single line of code.
Parameters
- array $properties optional array()
- An associative array containing properties and corresponding values.
_stop() public ¶
_stop( $status = 0 )
Stop execution of the current script
Parameters
- $status optional 0
- http://php.net/exit for values
cakeError() public ¶
cakeError( string $method , array $messages = array() )
Used to report user friendly errors. If there is a file app/error.php or app/app_error.php this file will be loaded error.php is the AppError class it should extend ErrorHandler class.
Parameters
- string $method
- Method to be called in the error class (AppError or ErrorHandler classes)
- array $messages optional array()
- Message that is to be displayed by the error class
Returns
message
dispatchMethod() public ¶
dispatchMethod( string $method , array $params = array() )
Calls a method on this object with the given parameters. Provides an OO wrapper for call_user_func_array, and improves performance by using straight method calls in most cases.
Parameters
- string $method
- Name of the method to call
- array $params optional array()
- Parameter list to use when calling $method
Returns
Returns the result of the method call
log() public ¶
log( string $msg , integer $type = LOG_ERROR )
API for logging events.
Parameters
- string $msg
- Log message
- integer $type optional LOG_ERROR
- Error type constant. Defined in app/config/core.php.
Returns
Success of log write
requestAction() public ¶
requestAction( mixed $url , array $extra = array() )
Calls a controller's method from any location.
Parameters
- mixed $url
- String or array-based url.
- array $extra optional array()
- if array includes the key "return" it sets the AutoRender to true.
Returns
Boolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.
toString() public ¶
toString( )
Object-to-string conversion. Each class can override this method as necessary.
Returns
The name of this class
Properties detail
$config ¶
Default configuration settings for the HttpSocket
array( 'persistent' => false, 'host' => 'localhost', 'protocol' => 'tcp', 'port' => 80, 'timeout' => 30, 'request' => array( 'uri' => array( 'scheme' => 'http', 'host' => 'localhost', 'port' => 80 ), 'auth' => array( 'method' => 'Basic', 'user' => null, 'pass' => null ), 'cookies' => array() ) )
$quirksMode ¶
When one activates the $quirksMode by setting it to true, all checks meant to enforce RFC 2616 (HTTP/1.1 specs) will be disabled and additional measures to deal with non-standard responses will be enabled.
false
$request ¶
The default values to use for a request
array( 'method' => 'GET', 'uri' => array( 'scheme' => 'http', 'host' => null, 'port' => 80, 'user' => null, 'pass' => null, 'path' => null, 'query' => null, 'fragment' => null ), 'auth' => array( 'method' => 'Basic', 'user' => null, 'pass' => null ), 'version' => '1.1', 'body' => '', 'line' => null, 'header' => array( 'Connection' => 'close', 'User-Agent' => 'CakePHP' ), 'raw' => null, 'cookies' => array() )
$response ¶
The default structure for storing the response
array( 'raw' => array( 'status-line' => null, 'header' => null, 'body' => null, 'response' => null ), 'status' => array( 'http-version' => null, 'code' => null, 'reason-phrase' => null ), 'header' => array(), 'body' => '', 'cookies' => array() )