Difference between revisions for Users / Eo Ny / dev




← Previous edit
Next edit →

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