ci4

Форк
0
/
OutgoingRequest.php 
163 строки · 3.6 Кб
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\HTTP;
15

16
/**
17
 * Representation of an outgoing, client-side request.
18
 *
19
 * @see \CodeIgniter\HTTP\OutgoingRequestTest
20
 */
21
class OutgoingRequest extends Message implements OutgoingRequestInterface
22
{
23
    /**
24
     * Request method.
25
     *
26
     * @var string
27
     */
28
    protected $method;
29

30
    /**
31
     * A URI instance.
32
     *
33
     * @var URI|null
34
     */
35
    protected $uri;
36

37
    /**
38
     * @param string      $method HTTP method
39
     * @param string|null $body
40
     */
41
    public function __construct(
42
        string $method,
43
        ?URI $uri = null,
44
        array $headers = [],
45
        $body = null,
46
        string $version = '1.1'
47
    ) {
48
        $this->method = $method;
49
        $this->uri    = $uri;
50

51
        foreach ($headers as $header => $value) {
52
            $this->setHeader($header, $value);
53
        }
54

55
        $this->body            = $body;
56
        $this->protocolVersion = $version;
57

58
        if (! $this->hasHeader('Host') && $this->uri->getHost() !== '') {
59
            $this->setHeader('Host', $this->getHostFromUri($this->uri));
60
        }
61
    }
62

63
    private function getHostFromUri(URI $uri): string
64
    {
65
        $host = $uri->getHost();
66

67
        return $host . ($uri->getPort() ? ':' . $uri->getPort() : '');
68
    }
69

70
    /**
71
     * Retrieves the HTTP method of the request.
72
     *
73
     * @return string Returns the request method (always uppercase)
74
     */
75
    public function getMethod(): string
76
    {
77
        return $this->method;
78
    }
79

80
    /**
81
     * Sets the request method. Used when spoofing the request.
82
     *
83
     * @return $this
84
     *
85
     * @deprecated Use withMethod() instead for immutability
86
     */
87
    public function setMethod(string $method)
88
    {
89
        $this->method = $method;
90

91
        return $this;
92
    }
93

94
    /**
95
     * Returns an instance with the specified method.
96
     *
97
     * @param string $method
98
     *
99
     * @return static
100
     */
101
    public function withMethod($method)
102
    {
103
        $request         = clone $this;
104
        $request->method = $method;
105

106
        return $request;
107
    }
108

109
    /**
110
     * Retrieves the URI instance.
111
     *
112
     * @return URI|null
113
     */
114
    public function getUri()
115
    {
116
        return $this->uri;
117
    }
118

119
    /**
120
     * Returns an instance with the provided URI.
121
     *
122
     * @param URI  $uri          New request URI to use.
123
     * @param bool $preserveHost Preserve the original state of the Host header.
124
     *
125
     * @return static
126
     */
127
    public function withUri(URI $uri, $preserveHost = false)
128
    {
129
        $request      = clone $this;
130
        $request->uri = $uri;
131

132
        if ($preserveHost) {
133
            if ($this->isHostHeaderMissingOrEmpty() && $uri->getHost() !== '') {
134
                $request->setHeader('Host', $this->getHostFromUri($uri));
135

136
                return $request;
137
            }
138

139
            if ($this->isHostHeaderMissingOrEmpty() && $uri->getHost() === '') {
140
                return $request;
141
            }
142

143
            if (! $this->isHostHeaderMissingOrEmpty()) {
144
                return $request;
145
            }
146
        }
147

148
        if ($uri->getHost() !== '') {
149
            $request->setHeader('Host', $this->getHostFromUri($uri));
150
        }
151

152
        return $request;
153
    }
154

155
    private function isHostHeaderMissingOrEmpty(): bool
156
    {
157
        if (! $this->hasHeader('Host')) {
158
            return true;
159
        }
160

161
        return $this->header('Host')->getValue() === '';
162
    }
163
}
164

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.