Files
godoxy-yusing/internal/net/gphttp
yusing dd33980d18 fix(middleware): allow HTML rewrite for chunked and unknown-length bodies
Relax response-body gating so HTML and XHTML can be buffered when
Transfer-Encoding is chunked-only, or when Content-Length is missing,
while still rejecting non-identity encodings that are not chunked HTML
and other non-HTML cases.

Update modifyHTML to cap reads for unknown length, splice the original
stream back when the cap is hit, and document the behavior in the
package README. Extend tests for themed middleware and the rewrite gate.
2026-04-23 17:23:39 +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.