Source for file encoding.php
Documentation is available at encoding.php
* base include file for SimpleTest
* @version $Id: encoding.php 1964 2009-10-13 15:27:31Z maetl_ $
* include other SimpleTest class files
require_once(dirname(__FILE__
) .
'/socket.php');
* Stashes the data for rendering later.
* @param string $key Form element name.
* @param string $value Data to send.
* The pair as a single string.
* @return string Encoded pair.
* The MIME part as a string.
* @return string MIME part encoding.
$part =
'Content-Disposition: form-data; ';
$part .=
"name=\"" .
$this->key .
"\"\r\n";
$part .=
"\r\n" .
$this->value;
* Is this the value we are looking for?
* @param string $key Identifier.
* @return boolean True if matched.
return $key ==
$this->key;
* Is this the value we are looking for?
* @return string Identifier.
* Is this the value we are looking for?
* @return string Content.
* Stashes the data for rendering later.
* @param string $key Key to add value to.
* @param string $content Raw data.
* @param hash $filename Original filename.
$this->content =
$content;
$this->filename =
$filename;
* The pair as a single string.
* @return string Encoded pair.
* The MIME part as a string.
* @return string MIME part encoding.
$part =
'Content-Disposition: form-data; ';
$part .=
'name="' .
$this->key .
'"; ';
$part .=
'filename="' .
$this->filename .
'"';
$part .=
"\r\n\r\n" .
$this->content;
* Attempts to figure out the MIME type from the
* file extension and the content.
* @return string MIME type.
return 'application/octet-stream';
* Tests each character is in the range 0-127.
* @param string $ascii String to test.
for ($i =
0, $length =
strlen($ascii); $i <
$length; $i++
) {
if (ord($ascii[$i]) >
127) {
* Is this the value we are looking for?
* @param string $key Identifier.
* @return boolean True if matched.
return $key ==
$this->key;
* Is this the value we are looking for?
* @return string Identifier.
* Is this the value we are looking for?
* @return string Content.
* Bundle of GET/POST parameters. Can include
* @param array $query Hash of parameters.
* as lists on a single key.
* Empties the request of parameters.
$this->request =
array();
* Adds a parameter to the query.
* @param string $key Key to add value to.
* @param string/array $value New data.
function add($key, $value) {
foreach ($value as $item) {
* Adds a new value into the request.
* @param string $key Key to add value to.
* @param string/array $value New data.
protected function addPair($key, $value) {
* Adds a MIME part to the query. Does nothing for a
* @param string $key Key to add value to.
* @param string $content Raw data.
* @param hash $filename Original filename.
function attach($key, $content, $filename) {
* Adds a set of parameters to this query.
* @param array/SimpleQueryString $query Multiple values are
* as lists on a single key.
$this->request =
array_merge($this->request, $query->getAll());
foreach ($query as $key =>
$value) {
$this->add($key, $value);
* Accessor for single value.
* @return string/array False if missing, string
* if present and array if
foreach ($this->request as $pair) {
if ($pair->isKey($key)) {
$values[] =
$pair->getValue();
if (count($values) ==
0) {
} elseif (count($values) ==
1) {
* Accessor for listing of pairs.
* @return array All pair objects.
* Renders the query string as a URL encoded
* @return string Part of URL.
foreach ($this->request as $pair) {
if ($statement =
$pair->asRequest()) {
$statements[] =
$statement;
* Bundle of GET parameters. Can include
* @param array $query Hash of parameters.
* as lists on a single key.
* @return string Always GET.
* Writes no extra headers.
* @param SimpleSocket $socket Socket to write to.
* No data is sent to the socket as the data is encoded into
* @param SimpleSocket $socket Socket to write to.
* Renders the query string as a URL encoded
* request part for attaching to a URL.
* @return string Part of URL.
* Bundle of URL parameters for a HEAD request.
* @param array $query Hash of parameters.
* as lists on a single key.
* @return string Always HEAD.
* Bundle of URL parameters for a DELETE request.
* @param array $query Hash of parameters.
* as lists on a single key.
* @return string Always DELETE.
* Bundles an entity-body for transporting
* a raw content payload with the request.
function __construct($query =
false, $content_type =
false) {
$this->content_type =
$content_type;
* Returns the media type of the entity body
if (!$this->content_type) {
return ($this->body) ?
'text/plain' :
'application/x-www-form-urlencoded';
return $this->content_type;
* Dispatches the form headers down the socket.
* @param SimpleSocket $socket Socket to write to.
$socket->write("Content-Length: " . (integer)
strlen($this->encode()) .
"\r\n");
* Dispatches the form data down the socket.
* @param SimpleSocket $socket Socket to write to.
$socket->write($this->encode());
* Renders the request body
* @return Encoded entity body
return ($this->body) ?
$this->body :
parent::encode();
* Bundle of POST parameters. Can include
* @param array $query Hash of parameters.
* as lists on a single key.
function __construct($query =
false, $content_type =
false) {
foreach ($query as $key =>
$value) {
foreach ($query as $key =>
$value) {
foreach ($value as $sub_key =>
$sub_value) {
$query_[$key.
"[".
$sub_key.
"]"] =
$sub_value;
* @return string Always POST.
* Renders the query string as a URL encoded
* request part for attaching to a URL.
* @return string Part of URL.
* Encoded entity body for a PUT request.
* @param array $query Hash of parameters.
* as lists on a single key.
function __construct($query =
false, $content_type =
false) {
* @return string Always PUT.
* Bundle of POST parameters in the multipart
* format. Can include file uploads.
* @param array $query Hash of parameters.
* as lists on a single key.
function __construct($query =
false, $boundary =
false) {
$this->boundary =
($boundary ===
false ?
uniqid('st') :
$boundary);
* Dispatches the form headers down the socket.
* @param SimpleSocket $socket Socket to write to.
$socket->write("Content-Length: " . (integer)
strlen($this->encode()) .
"\r\n");
$socket->write("Content-Type: multipart/form-data; boundary=" .
$this->boundary .
"\r\n");
* Dispatches the form data down the socket.
* @param SimpleSocket $socket Socket to write to.
$socket->write($this->encode());
* Renders the query string as a URL encoded
* @return string Part of URL.
foreach ($this->getAll() as $pair) {
$stream .=
"--" .
$this->boundary .
"\r\n";
$stream .=
$pair->asMime() .
"\r\n";
$stream .=
"--" .
$this->boundary .
"--\r\n";
Documentation generated on Sun, 31 Oct 2010 16:31:19 -0500 by phpDocumentor 1.4.3