-
-
Notifications
You must be signed in to change notification settings - Fork 728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[0.3] Replace guzzle/http with custom implementation #131
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d0743b6
Remove guzzle/http dependency from composer.json
igorw 8348e57
Introduce ResponseCodes class
igorw a383c40
Make react/http work without guzzle
igorw dba15dc
Make http/client independent from guzzle/http
igorw bcd9ca7
Use port 443 for SSL connections
igorw 79a67e7
[http-client] Remove newlines from response headers (thanks @staabm)
igorw af59cf3
[http-client] Gracefully handle undefined response status codes
igorw 1142522
Ensure that response status is an int
igorw File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
namespace React\Http; | ||
|
||
use Evenement\EventEmitter; | ||
use Guzzle\Http\Message\Response as GuzzleResponse; | ||
use React\Socket\ConnectionInterface; | ||
use React\Stream\WritableStreamInterface; | ||
|
||
|
@@ -59,18 +58,37 @@ public function writeHead($status = 200, array $headers = array()) | |
$this->chunkedEncoding = false; | ||
} | ||
|
||
$response = new GuzzleResponse($status); | ||
$response->setHeader('X-Powered-By', 'React/alpha'); | ||
$response->addHeaders($headers); | ||
$headers = array_merge( | ||
array('X-Powered-By' => 'React/alpha'), | ||
$headers | ||
); | ||
if ($this->chunkedEncoding) { | ||
$response->setHeader('Transfer-Encoding', 'chunked'); | ||
$headers['Transfer-Encoding'] = 'chunked'; | ||
} | ||
$data = (string) $response; | ||
|
||
$data = $this->formatHead($status, $headers); | ||
$this->conn->write($data); | ||
|
||
$this->headWritten = true; | ||
} | ||
|
||
private function formatHead($status, array $headers) | ||
{ | ||
$status = (int) $status; | ||
$text = isset(ResponseCodes::$statusTexts[$status]) ? ResponseCodes::$statusTexts[$status] : ''; | ||
$data = "HTTP/1.1 $status $text\r\n"; | ||
|
||
foreach ($headers as $name => $value) { | ||
$name = str_replace(array("\r", "\n"), '', $name); | ||
$value = str_replace(array("\r", "\n"), '', $value); | ||
|
||
$data .= "$name: $value\r\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
} | ||
$data .= "\r\n"; | ||
|
||
return $data; | ||
} | ||
|
||
public function write($data) | ||
{ | ||
if (!$this->headWritten) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace React\Http; | ||
|
||
/** | ||
* This is copy-pasted from Symfony2's Response class | ||
*/ | ||
class ResponseCodes | ||
{ | ||
public static $statusTexts = array( | ||
100 => 'Continue', | ||
101 => 'Switching Protocols', | ||
102 => 'Processing', // RFC2518 | ||
200 => 'OK', | ||
201 => 'Created', | ||
202 => 'Accepted', | ||
203 => 'Non-Authoritative Information', | ||
204 => 'No Content', | ||
205 => 'Reset Content', | ||
206 => 'Partial Content', | ||
207 => 'Multi-Status', // RFC4918 | ||
208 => 'Already Reported', // RFC5842 | ||
226 => 'IM Used', // RFC3229 | ||
300 => 'Multiple Choices', | ||
301 => 'Moved Permanently', | ||
302 => 'Found', | ||
303 => 'See Other', | ||
304 => 'Not Modified', | ||
305 => 'Use Proxy', | ||
306 => 'Reserved', | ||
307 => 'Temporary Redirect', | ||
308 => 'Permanent Redirect', // RFC-reschke-http-status-308-07 | ||
400 => 'Bad Request', | ||
401 => 'Unauthorized', | ||
402 => 'Payment Required', | ||
403 => 'Forbidden', | ||
404 => 'Not Found', | ||
405 => 'Method Not Allowed', | ||
406 => 'Not Acceptable', | ||
407 => 'Proxy Authentication Required', | ||
408 => 'Request Timeout', | ||
409 => 'Conflict', | ||
410 => 'Gone', | ||
411 => 'Length Required', | ||
412 => 'Precondition Failed', | ||
413 => 'Request Entity Too Large', | ||
414 => 'Request-URI Too Long', | ||
415 => 'Unsupported Media Type', | ||
416 => 'Requested Range Not Satisfiable', | ||
417 => 'Expectation Failed', | ||
418 => 'I\'m a teapot', // RFC2324 | ||
422 => 'Unprocessable Entity', // RFC4918 | ||
423 => 'Locked', // RFC4918 | ||
424 => 'Failed Dependency', // RFC4918 | ||
425 => 'Reserved for WebDAV advanced collections expired proposal', // RFC2817 | ||
426 => 'Upgrade Required', // RFC2817 | ||
428 => 'Precondition Required', // RFC6585 | ||
429 => 'Too Many Requests', // RFC6585 | ||
431 => 'Request Header Fields Too Large', // RFC6585 | ||
500 => 'Internal Server Error', | ||
501 => 'Not Implemented', | ||
502 => 'Bad Gateway', | ||
503 => 'Service Unavailable', | ||
504 => 'Gateway Timeout', | ||
505 => 'HTTP Version Not Supported', | ||
506 => 'Variant Also Negotiates (Experimental)', // RFC2295 | ||
507 => 'Insufficient Storage', // RFC4918 | ||
508 => 'Loop Detected', // RFC5842 | ||
510 => 'Not Extended', // RFC2774 | ||
511 => 'Network Authentication Required', // RFC6585 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace React\HttpClient; | ||
|
||
class RequestData | ||
{ | ||
private $method; | ||
private $url; | ||
private $headers; | ||
|
||
private $protocolVersion = '1.1'; | ||
|
||
public function __construct($method, $url, array $headers = array()) | ||
{ | ||
$this->method = $method; | ||
$this->url = $url; | ||
$this->headers = $headers; | ||
} | ||
|
||
private function mergeDefaultheaders(array $headers) | ||
{ | ||
$port = ($this->getDefaultPort() === $this->getPort()) ? '' : ":{$this->getPort()}"; | ||
$connectionHeaders = ('1.1' === $this->protocolVersion) ? array('Connection' => 'close') : array(); | ||
|
||
return array_merge( | ||
array( | ||
'Host' => $this->getHost().$port, | ||
'User-Agent' => 'React/alpha', | ||
), | ||
$connectionHeaders, | ||
$headers | ||
); | ||
} | ||
|
||
public function getScheme() | ||
{ | ||
return parse_url($this->url, PHP_URL_SCHEME); | ||
} | ||
|
||
public function getHost() | ||
{ | ||
return parse_url($this->url, PHP_URL_HOST); | ||
} | ||
|
||
public function getPort() | ||
{ | ||
return (int) parse_url($this->url, PHP_URL_PORT) ?: $this->getDefaultPort(); | ||
} | ||
|
||
public function getDefaultPort() | ||
{ | ||
return ('https' === $this->getScheme()) ? 443 : 80; | ||
} | ||
|
||
public function getPath() | ||
{ | ||
$path = parse_url($this->url, PHP_URL_PATH) ?: '/'; | ||
$queryString = parse_url($this->url, PHP_URL_QUERY); | ||
|
||
return $path.($queryString ? "?$queryString" : ''); | ||
} | ||
|
||
public function setProtocolVersion($version) | ||
{ | ||
$this->protocolVersion = $version; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
$headers = $this->mergeDefaultheaders($this->headers); | ||
|
||
$data = ''; | ||
$data .= "{$this->method} {$this->getPath()} HTTP/{$this->protocolVersion}\r\n"; | ||
foreach ($headers as $name => $value) { | ||
$data .= "$name: $value\r\n"; | ||
} | ||
$data .= "\r\n"; | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should verify that $status and $text cannot contain "\r\n" or "\n" to protect against http-header injections
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are from ResponseCodes, which is a trusted source.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$status is not enforced to be a number, therefore could also contain "\r\n" and finally used for a injection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like your attention to detail, fixed.