Completed
Push — master ( 143878...f45699 )
by Taosikai
14:33
created

HttpChunkServer::handlePublicConnection()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 1
nop 1
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the slince/spike package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Spike\Server\ChunkServer;
13
14
use GuzzleHttp\Psr7\Response;
15
use GuzzleHttp\Psr7;
16
use function Slince\Common\httpHeaderBuffer;
17
use Spike\Common\Protocol\HttpHeaderParser;
18
19
/**
20
 * @codeCoverageIgnore
21
 */
22
class HttpChunkServer extends TcpChunkServer
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function handlePublicConnection(PublicConnection $publicConnection)
28
    {
29
        $parser = new HttpHeaderParser();
30
        httpHeaderBuffer($publicConnection->getConnection(), $parser)->then(function($messages) use ($parser, $publicConnection){
31
            //Ignore empty message
32
            if (!$messages) {
33
                return;
34
            }
35
            $message = reset($messages);
36
            $psrRequest = Psr7\parse_request($message);
37
            $host = $psrRequest->getUri()->getHost();
38
            if ($this->tunnel->supportProxyHost($host)) {
0 ignored issues
show
Bug introduced by
The method supportProxyHost() does not seem to exist on object<Spike\Common\Tunnel\TcpTunnel>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
                $this->tunnel->setProxyHost($host);
0 ignored issues
show
Bug introduced by
The method setProxyHost() does not seem to exist on object<Spike\Common\Tunnel\TcpTunnel>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
                $httpMessage = $message.$parser->getRemainingChunk();
41
                $publicConnection->setInitBuffer($httpMessage);
42
                parent::handlePublicConnection($publicConnection);
43
            } else {
44
                $body = sprintf('The host "%s" was not bound.', $host);
45
                $response = $this->makeErrorResponse(404, $body);
46
                $publicConnection->end(Psr7\str($response));
47
            }
48
        });
49
    }
50
51
    /**
52
     * Make an error psr7 response.
53
     *
54
     * @param int    $code
55
     * @param string $message
56
     *
57
     * @return Response
58
     */
59
    protected function makeErrorResponse($code, $message)
60
    {
61
        $message = $message ?: 'Proxy error';
62
63
        return new Response($code, [
64
            'Content-Length' => strlen($message),
65
        ], $message);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function closePublicConnection(PublicConnection $publicConnection, $message = null)
72
    {
73
        $publicConnection->end(Psr7\str($this->makeErrorResponse(500, $message ?: 'Timeout')));
74
        $this->publicConnections->removeElement($publicConnection);
75
    }
76
}