• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sirn-se / websocket-php / 8346487915

19 Mar 2024 04:15PM UTC coverage: 22.584% (-77.4%) from 100.0%
8346487915

push

github

sirn-se
Temp test verification

2 of 2 new or added lines in 1 file covered. (100.0%)

742 existing lines in 32 files now uncovered.

222 of 983 relevant lines covered (22.58%)

0.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.38
/src/Http/HttpHandler.php
1
<?php
2

3
/**
4
 * Copyright (C) 2014-2024 Textalk and contributors.
5
 * This file is part of Websocket PHP and is free software under the ISC License.
6
 */
7

8
namespace WebSocket\Http;
9

10
use Phrity\Net\{
11
    SocketStream,
12
    Uri
13
};
14
use Psr\Http\Message\{
15
    MessageInterface,
16
    RequestInterface,
17
    ResponseInterface,
18
    StreamInterface
19
};
20
use Psr\Log\{
21
    LoggerInterface,
22
    LoggerAwareInterface,
23
    NullLogger
24
};
25
use RuntimeException;
26
use Stringable;
27
use WebSocket\Trait\StringableTrait;
28

29
/**
30
 * WebSocket\Http\HttpHandler class.
31
 * Reads and writes HTTP message to/from stream.
32
 */
33
class HttpHandler implements LoggerAwareInterface, Stringable
34
{
35
    use StringableTrait;
36

37
    private $stream;
38
    private $ssl;
39
    private $logger;
40

41
    public function __construct(SocketStream $stream, bool $ssl = false)
42
    {
43
        $this->stream = $stream;
1✔
44
        $this->ssl = $ssl;
1✔
45
        $this->setLogger(new NullLogger());
1✔
46
    }
47

48
    public function setLogger(LoggerInterface $logger): void
49
    {
50
        $this->logger = $logger;
1✔
51
    }
52

53
    public function pull(): MessageInterface
54
    {
55
        $data = '';
1✔
56
        do {
57
            $buffer = $this->stream->readLine(1024);
1✔
58
            $data .= $buffer;
1✔
59
        } while (substr_count($data, "\r\n\r\n") == 0);
1✔
60

61
        list ($head, $body) = explode("\r\n\r\n", $data);
1✔
62
        $headers = array_filter(explode("\r\n", $head));
1✔
63
        $status = array_shift($headers);
1✔
64

65
        // Pulling server request
66
        preg_match('!^(?P<method>[A-Z]+) (?P<path>[^ ]*) HTTP/(?P<version>[0-9/.]+)!', $status, $matches);
1✔
67
        if (!empty($matches)) {
1✔
UNCOV
68
            $message = new ServerRequest($matches['method']);
×
UNCOV
69
            $path = $matches['path'];
×
UNCOV
70
            $version = $matches['version'];
×
71
        }
72

73
        // Pulling response
74
        preg_match('!^HTTP/(?P<version>[0-9/.]+) (?P<code>[0-9]*) (?P<reason>.*)!', $status, $matches);
1✔
75
        if (!empty($matches)) {
1✔
76
            $message = new Response($matches['code'], $matches['reason']);
1✔
77
            $version = $matches['version'];
1✔
78
        }
79

80
        if (empty($message)) {
1✔
UNCOV
81
            throw new RuntimeException('Invalid Http request.');
×
82
        }
83

84
        $message = $message->withProtocolVersion($version);
1✔
85
        foreach ($headers as $header) {
1✔
86
            $parts = explode(':', $header, 2);
1✔
87
            if (count($parts) == 2) {
1✔
88
                if ($message->getheaderLine($parts[0]) === '') {
1✔
89
                    $message = $message->withHeader($parts[0], trim($parts[1]));
1✔
90
                } else {
UNCOV
91
                    $message = $message->withAddedHeader($parts[0], trim($parts[1]));
×
92
                }
93
            }
94
        }
95
        if ($message instanceof Request) {
1✔
UNCOV
96
            $scheme = $this->ssl ? 'wss' : 'ws';
×
UNCOV
97
            $uri = new Uri("{$scheme}://{$message->getHeaderLine('Host')}{$path}");
×
UNCOV
98
            $message = $message->withUri($uri, true);
×
99
        }
100

101
        return $message;
1✔
102
    }
103

104
    public function push(MessageInterface $message): MessageInterface
105
    {
106
        $data = implode("\r\n", $message->getAsArray()) . "\r\n\r\n";
1✔
107
        $this->stream->write($data);
1✔
108
        return $message;
1✔
109
    }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc