Root Handler Documentation

Overview

The RootHandler class is the primary HTTP request handler for the Java HTTP server. It implements HttpHandler and serves as the main router, responsible for processing all incoming HTTP requests and serving appropriate content based on the requested path and host.


Core Functionality

This handler performs the following key operations:


1. Virtual Host Resolution


2. Content Type Routing

The handler intelligently routes requests based on file extensions:


3. Path Resolution

The handler implements a sophisticated fallback mechanism:


4. Special Host Handling

Development URLs (dev.norlund-johan-lukas.com and ludwig.norlund-johan-lukas.com) receive special treatment with direct path mapping.


5. Video Streaming Support

For MP4 files, the handler supports:


Response Codes


Error Handling

The handler includes comprehensive error handling for:

All error conditions result in serving a custom 404 page from www/static/notfound.html.

flowchart TD A(Start) --> B[Get Headers and Host] B --> C{Host is null?} C -->|Yes| D[Set host = unknown] C -->|No| E[Extract request path and split] D --> E E --> F[Initialize variables:
htmlFilePath, targetFile, userPath] F --> G{userPath from
domainMap is null?} G -->|Yes| H[Set default userPath
Set targetFile = notfound.html] G -->|No| I{Path ends
with .html?} H --> I I -->|Yes| K[Extract targetFile from path] I -->|No| L{Path ends
with .php?} K --> R L -->|Yes| M[Run runPhp method] L -->|No| N[Continue with response = null] M --> O{PHP result
not null?} O -->|No| P[Read notfound.html
Set Content-Type HTML
Send 404 response
Return] O -->|Yes| Q[Set response = requestedPhp] N --> R Q --> R P --> Z(End) R{Host is dev or
ludwig domain?} R -->|Yes| S1[Set htmlFilePath = userPath] R -->|No| S2{Path != / AND
not ends with .php?} S1 --> FINAL S2 -->|Yes| T[Irregular path handling] S2 -->|No| FINAL T --> U[Construct fullPath from userPath/static/requestPath] U --> V{Path is not .html?} V -->|Yes| W[Check if resolvePath exists] V -->|No| CHECK_EXISTS W --> X{resolvePath
exists?} X -->|No| Y[Try resolvePath/index.html] X -->|Yes| STATIC_CHECK Y --> Y1{index.html
exists?} Y1 -->|No| Y2[Try resolvePath/index.php] Y1 -->|Yes| STATIC_CHECK Y2 --> Y3{index.php
exists?} Y3 -->|No| SERVE_404 Y3 -->|Yes| PHP_PROCESS[Process PHP via runPhp] PHP_PROCESS --> PHP_CHECK{PHP result
not null?} PHP_CHECK -->|Yes| SEND_PHP[Set Content-Type
Send 200 with PHP response
Return] PHP_CHECK -->|No| SERVE_404 STATIC_CHECK{File ends with
.css, .js, or .pdf?} STATIC_CHECK -->|Yes| SET_STATIC[Set fullPath = resolvePath
Set appropriate content type] STATIC_CHECK -->|No| IMG_CHECK IMG_CHECK{File ends with
.mp4 or image ext?} IMG_CHECK -->|Yes| SET_MEDIA[Set fullPath = resolvePath
Set content type] IMG_CHECK -->|No| FALLBACK[Append index.html to fullPath] SET_STATIC --> CHECK_EXISTS SET_MEDIA --> CHECK_EXISTS FALLBACK --> CHECK_EXISTS CHECK_EXISTS{fullPath
exists?} CHECK_EXISTS -->|No| SERVE_404[Read notfound.html
Send 404 response] CHECK_EXISTS -->|Yes| VIDEO_CHECK VIDEO_CHECK{Is .mp4
file?} VIDEO_CHECK -->|Yes| RANGE_CHECK{Range header
present?} VIDEO_CHECK -->|No| SERVE_FILE[Read file into response
Set Content-Type
Send 200 response] RANGE_CHECK -->|No| FULL_VIDEO[Send entire video file
with 200 response] RANGE_CHECK -->|Yes| PARTIAL_VIDEO[Parse range
Send partial content
with 206 response
Return] SERVE_FILE --> WRITE_CLOSE SERVE_404 --> WRITE_CLOSE FULL_VIDEO --> Z PARTIAL_VIDEO --> Z SEND_PHP --> Z WRITE_CLOSE[Write response to OutputStream
Close stream
Return] WRITE_CLOSE --> Z FINAL[Set htmlFilePath = userPath/static/targetFile] FINAL --> FINAL_CHECK{Path ends
with .php?} FINAL_CHECK -->|Yes| FINAL_SEND FINAL_CHECK -->|No| FINAL_EXISTS{htmlFilePath
file exists?} FINAL_EXISTS -->|No| FINAL_404[Set response = notfound.html] FINAL_EXISTS -->|Yes| FINAL_READ[Read htmlFilePath into response] FINAL_404 --> FINAL_SEND FINAL_READ --> FINAL_SEND FINAL_SEND[Set Content-Type HTML
Send 200 response
Write and close stream] FINAL_SEND --> Z Z(End)