# elixir-mint/mint architecture

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

- Interactive diagram: https://gitdiagram.com/elixir-mint/mint
- Repository: https://github.com/elixir-mint/mint

## Overview

##### Project type and purpose
- Mint is an open-source, low-level HTTP client library written in Elixir/Erlang.
- It provides a “process-less” functional API for HTTP/1.1 and HTTP/2 connections over TCP or SSL.

##### File structure
- Top-level:
- mix.exs / mix.lock – Elixir project definition
- lib/mint – source code
- test – test suite including integration and proxy tests
- pages – markdown docs (e.g. Architecture.md)
- docker-compose.yml, Caddyfile – test infrastructure
- Inside lib/mint:
- application.ex – OTP application entry point
- core/ – connection struct, headers, utility code, transport interface
- http.ex – public HTTP API that delegates to http1 or http2
- http1/ – HTTP/1.1 framing, parsing, request/response logic
- http2/ – HTTP/2 frame definitions, state machine
- negotiate.ex – protocol negotiation logic (HTTP/1 vs HTTP/2)
- transport/ – adapters for TCP and SSL
- No “frontend” or “backend” directory: this is a library, not a full-stack app.

##### README for additional insights
- Emphasizes:
- Stateless API: every call returns a new connection struct
- Transparent HTTP/1 vs HTTP/2 selection
- Active socket mode, user handles OTP socket messages, then passes them to Mint.stream/2
- Mentions optional dependencies (castore), integration with higher-level clients like Finch.

a. Main components
- Public API layer: Mint.HTTP (connect, request, stream)
- Protocol negotiation: Mint.negotiate
- HTTP/1.1 stack: Mint.HTTP1 modules (frame assembly, parsing, request, response)
- HTTP/2 stack: Mint.HTTP2 modules (frame definitions, state machine)
- Core connection data model: Mint.Core.Conn, Headers, Types
- Transport adapters: Mint.Core.Transport.TCP and .SSL
- OTP application: Mint.Application (supervision tree, if any)
- External dependencies: Erlang :gen_tcp, :ssl, optional CA store (castore)

b. Relationships and interactions
1. Client code calls Mint.HTTP.connect -> invokes Mint.negotiate -> selects TCP or SSL transport -> returns Conn struct
2. Client calls Mint.HTTP.request with Conn -> forwards to HTTP1 or HTTP2 module -> builds frames, calls transport.send
3. Socket messages (:tcp, :ssl) arrive to user process -> passed to Mint.HTTP.stream(conn, message)
4. stream/2 dispatches to appropriate protocol module, parses frames into response parts
5. Updated Conn struct flows back to client for next operations
6. Under the hood, transport modules wrap :gen_tcp / :ssl calls

c. Architectural patterns and design principles
- Functional, immutable “connection struct” as state container
- No internal processes per connection (process-less)
- Layered protocol stacks: core → negotiation → http1/http2 → transport
- Clear separation of concerns: framing vs parsing vs transport
- OTP application structure for tests and supervision (minimal)

d. Key technologies and libraries
- Elixir/Erlang OTP
- gen_tcp, ssl (Erlang)
- Optional castore for certificates
- Docker Compose + Tinyproxy/Caddy for proxy integration tests

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% OTP Application
    subgraph "OTP Application"
        App["Mint.Application"]:::app
    end

    %% Public API Layer
    subgraph "Public API Layer"
        HTTP["Mint.HTTP"]:::api
    end

    %% Protocol Negotiation
    subgraph "Protocol Negotiation"
        Negotiate["Mint.negotiate"]:::logic
    end

    %% HTTP/1.1 Stack
    subgraph "HTTP/1.1 Stack"
        H1API["Mint.HTTP1"]:::module
        H1Parse["Parse"]:::module
        H1Req["Request"]:::module
        H1Res["Response"]:::module
    end

    %% HTTP/2 Stack
    subgraph "HTTP/2 Stack"
        H2API["Mint.HTTP2"]:::module
        H2Frame["Frame"]:::module
    end

    %% Core Layer
    subgraph "Core Layer"
        Conn["Mint.Core.Conn"]:::core
        Headers["Mint.Core.Headers"]:::core
        Types["Mint.Types"]:::core
        TransportInt["Mint.Core.Transport"]:::core
    end

    %% Transport Adapters
    subgraph "Transport Adapters"
        TCP["Mint.Core.Transport.TCP"]:::transport
        SSL["Mint.Core.Transport.SSL"]:::transport
    end

    %% External Systems
    subgraph "External Systems"
        GenTCP(["gen_tcp"]):::external
        SSLMod(["ssl"]):::external
        CAStore(["castore"]):::optional
    end

    %% Data Flow
    Client["Client Code"]:::external -->|"calls connect/request/stream"| HTTP
    HTTP -->|"delegates connect"| Negotiate
    Negotiate -->|"selects protocol"| H1API
    Negotiate --> H2API
    H1API -->|"frame assembly/parsing"| H1Parse
    H1Parse --> H1Req
    H1Req --> H1Res
    H1API -->|"send frames"| TransportInt
    H2API -->|"build frames"| H2Frame
    H2Frame --> TransportInt
    TransportInt --> TCP
    TransportInt --> SSL
    TCP -->|":gen_tcp calls"| GenTCP
    SSL -->|"ssl calls"| SSLMod
    GenTCP -->|"socket message"| HTTP
    SSLMod --> HTTP
    HTTP -->|"streams frames"| Conn
    Conn --> Headers
    Conn --> Types
    HTTP --> CAStore

    %% Click Events
    click App "https://github.com/elixir-mint/mint/blob/main/lib/mint/application.ex"
    click HTTP "https://github.com/elixir-mint/mint/blob/main/lib/mint/http.ex"
    click Negotiate "https://github.com/elixir-mint/mint/blob/main/lib/mint/negotiate.ex"
    click H1API "https://github.com/elixir-mint/mint/blob/main/lib/mint/http1.ex"
    click H1Parse "https://github.com/elixir-mint/mint/blob/main/lib/mint/http1/parse.ex"
    click H1Req "https://github.com/elixir-mint/mint/blob/main/lib/mint/http1/request.ex"
    click H1Res "https://github.com/elixir-mint/mint/blob/main/lib/mint/http1/response.ex"
    click H2API "https://github.com/elixir-mint/mint/blob/main/lib/mint/http2.ex"
    click H2Frame "https://github.com/elixir-mint/mint/blob/main/lib/mint/http2/frame.ex"
    click Conn "https://github.com/elixir-mint/mint/blob/main/lib/mint/core/conn.ex"
    click Headers "https://github.com/elixir-mint/mint/blob/main/lib/mint/core/headers.ex"
    click Types "https://github.com/elixir-mint/mint/blob/main/lib/mint/types.ex"
    click TransportInt "https://github.com/elixir-mint/mint/blob/main/lib/mint/core/transport.ex"
    click TCP "https://github.com/elixir-mint/mint/blob/main/lib/mint/core/transport/tcp.ex"
    click SSL "https://github.com/elixir-mint/mint/blob/main/lib/mint/core/transport/ssl.ex"

    %% Styles
    classDef app fill:#f9f,stroke:#333,stroke-width:2px
    classDef api fill:#bbf,stroke:#333,stroke-width:2px
    classDef logic fill:#fbf,stroke:#333,stroke-width:2px
    classDef module fill:#bfb,stroke:#333,stroke-width:2px
    classDef core fill:#ffb,stroke:#333,stroke-width:2px
    classDef transport fill:#fbb,stroke:#333,stroke-width:2px
    classDef external fill:#ddd,stroke:#999,stroke-width:1px,stroke-dasharray: 2 2
    classDef optional fill:#eee,stroke:#999,stroke-width:1px,stroke-dasharray: 5 5
```
