这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions adapters/adapters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,34 @@ func TestAdapters(t *testing.T) {
return huma.DefaultConfig("Test", "1.0.0")
}

wrap := func(h huma.API, isFiber bool) huma.API {
h.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
assert.Nil(t, ctx.TLS())
v := ctx.Version()
if !isFiber {
assert.Equal(t, "HTTP/1.1", v.Proto)
assert.Equal(t, 1, v.ProtoMajor)
assert.Equal(t, 1, v.ProtoMinor)
} else {
assert.Equal(t, "http", v.Proto)
}
next(ctx)
})
return h
}

for _, adapter := range []struct {
name string
new func() huma.API
}{
{"chi", func() huma.API { return humachi.New(chi.NewMux(), config()) }},
{"echo", func() huma.API { return humaecho.New(echo.New(), config()) }},
{"fiber", func() huma.API { return humafiber.New(fiber.New(), config()) }},
{"gin", func() huma.API { return humagin.New(gin.New(), config()) }},
{"httprouter", func() huma.API { return humahttprouter.New(httprouter.New(), config()) }},
{"mux", func() huma.API { return humamux.New(mux.NewRouter(), config()) }},
{"bunrouter", func() huma.API { return humabunrouter.New(bunrouter.New(), config()) }},
{"bunroutercompat", func() huma.API { return humabunrouter.NewCompat(bunrouter.New().Compat(), config()) }},
{"chi", func() huma.API { return wrap(humachi.New(chi.NewMux(), config()), false) }},
{"echo", func() huma.API { return wrap(humaecho.New(echo.New(), config()), false) }},
{"fiber", func() huma.API { return wrap(humafiber.New(fiber.New(), config()), true) }},
{"gin", func() huma.API { return wrap(humagin.New(gin.New(), config()), false) }},
{"httprouter", func() huma.API { return wrap(humahttprouter.New(httprouter.New(), config()), false) }},
{"mux", func() huma.API { return wrap(humamux.New(mux.NewRouter(), config()), false) }},
{"bunrouter", func() huma.API { return wrap(humabunrouter.New(bunrouter.New(), config()), false) }},
{"bunroutercompat", func() huma.API { return wrap(humabunrouter.NewCompat(bunrouter.New().Compat(), config()), false) }},
} {
t.Run(adapter.name, func(t *testing.T) {
testAdapter(t, adapter.new())
Expand Down
25 changes: 25 additions & 0 deletions adapters/humabunrouter/humabunrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package humabunrouter

import (
"context"
"crypto/tls"
"fmt"
"io"
"mime/multipart"
Expand Down Expand Up @@ -107,6 +108,18 @@ func (c *bunContext) BodyWriter() io.Writer {
return c.w
}

func (c *bunContext) TLS() *tls.ConnectionState {
return c.r.TLS
}

func (c *bunContext) Version() huma.ProtoVersion {
return huma.ProtoVersion{
Proto: c.r.Proto,
ProtoMajor: c.r.ProtoMajor,
ProtoMinor: c.r.ProtoMinor,
}
}

// NewContext creates a new Huma context from an HTTP request and response.
func NewContext(op *huma.Operation, r bunrouter.Request, w http.ResponseWriter) huma.Context {
return &bunContext{op: op, r: r, w: w}
Expand Down Expand Up @@ -198,6 +211,18 @@ func (c *bunCompatContext) BodyWriter() io.Writer {
return c.w
}

func (c *bunCompatContext) TLS() *tls.ConnectionState {
return c.r.TLS
}

func (c *bunCompatContext) Version() huma.ProtoVersion {
return huma.ProtoVersion{
Proto: c.r.Proto,
ProtoMajor: c.r.ProtoMajor,
ProtoMinor: c.r.ProtoMinor,
}
}

// NewCompatContext creates a new Huma context from an HTTP request and response.
func NewCompatContext(op *huma.Operation, r *http.Request, w http.ResponseWriter) huma.Context {
return &bunCompatContext{op: op, r: r, w: w}
Expand Down
13 changes: 13 additions & 0 deletions adapters/humachi/humachi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package humachi

import (
"context"
"crypto/tls"
"io"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -106,6 +107,18 @@ func (c *chiContext) BodyWriter() io.Writer {
return c.w
}

func (c *chiContext) TLS() *tls.ConnectionState {
return c.r.TLS
}

func (c *chiContext) Version() huma.ProtoVersion {
return huma.ProtoVersion{
Proto: c.r.Proto,
ProtoMajor: c.r.ProtoMajor,
ProtoMinor: c.r.ProtoMinor,
}
}

// NewContext creates a new Huma context from an HTTP request and response.
func NewContext(op *huma.Operation, r *http.Request, w http.ResponseWriter) huma.Context {
return &chiContext{op: op, r: r, w: w}
Expand Down
14 changes: 14 additions & 0 deletions adapters/humaecho/humaecho.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package humaecho

import (
"context"
"crypto/tls"
"io"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -104,6 +105,19 @@ func (c *echoCtx) BodyWriter() io.Writer {
return c.orig.Response()
}

func (c *echoCtx) TLS() *tls.ConnectionState {
return c.orig.Request().TLS
}

func (c *echoCtx) Version() huma.ProtoVersion {
r := c.orig.Request()
return huma.ProtoVersion{
Proto: r.Proto,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
}
}
Comment on lines +108 to +119
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Verification Complete: Methods are currently used only in test files.

The newly added TLS() and Version() methods are utilized within adapters/adapters_test.go for testing purposes. To fully leverage their functionality, consider integrating these methods into the production codebase where appropriate.

🔗 Analysis chain

Summary: New methods enhance adapter functionality.

The addition of TLS() and Version() methods to the echoCtx struct successfully implements the PR objectives. These methods provide valuable access to TLS connection state and HTTP protocol version information, enhancing the adapter's capabilities.

To ensure consistency, please verify the usage of these new methods across the codebase. Run the following script to check for potential usage:

This will help identify any existing code that might benefit from using these new methods.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential usage of new TLS() and Version() methods

# Test: Search for potential usage of TLS() method
echo "Potential usage of TLS() method:"
rg --type go "\.TLS\(\)" --glob '!adapters/humaecho/humaecho.go'

# Test: Search for potential usage of Version() method
echo "Potential usage of Version() method:"
rg --type go "\.Version\(\)" --glob '!adapters/humaecho/humaecho.go'

Length of output: 397


type router interface {
Add(method, path string, handler echo.HandlerFunc, middlewares ...echo.MiddlewareFunc) *echo.Route
}
Expand Down
11 changes: 11 additions & 0 deletions adapters/humafiber/humafiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package humafiber
import (
"bytes"
"context"
"crypto/tls"
"io"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -111,6 +112,16 @@ func (c *fiberCtx) BodyWriter() io.Writer {
return c.orig
}

func (c *fiberCtx) TLS() *tls.ConnectionState {
return c.orig.Context().TLSConnectionState()
}

func (c *fiberCtx) Version() huma.ProtoVersion {
return huma.ProtoVersion{
Proto: c.orig.Protocol(),
}
}

type router interface {
Add(method, path string, handlers ...fiber.Handler) fiber.Router
}
Expand Down
13 changes: 13 additions & 0 deletions adapters/humaflow/humaflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package humaflow

import (
"context"
"crypto/tls"
"io"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -103,6 +104,18 @@ func (c *goContext) BodyWriter() io.Writer {
return c.w
}

func (c *goContext) TLS() *tls.ConnectionState {
return c.r.TLS
}

func (c *goContext) Version() huma.ProtoVersion {
return huma.ProtoVersion{
Proto: c.r.Proto,
ProtoMajor: c.r.ProtoMajor,
ProtoMinor: c.r.ProtoMinor,
}
}

// NewContext creates a new Huma context from an HTTP request and response.
func NewContext(op *huma.Operation, r *http.Request, w http.ResponseWriter) huma.Context {
return &goContext{op: op, r: r, w: w}
Expand Down
13 changes: 13 additions & 0 deletions adapters/humagin/humagin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package humagin

import (
"context"
"crypto/tls"
"io"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -104,6 +105,18 @@ func (c *ginCtx) BodyWriter() io.Writer {
return c.orig.Writer
}

func (c *ginCtx) TLS() *tls.ConnectionState {
return c.orig.Request.TLS
}

func (c *ginCtx) Version() huma.ProtoVersion {
return huma.ProtoVersion{
Proto: c.orig.Request.Proto,
ProtoMajor: c.orig.Request.ProtoMajor,
ProtoMinor: c.orig.Request.ProtoMinor,
}
}

// Router is an interface that wraps the Gin router's Handle method.
type Router interface {
Handle(string, string, ...gin.HandlerFunc) gin.IRoutes
Expand Down
13 changes: 13 additions & 0 deletions adapters/humago/humago.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package humago

import (
"context"
"crypto/tls"
"io"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -112,6 +113,18 @@ func (c *goContext) BodyWriter() io.Writer {
return c.w
}

func (c *goContext) TLS() *tls.ConnectionState {
return c.r.TLS
}

func (c *goContext) Version() huma.ProtoVersion {
return huma.ProtoVersion{
Proto: c.r.Proto,
ProtoMajor: c.r.ProtoMajor,
ProtoMinor: c.r.ProtoMinor,
}
}

// NewContext creates a new Huma context from an HTTP request and response.
func NewContext(op *huma.Operation, r *http.Request, w http.ResponseWriter) huma.Context {
return &goContext{op: op, r: r, w: w}
Expand Down
13 changes: 13 additions & 0 deletions adapters/humahttprouter/humahttprouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package humahttprouter

import (
"context"
"crypto/tls"
"io"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -107,6 +108,18 @@ func (c *httprouterContext) BodyWriter() io.Writer {
return c.w
}

func (c *httprouterContext) TLS() *tls.ConnectionState {
return c.r.TLS
}

func (c *httprouterContext) Version() huma.ProtoVersion {
return huma.ProtoVersion{
Proto: c.r.Proto,
ProtoMajor: c.r.ProtoMajor,
ProtoMinor: c.r.ProtoMinor,
}
}

type httprouterAdapter struct {
router *httprouter.Router
}
Expand Down
13 changes: 13 additions & 0 deletions adapters/humamux/humamux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package humamux

import (
"context"
"crypto/tls"
"io"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -63,6 +64,18 @@ func (c *gmuxContext) Header(name string) string {
return c.r.Header.Get(name)
}

func (c *gmuxContext) TLS() *tls.ConnectionState {
return c.r.TLS
}

func (c *gmuxContext) Version() huma.ProtoVersion {
return huma.ProtoVersion{
Proto: c.r.Proto,
ProtoMajor: c.r.ProtoMajor,
ProtoMinor: c.r.ProtoMinor,
}
}

func (c *gmuxContext) EachHeader(cb func(name, value string)) {
for name, values := range c.r.Header {
for _, value := range values {
Expand Down
14 changes: 14 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package huma

import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -66,6 +67,12 @@ type Context interface {
// Context returns the underlying request context.
Context() context.Context

// TLS / SSL connection information.
TLS() *tls.ConnectionState

// Version of the HTTP protocol as text and integers.
Version() ProtoVersion

// Method returns the HTTP method for the request.
Method() string

Expand Down Expand Up @@ -117,6 +124,13 @@ type Context interface {
BodyWriter() io.Writer
}

// Represent http protocol version
type ProtoVersion struct {
Proto string
ProtoMajor int
ProtoMinor int
}

type (
humaContext Context
subContext struct {
Expand Down
Loading