# square/okhttp architecture

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

- Interactive diagram: https://gitdiagram.com/square/okhttp
- Repository: https://github.com/square/okhttp

## Overview

##### Main Components

a. Core Components:
- HTTP Client Layer (OkHttpClient)
- Request/Response Processing
- Connection Management
- Protocol Support (HTTP/1.1, HTTP/2, WebSocket)
- Interceptor Chain
- Cache System

b. Supporting Components:
- TLS/Security Layer
- Connection Pool
- Event Listeners
- DNS Resolution
- Cookie Management

**┌─────────────────────────────────────────────────────┐**
- Application Layer                   │
- ┤
- OkHttpClient                      │
- ┤
- Interceptor Chain                   │
- ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌───────┐  │
- Cache   │ │  Bridge  │ │ Connect  │ │ Call  │  │
- Interceptor│ │Interceptor│ │Interceptor│ │Server │  │
- ┘ └──────────┘ └──────────┘ └───────┘  │
- ┤
- Connection Management                  │
- ┌──────────┐ ┌──────────┐ ┌──────────────────┐    │
- Connection│ │Connection│ │Route Selection &  │    │
- Pool   │ │ Handler  │ │   Load Balancing  │    │
- ┘ └──────────┘ └──────────────────┘    │
- ┤
- Protocol Support                     │
- ┌──────────┐ ┌──────────┐ ┌──────────┐           │
- HTTP/1.1 │ │  HTTP/2  │ │ WebSocket│           │
- ┘ └──────────┘ └──────────┘           │
- ┤
- Security & Platform Support             │
- ┌──────────┐ ┌──────────┐ ┌──────────┐           │
- TLS    │ │Certificate│ │Platform  │           │
- Layer   │ │  Pinning  │ │Adapters  │           │
- ┘ └──────────┘ └──────────┘           │
- ┘

##### Connection and Data Flow

- Request flow from application through interceptors
- Connection establishment and pooling
- Cache interactions
- Protocol negotiations
- Security handshakes

##### Important Details to Highlight

a. Interceptor Chain:
- Sequential processing of requests/responses
- Extensibility through custom interceptors
- Built-in interceptors (retry, bridge, cache)

b. Connection Management:
- Connection pooling
- Keep-alive handling
- Multiple address attempts
- Protocol selection (HTTP/1.1, HTTP/2)

c. Security Features:
- TLS support
- Certificate pinning
- Platform security integration

##### Additional Elements

- Notes for key features like automatic GZIP
- Integration points with external systems (DNS, proxy)
- Configuration options

##### Implementation Notes

- Version compatibility information
- Platform-specific optimizations

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Application
        AppLayer[Application Layer]
    end

    subgraph Client
        OkHttpClient["OkHttpClient"]:::client
        Call["Call Interface"]:::client
    end

    subgraph InterceptorChain["Interceptor Chain"]
        direction TB
        Interceptor["Interceptor Interface"]:::interceptor
        CacheInterceptor["Cache Interceptor"]:::interceptor
        BridgeInterceptor["Bridge Interceptor"]:::interceptor
        ConnectInterceptor["Connect Interceptor"]:::interceptor
        CallServerInterceptor["Call Server"]:::interceptor
    end

    subgraph ConnectionMgmt["Connection Management"]
        direction TB
        RealConnection["Real Connection"]:::connection
        ConnectionPool["Connection Pool"]:::connection
        ExchangeFinder["Route Selection & Load Balancing"]:::connection
    end

    subgraph ProtocolSupport["Protocol Support"]
        direction TB
        HTTP1["HTTP/1.1"]:::protocol
        HTTP2["HTTP/2"]:::protocol
        WebSocket["WebSocket"]:::protocol
    end

    subgraph Security["Security & Platform Support"]
        direction TB
        TLS["TLS Layer"]:::security
        CertPinning["Certificate Pinning"]:::security
        PlatformAdapters["Platform Adapters"]:::security
    end

    subgraph Supporting["Supporting Components"]
        direction TB
        DNS["DNS Resolution"]:::support
        Cache["Cache System"]:::support
        EventListener["Event Listener"]:::support
        CookieJar["Cookie Management"]:::support
    end

    %% Connections
    AppLayer --> OkHttpClient
    OkHttpClient --> Call
    Call --> Interceptor
    Interceptor --> CacheInterceptor
    CacheInterceptor --> BridgeInterceptor
    BridgeInterceptor --> ConnectInterceptor
    ConnectInterceptor --> CallServerInterceptor
    
    ConnectInterceptor --> ConnectionMgmt
    RealConnection --> ProtocolSupport
    ConnectionPool --> RealConnection
    ExchangeFinder --> RealConnection

    CallServerInterceptor --> ProtocolSupport
    ProtocolSupport --> Security
    
    CacheInterceptor --> Cache
    OkHttpClient --> DNS
    OkHttpClient --> EventListener
    OkHttpClient --> CookieJar

    %% Styles
    classDef client fill:#2196F3,stroke:#1565C0,color:white
    classDef interceptor fill:#FF9800,stroke:#F57C00,color:white
    classDef connection fill:#4CAF50,stroke:#388E3C,color:white
    classDef protocol fill:#9C27B0,stroke:#7B1FA2,color:white
    classDef security fill:#F44336,stroke:#D32F2F,color:white
    classDef support fill:#607D8B,stroke:#455A64,color:white

    %% Click events
    click OkHttpClient "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/OkHttpClient.kt"
    click Call "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/Call.kt"
    click Interceptor "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/Interceptor.kt"
    click BridgeInterceptor "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/http/BridgeInterceptor.kt"
    click CacheInterceptor "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/cache/CacheInterceptor.kt"
    click RealConnection "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/connection/RealConnection.kt"
    click ConnectionPool "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/ConnectionPool.kt"
    click ExchangeFinder "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/connection/ExchangeFinder.kt"
    click HTTP1 "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/http1/Http1ExchangeCodec.kt"
    click HTTP2 "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/http2/Http2Connection.kt"
    click WebSocket "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/ws/RealWebSocket.kt"
    click TLS "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/tls/OkHostnameVerifier.kt"
    click CertPinning "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/tls/CertificateChainCleaner.kt"
    click DNS "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/Dns.kt"
    click Cache "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/Cache.kt"
    click EventListener "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/EventListener.kt"
    click CookieJar "https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/CookieJar.kt"
```
