# dungyichao/http_server architecture

> Architecture diagram of the GitHub repository dungyichao/http_server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dungyichao/http_server
- Repository: https://github.com/dungyichao/http_server

## Overview

##### Project Type and Purpose
- This is an open-source tutorial/project that implements a simple HTTP web server in C/C++ on Linux from scratch, plus two streaming extensions:
- HLS-based video streaming (using FFmpeg and file segments)
- MJPEG streaming (using a Python capture script piped into a C server)
- The goal is to teach how to parse HTTP requests, classify by MIME type, serve static content, fork per connection, and extend to live video streaming.

##### File Structure Analysis
- Top-level directories:
- src/ – C/C++ sources and web-content archives
- doc/ – PDF references and tutorial materials
- Project/simple_streameye/ – MJPEG demo (rasp_test.py & test001.c)
- README.md – detailed tutorial and architecture description
- Key files in src/:
- helloworld.cpp – core HTTP server implementation
- api_server/main.cpp + json.hpp – alternative C++ “API server” example
- index.html, Web_Content_1.zip, Web_Content_2.zip – static site assets
- Streaming support:
- HLS: bash script (ffmpeg-rpi-stream) invoked as a systemd service, generates .ts segments + .m3u8 playlist in a folder served by the same HTTP server
- MJPEG: rasp_test.py captures JPEG frames via raspivid; frames piped into test001 (a standalone C server derived from StreamEye) which listens on a socket and serves multipart JPEG over HTTP.

##### Architecture Insights from README
- Core HTTP server loop:
- socket() → bind() → listen() → accept()
- fork() per client → parse() request line for URI and extension
- classify() extension → determine MIME type header
- send_message() → write header + open/fstat/sendfile for static content
- HLS flow: raspivid → FFmpeg → disk segments (.ts + .m3u8) → HTTP GET by client → served as static files by helloworld.cpp
- MJPEG flow: rasp_test.py → stdout JPEG frames → test001 C server → HTTP clients connect to /stream.mjpg → server streams multipart JPEG.

##### Detail Level
- Logical module as its own component, even if it shares the same binary (e.g., static file handler vs. streaming handler within helloworld.cpp)
- Directory mounts or paths:
- /var/www/http_server/static → index.html, CSS, JS, img
- /var/www/http_server/hls → s.m3u8 + s_*.ts

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client
    Client["Browser (PC/Mobile)"]:::client

    %% HTTP Server Subgraph
    subgraph "HTTP Server"
        direction TB
        Static["Static File Handler\n(port 8080)"]:::server
        HLS_Handler["HLS Handler\n(port 8080)"]:::server
        MJPEG_Handler["MJPEG Handler\n(port 8084)"]:::server
        noteFork["fork() per connection"]:::annotation
    end

    %% File System
    FileSystem["File System\n/var/www/http_server/\n(static & hls dirs)"]:::storage

    %% Generators and Capture
    HLSSrc["HLS Generator\n(raspivid + FFmpeg)"]:::encode
    MJPEGCap["MJPEG Capture Script\n(rasp_test.py)"]:::encode
    MJPEGServer["MJPEG Server\n(test001.c)"]:::server

    %% Alternative API Server
    API_Server["API Server Example"]:::server

    %% Documentation
    subgraph "Documentation"
        README["Project README"]:::doc
        Doc1["HTTP Server Tutorial PDF"]:::doc
        Doc2["sendfile(2) Manual"]:::doc
        Doc3["Common MIME Types"]:::doc
    end

    %% Data Flows
    Client -->|"GET /index.html etc"| Static
    Client -->|"GET /s.m3u8 & .ts"| HLS_Handler
    Client -->|"GET /stream.mjpg"| MJPEG_Handler

    Static -->|"open/read file"| FileSystem
    HLS_Handler -->|"open/read segments"| FileSystem
    HLSSrc -->|"write .ts & .m3u8"| FileSystem
    MJPEGCap -->|"stdout JPEG frames"| MJPEGServer

    %% Click Events
    click Static "https://github.com/dungyichao/http_server/blob/master/src/helloworld.cpp"
    click HLS_Handler "https://github.com/dungyichao/http_server/blob/master/src/helloworld.cpp"
    click API_Server "https://github.com/dungyichao/http_server/blob/master/src/api_server/main.cpp"
    click API_Server "https://github.com/dungyichao/http_server/blob/master/src/api_server/json.hpp"
    click Static "https://github.com/dungyichao/http_server/blob/master/src/index.html"
    click Static "https://github.com/dungyichao/http_server/blob/master/src/Web_Content_1.zip"
    click Static "https://github.com/dungyichao/http_server/blob/master/src/Web_Content_2.zip"
    click MJPEGCap "https://github.com/dungyichao/http_server/blob/master/Project/simple_streameye/rasp_test.py"
    click MJPEGServer "https://github.com/dungyichao/http_server/blob/master/Project/simple_streameye/test001.c"
    click MJPEGServer "https://github.com/dungyichao/http_server/blob/master/Project/simple_streameye/streameye-master.zip"
    click README "https://github.com/dungyichao/http_server/blob/master/README.md"
    click Doc1 "https://github.com/dungyichao/http_server/blob/master/doc/HTTP Server_ Everything you need to know to Build a simple HTTP server from scratch_pdf2.pdf"
    click Doc2 "https://github.com/dungyichao/http_server/blob/master/doc/linux Command/sendfile(2) - Linux manual page.pdf"
    click Doc3 "https://github.com/dungyichao/http_server/blob/master/doc/Common MIME types - HTTP Content Type.pdf"

    %% Styles
    classDef server fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef client fill:#D3D3D3,stroke:#000,stroke-width:1px
    classDef storage fill:#FFA500,stroke:#000,stroke-width:1px
    classDef encode fill:#90EE90,stroke:#000,stroke-width:1px
    classDef doc fill:#E8E8E8,stroke:#000,stroke-width:1px
    classDef annotation fill:#FFFFFF,stroke:#000,stroke-dasharray: 2 2,stroke-width:1px
```
