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

sirn-se / websocket-php / 12619595558

05 Jan 2025 12:40PM UTC coverage: 99.715% (-0.3%) from 100.0%
12619595558

Pull #90

github

web-flow
Merge 60d71d18a into 52a374457
Pull Request #90: Adding phpstan for static analysis

15 of 18 new or added lines in 7 files covered. (83.33%)

2 existing lines in 2 files now uncovered.

1048 of 1051 relevant lines covered (99.71%)

22.23 hits per line

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

97.5
/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\MessageInterface;
15
use Psr\Log\{
16
    LoggerInterface,
17
    LoggerAwareInterface,
18
};
19
use RuntimeException;
20
use Stringable;
21
use WebSocket\Trait\StringableTrait;
22

23
/**
24
 * WebSocket\Http\HttpHandler class.
25
 * Reads and writes HTTP message to/from stream.
26
 * @deprecated Remove LoggerAwareInterface in v4
27
 */
28
class HttpHandler implements LoggerAwareInterface, Stringable
29
{
30
    use StringableTrait;
31

32
    private SocketStream $stream;
33
    private bool $ssl;
34

35
    public function __construct(SocketStream $stream, bool $ssl = false)
36
    {
37
        $this->stream = $stream;
107✔
38
        $this->ssl = $ssl;
107✔
39
    }
40

41
    /**
42
     * @deprecated Remove in v4
43
     */
44
    public function setLogger(LoggerInterface $logger): void
45
    {
46
    }
99✔
47

48
    public function pull(): MessageInterface
49
    {
50
        $status = $this->readLine();
78✔
51
        $path = $version = null;
74✔
52

53
        // Pulling server request
54
        preg_match('!^(?P<method>[A-Z]+) (?P<path>[^ ]*) HTTP/(?P<version>[0-9/.]+)!', $status, $matches);
74✔
55
        if (!empty($matches)) {
74✔
56
            $message = new ServerRequest($matches['method']);
26✔
57
            $path = $matches['path'];
26✔
58
            $version = $matches['version'];
26✔
59
        }
60

61
        // Pulling response
62
        preg_match('!^HTTP/(?P<version>[0-9/.]+) (?P<code>[0-9]*) (?P<reason>.*)!', $status, $matches);
74✔
63
        if (!empty($matches)) {
74✔
64
            $message = new Response((int)$matches['code'], $matches['reason']);
47✔
65
            $version = $matches['version'];
47✔
66
        }
67

68
        if (empty($message)) {
74✔
69
            throw new RuntimeException('Invalid Http request.');
1✔
70
        }
71

72
        $message = $message->withProtocolVersion($version);
73✔
73

74
        while ($header = $this->readLine()) {
73✔
75
            $parts = explode(':', $header, 2);
69✔
76
            if (count($parts) == 2) {
69✔
77
                if ($message->getheaderLine($parts[0]) === '') {
69✔
78
                    $message = $message->withHeader($parts[0], trim($parts[1]));
69✔
79
                } else {
80
                    $message = $message->withAddedHeader($parts[0], trim($parts[1]));
4✔
81
                }
82
            }
83
        }
84
        if ($message instanceof Request) {
73✔
85
            $scheme = $this->ssl ? 'wss' : 'ws';
26✔
86
            $uri = new Uri("{$scheme}://{$message->getHeaderLine('Host')}{$path}");
26✔
87
            $message = $message->withUri($uri, true);
26✔
88
        }
89

90
        return $message;
73✔
91
    }
92

93
    /**
94
     * @param MessageInterface $message
95
     * @return MessageInterface
96
     */
97
    public function push(MessageInterface $message): MessageInterface
98
    {
99
        if (!$message instanceof Message) {
72✔
NEW
100
            throw new RuntimeException('Generic MessageInterface currently not supported.');
×
101
        }
102
        $data = implode("\r\n", $message->getAsArray()) . "\r\n\r\n";
72✔
103
        $this->stream->write($data);
72✔
104
        return $message;
70✔
105
    }
106

107
    private function readLine(): string
108
    {
109
        $data = '';
78✔
110
        do {
111
            $buffer = $this->stream->readLine(1024);
78✔
112
            if (is_null($buffer)) {
75✔
113
                throw new RuntimeException('Could not read Http request.');
1✔
114
            }
115
            $data .= $buffer;
74✔
116
        } while (!str_ends_with($data, "\r\n"));
74✔
117
        return trim($data);
74✔
118
    }
119
}
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

© 2025 Coveralls, Inc