Skip to content

Commit 0d8d6da

Browse files
committed
docs: complete documentation update for all classes
- Response: added download(), file(), streamDownload(), mime type detection - Session: added CSRF protection methods (csrfToken, csrfField, validateCsrf, validateRequestCsrf) - Request: added getMethod, setUri, getTarget, isFormData documentation - Uri: added getQueryToArray, fromString, getAuthority, getUserInfo methods - Cookie: added escaped() method, SameSite parameter documentation - Headers: added getHeaderLine, protocol version methods - Status: added complete list of HTTP status codes - UploadedFile: added getStream, getExtension, getBasename, isMoved methods - README: added quick usage examples
1 parent 69f5b89 commit 0d8d6da

9 files changed

Lines changed: 515 additions & 36 deletions

File tree

docs/client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ $response = $client->request('GET', 'https://api.example.com/resource');
4444
```
4545

4646
### Handling Responses
47-
The response from the request is returned as a [Response](https://github.com/alexsandrov16/http/blob/main/docs/response.md) object. You can access the content, status code, and headers of the response.
47+
The response from the request is returned as a [Response](https://github.com/al3x5dev/http/blob/main/docs/response.md) object. You can access the content, status code, and headers of the response.
4848

4949
```php
5050
$content = $response->getBody(); // Response content

docs/cookie.md

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
# Cookie
22
This `Cookie` class allows you to manage cookies in your application.
33

4-
## Method `Cookie::set($name, $value, $expires, $path, $domain, $secure, $httponly)`.
4+
## Method `Cookie::set($name, $value, $expires, $path, $domain, $secure, $httponly, $sameSite)`.
55
This static method adds a cookie before sending it to the browser with the specified parameters. Returns true if the cookie was set correctly, otherwise returns false.
66

77
**Parameters:**
88
- `$name` (string): The name of the cookie.
99
- `$value` (mixed): The value of the cookie.
1010
- `$expires` (int): The expiration time of the cookie in seconds from the current time. Default is 0 (does not expire).
1111
- `$path` (string): Path where the cookie will be available. Default is '/'.
12-
- `$domain` (string|null): Domain to which the cookie is associated. Default is null.
13-
- `$secure` (bool): Indicates if the cookie should only be sent through secure connections. Default is false.
14-
- `$httponly` (bool): Indicates whether the cookie should only be accessible via HTTP. Default is false.
12+
- `$domain` (string): Domain to which the cookie is associated. Default is empty string.
13+
- `$secure` (bool): Indicates if the cookie should only be sent through secure connections (HTTPS). Default is false.
14+
- `$httponly` (bool): Indicates whether the cookie should only be accessible via HTTP (not JavaScript). Default is true.
15+
- `$sameSite` (string): SameSite attribute - must be 'Strict', 'Lax', or 'None'. Default is 'Lax'.
16+
1517
```php
16-
Cookie::set('helloworld','Hello World!');
17-
// or
18-
Cookie::set('hellophp','Hello PHP!',300,'/','localhost');
18+
// Basic cookie
19+
Cookie::set('helloworld', 'Hello World!');
20+
21+
// With options
22+
Cookie::set('hellophp', 'Hello PHP!', 300, '/', 'localhost', true, true, 'Strict');
23+
24+
// SameSite=None requires secure=true (automatically enforced)
25+
Cookie::set('crosssite', 'value', 0, '/', '', true, true, 'None');
1926
```
2027

2128
## Method `Cookie::get(?string $name = null, mixed $default = null)`.
@@ -34,16 +41,45 @@ Cookie::get('unavailable','value');
3441
// return "value"
3542
```
3643

44+
## Method `Cookie::escaped(?string $name = null, mixed $default = null)`.
45+
This static method retrieves cookie values HTML-escaped to prevent XSS attacks. Uses ENT_QUOTES | ENT_HTML5 for complete coverage.
46+
47+
**Parameters:**
48+
- `$name` (string|null): The cookie name, or null for all cookies.
49+
- `$default` (mixed): Default value if cookie doesn't exist.
50+
51+
```php
52+
// Get single escaped cookie
53+
Cookie::escaped('username');
54+
// Returns HTML-escaped value
55+
56+
// Get all escaped cookies
57+
Cookie::escaped();
58+
/* Returns all $_COOKIE values with HTML entities escaped */
59+
```
60+
**Note:** Use this method when outputting cookie values in HTML to prevent XSS attacks.
61+
3762
## Method `Cookie::has(string $name)`.
3863
This static method checks if a specific cookie exists in `$_COOKIE` and returns true otherwise it returns false.
3964
```php
4065
Cookie::has('helloworld');
4166
// return true
4267
```
4368

44-
## Method `Cookie::remove(string $name)`.
45-
This method deletes a specified cookie.
69+
## Method `Cookie::remove(string $name, string $path, string $domain, bool $secure, bool $httponly, string $sameSite)`.
70+
This method deletes a specified cookie by setting its expiration to the past. Uses the same parameters as the original cookie for proper removal.
71+
72+
**Parameters:**
73+
- `$name` (string): The cookie name to remove.
74+
- `$path` (string): Path where the cookie was set. Default is '/'.
75+
- `$domain` (string): Domain where the cookie was set. Default is empty.
76+
- `$secure` (bool): Whether the cookie was secure. Default is false.
77+
- `$httponly` (bool): Whether the cookie was HTTP-only. Default is true.
78+
- `$sameSite` (string): SameSite attribute. Default is 'Lax'.
4679

4780
```php
4881
Cookie::remove('helloworld');
82+
83+
// Remove with specific parameters (must match original cookie)
84+
Cookie::remove('session', '/', 'example.com', true, true, 'Strict');
4985
```

docs/headers.md

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# Headers
2-
The Trait `Headers` provides functionality related to the headers of an HTTP message. It is used only by the [Mk4U\Http\Request.php](https://github.com/alexsandrov16/http/blob/main/docs/request.md) and [Mk4U\Http\Response.php](https://github.com/alexsandrov16/http/blob/main/docs/response.md) classes.
2+
The Trait `Headers` provides functionality related to the headers of an HTTP message. It is used only by the [Mk4U\Http\Request.php](https://github.com/al3x5dev/http/blob/main/docs/request.md) and [Mk4U\Http\Response.php](https://github.com/al3x5dev/http/blob/main/docs/response.md) classes.
3+
4+
## Protocol Version
5+
6+
### `getProtocolVersion()`
7+
Returns the HTTP protocol version.
8+
```php
9+
$request->getProtocolVersion();
10+
// or
11+
$response->getProtocolVersion();
12+
// returns "HTTP/1.1", "HTTP/2.0", etc.
13+
```
14+
15+
### `setProtocolVersion(?string $version = null)`
16+
Sets the HTTP protocol version. Valid versions are: '1.0', '1.1', '2.0', '3.0'.
17+
18+
```php
19+
$request->setProtocolVersion('1.1');
20+
// or
21+
$response->setProtocolVersion('2.0');
22+
```
23+
24+
## Headers Methods
325

426
## `getHeaders`
527
This method returns an array with all the headers of the response.
@@ -11,24 +33,42 @@ $response->getHeaders();
1133
```
1234

1335
## `getHeader`
14-
This method returns the value of a specific header.
36+
This method returns the value of a specific header as an array.
1537

1638
**Parameters:**
1739
- `$name` (string): The name of the header.
1840

1941
```php
20-
$request->getHeader($name);
21-
// or
22-
$response->getHeader($name);
42+
$request->getHeader('Content-Type');
43+
// returns ["application/json"]
44+
45+
$response->getHeader('Cache-Control');
46+
// returns ["max-age=3600"]
47+
```
48+
49+
## `getHeaderLine`
50+
This method returns the value of a specific header as a comma-separated string (useful for headers with multiple values).
51+
52+
**Parameters:**
53+
- `$name` (string): The name of the header.
54+
55+
```php
56+
$request->getHeaderLine('Accept');
57+
// returns "text/html, application/json"
58+
59+
$response->getHeaderLine('Set-Cookie');
60+
// returns "cookie1=value1, cookie2=value2"
2361
```
2462

2563
## `hasHeader`
2664
This method checks if a specific header exists in the response. Returns a boolean value.
2765

2866
```php
29-
$request->hasHeader($name);
30-
// or
31-
$response->hasHeader($name);
67+
$request->hasHeader('Content-Type');
68+
// returns true or false
69+
70+
$response->hasHeader('X-Custom-Header');
71+
// returns true or false
3272
```
3373

3474
## `setHeader`
@@ -73,9 +113,9 @@ This method adds a header to the response. If the header already exists, the val
73113
- `$value` (string|array): The value of the header.
74114

75115
```php
76-
$request->addHeader($name, $value);
116+
$request->addHeader('Accept', 'application/json');
77117
// or
78-
$response->addHeader($name, $value);
118+
$response->addHeader('Set-Cookie', 'cookie2=value2');
79119
```
80120

81121
## `removeHeader`
@@ -85,7 +125,7 @@ This method removes a header from the response.
85125
- `$name` (string): The name of the header to remove.
86126

87127
```php
88-
$request->removeHeader($headers);
128+
$request->removeHeader('X-Debug-Header');
89129
// or
90-
$response->removeHeader($headers);
130+
$response->removeHeader('X-Cache-Header');
91131
```

docs/request.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The `Request` class allows you to interact with the data coming into your applic
66
There are two ways to create a new Request object, you can create a request based on PHP's superglobal variables, or simply simulate a request:
77

88
### Simulating a request
9-
When you simulate a request you must pass as parameters the http method, the uri or a [Uri object](https://github.com/alexsandrov16/http/blob/main/docs/uri.md), the headers (optional), the request body (optional) and the protocol version (optional).
9+
When you simulate a request you must pass as parameters the http method, the uri or a [Uri object](https://github.com/al3x5dev/http/blob/main/docs/uri.md), the headers (optional), the request body (optional) and the protocol version (optional).
1010
```php
1111
require __DIR__ . '/vendor/autoload.php';
1212

@@ -85,10 +85,10 @@ $request->getMethod();
8585
```
8686

8787
### Method `Request::setUri($uri, $preserv_host = false)`.
88-
This method sets the [Uri object](https://github.com/alexsandrov16/http/blob/main/docs/uri.md) for the current request and optionally preserves the host in the request headers. Returns a copy of the Request object with the updated [Uri](https://github.com/alexsandrov16/http/blob/main/docs/uri.md) object and, optionally, the preserved host in the headers.
88+
This method sets the [Uri object](https://github.com/al3x5dev/http/blob/main/docs/uri.md) for the current request and optionally preserves the host in the request headers. Returns a copy of the Request object with the updated [Uri](https://github.com/al3x5dev/http/blob/main/docs/uri.md) object and, optionally, the preserved host in the headers.
8989

9090
**Parameters:**
91-
- `$uri` (Uri): the [Uri object](https://github.com/alexsandrov16/http/blob/main/docs/uri.md) to set for the request.
91+
- `$uri` (Uri): the [Uri object](https://github.com/al3x5dev/http/blob/main/docs/uri.md) to set for the request.
9292
- `$preserv_host` (bool): Indicates whether to preserve the host in the request
9393
```php
9494
$request->setUri($uri);
@@ -97,7 +97,7 @@ $request->setUri($uri,true);
9797
```
9898

9999
### Method `Request::getUri()`.
100-
This method returns the [Uri object](https://github.com/alexsandrov16/http/blob/main/docs/uri.md) associated with the current request.
100+
This method returns the [Uri object](https://github.com/al3x5dev/http/blob/main/docs/uri.md) associated with the current request.
101101
```php
102102
$request->getUri();
103103
// return object(Mk4U\Http\Uri)
@@ -174,7 +174,7 @@ $request->rawData();
174174
```
175175

176176
### Method `Request::files()`.
177-
This method returns an array containing the files uploaded to the server in the current request stored in the [UploadedFile](https://github.com/alexsandrov16/http/blob/main/docs/uploadedfile.md) or an empty array if there are no files.
177+
This method returns an array containing the files uploaded to the server in the current request stored in the [UploadedFile](https://github.com/al3x5dev/http/blob/main/docs/uploadedfile.md) or an empty array if there are no files.
178178
```php
179179
$request->files();
180180
/* return [
@@ -187,3 +187,46 @@ $request->files();
187187
}
188188
]*/
189189
```
190+
191+
### Method `Request::getMethod()`.
192+
This method returns the HTTP method used in the request as an uppercase string.
193+
```php
194+
$request->getMethod();
195+
// return "GET", "POST", "PUT", etc.
196+
```
197+
198+
### Method `Request::getUri()`.
199+
This method returns the [Uri object](https://github.com/al3x5dev/http/blob/main/docs/uri.md) associated with the current request.
200+
```php
201+
$request->getUri();
202+
// return object(Mk4U\Http\Uri)
203+
```
204+
205+
### Method `Request::setUri(Uri $uri, bool $preserveHost = false)`.
206+
This method sets the [Uri object](https://github.com/al3x5dev/http/blob/main/docs/uri.md) for the current request. Optionally preserves the host header.
207+
208+
**Parameters:**
209+
- `$uri` (Uri): The Uri object to set.
210+
- `$preserveHost` (bool): If true, preserves the existing Host header. Default is false.
211+
212+
```php
213+
$uri = new Uri('https://example.com/path');
214+
$request->setUri($uri);
215+
216+
// With host preservation
217+
$request->setUri($uri, true);
218+
```
219+
220+
### Method `Request::getTarget()`.
221+
This method returns the target path of the request (the URI path).
222+
```php
223+
$request->getTarget();
224+
// return "/path/to/resource" or "/" if empty
225+
```
226+
227+
### Method `Request::isFormData()`.
228+
This method determines if the request contains form data (application/x-www-form-urlencoded or multipart/form-data with POST method).
229+
```php
230+
$request->isFormData();
231+
// return true or false
232+
```

docs/response.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ return $response->plain('Hello World!');
2020
// Both implementations return "Hello World!"" and the default status code is 200.
2121
```
2222

23-
Can also create responses for different types of content and [status codes](https://github.com/alexsandrov16/http/blob/main/docs/status.md).
23+
Can also create responses for different types of content and [status codes](https://github.com/al3x5dev/http/blob/main/docs/status.md).
2424
```php
2525
$response=new Response();
2626

@@ -143,4 +143,77 @@ $xml=<<<XML
143143
</note>
144144
XML;
145145
Response::xml($xml, $status, $headers);
146+
```
147+
148+
### Method `Response::download(string $filePath, ?string $filename = null, array $headers = [], bool $display = false)`.
149+
This method generates a response that forces the user's browser to download the file at the given path.
150+
151+
**Parameters:**
152+
- `$filePath` (string): Absolute path to the file to download.
153+
- `$filename` (string|null): Custom filename for the download (optional, defaults to original filename).
154+
- `$headers` (array): Additional HTTP headers (optional).
155+
- `$display` (bool): If true, displays file inline instead of forcing download (default: false).
156+
157+
```php
158+
// Basic download
159+
Response::download('/path/to/file.pdf');
160+
161+
// With custom filename
162+
Response::download('/path/to/file.pdf', 'my-document.pdf');
163+
164+
// With custom headers
165+
Response::download('/path/to/file.pdf', null, ['Cache-Control' => 'no-cache']);
166+
167+
// Display file inline (in browser)
168+
Response::download('/path/to/image.png', display: true);
169+
```
170+
171+
### Method `Response::file(string $filePath, array $headers = [])`.
172+
This method displays a file directly in the user's browser instead of downloading it (inline).
173+
174+
**Parameters:**
175+
- `$filePath` (string): Absolute path to the file.
176+
- `$headers` (array): Additional HTTP headers (optional).
177+
178+
```php
179+
// Display image in browser
180+
Response::file('/path/to/image.png');
181+
182+
// With custom headers
183+
Response::file('/path/to/image.png', ['Cache-Control' => 'public, max-age=3600']);
184+
```
185+
186+
### Method `Response::streamDownload($stream, ?string $filename = null, ?int $filesize = null, array $headers = [])`.
187+
This method downloads a file from a stream, useful for large files to avoid loading them entirely into memory.
188+
189+
**Parameters:**
190+
- `$stream` (resource): A readable stream resource.
191+
- `$filename` (string|null): Custom filename for the download (optional).
192+
- `$filesize` (int|null): Size of the file in bytes (optional).
193+
- `$headers` (array): Additional HTTP headers (optional).
194+
195+
```php
196+
// Download from stream
197+
$stream = fopen('/path/to/large-file.zip', 'r');
198+
Response::streamDownload($stream, 'download.zip', filesize('/path/to/large-file.zip'));
199+
200+
// Or from a URL stream
201+
$stream = fopen('http://example.com/file.zip', 'r');
202+
Response::streamDownload($stream, 'file.zip');
203+
```
204+
205+
### MIME Type Detection
206+
The `Response::download()`, `Response::file()`, and `Response::streamDownload()` methods automatically detect the MIME type based on the file extension:
207+
208+
```php
209+
$response = Response::download('/path/to/document.pdf');
210+
// Content-Type: application/pdf
211+
212+
$response = Response::download('/path/to/image.png');
213+
// Content-Type: image/png
214+
215+
$response = Response::download('/path/to/data.json');
216+
// Content-Type: application/json
217+
218+
// Unknown extensions default to: application/octet-stream
146219
```

0 commit comments

Comments
 (0)