Files
godoxy/internal/net/gphttp
yusing b122d42a0b fix(test): correct test expectations and logic
Httptest and similar callers often leave Host unset; fall back to URL
for scheme, host, port, and addr substitution.

jsonstore drops the IsTest load short-circuit and duplicate loadNS map
registration; tests isolate storesPath. Skip MaxMind background updates
when IsTest. Tests restore APISkipOriginCheck, use app-scoped OIDC
state cookies, attach route context in middleware helpers, and use
locked buffers for concurrent log capture.
2026-04-19 14:40:22 +08:00
..

internal/net/gphttp

HTTP utilities package providing transport configuration, default HTTP client, and a wrapper around http.ServeMux with panic recovery.

Overview

This package provides shared HTTP utilities used throughout GoDoxy:

  • Default HTTP Client: Pre-configured http.Client with secure settings
  • Transport Factory: Functions to create optimized http.Transport configurations
  • ServeMux Wrapper: Extended http.ServeMux with panic recovery for handler registration

Architecture

graph TD
    A[HTTP Request] --> B[gphttp.Client]
    B --> C[Transport]
    C --> D[Network Connection]

    E[Server Setup] --> F[gphttp.ServeMux]
    F --> G[http.ServeMux]
    G --> H[HTTP Handlers]

Core Components

HTTP Client

The package exports a pre-configured http.Client with secure defaults:

var (
    httpClient = &http.Client{
        Timeout: 5 * time.Second,
        Transport: &http.Transport{
            DisableKeepAlives: true,
            ForceAttemptHTTP2: false,
            DialContext: (&net.Dialer{
                Timeout:   3 * time.Second,
                KeepAlive: 60 * time.Second,
            }).DialContext,
            TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
        },
    }

    Get  = httpClient.Get
    Post = httpClient.Post
    Head = httpClient.Head
    Do   = httpClient.Do
)

Transport Factory

Functions for creating optimized HTTP transports:

// NewTransport creates an http.Transport with proxy support and optimized settings
func NewTransport() *http.Transport

// NewTransportWithTLSConfig creates an http.Transport with custom TLS configuration
func NewTransportWithTLSConfig(tlsConfig *tls.Config) *http.Transport

Default transport settings:

  • MaxIdleConnsPerHost: 1000
  • IdleConnTimeout: 90 seconds
  • TLSHandshakeTimeout: 10 seconds
  • ResponseHeaderTimeout: 60 seconds
  • WriteBufferSize / ReadBufferSize: 16KB

ServeMux Wrapper

Extended http.ServeMux with panic recovery:

type ServeMux struct {
    *http.ServeMux
}

func NewServeMux() ServeMux
func (mux ServeMux) Handle(pattern string, handler http.Handler) (err error)
func (mux ServeMux) HandleFunc(pattern string, handler http.HandlerFunc) (err error)

The Handle and HandleFunc methods recover from panics and return them as errors, preventing one bad handler from crashing the entire server.

Usage Examples

Creating Custom Transports

import (
    "crypto/tls"
    "net/http"
    "github.com/yusing/godoxy/internal/net/gphttp"
)

// Default transport with environment proxy
transport := gphttp.NewTransport()

// Custom TLS configuration
tlsConfig := &tls.Config{
    ServerName: "example.com",
}
transport := gphttp.NewTransportWithTLSConfig(tlsConfig)

Using ServeMux with Panic Recovery

mux := gphttp.NewServeMux()

// Register handlers - panics are converted to errors
if err := mux.HandleFunc("/api", apiHandler); err != nil {
    log.Printf("handler registration failed: %v", err)
}

Integration Points

  • Used by internal/net/gphttp/middleware for HTTP request/response processing
  • Used by internal/net/gphttp/loadbalancer for backend connections
  • Used throughout the route handling system

Configuration

The default client disables HTTP/2 (ForceAttemptHTTP2: false) and keep-alives (DisableKeepAlives: true) for security and compatibility reasons. The transport uses environment proxy settings via http.ProxyFromEnvironment.