Skip to content

Latest commit

 

History

History
89 lines (67 loc) · 3.2 KB

File metadata and controls

89 lines (67 loc) · 3.2 KB

TODO: Refactor to use http crate

This document outlines the tasks for refactoring lang_handler to use the standard http crate types instead of custom implementations.

Overview

Replace custom Request, Response, and Headers types with types from the http crate, preserving custom functionality through the Extensions API and extension traits.

Phase 1: Setup and Dependencies

  • Add dependencies to Cargo.toml:
    • http = "1.0"
    • http-body = "1.0"
    • bytes = "1.0" (already present)
  • Create src/extensions.rs module for extension types:
    • SocketInfo - store local/remote socket addresses
    • ResponseLog - log buffer using Bytes
    • ResponseException - exception message storage
    • Implement Clone, Debug traits for all extensions

Phase 2: Extension Traits

Create extension traits that add convenience methods to http types:

  • RequestExt trait for http::Request<T>:

    • socket_info(&self) -> Option<&SocketInfo>
    • socket_info_mut(&mut self) -> &mut SocketInfo
    • set_socket_info(&mut self, info: SocketInfo)
  • ResponseExt trait for http::Response<T>:

    • log(&self) -> Option<&ResponseLog>
    • log_mut(&mut self) -> &mut ResponseLog
    • set_log(&mut self, log: impl Into<Bytes>)
    • append_log(&mut self, data: impl AsRef<[u8]>)
    • exception(&self) -> Option<&ResponseException>
    • set_exception(&mut self, exception: impl Into<String>)

Phase 3: Core Type Migration

Request Migration

  • Remove custom Request struct and RequestBuilder
  • Define type alias: pub type Request = http::Request<Bytes>
  • Implement RequestExt for http::Request<T>
  • Update all Request construction to use http::Request::builder()

Response Migration

  • Remove custom Response struct and ResponseBuilder
  • Define type alias: pub type Response = http::Response<Bytes>
  • Implement ResponseExt for http::Response<T>
  • Update all Response construction to use http::Response::builder()

Headers Migration

  • Remove custom Headers type and Header enum
  • Use http::HeaderMap directly
  • Update all header operations to use HeaderMap API

Phase 4: Handler Trait Update

  • Update Handler trait:
    trait Handler {
        type Error;
        fn handle(&self, request: http::Request<Bytes>) -> Result<http::Response<Bytes>, Self::Error>;
    }
  • Update all Handler implementations

Phase 5: Extract Rewrite Module

  • Delete old rewrite module as its functionality has been reproduced in a separate crate.

Phase 6: NAPI Bindings

Create intermediate types for NAPI that implement Deref/DerefMut:

  • NapiRequest - wraps http::Request<Bytes>

    • Implement Deref/DerefMut to http::Request<Bytes>
    • Implement NAPI conversion traits
    • Handle socket info in conversions
  • NapiResponse - wraps http::Response<Bytes>

    • Implement Deref/DerefMut to http::Response<Bytes>
    • Implement NAPI conversion traits
    • Handle log/exception in conversions
  • NapiHeaders - wraps http::HeaderMap

    • Implement Deref/DerefMut to http::HeaderMap
    • Implement NAPI conversion traits
    • Maintain multi-value support