Difference between revisions for Users / Eo Ny / dev




← Previous edit
Next edit →

Version1 Version2 Differences
1   == HTTP Class Technical Documentation ==
  1 {{toc numerate=1}}
2 2
3 3 === Overview ===
4 4
40 40 ----
41 41 === Constructor ===
42 42
43   %%php
  43 %%(hl php)
44 44 public function __construct(&$db)
45 45 %%
46 46
58 58   6. Enforces TLS session upgrade if needed
59 59
60 60 **Example:**
61   %%php
  61 %%(hl php)
62 62 $http = new Http($db);
63 63 %%
64 64
82 82   - Recovers diagnostic logs from previous session
83 83
84 84 **Example:**
85   %%php
  85 %%(hl php)
86 86 $http->session(0); // Normal session
87 87 $http->session(2); // Static file serving mode
88 88 %%
105 105   - ✅ Only cached for anonymous users (no logged-in users)
106 106
107 107 **Example:**
108   %%php
  108 %%(hl php)
109 109 $http->check_cache('HomePage', 'show');
110 110 %%
111 111
112 112 ----
113 113
114   ===== ##store_cache(): void## =====
  114 =====##store_cache(): void##=====
115 115 Saves the generated page content to cache file.
116 116
117 117 **Features:**
121 121   - Only executes if caching flag is set and user is anonymous
122 122
123 123 **Example:**
124   %%php
  124 %%(hl php)
125 125 // Called at end of page rendering
126 126 $http->store_cache();
127 127 %%
144 144   4. Returns count of invalidated caches
145 145
146 146 **Example:**
147   %%php
  147 %%(hl php)
148 148 $count = $http->invalidate_page('HomePage');
149 149 echo "Invalidated $count cache entries";
150 150 %%
162 162   - Called when TLS session is detected
163 163
164 164 **Example:**
165   %%php
  165 %%(hl php)
166 166 $http->secure_base_url();
167 167 // $db->base_url now uses https://
168 168 %%
181 181   - Converts relative URLs using current server name
182 182
183 183 **Example:**
184   %%php
  184 %%(hl php)
185 185 $http->ensure_tls('/secure/payment');
186 186 %%
187 187
209 209   - ##reverse_proxy_header## - Custom header name (default: ##X-Forwarded-For##)
210 210
211 211 **Example:**
212   %%php
  212 %%(hl php)
213 213 $client_ip = $http->ip; // e.g., "203.0.113.42"
214 214 %%
215 215
253 253   - ##2## - Custom policy (from ##csp_custom.conf##)
254 254
255 255 **Example:**
256   %%php
  256 %%(hl php)
257 257 $http->http_security_headers();
258 258 %%
259 259
273 273   - Uses output buffering to work anywhere in page processing
274 274
275 275 **Example:**
276   %%php
  276 %%(hl php)
277 277 $http->redirect('http://example.com/new-page', true); // 301
278 278 $http->redirect('/wiki/HomePage'); // 302
279 279 %%
288 288   - Ends script execution
289 289
290 290 **Example:**
291   %%php
  291 %%(hl php)
292 292 $http->terminate();
293 293 %%
294 294
298 298 Sets HTTP response status code.
299 299
300 300 **Supported Status Codes:**
301   %%php
  301 %%(hl php)
302 302 200 => 'OK'
303 303 206 => 'Partial Content'
304 304 301 => 'Moved Permanently'
318 318 %%
319 319
320 320 **Example:**
321   %%php
  321 %%(hl php)
322 322 $http->status(404); // Send 404 Not Found
323 323 %%
324 324
339 339   - ##Cache-Control: no-store##
340 340
341 341 **Example:**
342   %%php
  342 %%(hl php)
343 343 $http->no_cache(); // Client-side only
344 344 $http->no_cache(false); // Both client & server
345 345 %%
353 353   - ##Cache-Control: public##
354 354
355 355 **Example:**
356   %%php
  356 %%(hl php)
357 357 $http->cache_promisc();
358 358 %%
359 359
398 398   - Associative array: ##['en' => 'en', 'de' => 'de', ...]##
399 399
400 400 **Example:**
401   %%php
  401 %%(hl php)
402 402 $all_langs = $http->available_languages(false);
403 403 $allowed = $http->available_languages(true);
404 404 %%
424 424   - GZip compression for text files
425 425
426 426 **Special Paths:**
427   %%php
  427 %%(hl php)
428 428 $http->sendfile(404); // Serves file defined by HTTP_404 constant
429 429 $http->sendfile(403); // Serves file defined by HTTP_403 constant
430 430 %%
431 431
432 432 **Example:**
433   %%php
  433 %%(hl php)
434 434 $http->sendfile('uploads/document.pdf', 'my-document.pdf', 30);
435 435 %%
436 436
444 444   - Default: ##'application/octet-stream'##
445 445
446 446 **Example:**
447   %%php
  447 %%(hl php)
448 448 $mime = $http->mime_type('file.pdf'); // 'application/pdf'
449 449 %%
450 450
474 474   - Headers not already sent
475 475
476 476 **Example:**
477   %%php
  477 %%(hl php)
478 478 $http->gzip();
479 479 %%
480 480
490 490   - Converts encoding properly
491 491
492 492 **Example:**
493   %%php
  493 %%(hl php)
494 494 $data = $http->parse_str('name=John&age=30');
495 495 %%
496 496
572 572
573 573 === Workflow Examples ===
574 574
575   ==== Example 1: Handling a GET Request ====
576  
577   %%php
  575 ====Example 1: Handling a GET Request====
  576
  577 %%(hl php)
578 578 // In main wiki entry point
579 579 $http = new Http($db);
580 580 $http->session(0); // Start session
596 596
597 597 ==== Example 2: Handling TLS/HTTPS Upgrade ====
598 598
599   %%php
  599 %%(hl php)
600 600 $http = new Http($db); // Constructor detects TLS requirement
601 601 // If TLS is enabled and user wasn't in TLS before:
602 602 // - Sets TLS session flag
606 606
607 607 ==== Example 3: Invalidating Cache After Page Edit ====
608 608
609   %%php
  609 %%(hl php)
610 610 // User edits a page
611 611 $http = new Http($db);
612 612 $count = $http->invalidate_page('HomePage');
615 615
616 616 ==== Example 4: Serving a File ====
617 617
618   %%php
  618 %%(hl php)
619 619 $http = new Http($db);
620 620 $http->session(2); // Static file mode - no session replay prevention
621 621
690 690
691 691 The class integrates with WackoWiki's diagnostic system:
692 692
693   %%php
  693 %%(hl php)
694 694 // Diagnostic messages are preserved across redirects
695 695 // via session flash data
696 696