Issues (3)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Response.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Gemz\HttpClient;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Symfony\Contracts\HttpClient\ResponseInterface;
8
9
class Response
10
{
11
    /** @var ResponseInterface */
12
    protected $response;
13
14
    /** @var bool */
15
    protected $throwErrors;
16
17
    /**
18
     * @param ResponseInterface $response
19
     * @param bool $throwErrors
20
     *
21
     * @return Response
22
     */
23
    public static function createFromResponse(ResponseInterface $response, $throwErrors = false)
24
    {
25
        return new self($response, $throwErrors);
26
    }
27
28
    /**
29
     * @param ResponseInterface $response
30
     * @param bool $throwErrors
31
     */
32
    public function __construct(ResponseInterface $response, $throwErrors = false)
33
    {
34
        $this->response = $response;
35
        $this->throwErrors = $throwErrors;
36
    }
37
38
    /**
39
     * @return int
40
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
41
     */
42
    public function status(): int
43
    {
44
        return $this->response->getStatusCode();
45
    }
46
47
    /**
48
     * @return bool
49
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
50
     */
51
    public function isSuccess(): bool
52
    {
53
        return $this->status() >= 200 && $this->status() < 300;
54
    }
55
56
    /**
57
     * @return bool
58
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
59
     */
60
    public function isOk(): bool
61
    {
62
        return $this->isSuccess();
63
    }
64
65
    /**
66
     * @return bool
67
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
68
     */
69
    public function isRedirect(): bool
70
    {
71
        return $this->status() >= 300 && $this->status() < 400;
72
    }
73
74
    /**
75
     * @return bool
76
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
77
     */
78
    public function isClientError(): bool
79
    {
80
        return $this->status() >= 400 && $this->status() < 500;
81
    }
82
83
    /**
84
     * @return bool
85
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
86
     */
87
    public function isServerError(): bool
88
    {
89
        return $this->status() >= 500;
90
    }
91
92
    /**
93
     * @return ResponseInterface
94
     */
95
    public function response()
96
    {
97
        return $this->response;
98
    }
99
100
    /**
101
     * @return string
102
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
103
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
104
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
105
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
106
     */
107
    public function body(): string
108
    {
109
        return $this->response->getContent($this->throwErrors);
110
    }
111
112
    /**
113
     * @return string
114
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
115
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
116
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
117
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
118
     */
119
    public function asString(): string
120
    {
121
        return $this->body();
122
    }
123
124
    /**
125
     * @return mixed
126
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
127
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
128
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
129
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
130
     */
131
    public function asObject()
132
    {
133
        return json_decode($this->body());
134
    }
135
136
    /**
137
     * @return \Illuminate\Support\Collection<mixed>
0 ignored issues
show
The doc-type \Illuminate\Support\Collection<mixed> could not be parsed: Expected "|" or "end of type", but got "<" at position 30. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
138
     *
139
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
140
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
141
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
142
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
143
     */
144
    public function asCollection(): Collection
145
    {
146
        return collect($this->asArray());
147
    }
148
149
    /**
150
     * @return mixed
151
     *
152
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
153
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
154
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
155
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
156
     */
157
    public function asArray()
158
    {
159
        return json_decode($this->body(), true);
160
    }
161
162
    /**
163
     * @return resource
164
     */
165
    public function toStream()
166
    {
167
        if (method_exists($this->response, 'toStream')) {
168
            return $this->response->toStream($this->throwErrors);
169
        }
170
171
        throw new \BadMethodCallException('method toStream does not exists');
172
    }
173
174
    /**
175
     * @return bool
176
     *
177
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
178
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
179
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
180
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
181
     */
182
    public function isJson(): bool
183
    {
184
        return $this->contentType() == 'application/json';
185
    }
186
187
    /**
188
     * @param string $header
189
     *
190
     * @return mixed|string
191
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
192
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
193
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
194
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
195
     */
196
    public function header(string $header)
197
    {
198
        $header = Str::lower($header);
199
200
        if (array_key_exists($header, $this->headers())) {
201
            return $this->headers()[$header];
202
        }
203
204
        return '';
205
    }
206
207
    /**
208
     * @param array<mixed> $headers
209
     *
210
     * @return array<String>
211
     */
212
    protected function normalizeHeaders(array $headers)
213
    {
214
        return collect($headers)->transform(function ($item, $key) {
0 ignored issues
show
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
215
            return $item[0];
216
        })->all();
217
    }
218
219
    /**
220
     * @return array<String>
221
     *
222
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
223
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
224
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
225
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
226
     */
227
    public function headers(): array
228
    {
229
        return $this->normalizeHeaders($this->response->getHeaders());
230
    }
231
232
    /**
233
     * @return string
234
     */
235
    public function requestUrl(): string
236
    {
237
        return $this->info()['url'] ?: '';
238
    }
239
240
    /**
241
     * @return float
242
     */
243
    public function executionTime(): float
244
    {
245
        return $this->info()['total_time'] ?: 0.0;
246
    }
247
248
    /**
249
     * @return array|mixed|null
250
     */
251
    public function customData()
252
    {
253
        return $this->info()['user_data'];
254
    }
255
256
    /**
257
     * @return array|mixed|null
258
     */
259
    public function info()
260
    {
261
        return $this->response->getInfo();
262
    }
263
264
    /**
265
     * @return mixed|string
266
     *
267
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
268
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
269
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
270
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
271
     */
272
    public function contentType()
273
    {
274
        return $this->header('content-type');
275
    }
276
277
    /**
278
     * @return mixed|string
279
     *
280
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
281
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
282
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
283
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
284
     */
285
    public function userAgent()
286
    {
287
        return $this->header('user-agent');
288
    }
289
}
290