Skip to content

Conversation

@ensh63
Copy link
Contributor

@ensh63 ensh63 commented Oct 22, 2025

Trait HttpRequestHandler<ReturnType> is defined. This trait allows to implement HTTP request handler with arbitrary return type. Special auxiliary trait HttpHandlerWrapper<ReturnType> adds function to convert this return type to ngx_int_t. Two conversions are predefined: for Option<ngx_int_t> and for Result<ngx_int_t, E>. Conversion function may include any desired additional actions common for all handlers. For instance, convertor for Result<ngx_int_t, E> logs the error, if any.

Trait HttpRequestHandler<ReturnType> also includes associated type for the HTTP module and associated constant for the handler phase.

Handler registration is simplified to a single call of ngx::http::add_phase_handler::<H: crate::http::HttpHandlerWrapper<R>, R>() function which is implemented by default.

All examples are modified to use this trait.

There are some unclear moments:

  • Should old handler macro be removed?
  • If handler is defined as a separate struct, what can be done with data members of this structure? Are they usable at all?
  • Is it needed to define a new set of constants for common return values or current approach with Status and into() is acceptable?

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a trait-based approach for HTTP request handlers in nginx modules, simplifying handler registration and providing type-safe error handling. The new HttpRequestHandler trait allows handlers to return arbitrary types (Option<ngx_int_t> or Result<ngx_int_t, E>) with automatic conversion to nginx's native return type, replacing the previous macro-based approach.

Key changes:

  • Added HttpRequestHandler trait with associated types for module and phase configuration
  • Implemented automatic error logging for Result return types via HttpHandlerWrapper trait
  • Introduced NgxHttpPhases enum for type-safe phase specification
  • Migrated all example modules (curl, awssig, async) to use the new trait system

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/http/status.rs Added From<HTTPStatus> implementation for ngx_int_t to support status code conversion
src/http/request.rs Defined HttpRequestHandler and HttpHandlerWrapper traits with default implementations for Option and Result return types
nginx-sys/src/http.rs Added NgxHttpPhases enum representing nginx HTTP processing phases
examples/curl.rs Converted from macro-based handler to HttpRequestHandler trait implementation
examples/awssig.rs Converted from macro-based handler to HttpRequestHandler trait implementation
examples/async.rs Converted from macro-based handler to HttpRequestHandler trait implementation

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@ensh63 ensh63 force-pushed the shirykalov/handler-trait branch from c57f8ea to 8878948 Compare October 22, 2025 20:08
}

/// Implementation of `HttpHandlerWrapper` for `Result<ngx_int_t, E>`.
impl<T, E> HttpHandlerWrapper<Result<ngx_int_t, E>> for T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result<HTTPStatus, E>?

Also, the bounds for E can be relaxed to Display. I doubt we'll need anything more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bounds relaxed. I'm not sure about HTTPStatus here. Probably we need to have more generic status type, but this maybe out of the scope of current PR.

@ensh63 ensh63 force-pushed the shirykalov/handler-trait branch from 8878948 to 9ed8efc Compare October 29, 2025 21:14
@ensh63 ensh63 force-pushed the shirykalov/handler-trait branch from 9ed8efc to 6ab1de8 Compare October 29, 2025 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants