MiniHTTPServer is a lightweight C++20 web server that shows the full request → response flow from raw TCP sockets to REST-style JSON APIs, static-file delivery, and SQLite logging
EN: Entry point of the network layer:
- Creates/listens on a TCP socket (
socket(),bind(),listen()). - In
acceptLoop(), on eachaccept(), constructsSession(client_fd, router, db)and callsHandle().
KR: 네트워크 진입점 역할:
- TCP 소켓 생성/바인딩/리스닝(
socket(),bind(),listen()). acceptLoop()에서 들어오는 연결마다Session(client_fd, router, db)를 생성해Handle()호출.
EN: Manages registration and dispatch of HTTP endpoints:
AddRoute(method, path, handler): map"METHOD PATH"to a handler function.Dispatch(req): look up the handler byreq.method + " " + req.path, with optional prefix‐matching for dynamic paths.
KR: HTTP 경로와 핸들러를 관리하는 라우터:
AddRoute(method, path, handler):"METHOD PATH"키로 핸들러 등록.Dispatch(req):req.method + " " + req.path을 찾아 실행, 동적 경로(prefix 매칭)도 지원.
EN: Handles one TCP connection's HTTP request/response cycle:
readRequest(): accumulates TCP data, parses request‐line, headers, and body.dispatchRequest(req): forwards toRouter.sendResponse(res): serializesResponseand ensures full send().- Logs each request via provided
DBreference.
KR: 하나의 TCP 연결에 대한 HTTP 요청/응답 처리를 담당:
readRequest(): TCP로 들어온 데이터를 모아 요청 라인·헤더·바디 파싱.dispatchRequest(req): 라우터로 전달.sendResponse(res):Response직렬화 후 전송(전송 루프 포함).DB참조를 통해 요청을 SQLite에 로깅.
EN: Bootstraps the server:
- Instantiate
Serveron port 8080. - Register handlers via
AddRoute():- Static files under
/static/...usingutil. - Simple
GET /hello,POST /api/echo.
- Static files under
- Call
Run()to start the accept–dispatch loop.
KR: 서버 실행부:
Server객체를 8080 포트로 생성.AddRoute()로 엔드포인트 등록:GET /hello,POST /api/echo./static/*경로로 정적 파일 제공(util사용).
Run()호출로 클라이언트 연결 수락 및 요청 처리 시작.
EN: Provides helper functions for serving static files:
ReadFile(path, ok): reads a file into a string, setsokto true on success.MimeType(ext): returns the correctContent-Typeheader value based on a file extension.
KR: 정적 파일 제공을 위한 유틸 함수 모음:
ReadFile(path, ok): 파일을 읽어std::string으로 반환하고, 성공 시ok=true.MimeType(ext): 확장자에 맞는Content-Type문자열을 반환.
