-
Notifications
You must be signed in to change notification settings - Fork 2
Networking
The Network module handles all network communication for the browser. It manages HTTP requests, responses, resource fetching, and provides caching mechanisms for efficient resource loading.
- Handle HTTP requests and responses
- Fetch web resources (HTML, CSS, images, fonts)
- Manage network timeouts and retries
- Implement resource caching
- Handle redirects and authentication
- Support various content types
Central component coordinating network operations:
- Manages active connections
- Implements request queuing
- Handles resource caching
- Manages connection pooling
- Tracks download progress
Different types of resources that can be fetched:
| Type | Purpose | Example |
|---|---|---|
| HTML | Document content | index.html |
| CSS | Stylesheets | style.css |
| Image | Visual content | image.png |
| Font | Typography | roboto.ttf |
| Script | Executable code | script.js |
The module implements HTTP/1.1 protocol:
- Request: Method (GET, POST), URL, headers
- Response: Status code, headers, body
- Methods: GET for fetching, POST for forms
- Status Codes: Success (2xx), Redirect (3xx), Error (4xx, 5xx)
Primary method for fetching resources:
- Retrieves resource content
- No request body
- Used for HTML, CSS, images, fonts
- Can be cached
For form submissions and data:
- Includes request body
- Used for form data submission
- Generally not cached
- Requires more bandwidth
- 200 OK: Successful request
- 301/302 Redirect: Resource moved
- 304 Not Modified: Cached content valid
- 404 Not Found: Resource doesn't exist
- 500 Error: Server error
Handled content MIME types:
-
text/html: HTML documents -
text/css: CSS stylesheets -
image/*: Various image formats -
font/*: Font files -
text/javascript: JavaScript code
Resources are cached to improve performance:
- Memory Cache: Fast in-memory storage
- Disk Cache: Persistent storage (planned)
- Browser Cache: Managed by HTTP cache headers
Determined by HTTP headers:
-
Cache-Control: Caching directives -
Expires: Expiration date -
ETag: Entity tag for validation -
Last-Modified: Last modification time
Checking if cached content is still valid:
- If-Modified-Since: Conditional request
- ETag: Strong validation
- Expires: Time-based validation
URL Input
↓
Check Cache
↓
If cached and valid → Return cached
If not valid → Send request
↓
HTTP Request
↓
Response Received
↓
Parse Response Headers
↓
Cache if applicable
↓
Return Content
Network resilience features:
- Connection Timeout: Maximum time to connect
- Read Timeout: Maximum time to receive data
- Retries: Automatic retry on failure
- Backoff: Exponential backoff for retries
Automatic redirect support:
- 3xx Status Codes: Indicate redirect
- Location Header: New URL destination
- Chain Following: Up to limit (default 5)
- Infinite Loop Detection: Prevent redirect loops
HTTP header handling:
-
User-Agent: Browser identification -
Accept: Accepted content types -
Accept-Encoding: Compression support -
Referer: Referring page -
Cookie: Session and tracking data
-
Content-Type: MIME type of response -
Content-Length: Size of response body -
Content-Encoding: Compression method -
Set-Cookie: Session cookies
Provides HTML and CSS content for parsing
Fetches font files from URLs
Initiates resource requests for page loading
Handles XMLHttpRequest and fetch API
- Connection Pooling: Reuse connections
- Pipelining: Multiple requests per connection
- Compression: GZIP/DEFLATE support
- Parallel Downloads: Multiple simultaneous requests
- Priority Queuing: Prioritize critical resources
- HTTP/1.1 specification (RFC 7230-7235)
- URL specification
- MIME types standard
- Cache control specifications
- Cookie specifications (RFC 6265)
- HTTP GET requests
- HTTP response parsing
- Status code handling
- Basic header support
- Memory caching
- Simple redirect handling
- Timeout support
Current Limitations:
- HTTP/1.1 only (no HTTP/2)
- No disk cache persistence
- Limited POST support
- No authentication
- No compression (GZIP/DEFLATE)
- No proxy support
- No SSL/TLS (HTTP only)
Planned Features:
- HTTP/2 support
- Persistent disk cache
- Full POST support
- Basic authentication
- Compression support
- HTTPS/SSL support
- Proxy support
- Cookie management
- Form data handling
Current Status:
- No HTTPS support yet
- Limited header validation
- No CSRF protection
Future Plans:
- HTTPS/TLS support
- Security header validation
- CORS support
- CSRF token handling
- Content Security Policy
Network errors are handled gracefully:
- Connection Errors: Network unreachable
- Timeout Errors: Request took too long
- Protocol Errors: Invalid response format
- HTTP Errors: 4xx and 5xx status codes
- DNS Errors: Domain name resolution failure
Different resources may have different settings:
// HTML document - higher priority
request.priority = RequestPriority::High;
request.timeout = Duration::from_secs(30);
// Image - can be lower priority
request.priority = RequestPriority::Low;
request.timeout = Duration::from_secs(15);
// Font - medium priority
request.priority = RequestPriority::Medium;
request.timeout = Duration::from_secs(20);Techniques to reduce bandwidth usage:
- Caching: Avoid redundant downloads
- Compression: GZIP to reduce size
- Image Optimization: Scaled images
- Lazy Loading: Load resources on demand
- Resource Prioritization: Load critical first