Skip to content

Implement static file handler (foundation for #18)#8

Closed
annikaholmqvist94 wants to merge 11 commits intomainfrom
feature/18-static-file-handler
Closed

Implement static file handler (foundation for #18)#8
annikaholmqvist94 wants to merge 11 commits intomainfrom
feature/18-static-file-handler

Conversation

@annikaholmqvist94
Copy link

@annikaholmqvist94 annikaholmqvist94 commented Feb 11, 2026

Changes

Foundation work for #18 - GET handling for static files

What's Added

Core Logic

  • StaticFileHandler - Serves files from /resources/static/
  • MimeTypeResolver - Maps file extensions to Content-Type headers

Security Features

  • Path validation prevents directory traversal attacks
  • Blocks suspicious patterns: ../, //, \
  • Returns 403 Forbidden for security violations
  • Comprehensive security tests included

Testing (35+ test cases)

MimeTypeResolverTest (15 tests):

  • HTML, CSS, JS, image formats
  • Edge cases: null, empty, unknown extensions
  • Case-insensitive extension handling
  • Path handling with directories

StaticFileHandlerTest (20+ tests):

  • Successful file serving (200 OK)
  • Correct Content-Type headers
  • 404 for missing files
  • 403 for path traversal attempts
  • 405 for non-GET methods
  • Root path serves index.html

Example Static Files

  • index.html - Demo homepage with modern design
  • styles.css - Professional gradient styling
  • app.js - JavaScript functionality demo

File Structure

src/main/java/org/juv25d/handler/
├── MimeTypeResolver.java          (67 lines)
└── StaticFileHandler.java         (198 lines)

src/main/resources/static/
├── index.html                      (40 lines)
├── css/
│   └── styles.css                  (125 lines)
└── js/
    └── app.js                      (10 lines)

src/test/java/org/juv25d/handler/
├── MimeTypeResolverTest.java      (88 lines)
└── StaticFileHandlerTest.java     (147 lines)

Integration Status

⏸️ Not yet integrated with Server/ConnectionHandler

This PR contains all core logic but avoids modifying
Server/SocketServer/ConnectionHandler to prevent conflicts with PR #28.

Integration Plan (after PR #28 merges):

  1. Rebase this branch on main
  2. Add integration code in ConnectionHandler:
if (request.method().equalsIgnoreCase("GET")) {
    HttpResponse response = StaticFileHandler.handle(request);
    HttpResponseWriter.write(socket.getOutputStream(), response);
}
  1. Request final review
  2. Merge

Architecture

Architecture decisions documented in ADR-001 (see PR #7 ).

Dependencies

Review Focus

  1. Security validation logic - Is path traversal prevention robust?
  2. MIME type mappings - Are common file types covered?
  3. Test coverage - Are edge cases properly tested?
  4. Code quality - Clear naming and documentation?
  5. Architecture - Does it follow ADR-001 decisions?

Notes

  • Static files will be bundled in JAR (SpringBoot style)
  • Currently returns 405 for POST/PUT/DELETE (only GET supported)
  • 404 error page includes styled HTML response
  • Logging integrated with existing ServerLogging class

Summary by CodeRabbit

  • New Features

    • HTTP server with request parsing and response handling
    • Docker containerization for deployment
    • Automated CI/CD pipelines for testing and container image publishing
  • Tests

    • Added HTTP response tests
  • Chores

    • Configured project build infrastructure and code quality tools

fmazmz and others added 11 commits February 6, 2026 08:44
* chore: Update POM to Java 25 and rename artifactId/groupId

* update folder name from example to juv25d

---------

Co-authored-by: WHITEROSE <firasmoussa60@gmail.com>
* http parser

* Bunny fixes. (only using input stream to recieve requests)

* Bunny review improvements

* Improved http parser ReadLine helper method to eliminate dependency on mark() and reset(). Implemented handleClient() using socket as a try-with-resources to avoid memory leakage in case of exception thrown by httpparser-methods.

* NumberFormatException fix on line 53 -> 60

* chore: Update POM to Java 25 and rename artifactId/groupId (#11)

* chore: Update POM to Java 25 and rename artifactId/groupId

* update folder name from example to juv25d

---------

Co-authored-by: WHITEROSE <firasmoussa60@gmail.com>

* resolve conflicts

---------

Co-authored-by: Kristina <kristina0x7@gmail.com>
Co-authored-by: WHITEROSE <firasmoussa60@gmail.com>
* Add ServerLogging.java as separate class for logging. Implement said class in SocketServer.java to return logging information upon opening socket and user connecting to server.

* Update ServerLogging.java to include a static initializer block and an empty utility class to prevent instantiation.

* Update ServerLogging.java to reference same class in getLogger argument.

* Update ServerLogging.java to check if handler has already been instantiated and allow for log level to be set by args in JVM (default level 'INFO' if no args provided).

* normalize logging statements to be consistent

* remove unused imports

* Update SockerServer.java to properly log server socket errors.

---------

Co-authored-by: Mats Rönnqvist <mats.f.ronnqvist@gmail.com>
Co-authored-by: WHITEROSE <firasmoussa60@gmail.com>
* update POM with pitest

* add junit plugin dependency
Rebased 4 commits in this PR.
…#27)

* Fix PiTest by defining argLine and removing invalid Mockito javaagent

* self close argline
Core Implementation:
- Add StaticFileHandler for serving files from /resources/static/
- Add MimeTypeResolver for Content-Type detection
- Add security validation to prevent path traversal attacks

Testing:
- Add MimeTypeResolverTest (15 test cases)
- Add StaticFileHandlerTest (20+ test cases)
- All tests passing

Example Files:
- Add index.html demo page with gradient styling
- Add styles.css for professional styling
- Add app.js for JavaScript functionality demo

Note: Integration with Server/ConnectionHandler will be added
after PR #28 merges to avoid conflicts.

Foundation work for #18
@annikaholmqvist94 annikaholmqvist94 added the enhancement New feature or request label Feb 11, 2026
@coderabbitai
Copy link

coderabbitai bot commented Feb 11, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

This PR establishes a complete Java HTTP server project. It renames the project from org.example/JavaTemplate to org.juv25d/JavaHttpServer, adds Maven wrapper scripts and Docker support, implements core HTTP handling with request parsing and response writing, configures CI/CD workflows, and introduces code formatting and mutation testing tools.

Changes

Cohort / File(s) Summary
Build Configuration & Tooling
.editorconfig, .mvn/wrapper/maven-wrapper.properties, .github/workflows/ci.yml, .github/workflows/docker-release.yml, Dockerfile, mvnw, mvnw.cmd, pom.xml
Adds project-wide style rules, Maven wrapper configuration, GitHub Actions workflows for CI and Docker image publishing, multi-stage Dockerfile for Java 25 builds, Maven wrapper startup scripts (POSIX and Windows), and updates pom.xml with new project coordinates (org.juv25d/JavaHttpServer), Java 25 target, and new plugins for code formatting (Spotless) and mutation testing (Pitest).
Core HTTP Server Implementation
src/main/java/org/juv25d/App.java, src/main/java/org/juv25d/SocketServer.java, src/main/java/org/juv25d/parser/HttpParser.java, src/main/java/org/juv25d/logging/ServerLogging.java
Implements HTTP server infrastructure: SocketServer accepts connections on port 3000 and dispatches to virtual threads; HttpParser parses HTTP requests with request line, headers, and optional body validation; ServerLogging provides centralized logger configuration; App.main() invokes SocketServer.createSocket().
HTTP Request & Response Models
src/main/java/org/juv25d/HttpRequest.java, src/main/java/org/juv25d/http/HttpResponse.java, src/main/java/org/juv25d/http/HttpResponseWriter.java
Introduces immutable data carriers: HttpRequest record encapsulates method, path, query, version, headers, and body; HttpResponse class encapsulates status code, text, headers, and body with defensive copies; HttpResponseWriter utility serializes HttpResponse to OutputStream with proper status line, headers, and Content-Length calculation.
Handler Skeletons & Test Stubs
src/main/java/org/juv25d/handler/MimeTypeResolver.java, src/main/java/org/juv25d/handler/StaticFileHandler.java, src/test/java/org/juv25d/handler/MimeTypeResolverTest.java, src/test/java/org/juv25d/handler/StaticFileHandlerTest.java
Adds empty handler classes and test stubs for future static file and MIME type handling.
Test Updates & New Tests
src/test/java/org/juv25d/AppIT.java, src/test/java/org/juv25d/AppTest.java, src/test/java/org/juv25d/http/HttpResponseWriterTest.java
Updates existing tests to new org.juv25d package; adds HttpResponseWriterTest with assertions for 200 OK and 404 Not Found response formatting.

Sequence Diagram

sequenceDiagram
    participant Client
    participant SocketServer
    participant HttpParser
    participant App
    
    Client->>SocketServer: TCP connect to port 3000
    SocketServer->>SocketServer: Accept connection
    SocketServer->>SocketServer: Spawn virtual thread
    SocketServer->>HttpParser: new HttpParser()
    Client->>HttpParser: Send HTTP request bytes
    HttpParser->>HttpParser: readLine() request line
    HttpParser->>HttpParser: Parse method, path, version
    HttpParser->>HttpParser: readLine() headers until blank
    HttpParser->>HttpParser: Parse Content-Length if present
    HttpParser->>HttpParser: Read body bytes
    HttpParser->>SocketServer: Return HttpRequest
    SocketServer->>SocketServer: Log method & path
    SocketServer->>Client: (connection remains open)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A server springs to life with care,
Request lines parsed through the air,
Java threads hop and bound so free,
HTTP responses flowing, just you see! ✨
From socket to response, a hop and a leap,
This rabbit's proud—the code runs deep.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants