HttpChunkServer::makeErrorResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 9
rs 9.9666
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
use Spike\Server\Server;
19
use Spike\Version;
20
21
/**
22
 * @codeCoverageIgnore
23
 */
24
class HttpChunkServer extends TcpChunkServer
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function handlePublicConnection(PublicConnection $publicConnection)
30
    {
31
        $parser = new HttpHeaderParser();
32
        httpHeaderBuffer($publicConnection->getConnection(), $parser)->then(function($messages) use ($parser, $publicConnection){
33
            //Ignore empty message
34
            if (!$messages) {
35
                return;
36
            }
37
            $message = reset($messages);
38
            $psrRequest = Psr7\parse_request($message);
39
            $host = $psrRequest->getUri()->getHost();
40
            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...
41
                $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...
42
                $httpMessage = $message.$parser->getRemainingChunk();
43
                $publicConnection->setInitBuffer($httpMessage);
44
                parent::handlePublicConnection($publicConnection);
45
            } else {
46
                $body = sprintf('The host "%s" was not bound.', $host);
47
                $response = $this->makeErrorResponse(404, $body);
48
                $publicConnection->end(Psr7\str($response));
49
            }
50
        });
51
    }
52
53
    /**
54
     * Make an error psr7 response.
55
     *
56
     * @param int    $code
57
     * @param string $message
58
     *
59
     * @return Response
60
     */
61
    protected function makeErrorResponse($code, $message)
62
    {
63
        $message = $message ?: 'Proxy error';
64
65
        return new Response($code, [
66
            'Content-Length' => strlen($message),
67
            'X-Spiked' => Server::NAME .' '. Version::VERSION,
68
        ], $message);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function closePublicConnection(PublicConnection $publicConnection, $message = null)
75
    {
76
        $publicConnection->end(Psr7\str($this->makeErrorResponse(500, $message ?: 'Timeout')));
77
        $this->publicConnections->removeElement($publicConnection);
78
    }
79
}