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

sirn-se / websocket-php / 5608975860

pending completion
5608975860

push

github

Sören Jensen
Middleware support

90 of 90 new or added lines in 8 files covered. (100.0%)

245 of 671 relevant lines covered (36.51%)

1.27 hits per line

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

0.0
/src/Http/Request.php
1
<?php
2

3
/**
4
 * Copyright (C) 2014-2023 Textalk and contributors.
5
 *
6
 * This file is part of Websocket PHP and is free software under the ISC License.
7
 * License text: https://raw.githubusercontent.com/sirn-se/websocket-php/master/COPYING.md
8
 */
9

10
namespace WebSocket\Http;
11

12
use Phrity\Net\Uri;
13
use Psr\Http\Message\{
14
    RequestInterface,
15
    UriInterface
16
};
17
use RuntimeException;
18

19
/**
20
 * WebSocket\Http\Request class.
21
 * Only used for handshake procedure.
22
 */
23
class Request extends Message implements RequestInterface
24
{
25
    private $target;
26
    private $method;
27
    private $uri;
28

29
    public function __construct(string $method = 'GET', $uri = null)
30
    {
31
        $this->uri = $uri instanceof Uri ? $uri : new Uri((string)$uri);
×
32
        $this->method = $method;
×
33
        if ($this->uri->getHost()) {
×
34
            $this->headers = ['host' => ['Host' => [$this->uri->getAuthority()]]];
×
35
        }
36
    }
37

38
    /**
39
     * Retrieves the message's request target.
40
     * @return string
41
     */
42
    public function getRequestTarget(): string
43
    {
44
        if ($this->target) {
×
45
            return $this->target;
×
46
        }
47
        $uri = (new Uri())->withPath($this->uri->getPath())->withQuery($this->uri->getQuery());
×
48
        return $uri->toString(Uri::ABSOLUTE_PATH);
×
49
    }
50

51
    /**
52
     * Return an instance with the specific request-target.
53
     * @param mixed $requestTarget
54
     * @return static
55
     */
56
    public function withRequestTarget($requestTarget): self
57
    {
58
        $new = clone $this;
×
59
        $new->target = $requestTarget;
×
60
        return $new;
×
61
    }
62

63
    /**
64
     * Retrieves the HTTP method of the request.
65
     * @return string Returns the request method.
66
     */
67
    public function getMethod(): string
68
    {
69
        return $this->method;
×
70
    }
71

72
    /**
73
     * Return an instance with the provided HTTP method.
74
     * @param string $method Case-sensitive method.
75
     * @return static
76
     * @throws \InvalidArgumentException for invalid HTTP methods.
77
     */
78
    public function withMethod(string $method): self
79
    {
80
        $new = clone $this;
×
81
        $new->method = $method;
×
82
        return $new;
×
83
    }
84

85
    /**
86
     * Retrieves the URI instance.
87
     * This method MUST return a UriInterface instance.
88
     * @return UriInterface Returns a UriInterface instance representing the URI of the request.
89
     */
90
    public function getUri(): UriInterface
91
    {
92
        return $this->uri;
×
93
    }
94

95
    /**
96
     * Returns an instance with the provided URI.
97
     * @param UriInterface $uri New request URI to use.
98
     * @param bool $preserveHost Preserve the original state of the Host header.
99
     * @return static
100
     */
101
    public function withUri(UriInterface $uri, bool $preserveHost = false): self
102
    {
103
        $new = clone $this;
×
104
        $new->uri = $uri instanceof Uri ? $uri : new Uri((string)$uri);
×
105
        if (!$preserveHost || !$new->hasHeader('host')) {
×
106
            if (isset($new->headers['host'])) {
×
107
                unset($new->headers['host']);
×
108
            }
109
            if ($host = $uri->getHost()) {
×
110
                $new->headers = array_merge(['host' => ['Host' => [$uri->getAuthority()]]], $new->headers);
×
111
            }
112
        }
113
        return $new;
×
114
    }
115

116
    public function getAsArray(): array
117
    {
118
        return array_merge([
×
119
            "GET {$this->getRequestTarget()} HTTP/{$this->getProtocolVersion()}",
×
120
        ], parent::getAsArray());
×
121
    }
122
}
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