Skip to content

Commit 5b662fc

Browse files
committed
eip
1 parent c64a266 commit 5b662fc

8 files changed

Lines changed: 126 additions & 10 deletions

File tree

http/cs/request.texy

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,46 @@ $body = $httpRequest->getRawBody();
184184
```
185185

186186

187+
getDecodedBody(): mixed .[method]
188+
---------------------------------
189+
Vrací dekódovaný obsah těla HTTP požadavku na základě hlavičky `Content-Type`.
190+
191+
```php
192+
// Pro Content-Type: application/json
193+
$data = $httpRequest->getDecodedBody();
194+
echo $data->name; // přístup k JSON vlastnostem
195+
196+
// Pro Content-Type: application/x-www-form-urlencoded
197+
$data = $httpRequest->getDecodedBody(); // stejné jako $_POST
198+
```
199+
200+
Aktuálně podporuje pouze `application/json` a `application/x-www-form-urlencoded`. Jiné typy obsahu vyvolají výjimku.
201+
202+
203+
getOrigin(): ?UrlImmutable .[method]
204+
------------------------------------
205+
Vrací origin (počátek), ze kterého požadavek přišel. Origin se skládá ze schématu (protokolu), hostname a portu - například `https://example.com:8080`. Vrací `null`, pokud hlavička origin není přítomna nebo je nastavena na `'null'`.
206+
207+
```php
208+
$origin = $httpRequest->getOrigin();
209+
echo $origin; // https://example.com:8080
210+
echo $origin?->getHost(); // example.com
211+
```
212+
213+
Prohlížeč posílá hlavičku `Origin` v následujících případech:
214+
- Požadavky mezi doménami (AJAX volání na jinou doménu)
215+
- POST, PUT, DELETE a další modifikující požadavky
216+
- Požadavky provedené pomocí Fetch API
217+
218+
Prohlížeč NEPOSÍLÁ hlavičku `Origin` při:
219+
- Běžných GET požadavcích na stejnou doménu (navigace v rámci téže domény)
220+
- Přímé navigaci zadáním URL do adresního řádku
221+
- Požadavcích z jiných klientů než prohlížeče (pokud není ručně přidána)
222+
223+
.[note]
224+
Na rozdíl od hlavičky `Referer` obsahuje `Origin` pouze schéma, host a port - nikoli celou cestu URL. To ji činí vhodnější pro bezpečnostní kontroly při zachování soukromí uživatele. Hlavička `Origin` se primárně používá pro validaci [CORS |nette:glossary#Cross-Origin Resource Sharing (CORS)] (Cross-Origin Resource Sharing).
225+
226+
187227
detectLanguage(array $langs): ?string .[method]
188228
-----------------------------------------------
189229
Detekuje jazyk. Jako parametr `$lang` předáme pole s jazyky, které aplikace podporuje, a ona vrátí ten, který by viděl návštěvníkův prohlížeč nejraději. Nejsou to žádná kouzla, jen se využívá hlavičky `Accept-Language`. Pokud nedojde k žádné shodě, vrací `null`.
@@ -389,6 +429,11 @@ getTemporaryFile(): string .[method]
389429
Vrací cestu k dočasné lokaci uploadovaného souboru. V případě, že upload nebyl úspěšný, vrací `''`.
390430

391431

432+
__toString(): string .[method]
433+
------------------------------
434+
Vrací cestu k dočasnému umístění nahraného souboru. To umožňuje objekt `FileUpload` použít přímo jako řetězec.
435+
436+
392437
isImage(): bool .[method]
393438
-------------------------
394439
Vrací `true`, pokud nahraný soubor je obrázek ve formátu JPEG, PNG, GIF, WebP nebo AVIF. Detekce probíhá na základě jeho signatury a neověřuje se integrita celého souboru. Zda není obrázek poškozený lze zjistit například pokusem o jeho [načtení |#toImage].

http/cs/response.texy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ isSent(): bool .[method]
3434
Vrací, zda už došlo k odeslání hlaviček ze serveru do prohlížeče, a tedy již není možné odesílat hlavičky či měnit stavový kód.
3535

3636

37-
setHeader(string $name, string $value) .[method]
38-
------------------------------------------------
39-
Odešle HTTP hlavičku a **přepíše** dříve odeslanou hlavičkou stejného jména.
37+
setHeader(string $name, ?string $value) .[method]
38+
-------------------------------------------------
39+
Odešle HTTP hlavičku a **přepíše** dříve odeslanou hlavičkou stejného jména. Pokud je `$value` `null`, bude záhlaví odstraněno.
4040

4141
```php
4242
$httpResponse->setHeader('Pragma', 'no-cache');
@@ -115,8 +115,8 @@ $httpResponse->sendAsFile('faktura.pdf');
115115
```
116116

117117

118-
setCookie(string $name, string $value, $time, ?string $path=null, ?string $domain=null, ?bool $secure=null, ?bool $httpOnly=null, ?string $sameSite=null) .[method]
119-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
118+
setCookie(string $name, string $value, $time, ?string $path=null, ?string $domain=null, ?bool $secure=null, ?bool $httpOnly=null, ?string $sameSite='Lax') .[method]
119+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
120120
Odešle cookie. Výchozí hodnoty parametrů:
121121

122122
| `$path` | `'/'` | cookie má dosah na všechny cesty v (sub)doméně *(konfigurovatelné)*

http/cs/urls.texy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Můžeme pracovat i s jednotlivými query parametry pomocí:
8282
|---------------------------------------------------
8383
| `setQuery(string\|array $query)` | `getQueryParameters(): array`
8484
| `setQueryParameter(string $name, $val)` | `getQueryParameter(string $name)`
85+
| `appendQuery(string|array $query)` |
8586

8687

8788
getDomain(int $level = 2): string .[method]
@@ -107,6 +108,11 @@ $url->isEqual('https://nette.org');
107108
```
108109

109110

111+
canonicalize() .[method]
112+
------------------------
113+
Převede URL do kanonického tvaru. To zahrnuje například seřazení parametrů v query stringu podle abecedy, převod hostname na malá písmena a odstranění nadbytečných znaků.
114+
115+
110116
Url::isAbsolute(string $url): bool .[method]{data-version:3.3.2}
111117
----------------------------------------------------------------
112118
Ověřuje, zda je URL absolutní. URL je považována za absolutní, pokud začíná schématem (např. http, https, ftp) následovaným dvojtečkou.

http/en/request.texy

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,46 @@ $body = $httpRequest->getRawBody();
184184
```
185185

186186

187+
getDecodedBody(): mixed .[method]
188+
---------------------------------
189+
Returns the decoded content of the HTTP request body based on the `Content-Type` header.
190+
191+
```php
192+
// For Content-Type: application/json
193+
$data = $httpRequest->getDecodedBody();
194+
echo $data->name; // access JSON properties
195+
196+
// For Content-Type: application/x-www-form-urlencoded
197+
$data = $httpRequest->getDecodedBody(); // same as $_POST
198+
```
199+
200+
Currently supports only `application/json` and `application/x-www-form-urlencoded`. Other content types will throw an exception.
201+
202+
203+
getOrigin(): ?UrlImmutable .[method]
204+
------------------------------------
205+
Returns the origin from which the request came. An origin consists of the scheme (protocol), hostname, and port - for example, `https://example.com:8080`. Returns `null` if the origin header is not present or is set to `'null'`.
206+
207+
```php
208+
$origin = $httpRequest->getOrigin();
209+
echo $origin; // https://example.com:8080
210+
echo $origin?->getHost(); // example.com
211+
```
212+
213+
The browser sends the `Origin` header in the following cases:
214+
- Cross-origin requests (AJAX calls to a different domain)
215+
- POST, PUT, DELETE, and other modifying requests
216+
- Requests made using the Fetch API
217+
218+
The browser does NOT send the `Origin` header for:
219+
- Regular GET requests to the same domain (same-origin navigation)
220+
- Direct navigation by typing a URL into the address bar
221+
- Requests from non-browser clients
222+
223+
.[note]
224+
Unlike the `Referer` header, `Origin` contains only the scheme, host, and port - not the full URL path. This makes it more suitable for security checks while preserving user privacy. The `Origin` header is primarily used for [CORS |nette:glossary#Cross-Origin Resource Sharing (CORS)] (Cross-Origin Resource Sharing) validation.
225+
226+
187227
detectLanguage(array $langs): ?string .[method]
188228
-----------------------------------------------
189229
Detects the language. Pass an array of languages supported by the application as the `$langs` parameter, and it will return the one preferred by the visitor's browser. It's not magic; it just uses the `Accept-Language` header. If no match is found, it returns `null`.
@@ -389,6 +429,11 @@ getTemporaryFile(): string .[method]
389429
Returns the path to the temporary location of the uploaded file. If the upload was not successful, it returns `''`.
390430

391431

432+
__toString(): string .[method]
433+
------------------------------
434+
Returns the path to the temporary location of the uploaded file. This allows the `FileUpload` object to be used directly as a string.
435+
436+
392437
isImage(): bool .[method]
393438
-------------------------
394439
Returns `true` if the uploaded file is a JPEG, PNG, GIF, WebP, or AVIF image. Detection is based on its signature and does not verify the integrity of the entire file. Whether an image is corrupted can be determined, for example, by trying to [load it |#toImage].

http/en/response.texy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ isSent(): bool .[method]
3434
Returns whether headers have already been sent from the server to the browser, meaning it is no longer possible to send headers or change the status code.
3535

3636

37-
setHeader(string $name, string $value) .[method]
38-
------------------------------------------------
39-
Sends an HTTP header and **overwrites** a previously sent header of the same name.
37+
setHeader(string $name, ?string $value) .[method]
38+
-------------------------------------------------
39+
Sends an HTTP header and **overwrites** a previously sent header of the same name. If `$value` is `null`, the header will be removed.
4040

4141
```php
4242
$httpResponse->setHeader('Pragma', 'no-cache');
@@ -115,8 +115,8 @@ $httpResponse->sendAsFile('invoice.pdf');
115115
```
116116

117117

118-
setCookie(string $name, string $value, $time, ?string $path=null, ?string $domain=null, ?bool $secure=null, ?bool $httpOnly=null, ?string $sameSite=null) .[method]
119-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
118+
setCookie(string $name, string $value, $time, ?string $path=null, ?string $domain=null, ?bool $secure=null, ?bool $httpOnly=null, ?string $sameSite='Lax') .[method]
119+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
120120
Sends a cookie. Default parameter values:
121121

122122
| `$path` | `'/'` | cookie is available for all paths within the (sub)domain *(configurable)*

http/en/urls.texy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ We can also work with individual query parameters using:
8282
|---------------------------------------------------
8383
| `setQuery(string\|array $query)` | `getQueryParameters(): array`
8484
| `setQueryParameter(string $name, $val)` | `getQueryParameter(string $name)`
85+
| `appendQuery(string|array $query)` |
8586

8687

8788
getDomain(int $level = 2): string .[method]
@@ -107,6 +108,11 @@ $url->isEqual('https://nette.org');
107108
```
108109

109110

111+
canonicalize() .[method]
112+
------------------------
113+
Converts the URL to canonical form. This includes, for example, sorting the parameters in the query string alphabetically, converting the hostname to lowercase, and removing redundant characters.
114+
115+
110116
Url::isAbsolute(string $url): bool .[method]{data-version:3.3.2}
111117
----------------------------------------------------------------
112118
Checks if a URL is absolute. A URL is considered absolute if it begins with a scheme (e.g., http, https, ftp) followed by a colon.

nette/cs/glossary.texy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ Cross-Site Request Forgery (CSRF)
3636
Nette Framework **automaticky chrání formuláře a signály v presenterech** před tímto typem útoku. A to tím, že zabraňuje jejich odeslání či vyvolání z jiné domény.
3737

3838

39+
Cross-Origin Resource Sharing (CORS)
40+
------------------------------------
41+
CORS je bezpečnostní mechanismus, který umožňuje webové stránce provádět JavaScriptové požadavky na jinou doménu, než ze které byla stránka načtena. Bez CORS prohlížeče takové požadavky z bezpečnostních důvodů blokují.
42+
43+
Například pokud vaše webová stránka běží na `https://myapp.com` a pokusí se pomocí JavaScriptu (AJAX, Fetch API) načíst data z `https://api.example.com`, prohlížeč ověří, zda API server tento požadavek mezi doménami povoluje. API server musí odpovědět speciálními HTTP hlavičkami, jako je `Access-Control-Allow-Origin: https://myapp.com`, aby udělil povolení.
44+
45+
3946
Dependency Injection
4047
--------------------
4148
Dependency Injection (DI) je návrhový vzor, který říká, jak oddělit vytváření objektů od jejich závislostí. Tedy že třída není zodpovědná za vytváření nebo inicializaci svých závislostí, ale místo toho jsou jí tyto závislosti poskytovány externím kódem (tím může i [DI kontejner |#Dependency Injection kontejner]). Výhoda spočívá v tom, že umožňuje větší flexibilitu kódu, lepší srozumitelnost a snazší testování aplikace, protože závislosti jsou snadno nahraditelné a izolované od ostatních částí kódu. Více v kapitole [Co je Dependency Injection? |dependency-injection:introduction]

nette/en/glossary.texy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ A Cross-Site Request Forgery attack involves the attacker luring a victim to a p
3636
Nette Framework **automatically protects forms and signals in presenters** against this type of attack by preventing them from being submitted or triggered from another domain.
3737

3838

39+
Cross-Origin Resource Sharing (CORS)
40+
------------------------------------
41+
CORS is a security mechanism that allows a web page to make JavaScript requests to a different domain than the one from which the page was loaded. Without CORS, browsers block such requests for security reasons.
42+
43+
For example, if your website runs at `https://myapp.com` and tries to fetch data from `https://api.example.com` using JavaScript (AJAX, Fetch API), the browser will check if the API server allows this cross-origin request. The API server must respond with special HTTP headers, such as `Access-Control-Allow-Origin: https://myapp.com`, to grant permission.
44+
45+
3946
Dependency Injection
4047
--------------------
4148
Dependency Injection (DI) is a design pattern that dictates how to separate the creation of objects from their dependencies. This means a class is not responsible for creating or initializing its dependencies; instead, these dependencies are provided by external code (which could be a [DI container |#Dependency Injection Container]). The advantage lies in increased code flexibility, better understandability, and easier application testing, as dependencies are easily replaceable and isolated from other code parts. More in the chapter [What is Dependency Injection? |dependency-injection:introduction]

0 commit comments

Comments
 (0)