|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +**Nette HTTP Component** - A standalone PHP library providing HTTP abstraction for request/response handling, URL manipulation, session management, and file uploads. Part of the Nette Framework ecosystem but usable independently. |
| 8 | + |
| 9 | +- **PHP Version**: 8.1 - 8.5 |
| 10 | +- **Package**: `nette/http` |
| 11 | + |
| 12 | +## Essential Commands |
| 13 | + |
| 14 | +### Running Tests |
| 15 | + |
| 16 | +```bash |
| 17 | +# Run all tests |
| 18 | +vendor/bin/tester tests -s -C |
| 19 | + |
| 20 | +# Run specific test file |
| 21 | +php tests/Http/Request.files.phpt |
| 22 | + |
| 23 | +# Run tests in specific directory |
| 24 | +vendor/bin/tester tests/Http -s -C |
| 25 | +``` |
| 26 | + |
| 27 | +### Static Analysis |
| 28 | + |
| 29 | +```bash |
| 30 | +# Run PHPStan |
| 31 | +composer phpstan |
| 32 | + |
| 33 | +# Or directly |
| 34 | +vendor/bin/phpstan analyse |
| 35 | +``` |
| 36 | + |
| 37 | +## Core Architecture |
| 38 | + |
| 39 | +### Immutability Pattern |
| 40 | + |
| 41 | +The codebase uses a sophisticated immutability strategy: |
| 42 | + |
| 43 | +- **`Request`** - Immutable HTTP request with single wither method `withUrl()` |
| 44 | +- **`Response`** - Mutable for managing response state (headers, cookies, status) |
| 45 | +- **`UrlImmutable`** - Immutable URL with wither methods (`withHost()`, `withPath()`, etc.) |
| 46 | +- **`Url`** - Mutable URL with setters for flexible building |
| 47 | +- **`UrlScript`** - Extends UrlImmutable with script path information |
| 48 | + |
| 49 | +**Design principle**: Data objects (Request) are immutable for integrity; state managers (Response, Session) are mutable for practical management. |
| 50 | + |
| 51 | +### Security-First Design |
| 52 | + |
| 53 | +Input sanitization is mandatory, not optional: |
| 54 | + |
| 55 | +1. **RequestFactory** sanitizes ALL input: |
| 56 | + - Removes invalid UTF-8 sequences |
| 57 | + - Strips control characters (except tab, newline, carriage return) |
| 58 | + - Validates with regex: `[\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]` |
| 59 | + |
| 60 | +2. **Secure defaults everywhere**: |
| 61 | + - Cookies are `httpOnly` by default |
| 62 | + - `SameSite=Lax` by default |
| 63 | + - Session uses strict mode and one-time cookies only |
| 64 | + - HTTPS auto-detection via proxy configuration |
| 65 | + |
| 66 | +3. **FileUpload** security: |
| 67 | + - Content-based MIME detection (not client-provided) |
| 68 | + - `getSanitizedName()` removes dangerous characters |
| 69 | + - Documentation warns against trusting `getUntrustedName()` |
| 70 | + |
| 71 | +### Key Components |
| 72 | + |
| 73 | +#### Request (`src/Http/Request.php`) |
| 74 | +- Immutable HTTP request representation |
| 75 | +- Type-safe access to GET/POST/COOKIE/FILES/headers |
| 76 | +- AJAX detection, same-site checking (`_nss` cookie, formerly `nette-samesite`), language detection |
| 77 | +- Sanitized by RequestFactory before construction |
| 78 | +- **Origin detection**: `getOrigin()` returns scheme + host + port for CORS validation |
| 79 | +- **Basic Auth**: `getBasicCredentials()` returns `[user, password]` array |
| 80 | +- **File access**: `getFile(['my-form', 'details', 'avatar'])` accepts array of keys for nested structures |
| 81 | +- **Warning**: Browsers don't send URL fragments to the server (`$url->getFragment()` returns empty string) |
| 82 | + |
| 83 | +#### Response (`src/Http/Response.php`) |
| 84 | +- Mutable HTTP response management |
| 85 | +- Header manipulation (set/add/delete) |
| 86 | +- Cookie handling with security defaults (use `Response::SameSiteLax`, `SameSiteStrict`, `SameSiteNone` constants) |
| 87 | +- Redirect, cache control, content-type helpers |
| 88 | +- **Download support**: `sendAsFile('invoice.pdf')` triggers browser download dialog |
| 89 | +- Checks `isSent()` to prevent modification after output starts |
| 90 | +- **Cookie domain**: If specified, includes subdomains; if omitted, excludes subdomains |
| 91 | + |
| 92 | +#### RequestFactory (`src/Http/RequestFactory.php`) |
| 93 | +- Creates Request from PHP superglobals (`$_GET`, `$_POST`, etc.) |
| 94 | +- Configurable proxy support (RFC 7239 Forwarded header + X-Forwarded-*) |
| 95 | +- URL filters for cleaning malformed URLs |
| 96 | +- File upload normalization into FileUpload objects |
| 97 | + |
| 98 | +#### URL Classes |
| 99 | +- **`Url`** - Mutable URL builder with setters, supports `appendQuery()` to add parameters |
| 100 | +- **`UrlImmutable`** - Immutable URL with wither methods |
| 101 | + - `resolve($reference)` - Resolves relative URLs like a browser (v3.3.2+) |
| 102 | + - `withoutUserInfo()` - Removes user and password |
| 103 | +- **`UrlScript`** - Request URL with virtual components: |
| 104 | + - `baseUrl` - Base URL including domain and path to app root |
| 105 | + - `basePath` - Path to application root directory |
| 106 | + - `scriptPath` - Path to current script |
| 107 | + - `relativePath` - Script name relative to basePath |
| 108 | + - `relativeUrl` - Everything after baseUrl (query + fragment) |
| 109 | + - `pathInfo` - Rarely used part after script name |
| 110 | +- **Static helpers**: |
| 111 | + - `Url::isAbsolute($url)` - Checks if URL has scheme (v3.3.2+) |
| 112 | + - `Url::removeDotSegments($path)` - Normalizes path by removing `.` and `..` (v3.3.2+) |
| 113 | +- All support IDN (via `ext-intl`), canonicalization, query manipulation |
| 114 | + |
| 115 | +#### Session (`src/Http/Session.php` + `SessionSection.php`) |
| 116 | +- **Auto-start modes**: |
| 117 | + - `smart` - Start only when session data is accessed (default) |
| 118 | + - `always` - Start immediately with application |
| 119 | + - `never` - Manual start required |
| 120 | +- Namespaced sections to prevent naming conflicts |
| 121 | +- **SessionSection API**: Use explicit methods instead of magic properties: |
| 122 | + - `$section->set('userName', 'john')` - Write variable |
| 123 | + - `$section->get('userName')` - Read variable (returns null if missing) |
| 124 | + - `$section->remove('userName')` - Delete variable |
| 125 | + - `$section->set('flash', $message, '30 seconds')` - Third parameter sets expiration |
| 126 | +- Per-section or per-variable expiration |
| 127 | +- Custom session handler support |
| 128 | +- **Events**: `$onStart`, `$onBeforeWrite` - Callbacks invoked after session starts or before write to disk |
| 129 | +- **Session ID management**: `regenerateId()` generates new ID (e.g., after login for security) |
| 130 | + |
| 131 | +#### FileUpload (`src/Http/FileUpload.php`) |
| 132 | +- Safe file upload handling |
| 133 | +- Content-based MIME detection (requires `ext-fileinfo`) |
| 134 | +- Image validation and conversion (supports JPEG, PNG, GIF, WebP, AVIF) |
| 135 | +- Sanitized filename generation |
| 136 | +- **Filename methods**: |
| 137 | + - `getUntrustedName()` - Original browser-provided name (⚠️ never trust!) |
| 138 | + - `getSanitizedName()` - Safe ASCII-only name `[a-zA-Z0-9.-]` with correct extension |
| 139 | + - `getSuggestedExtension()` - Extension based on MIME type (v3.2.4+) |
| 140 | + - `getUntrustedFullPath()` - Full path for directory uploads (PHP 8.1+, ⚠️ never trust!) |
| 141 | + |
| 142 | +### Nette DI Integration |
| 143 | + |
| 144 | +Two extensions provide auto-wiring: |
| 145 | + |
| 146 | +1. **HttpExtension** (`src/Bridges/HttpDI/HttpExtension.php`) |
| 147 | + - **Registers**: `http.requestFactory`, `http.request`, `http.response` |
| 148 | + - **Configuration**: proxy IPs, headers, CSP, X-Frame-Options, cookie defaults |
| 149 | + - **CSP with nonce**: Automatically generates nonce for inline scripts |
| 150 | + ```neon |
| 151 | + http: |
| 152 | + csp: |
| 153 | + script-src: [nonce, strict-dynamic, self] |
| 154 | + ``` |
| 155 | + Use in templates: `<script n:nonce>...</script>` - nonce filled automatically |
| 156 | + - **Cookie defaults**: `cookiePath`, `cookieDomain: domain` (includes subdomains), `cookieSecure: auto` |
| 157 | + - **X-Frame-Options**: `frames: SAMEORIGIN` (default) or `frames: true` to allow all |
| 158 | +
|
| 159 | +2. **SessionExtension** (`src/Bridges/HttpDI/SessionExtension.php`) |
| 160 | + - **Registers**: `session.session` |
| 161 | + - **Configuration**: `autoStart: smart|always|never`, expiration, handler, all PHP `session.*` directives in camelCase |
| 162 | + - **Tracy debugger panel**: Enable with `debugger: true` in config |
| 163 | + - **Session cookie**: Configure separately with `cookieDomain`, `cookieSamesite: Strict|Lax|None` |
| 164 | +
|
| 165 | +## Code Conventions |
| 166 | +
|
| 167 | +### Strict PHP Standards |
| 168 | +
|
| 169 | +Every file must start with: |
| 170 | +```php |
| 171 | +declare(strict_types=1); |
| 172 | +``` |
| 173 | + |
| 174 | +### Modern PHP Features |
| 175 | + |
| 176 | +Heavily uses PHP 8.1+ features: |
| 177 | + |
| 178 | +```php |
| 179 | +// Constructor property promotion with readonly |
| 180 | +public function __construct( |
| 181 | + private readonly UrlScript $url, |
| 182 | + private readonly array $post = [], |
| 183 | + private readonly string $method = 'GET', |
| 184 | +) { |
| 185 | +} |
| 186 | + |
| 187 | +// Named arguments |
| 188 | +setcookie($name, $value, [ |
| 189 | + 'expires' => $expire ? (int) DateTime::from($expire)->format('U') : 0, |
| 190 | + 'httponly' => $httpOnly ?? true, |
| 191 | + 'samesite' => $sameSite ?? self::SameSiteLax, |
| 192 | +]); |
| 193 | + |
| 194 | +// First-class callables |
| 195 | +Nette\Utils\Callback::invokeSafe( |
| 196 | + 'session_start', |
| 197 | + [['read_and_close' => $this->readAndClose]], |
| 198 | + fn(string $message) => throw new Exception($message) |
| 199 | +); |
| 200 | +``` |
| 201 | + |
| 202 | +### Property Documentation with SmartObject |
| 203 | + |
| 204 | +Uses `@property-read` magic properties with Nette's SmartObject trait: |
| 205 | + |
| 206 | +```php |
| 207 | +/** |
| 208 | + * @property-read UrlScript $url |
| 209 | + * @property-read array $query |
| 210 | + * @property-read string $method |
| 211 | + */ |
| 212 | +class Request implements IRequest |
| 213 | +{ |
| 214 | + use Nette\SmartObject; |
| 215 | +} |
| 216 | +``` |
| 217 | + |
| 218 | +### Testing with Nette Tester |
| 219 | + |
| 220 | +Test files use `.phpt` extension and follow this pattern: |
| 221 | + |
| 222 | +```php |
| 223 | +<?php |
| 224 | + |
| 225 | +declare(strict_types=1); |
| 226 | + |
| 227 | +use Nette\Http\Url; |
| 228 | +use Tester\Assert; |
| 229 | + |
| 230 | +require __DIR__ . '/../bootstrap.php'; |
| 231 | + |
| 232 | +test('Url canonicalization removes duplicate slashes', function () { |
| 233 | + $url = new Url('http://example.com/path//to/../file.txt'); |
| 234 | + $url->canonicalize(); |
| 235 | + Assert::same('http://example.com/path/file.txt', (string) $url); |
| 236 | +}); |
| 237 | + |
| 238 | +test('Url handles IDN domains', function () { |
| 239 | + $url = new Url('https://xn--tst-qla.de/'); |
| 240 | + $url->canonicalize(); |
| 241 | + Assert::same('https://täst.de/', (string) $url); |
| 242 | +}); |
| 243 | +``` |
| 244 | + |
| 245 | +The `test()` helper is defined in `tests/bootstrap.php`. |
| 246 | + |
| 247 | +## Development Guidelines |
| 248 | + |
| 249 | +### When Adding Features |
| 250 | + |
| 251 | +1. **Read existing code first** - Understand patterns before modifying |
| 252 | +2. **Security first** - Consider injection, XSS, CSRF implications |
| 253 | +3. **Maintain immutability contracts** - Don't add setters to immutable classes |
| 254 | +4. **Test thoroughly** - Add `.phpt` test files in `tests/Http/` |
| 255 | +5. **Check Windows compatibility** - Tests run on Windows in CI |
| 256 | + |
| 257 | +### Common Patterns |
| 258 | + |
| 259 | +**Proxy detection** - Use RequestFactory's `setProxy()` for trusted proxy IPs: |
| 260 | +```php |
| 261 | +$factory->setProxy(['127.0.0.1', '::1']); |
| 262 | +``` |
| 263 | + |
| 264 | +**URL filtering** - Clean malformed URLs via urlFilters: |
| 265 | +```php |
| 266 | +// Remove spaces from path |
| 267 | +$factory->urlFilters['path']['%20'] = ''; |
| 268 | + |
| 269 | +// Remove trailing punctuation |
| 270 | +$factory->urlFilters['url']['[.,)]$'] = ''; |
| 271 | +``` |
| 272 | + |
| 273 | +**Cookie security** - Response uses secure defaults: |
| 274 | +```php |
| 275 | +// httpOnly=true, sameSite=Lax by default |
| 276 | +$response->setCookie('name', 'value', '1 hour'); |
| 277 | +``` |
| 278 | + |
| 279 | +**Session sections** - Namespace session data with explicit methods: |
| 280 | +```php |
| 281 | +$section = $session->getSection('cart'); |
| 282 | +$section->set('items', []); |
| 283 | +$section->setExpiration('20 minutes'); |
| 284 | + |
| 285 | +// Per-variable expiration |
| 286 | +$section->set('flash', $message, '30 seconds'); |
| 287 | + |
| 288 | +// Events |
| 289 | +$session->onBeforeWrite[] = function () use ($section) { |
| 290 | + $section->set('lastSaved', time()); |
| 291 | +}; |
| 292 | +``` |
| 293 | + |
| 294 | +## CI/CD Pipeline |
| 295 | + |
| 296 | +GitHub Actions runs: |
| 297 | + |
| 298 | +1. **Tests** (`.github/workflows/tests.yml`): |
| 299 | + - Matrix: Ubuntu/Windows/macOS × PHP 8.1-8.5 × php/php-cgi |
| 300 | + - Lowest dependencies test |
| 301 | + - Code coverage with Coveralls |
| 302 | + |
| 303 | +2. **Static Analysis** (`.github/workflows/static-analysis.yml`): |
| 304 | + - PHPStan level 5 (informative only) |
| 305 | + |
| 306 | +3. **Coding Style** (`.github/workflows/coding-style.yml`): |
| 307 | + - Nette Coding Standard (PSR-12 based) |
| 308 | + |
| 309 | +## Architecture Principles |
| 310 | + |
| 311 | +- **Single Responsibility** - Each class has one clear purpose |
| 312 | +- **Dependency Injection** - Constructor injection, no service locators |
| 313 | +- **Type Safety** - Everything typed (properties, parameters, returns) |
| 314 | +- **Fail Fast** - Validation at boundaries, exceptions for errors |
| 315 | +- **Framework Optional** - Works standalone or with Nette DI |
| 316 | + |
| 317 | +## Important Notes |
| 318 | + |
| 319 | +- **Browser behavior** - Browsers don't send URL fragments or Origin header for same-origin GET requests |
| 320 | +- **Proxy configuration critical** - Required for correct IP detection and HTTPS detection |
| 321 | +- **Session auto-start modes**: |
| 322 | + - `smart` - Starts only when `$section->get()`/`set()` is called (default) |
| 323 | + - `always` - Starts immediately on application bootstrap |
| 324 | + - `never` - Must call `$session->start()` manually |
| 325 | +- **URL encoding nuances** - Respects PHP's `arg_separator.input` for query parsing |
| 326 | +- **FileUpload validation** - Always check `hasFile()` and `isOk()` before processing |
| 327 | +- **UrlScript virtual components** - Generated by RequestFactory, understand baseUrl vs basePath distinction |
| 328 | +- **CSP nonce in templates** - Use `<script n:nonce>` for automatic nonce insertion with CSP headers |
0 commit comments