SQLite Testing: dev

https://wackowiki.org/test     Version: 8 (05/05/2026 19:24)
This is an old revision of Users/EoNy/dev from 05/05/2026 19:24 edited by WikiAdmin.

dev

HTTP Class Technical Documentation

Overview


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.

File Location: src/class/http.php
Language: PHP
Dependencies: Database class, Session classes, Utility classes (Ut), Diagnostics class (Diag)


Class Properties

Public Properties


Property Type Description
$tls_session bool Indicates if the current session uses HTTPS/TLS encryption
$request_uri string Normalized REQUEST_URI (e.g., 'PageOfNoReturn/show?a=1')
$ip string Client's real IP address (accounts for proxies)
$sess Session Reference to the Session object
$method string Current HTTP method/request type

Private Properties


Constructor


php
public function __construct(&$db)	


Purpose: Initializes the Http object and sets up HTTP session handling.

Parameters:

Initialization Steps:
  1. Stores database reference
  2. Extracts and normalizes REQUEST_URI
  3. Detects TLS/HTTPS session status
  4. Determines client's real IP address
  5. Sets up TLS mark cookie name
  6. Enforces TLS session upgrade if needed

Example:
php
$http = new Http($db);	



Core Methods

Session Management

session($route): void

Initializes the session handler (file-based or database-based).

Parameters:

Features:

Example:
php
$http->session(0);  // Normal session
$http->session(2);  // Static file serving mode	



Caching System

check_cache($page, $method): void

Determines if a page can be cached and prepares the cache check.

Parameters:

Caching Rules:

Example:
php
$http->check_cache('HomePage', 'show');	



store_cache(): void

Saves the generated page content to cache file.

Features:

Example:
php
// Called at end of page rendering
$http->store_cache();	



invalidate_page($page): int

Invalidates all cached versions of a page.

Parameters:

Returns:

Process:
  1. Finds all cached versions (different methods/languages)
  2. Touches files to past timestamp (faster than deletion)
  3. Removes entries from cache metadata table
  4. Returns count of invalidated caches

Example:
php
$count = $http->invalidate_page('HomePage');
echo "Invalidated $count cache entries";	



TLS/HTTPS Security

secure_base_url(): void

Switches base URL from HTTP to HTTPS.

Purpose:

Example:
php
$http->secure_base_url();
// $db->base_url now uses https://	



ensure_tls($url): void

Enforces HTTPS for a specific URL and redirects if necessary.

Parameters:

Behavior:

Example:
php
$http->ensure_tls('/secure/payment');	



IP Address Detection

real_ip(): string (Private)

Detects client's real IP address accounting for proxies.

Proxy Headers Checked (in order):
  1. HTTP_X_CLUSTER_CLIENT_IP
  2. HTTP_X_FORWARDED_FOR (or custom header)
  3. HTTP_CLIENT_IP
  4. HTTP_X_REMOTE_ADDR
  5. REMOTE_ADDR (fallback)

Features:

Configuration in Database:

Example:
php
$client_ip = $http->ip;  // e.g., "203.0.113.42"	



HTTPS Detection

tls_session(): bool (Private)

Detects if current connection uses HTTPS/TLS.

Checks (any being true = HTTPS):


Security Headers

http_security_headers(): void


HTTP Methods

redirect($url, $permanent = false): void

Performs an HTTP redirect.

Parameters:

Features:

Example:
php
$http->redirect('http://example.com/new-page', true);  // 301
$http->redirect('/wiki/HomePage');                       // 302	



terminate(): void

Safe exit/die with cleanup.

Cleanup Operations:

Example:
php
$http->terminate();	



status($code): void

Sets HTTP response status code.

Supported Status Codes:
php
200 => 'OK'
206 => 'Partial Content'
301 => 'Moved Permanently'
302 => 'Moved Temporarily'
304 => 'Not Modified'
400 => 'Bad Request'
401 => 'Unauthorized'
403 => 'Forbidden'
404 => 'Not Found'
405 => 'Method Not Allowed'
409 => 'Conflict'
410 => 'Gone'
416 => 'Requested Range Not Satisfiable'
500 => 'Internal Server Error'
501 => 'Not Implemented'
503 => 'Service Unavailable'	


Example:
php
$http->status(404);  // Send 404 Not Found	



Caching Control

no_cache($client_only = true): void

Disables caching of the current page.

Parameters:

Headers Set:

Example:
php
$http->no_cache();        // Client-side only
$http->no_cache(false);   // Both client & server	



cache_promisc(): void

Marks page as publicly cacheable.

Headers Set:

Example:
php
$http->cache_promisc();	



Language Negotiation

user_agent_language(): string

Determines best language based on browser preferences.

Features:

Example Header:
Accept-Language: en-US,en;q=0.9,de;q=0.8	


Returns:


available_languages($subset = true): array

Returns list of available language translations.

Parameters:

Features:

Returns:

Example:
php
$all_langs = $http->available_languages(false);
$allowed = $http->available_languages(true);	



File Serving

sendfile($path, $filename = null, $age = null): void

Serves files with proper HTTP headers and caching.

Parameters:

Features:

Special Paths:
php
$http->sendfile(404);  // Serves file defined by HTTP_404 constant
$http->sendfile(403);  // Serves file defined by HTTP_403 constant	


Example:
php
$http->sendfile('uploads/document.pdf', 'my-document.pdf', 30);	



mime_type($path): string

Returns MIME type for a file.

Returns:

Example:
php
$mime = $http->mime_type('file.pdf');  // 'application/pdf'	



mime_types(): array (Private)

Loads and caches MIME types from configuration.

Features:


Compression

gzip(): void

Compresses HTTP response with gzip/x-gzip.

Features:

Example:
php
$http->gzip();	



Utility Methods

parse_str($str): array (Private)

Parses URL-encoded strings with special character handling.

Purpose:

Example:
php
$data = $http->parse_str('name=John&age=30');	



request_uri(): string (Private)

Extracts and normalizes REQUEST_URI from server.

Normalization:


cut_prefix($prefix, $path): string (Private)

Removes prefix from path (case-insensitive).


get_header_conf($file_name): string (Private)

Loads security header configuration from files.

Files Supported:


Configuration Dependencies



Constants Used



Workflow Examples

Example 1: Handling a GET Request


php
// In main wiki entry point
$http = new Http($db);
$http->session(0);  // Start session

// Check if page can be served from cache
$http->check_cache('HomePage', 'show');

// ... render page content ...

// Store rendered page in cache if applicable
$http->store_cache();

// Send security headers
$http->http_security_headers();

// Possibly compress output
$http->gzip();	

Example 2: Handling TLS/HTTPS Upgrade


php
$http = new Http($db);  // Constructor detects TLS requirement
// If TLS is enabled and user wasn't in TLS before:
// - Sets TLS session flag
// - Marks session with TLS cookie
// - Redirects to HTTPS version	

Example 3: Invalidating Cache After Page Edit


php
// User edits a page
$http = new Http($db);
$count = $http->invalidate_page('HomePage');
// All cached versions (different languages, methods) are invalidated	

Example 4: Serving a File


php
$http = new Http($db);
$http->session(2);  // Static file mode - no session replay prevention

// Serve with 30-day cache
$http->sendfile('uploads/manual.pdf', 'user-manual.pdf', 30);	



Security Considerations

1. IP Address Spoofing

2. Session Security

3. TLS Enforcement

4. Content Security

5. File Serving

6. Cache Security



Performance Optimization

1. Page Caching

2. MIME Type Caching

3. Session Options

4. Compression



Debugging


The class integrates with WackoWiki's diagnostic system:

php
// Diagnostic messages are preserved across redirects
// via session flash data

// Check cached pages (debug comments in output):
// <!-- WackoWiki Caching Engine: page cached at 2024-01-15 12:30:45 GMT -->	



Related Classes



Version History



Conclusion


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: