Difference between revisions for Users / Eo Ny / dev
| Merge of Version1 & Version2 | |
|---|---|
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | The |
| 6 | |
| 7 | **File Location:** |
| 8 | **Language:** PHP |
| 9 | **Dependencies:** Database class, Session classes, Utility classes (##Ut##), Diagnostics class (##Diag##) |
| 10 | |
| 11 | ---- |
| 12 | |
| 13 | === Class Properties === |
| 14 | |
| 15 | ====Public Properties==== |
| 16 | |
| 17 | #| |
| 18 | *| Property | Type | Description |* |
| 19 | || ##$tls_session## | bool | Indicates if the current session uses HTTPS/TLS encryption || |
| 20 | || ##$request_uri## | string | Normalized REQUEST_URI (e.g., 'PageOfNoReturn/show?a=1') || |
| 21 | || ##$ip## | string | Client's real IP address (accounts for proxies) || |
| 22 | || ##$sess## | Session | Reference to the Session object || |
| 23 | || ##$method## | string | Current HTTP method/request type || |
| 24 | |# |
| 25 | |
| 26 | ==== Private Properties ==== |
| 27 | |
| 28 | |
| 29 | === Constructor === |
| 30 | |
| 31 | %%php |
| 32 | public function __construct(&$db) |
| 33 | |
| 34 | |
| 35 | **Purpose:** Initializes the Http object and sets up HTTP session handling. |
| 36 | |
| 37 | **Parameters:** |
| 38 | |
| 39 | |
| 40 | **Initialization Steps:** |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | **Example:** |
| 49 | |
| 50 | $http = new Http($db); |
| 51 | |
| 52 | |
| 53 | --- |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | Initializes the session handler (file-based or database-based). |
| 61 | |
| 62 | **Parameters:** |
| 63 | |
| 64 | - Bit 2 ( |
| 65 | |
| 66 | **Features:** |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | **Example:** |
| 73 | |
| 74 | $http->session(0); // Normal session |
| 75 | $http->session(2); // Static file serving mode |
| 76 | |
| 77 | |
| 78 | --- |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | Determines if a page can be cached and prepares the cache check. |
| 84 | |
| 85 | **Parameters:** |
| 86 | |
| 87 | |
| 88 | |
| 89 | **Caching Rules:** |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | **Example:** |
| 96 | |
| 97 | $http->check_cache('HomePage', 'show'); |
| 98 | |
| 99 | |
| 100 | --- |
| 101 | |
| 102 | |
| 103 | Saves the generated page content to cache file. |
| 104 | |
| 105 | **Features:** |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | **Example:** |
| 112 | |
| 113 | // Called at end of page rendering |
| 114 | $http->store_cache(); |
| 115 | |
| 116 | |
| 117 | --- |
| 118 | |
| 119 | |
| 120 | Invalidates all cached versions of a page. |
| 121 | |
| 122 | **Parameters:** |
| 123 | |
| 124 | |
| 125 | **Returns:** |
| 126 | |
| 127 | |
| 128 | **Process:** |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | **Example:** |
| 135 | |
| 136 | $count = $http->invalidate_page('HomePage'); |
| 137 | echo "Invalidated $count cache entries"; |
| 138 | |
| 139 | |
| 140 | --- |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | Switches base URL from HTTP to HTTPS. |
| 146 | |
| 147 | **Purpose:** |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | **Example:** |
| 153 | |
| 154 | $http->secure_base_url(); |
| 155 | // $db->base_url now uses https:// |
| 156 | |
| 157 | |
| 158 | --- |
| 159 | |
| 160 | |
| 161 | Enforces HTTPS for a specific URL and redirects if necessary. |
| 162 | |
| 163 | **Parameters:** |
| 164 | |
| 165 | |
| 166 | **Behavior:** |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | **Example:** |
| 172 | |
| 173 | $http->ensure_tls('/secure/payment'); |
| 174 | |
| 175 | |
| 176 | --- |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | Detects client's real IP address accounting for proxies. |
| 182 | |
| 183 | **Proxy Headers Checked (in order):** |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | **Features:** |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | **Configuration in Database:** |
| 196 | |
| 197 | |
| 198 | |
| 199 | **Example:** |
| 200 | |
| 201 | $client_ip = $http->ip; // e.g., "203.0.113.42" |
| 202 | |
| 203 | |
| 204 | --- |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | Detects if current connection uses HTTPS/TLS. |
| 210 | |
| 211 | **Checks (any being true = HTTPS):** |
| 212 | - ##$_SERVER['HTTPS']## is 'on' |
| 213 | - ##$_SERVER['SERVER_PORT']## is 443 |
| 214 | - ##$_SERVER['HTTP_X_FORWARDED_PROTO']## is 'https' |
| 215 | - ##$_SERVER['HTTP_X_FORWARDED_SSL']## is 'on' |
| 216 | - ##$_SERVER['HTTP_X_FORWARDED_PORT']## is 443 |
| 217 | |
| 218 | ---- |
| 219 | |
| 220 | ==== Security Headers ==== |
| 221 | |
| 222 | ===== ##http_security_headers(): void## ===== |
| 223 | |
| 224 | |
| 225 | ==== HTTP Methods ==== |
| 226 | |
| 227 | ===== ##redirect($url, $permanent = false): void## ===== |
| 228 | Performs an HTTP redirect. |
| 229 | |
| 230 | **Parameters:** |
| 231 | |
| 232 | |
| 233 | |
| 234 | **Features:** |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | **Example:** |
| 240 | |
| 241 | $http->redirect('http://example.com/new-page', true); // 301 |
| 242 | $http->redirect('/wiki/HomePage'); // 302 |
| 243 | |
| 244 | |
| 245 | --- |
| 246 | |
| 247 | |
| 248 | Safe exit/die with cleanup. |
| 249 | |
| 250 | **Cleanup Operations:** |
| 251 | |
| 252 | |
| 253 | |
| 254 | **Example:** |
| 255 | |
| 256 | $http->terminate(); |
| 257 | |
| 258 | |
| 259 | --- |
| 260 | |
| 261 | |
| 262 | Sets HTTP response status code. |
| 263 | |
| 264 | **Supported Status Codes:** |
| 265 | |
| 266 | 200 => 'OK' |
| 267 | 206 => 'Partial Content' |
| 268 | 301 => 'Moved Permanently' |
| … | … |
| 279 | 500 => 'Internal Server Error' |
| 280 | 501 => 'Not Implemented' |
| 281 | 503 => 'Service Unavailable' |
| 282 | |
| 283 | |
| 284 | **Example:** |
| 285 | |
| 286 | $http->status(404); // Send 404 Not Found |
| 287 | |
| 288 | |
| 289 | --- |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | Disables caching of the current page. |
| 295 | |
| 296 | **Parameters:** |
| 297 | |
| 298 | - |
| 299 | - |
| 300 | |
| 301 | **Headers Set:** |
| 302 | |
| 303 | |
| 304 | |
| 305 | **Example:** |
| 306 | |
| 307 | $http->no_cache(); // Client-side only |
| 308 | $http->no_cache(false); // Both client & server |
| 309 | |
| 310 | |
| 311 | --- |
| 312 | |
| 313 | |
| 314 | Marks page as publicly cacheable. |
| 315 | |
| 316 | **Headers Set:** |
| 317 | |
| 318 | |
| 319 | **Example:** |
| 320 | |
| 321 | $http->cache_promisc(); |
| 322 | |
| 323 | |
| 324 | --- |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | Determines best language based on browser preferences. |
| 330 | |
| 331 | **Features:** |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | **Example Header:** |
| 338 | |
| 339 | Accept-Language: en-US,en;q=0.9,de;q=0.8 |
| 340 | |
| 341 | |
| 342 | **Returns:** |
| 343 | |
| 344 | |
| 345 | --- |
| 346 | |
| 347 | |
| 348 | Returns list of available language translations. |
| 349 | |
| 350 | **Parameters:** |
| 351 | |
| 352 | - |
| 353 | - |
| 354 | |
| 355 | **Features:** |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | **Returns:** |
| 362 | |
| 363 | |
| 364 | **Example:** |
| 365 | |
| 366 | $all_langs = $http->available_languages(false); |
| 367 | $allowed = $http->available_languages(true); |
| 368 | |
| 369 | |
| 370 | --- |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | Serves files with proper HTTP headers and caching. |
| 376 | |
| 377 | **Parameters:** |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | **Features:** |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | **Special Paths:** |
| 391 | |
| 392 | $http->sendfile(404); // Serves file defined by HTTP_404 constant |
| 393 | $http->sendfile(403); // Serves file defined by HTTP_403 constant |
| 394 | |
| 395 | |
| 396 | **Example:** |
| 397 | |
| 398 | $http->sendfile('uploads/document.pdf', 'my-document.pdf', 30); |
| 399 | |
| 400 | |
| 401 | --- |
| 402 | |
| 403 | |
| 404 | Returns MIME type for a file. |
| 405 | |
| 406 | **Returns:** |
| 407 | |
| 408 | |
| 409 | |
| 410 | **Example:** |
| 411 | |
| 412 | $mime = $http->mime_type('file.pdf'); // 'application/pdf' |
| 413 | |
| 414 | |
| 415 | --- |
| 416 | |
| 417 | |
| 418 | Loads and caches MIME types from configuration. |
| 419 | |
| 420 | **Features:** |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | --- |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | Compresses HTTP response with gzip/x-gzip. |
| 431 | |
| 432 | **Features:** |
| 433 | |
| 434 | |
| 435 | |
| 436 | - 860 bytes < content < 1 MB |
| 437 | - Client accepts compression |
| 438 | - Headers not already sent |
| 439 | |
| 440 | **Example:** |
| 441 | |
| 442 | $http->gzip(); |
| 443 | |
| 444 | |
| 445 | --- |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | Parses URL-encoded strings with special character handling. |
| 451 | |
| 452 | **Purpose:** |
| 453 | |
| 454 | |
| 455 | |
| 456 | **Example:** |
| 457 | |
| 458 | $data = $http->parse_str('name=John&age=30'); |
| 459 | |
| 460 | |
| 461 | --- |
| 462 | |
| 463 | |
| 464 | Extracts and normalizes REQUEST_URI from server. |
| 465 | |
| 466 | **Normalization:** |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | --- |
| 474 | |
| 475 | |
| 476 | Removes prefix from path (case-insensitive). |
| 477 | |
| 478 | --- |
| 479 | |
| 480 | |
| 481 | Loads security header configuration from files. |
| 482 | |
| 483 | **Files Supported:** |
| 484 | - ##csp.conf## / ##csp_custom.conf## |
| 485 | - ##permissions_policy.conf## / ##permissions_policy_custom.conf## |
| 486 | |
| 487 | ---- |
| 488 | |
| 489 | === Configuration Dependencies === |
| 490 | |
| 491 | |
| 492 | |
| 493 | === Constants Used === |
| 494 | |
| 495 | |
| 496 | |
| 497 | === Workflow Examples === |
| 498 | |
| 499 | ==== Example 1: Handling a GET Request ==== |
| 500 | |
| 501 | %%php |
| 502 | // In main wiki entry point |
| 503 | $http = new Http($db); |
| 504 | $http->session(0); // Start session |
| … | … |
| 516 | |
| 517 | // Possibly compress output |
| 518 | $http->gzip(); |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | $http = new Http($db); // Constructor detects TLS requirement |
| 525 | // If TLS is enabled and user wasn't in TLS before: |
| 526 | // - Sets TLS session flag |
| 527 | // - Marks session with TLS cookie |
| 528 | // - Redirects to HTTPS version |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | // User edits a page |
| 535 | $http = new Http($db); |
| 536 | $count = $http->invalidate_page('HomePage'); |
| 537 | // All cached versions (different languages, methods) are invalidated |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | $http = new Http($db); |
| 544 | $http->session(2); // Static file mode - no session replay prevention |
| 545 | |
| 546 | // Serve with 30-day cache |
| 547 | $http->sendfile('uploads/manual.pdf', 'user-manual.pdf', 30); |
| 548 | |
| 549 | |
| 550 | --- |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | --- |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | --- |
| 612 | |
| 613 | |
| 614 | |
| 615 | The class integrates with WackoWiki's diagnostic system: |
| 616 | |
| 617 | |
| 618 | // Diagnostic messages are preserved across redirects |
| 619 | // via session flash data |
| 620 | |
| 621 | // Check cached pages (debug comments in output): |
| 622 | // <!-- WackoWiki Caching Engine: page cached at 2024-01-15 12:30:45 GMT --> |
| 623 | %% |
| 624 | |
| 625 | ---- |
| 626 | |
| 627 | === Related Classes === |
| 628 | - **Session Classes** (##SessionFileStore##, ##SessionDbalStore##) - Session management backends |
| 629 | - **Database Class** - Configuration and cache metadata storage |
| 630 | - **Ut Utility Class** - String/path utilities |
| 631 | - **Diag Class** - Diagnostic logging |
| 632 | |
| 633 | ---- |
| 634 | |
| 635 | === Version History === |
| 636 | - Supports PHP 8.0+ (uses match expressions, union types) |
| 637 | - Follows RFC 9110 for HTTP header handling |
| 638 | - Modern cookie security practices |
| 639 | |
| 640 | ---- |
| 641 | |
| 642 | === Conclusion === |
| 643 | |
| 644 | The ##Http## class is the central request/response handler in WackoWiki, managing everything from session initialization to security headers to file serving. Understanding this class is essential for: |
| 645 | - Extending WackoWiki with custom request handlers |
| 646 | - Implementing custom session logic |
| 647 | - Adding new security policies |
| 648 | - Optimizing cache strategies |
| 649 | - Debugging HTTP-related issues |