Skip to content

Commit

Permalink
Merge pull request #39 from fruux/deprecate-util
Browse files Browse the repository at this point in the history
Creating a new HeaderHelper
  • Loading branch information
evert committed May 19, 2015
2 parents ecac787 + 270f9f4 commit a6c9b35
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ before_script:

script:
- ./bin/phpunit --configuration tests/phpunit.xml
- ./bin/sabre-cs-fixer fix lib/ --dry-run --diff
- ./bin/sabre-cs-fixer fix . --dry-run --diff
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"source" : "https://github.com/fruux/sabre-http"
},
"autoload" : {
"files" : [
"lib/functions.php"
],
"psr-4" : {
"Sabre\\HTTP\\" : "lib/"
}
Expand Down
7 changes: 2 additions & 5 deletions examples/asyncclient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/

use
Sabre\HTTP\Request,
Sabre\HTTP\Client;

use Sabre\HTTP\Request;
use Sabre\HTTP\Client;

// Find the autoloader
$paths = [
Expand Down
12 changes: 4 additions & 8 deletions examples/basicauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/

$userList = [
"user1" => "password",
"user2" => "password",
];

use
Sabre\HTTP\Sapi,
Sabre\HTTP\Response,
Sabre\HTTP\Auth;

use Sabre\HTTP\Sapi;
use Sabre\HTTP\Response;
use Sabre\HTTP\Auth;

// Find the autoloader
$paths = [
Expand Down Expand Up @@ -45,7 +42,7 @@

} elseif (!isset($userList[$userPass[0]]) || $userList[$userPass[0]] !== $userPass[1]) {

// Username or password are incorrect
// Username or password are incorrect
$basicAuth->requireLogin();
} else {

Expand All @@ -56,4 +53,3 @@

// Sending the response
Sapi::sendResponse($response);

7 changes: 2 additions & 5 deletions examples/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/

use
Sabre\HTTP\Request,
Sabre\HTTP\Client;

use Sabre\HTTP\Request;
use Sabre\HTTP\Client;

// Find the autoloader
$paths = [
Expand Down
5 changes: 2 additions & 3 deletions examples/reverseproxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
$myBaseUrl = '/reverseproxy.php';
// $myBaseUrl = '/~evert/sabre/http/examples/reverseproxy.php/';

use
Sabre\HTTP\Sapi,
Sabre\HTTP\Client;
use Sabre\HTTP\Sapi;
use Sabre\HTTP\Client;

// Find the autoloader
$paths = [
Expand Down
11 changes: 4 additions & 7 deletions examples/stringify.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/

use
Sabre\HTTP\Request,
Sabre\HTTP\Response;

use Sabre\HTTP\Request;
use Sabre\HTTP\Response;

// Find the autoloader
$paths = [
Expand All @@ -32,7 +29,7 @@

$request = new Request('POST', '/foo');
$request->setHeaders([
'Host' => 'example.org',
'Host' => 'example.org',
'Content-Type' => 'application/json'
]);

Expand All @@ -44,7 +41,7 @@
$response = new Response(424);
$response->setHeaders([
'Content-Type' => 'text/plain',
'Connection' => 'close',
'Connection' => 'close',
]);

$response->setBody("ABORT! ABORT!");
Expand Down
97 changes: 30 additions & 67 deletions lib/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,6 @@
*/
class Util {

/**
* Parses a RFC2616-compatible date string
*
* This method returns false if the date is invalid
*
* @param string $dateHeader
* @return bool|DateTime
*/
static function parseHTTPDate($dateHeader) {

//RFC 2616 section 3.3.1 Full Date
//Only the format is checked, valid ranges are checked by strtotime below
$month = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)';
$weekday = '(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)';
$wkday = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun)';
$time = '([0-1]\d|2[0-3])(\:[0-5]\d){2}';
$date3 = $month . ' ([12]\d|3[01]| [1-9])';
$date2 = '(0[1-9]|[12]\d|3[01])\-' . $month . '\-\d{2}';
//4-digit year cannot begin with 0 - unix timestamp begins in 1970
$date1 = '(0[1-9]|[12]\d|3[01]) ' . $month . ' [1-9]\d{3}';

//ANSI C's asctime() format
//4-digit year cannot begin with 0 - unix timestamp begins in 1970
$asctime_date = $wkday . ' ' . $date3 . ' ' . $time . ' [1-9]\d{3}';
//RFC 850, obsoleted by RFC 1036
$rfc850_date = $weekday . ', ' . $date2 . ' ' . $time . ' GMT';
//RFC 822, updated by RFC 1123
$rfc1123_date = $wkday . ', ' . $date1 . ' ' . $time . ' GMT';
//allowed date formats by RFC 2616
$HTTP_date = "($rfc1123_date|$rfc850_date|$asctime_date)";

//allow for space around the string and strip it
$dateHeader = trim($dateHeader, ' ');
if (!preg_match('/^' . $HTTP_date . '$/', $dateHeader))
return false;

//append implicit GMT timezone to ANSI C time format
if (strpos($dateHeader, ' GMT') === false)
$dateHeader .= ' GMT';


$realDate = strtotime($dateHeader);
//strtotime can return -1 or false in case of error
if ($realDate !== false && $realDate >= 0)
return new \DateTime('@' . $realDate, new \DateTimeZone('UTC'));

}

/**
* Transforms a DateTime object to HTTP's most common date format.
*
* We're serializing it as the RFC 1123 date, which, for HTTP must be
* specified as GMT.
*
* @param \DateTime $dateTime
* @return string
*/
static function toHTTPDate(\DateTime $dateTime) {

// We need to clone it, as we don't want to affect the existing
// DateTime.
$dateTime = clone $dateTime;
$dateTime->setTimeZone(new \DateTimeZone('GMT'));
return $dateTime->format('D, d M Y H:i:s \G\M\T');

}

/**
* This method can be used to aid with content negotiation.
*
Expand Down Expand Up @@ -263,4 +196,34 @@ static function negotiate($acceptHeaderValue, array $availableOptions) {

}

/**
* Parses a RFC2616-compatible date string
*
* This method returns false if the date is invalid
*
* @deprecated Use parseDate
* @param string $dateHeader
* @return bool|DateTime
*/
static function parseHTTPDate($dateHeader) {

return parseDate($dateHeader);

}

/**
* Transforms a DateTime object to HTTP's most common date format.
*
* We're serializing it as the RFC 1123 date, which, for HTTP must be
* specified as GMT.
*
* @deprecated Use toDate
* @param \DateTime $dateTime
* @return string
*/
static function toHTTPDate(\DateTime $dateTime) {

return toDate($dateTime);

}
}
Loading

0 comments on commit a6c9b35

Please sign in to comment.