Skip to content

Commit 243400c

Browse files
committed
adding missing stuff in nette/http
1 parent c64a266 commit 243400c

File tree

8 files changed

+94
-10
lines changed

8 files changed

+94
-10
lines changed

http/cs/request.texy

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

186186

187+
getOrigin(): ?UrlImmutable .[method]
188+
------------------------------------
189+
Vrací origin, ze kterého požadavek přišel. Origin se skládá z protokolu, hostname a portu - například `https://example.com:8080`. Vrací `null`, pokud hlavička origin není přítomna nebo je nastavena na `'null'`.
190+
191+
```php
192+
$origin = $httpRequest->getOrigin();
193+
echo $origin; // https://example.com:8080
194+
echo $origin?->getHost(); // example.com
195+
```
196+
197+
Prohlížeč posílá hlavičku `Origin` v následujících případech:
198+
- Požadavky mezi doménami (AJAX volání na jinou doménu)
199+
- POST, PUT, DELETE a další modifikující požadavky
200+
- Požadavky provedené pomocí Fetch API
201+
202+
Prohlížeč NEPOSÍLÁ hlavičku `Origin` při:
203+
- Běžných GET požadavcích na stejnou doménu (navigace v rámci téže domény)
204+
- Přímé navigaci zadáním URL do adresního řádku
205+
- Požadavcích z jiných klientů než prohlížeče (pokud není ručně přidána)
206+
207+
.[note]
208+
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).
209+
210+
187211
detectLanguage(array $langs): ?string .[method]
188212
-----------------------------------------------
189213
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 +413,11 @@ getTemporaryFile(): string .[method]
389413
Vrací cestu k dočasné lokaci uploadovaného souboru. V případě, že upload nebyl úspěšný, vrací `''`.
390414

391415

416+
__toString(): string .[method]
417+
------------------------------
418+
Vrací cestu k dočasnému umístění nahraného souboru. To umožňuje objekt `FileUpload` použít přímo jako řetězec.
419+
420+
392421
isImage(): bool .[method]
393422
-------------------------
394423
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,30 @@ $body = $httpRequest->getRawBody();
184184
```
185185

186186

187+
getOrigin(): ?UrlImmutable .[method]
188+
------------------------------------
189+
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'`.
190+
191+
```php
192+
$origin = $httpRequest->getOrigin();
193+
echo $origin; // https://example.com:8080
194+
echo $origin?->getHost(); // example.com
195+
```
196+
197+
The browser sends the `Origin` header in the following cases:
198+
- Cross-origin requests (AJAX calls to a different domain)
199+
- POST, PUT, DELETE, and other modifying requests
200+
- Requests made using the Fetch API
201+
202+
The browser does NOT send the `Origin` header for:
203+
- Regular GET requests to the same domain (same-origin navigation)
204+
- Direct navigation by typing a URL into the address bar
205+
- Requests from non-browser clients
206+
207+
.[note]
208+
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.
209+
210+
187211
detectLanguage(array $langs): ?string .[method]
188212
-----------------------------------------------
189213
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 +413,11 @@ getTemporaryFile(): string .[method]
389413
Returns the path to the temporary location of the uploaded file. If the upload was not successful, it returns `''`.
390414

391415

416+
__toString(): string .[method]
417+
------------------------------
418+
Returns the path to the temporary location of the uploaded file. This allows the `FileUpload` object to be used directly as a string.
419+
420+
392421
isImage(): bool .[method]
393422
-------------------------
394423
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)