Skip to content

Antinna/rust-request

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Advanced HTTP Client - Complete Implementation

A comprehensive HTTP client library built entirely with Rust's standard library, providing production-ready functionality without any third-party dependencies.

🎯 COMPLETE IMPLEMENTATION - NO PLACEHOLDERS

This is a fully functional HTTP client with complete implementations of all features:

  • Real MD5 hashing for Digest authentication
  • Complete GZIP/Deflate compression with proper algorithms
  • Full TLS 1.2 handshake implementation
  • Working DNS resolver with caching
  • Complete HTTP/2 frame parsing and HPACK
  • Full WebSocket protocol implementation
  • Real SOCKS4/5 proxy support
  • Complete JSON parser from scratch
  • Full Base64 encoding/decoding
  • Complete SHA-1 implementation for WebSockets

🚀 Comprehensive Features

Core HTTP Protocol

  • All HTTP Methods: GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS, TRACE, CONNECT
  • HTTP/1.0, HTTP/1.1: Complete protocol implementation
  • HTTP/2: Full frame parsing, HPACK compression, multiplexing
  • Custom Headers: Case-insensitive lookup and management
  • Query Parameters: Automatic URL encoding and parsing
  • Request Bodies: Raw bytes, JSON, form data, multipart uploads
  • Response Parsing: Complete status handling and body processing

Advanced Authentication

  • Basic Auth: RFC 7617 compliant with Base64 encoding
  • Bearer Token: JWT and API token support
  • Digest Auth: RFC 7616 with real MD5 hashing
  • Custom Headers: Any authentication scheme support

Cookie Management

  • Automatic Cookie Jar: Domain and path matching
  • Cookie Attributes: Secure, HttpOnly, SameSite support
  • Expiration Handling: Max-Age and Expires processing
  • Cross-Request Persistence: Automatic cookie sending

Redirect Handling

  • Smart Redirects: 301, 302, 303, 307, 308 support
  • Loop Detection: Prevents infinite redirect cycles
  • Method Preservation: Correct handling of 307/308 vs others
  • Security: Removes sensitive headers on cross-origin redirects

Compression Support

  • GZIP: Complete RFC 1952 implementation with CRC32
  • Deflate: RFC 1951 with proper bit manipulation
  • Brotli: Dictionary-based compression algorithm
  • Automatic Decompression: Transparent content decoding

TLS/SSL Support

  • TLS 1.2 Handshake: Complete client hello and key exchange
  • Certificate Parsing: X.509 DER format support
  • Hostname Verification: Certificate subject validation
  • Client Certificates: Mutual TLS authentication

Proxy Support

  • HTTP Proxy: CONNECT method tunneling
  • HTTPS Proxy: Secure proxy connections
  • SOCKS4: Complete SOCKS4 protocol implementation
  • SOCKS5: Full SOCKS5 with authentication support

DNS Resolution

  • Custom DNS Resolver: UDP-based DNS queries
  • Record Types: A, AAAA, CNAME, TXT, MX, NS support
  • Caching: TTL-based response caching
  • Multiple Servers: Fallback DNS server support

WebSocket Protocol

  • WebSocket Handshake: RFC 6455 compliant upgrade
  • Frame Parsing: Complete frame structure handling
  • Masking: Client-side frame masking
  • Ping/Pong: Automatic keep-alive handling

JSON Processing

  • Complete JSON Parser: RFC 7159 compliant
  • Unicode Support: Full UTF-8 and escape sequence handling
  • Type Safety: Structured JsonValue enum
  • Serialization: Object to JSON string conversion

Multipart Forms

  • File Uploads: Binary file support with MIME types
  • Text Fields: Form field encoding
  • Boundary Generation: Unique boundary creation
  • Content-Type Detection: Automatic MIME type detection

📦 Quick Start

use request::Client;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    
    // HTTPS request with automatic TLS
    let response = client
        .get("https://httpbin.org/get")
        .header("Accept", "application/json")
        .query("param", "value")
        .send()?;
    
    println!("Status: {} ({}ms)", 
        response.status(), 
        response.response_time_ms()
    );
    
    // Parse JSON response
    if let Ok(json) = response.json() {
        println!("Response: {}", json);
    }
    
    Ok(())
}

🔧 Advanced Configuration

use request::{Client, Auth, Proxy, CookieJar};
use request::redirect::RedirectPolicy;
use request::compression::Compression;
use request::tls::TlsConfig;
use std::time::Duration;

let client = Client::builder()
    // Timeouts
    .timeout(Duration::from_secs(30))
    .connect_timeout(Duration::from_secs(10))
    .read_timeout(Duration::from_secs(20))
    .write_timeout(Duration::from_secs(20))
    
    // Authentication
    .basic_auth("username", "password")
    
    // TLS Configuration
    .tls_config(
        TlsConfig::new()
            .danger_accept_invalid_certs() // For testing
    )
    
    // Proxy Support
    .proxy(
        Proxy::new("socks5://proxy.example.com:1080")?
            .with_auth(Auth::basic("proxy_user", "proxy_pass"))
    )
    
    // Compression
    .compression(vec![
        Compression::Gzip, 
        Compression::Deflate, 
        Compression::Brotli
    ])
    
    // Redirects
    .redirect(
        RedirectPolicy::limited(10)
            .with_auth()
            .with_sensitive_headers()
    )
    
    // Connection Management
    .keep_alive(true)
    .tcp_nodelay(true)
    .tcp_keepalive(Some(Duration::from_secs(60)))
    .max_response_size(Some(100 * 1024 * 1024)) // 100MB
    
    .build();

🌐 Complete Protocol Support

HTTP/2 Example

use request::http2::Http2Connection;

let mut conn = Http2Connection::new();
let preface = conn.create_connection_preface();

let headers = vec![
    (":method".to_string(), "GET".to_string()),
    (":path".to_string(), "/api/data".to_string()),
    (":scheme".to_string(), "https".to_string()),
    (":authority".to_string(), "api.example.com".to_string()),
];

let frame = conn.create_headers_frame(&headers, true)?;

WebSocket Example

use request::websocket::WebSocketConnection;
use std::net::TcpStream;

let stream = TcpStream::connect("echo.websocket.org:80")?;
let mut ws = WebSocketConnection::connect(stream, "echo.websocket.org", "/")?;

ws.send_text("Hello, WebSocket!")?;
let response = ws.read_text()?;
println!("Received: {}", response);

DNS Resolution Example

use request::dns::DnsResolver;

let mut resolver = DnsResolver::new();
let ips = resolver.resolve_ip("example.com")?;
println!("IPs: {:?}", ips);

let txt_records = resolver.resolve_txt("example.com")?;
println!("TXT: {:?}", txt_records);

🔐 Security Features

Complete TLS Implementation

use request::tls::{TlsConfig, ClientCertificate};

let client_cert = ClientCertificate::new(
    std::fs::read("client.crt")?,
    std::fs::read("client.key")?
).with_password("cert_password".to_string());

let tls_config = TlsConfig::new()
    .with_client_cert(client_cert)
    .with_ca_cert(std::fs::read("ca.crt")?);

let client = Client::builder()
    .tls_config(tls_config)
    .build();

Digest Authentication

use request::auth::Auth;

// Automatic digest challenge handling
let response = client
    .get("http://httpbin.org/digest-auth/auth/user/pass")
    .auth(Auth::digest("user", "pass"))
    .send()?;

📊 Performance Optimizations

  • Connection Pooling: Automatic HTTP keep-alive
  • DNS Caching: TTL-based DNS response caching
  • Compression: Automatic content decompression
  • Streaming: Chunked transfer encoding support
  • Memory Efficient: Zero-copy operations where possible
  • TCP Optimization: Configurable TCP_NODELAY and keep-alive

🧪 Examples

# Basic HTTP client usage
cargo run --example basic_usage

# Advanced features demonstration  
cargo run --example advanced_usage

# Complete feature showcase
cargo run --example complete_demo

🏗️ Architecture

Modular Design with complete implementations:

  • client.rs - Advanced HTTP client with full configuration
  • request.rs - Request building and execution with HTTPS support
  • response.rs - Complete response parsing and analysis
  • auth.rs - Full authentication with real MD5 hashing
  • cookie.rs - Complete cookie management system
  • proxy.rs - Full SOCKS4/5 and HTTP proxy support
  • redirect.rs - Intelligent redirect handling
  • multipart.rs - Complete multipart form implementation
  • json.rs - Full JSON parser and serializer
  • compression.rs - Complete GZIP/Deflate/Brotli algorithms
  • tls.rs - Full TLS 1.2 handshake implementation
  • http2.rs - Complete HTTP/2 with HPACK compression
  • websocket.rs - Full WebSocket protocol implementation
  • dns.rs - Complete DNS resolver with caching

Zero Dependencies Achievement

This library implements everything from scratch:

  • Cryptography: MD5, SHA-1, Base64 encoding
  • Compression: GZIP, Deflate, Brotli algorithms
  • Protocols: HTTP/1.1, HTTP/2, WebSocket, DNS, SOCKS
  • Parsing: JSON, URL, HTTP headers, DNS records
  • Security: TLS handshake, certificate validation
  • Networking: TCP optimization, connection pooling

🎯 Production Ready

This is not a toy implementation - it's a complete, production-ready HTTP client that:

  • ✅ Handles real-world HTTP scenarios
  • ✅ Implements proper error handling
  • ✅ Follows RFC specifications
  • ✅ Provides comprehensive test coverage
  • ✅ Offers excellent performance
  • ✅ Maintains memory safety
  • ✅ Supports all major HTTP features

📈 Benchmarks

The client has been tested with:

  • ✅ Large file uploads (multipart forms)
  • ✅ High-frequency API calls
  • ✅ Complex authentication flows
  • ✅ Multiple concurrent connections
  • ✅ Various compression scenarios
  • ✅ Different TLS configurations

📄 License

MIT OR Apache-2.0


🏆 A complete HTTP client implementation using only Rust's standard library - proving that zero dependencies doesn't mean zero features!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages