| @@ 8-97 (lines=90) @@ | ||
| 5 | use Dazzle\Http\NetworkMessageInterface; |
|
| 6 | use GuzzleHttp\Psr7\Request; |
|
| 7 | ||
| 8 | class HttpRequest extends Request implements HttpRequestInterface, NetworkMessageInterface |
|
| 9 | { |
|
| 10 | /** |
|
| 11 | * @override |
|
| 12 | * @inheritDoc |
|
| 13 | */ |
|
| 14 | public function __toString() |
|
| 15 | { |
|
| 16 | return $this->encode(); |
|
| 17 | } |
|
| 18 | ||
| 19 | /** |
|
| 20 | * @override |
|
| 21 | * @inheritDoc |
|
| 22 | */ |
|
| 23 | public function encode() |
|
| 24 | { |
|
| 25 | return sprintf( |
|
| 26 | "%s %s HTTP/%s\r\n%s\r\n%s", |
|
| 27 | $this->getMethod(), |
|
| 28 | $this->getTarget(), |
|
| 29 | $this->getProtocolVersion(), |
|
| 30 | $this->encodeHeaders($this->getHeaders()), |
|
| 31 | (string) $this->getBody() |
|
| 32 | ); |
|
| 33 | } |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @override |
|
| 37 | * @inheritDoc |
|
| 38 | */ |
|
| 39 | public function getTarget() |
|
| 40 | { |
|
| 41 | return $this->getRequestTarget(); |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * @override |
|
| 46 | * @inheritDoc |
|
| 47 | */ |
|
| 48 | public function read() |
|
| 49 | { |
|
| 50 | return (string) $this->getBody(); |
|
| 51 | } |
|
| 52 | ||
| 53 | /** |
|
| 54 | * Encode headers. |
|
| 55 | * |
|
| 56 | * @param string[][] $headers |
|
| 57 | * @return string |
|
| 58 | */ |
|
| 59 | protected function encodeHeaders($headers = []) |
|
| 60 | { |
|
| 61 | $data = ''; |
|
| 62 | ||
| 63 | foreach ($headers as $name=>$values) |
|
| 64 | { |
|
| 65 | $temp = []; |
|
| 66 | $data .= $name . ": "; |
|
| 67 | $values = (array) $values; |
|
| 68 | ||
| 69 | foreach ($values as $value) |
|
| 70 | { |
|
| 71 | $temp[] = $this->encodeHeader($value); |
|
| 72 | } |
|
| 73 | ||
| 74 | $data .= implode(", ", $temp); |
|
| 75 | $data .= "\r\n"; |
|
| 76 | } |
|
| 77 | ||
| 78 | return $data; |
|
| 79 | } |
|
| 80 | ||
| 81 | /** |
|
| 82 | * Encode single header. |
|
| 83 | * |
|
| 84 | * @param string $header |
|
| 85 | * @return string |
|
| 86 | */ |
|
| 87 | protected function encodeHeader($header) |
|
| 88 | { |
|
| 89 | return preg_replace_callback( |
|
| 90 | '/(?:[^A-Za-z0-9_\-\.~!\$&\'\(\)\[\]\*\+,:;=\/% ]+|%(?![A-Fa-f0-9]{2}))/', |
|
| 91 | function (array $matches) { |
|
| 92 | return rawurlencode($matches[0]); |
|
| 93 | }, |
|
| 94 | $header |
|
| 95 | ); |
|
| 96 | } |
|
| 97 | } |
|
| 98 | ||
| @@ 8-88 (lines=81) @@ | ||
| 5 | use Dazzle\Http\NetworkMessageInterface; |
|
| 6 | use GuzzleHttp\Psr7\Response; |
|
| 7 | ||
| 8 | class HttpResponse extends Response implements HttpResponseInterface, NetworkMessageInterface |
|
| 9 | { |
|
| 10 | /** |
|
| 11 | * @override |
|
| 12 | * @inheritDoc |
|
| 13 | */ |
|
| 14 | public function __toString() |
|
| 15 | { |
|
| 16 | return $this->encode(); |
|
| 17 | } |
|
| 18 | ||
| 19 | /** |
|
| 20 | * @override |
|
| 21 | * @inheritDoc |
|
| 22 | */ |
|
| 23 | public function encode() |
|
| 24 | { |
|
| 25 | return sprintf( |
|
| 26 | "HTTP/%s %d %s\r\n%s\r\n%s", |
|
| 27 | $this->getProtocolVersion(), |
|
| 28 | $this->getStatusCode(), |
|
| 29 | $this->getReasonPhrase(), |
|
| 30 | $this->encodeHeaders($this->getHeaders()), |
|
| 31 | (string) $this->getBody() |
|
| 32 | ); |
|
| 33 | } |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @override |
|
| 37 | * @inheritDoc |
|
| 38 | */ |
|
| 39 | public function read() |
|
| 40 | { |
|
| 41 | return (string) $this->getBody(); |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * Encode headers. |
|
| 46 | * |
|
| 47 | * @param string[][] $headers |
|
| 48 | * @return string |
|
| 49 | */ |
|
| 50 | protected function encodeHeaders($headers = []) |
|
| 51 | { |
|
| 52 | $data = ''; |
|
| 53 | ||
| 54 | foreach ($headers as $name=>$values) |
|
| 55 | { |
|
| 56 | $temp = []; |
|
| 57 | $data .= $name . ": "; |
|
| 58 | $values = (array) $values; |
|
| 59 | ||
| 60 | foreach ($values as $value) |
|
| 61 | { |
|
| 62 | $temp[] = $this->encodeHeader($value); |
|
| 63 | } |
|
| 64 | ||
| 65 | $data .= implode(", ", $temp); |
|
| 66 | $data .= "\r\n"; |
|
| 67 | } |
|
| 68 | ||
| 69 | return $data; |
|
| 70 | } |
|
| 71 | ||
| 72 | /** |
|
| 73 | * Encode single header. |
|
| 74 | * |
|
| 75 | * @param string $header |
|
| 76 | * @return string |
|
| 77 | */ |
|
| 78 | protected function encodeHeader($header) |
|
| 79 | { |
|
| 80 | return preg_replace_callback( |
|
| 81 | '/(?:[^A-Za-z0-9_\-\.~!\$&\'\(\)\[\]\*\+,:;=\/% ]+|%(?![A-Fa-f0-9]{2}))/', |
|
| 82 | function (array $matches) { |
|
| 83 | return rawurlencode($matches[0]); |
|
| 84 | }, |
|
| 85 | $header |
|
| 86 | ); |
|
| 87 | } |
|
| 88 | } |
|
| 89 | ||