Skip to content

Commit

Permalink
feat: move more methods from Ctx to Request
Browse files Browse the repository at this point in the history
  • Loading branch information
nickajacks1 committed May 19, 2024
1 parent a94334a commit 1f1f814
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
29 changes: 5 additions & 24 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ package fiber
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"mime/multipart"
"net"
"strconv"
"time"

Expand Down Expand Up @@ -313,13 +311,9 @@ func (c *DefaultCtx) Hostname() string {
return c.req.Hostname()
}

// Port returns the remote port of the request.
// Port is an alias of [Request.Port].
func (c *DefaultCtx) Port() string {
tcpaddr, ok := c.fasthttp.RemoteAddr().(*net.TCPAddr)
if !ok {
panic(errors.New("failed to type-assert to *net.TCPAddr"))
}
return strconv.Itoa(tcpaddr.Port)
return c.req.Port()
}

// IP is an alias of [Request.IP].
Expand Down Expand Up @@ -393,10 +387,9 @@ func (c *DefaultCtx) Method(override ...string) string {
return c.req.Method(override...)
}

// MultipartForm parse form entries from binary.
// This returns a map[string][]string, so given a key the value will be a string slice.
// MultipartForm is an alias of [Request.MultipartForm].
func (c *DefaultCtx) MultipartForm() (*multipart.Form, error) {
return c.fasthttp.MultipartForm()
return c.req.MultipartForm()
}

// ClientHelloInfo return CHI from context
Expand Down Expand Up @@ -768,21 +761,9 @@ func (c *DefaultCtx) IsProxyTrusted() bool {
return false
}

var localHosts = [...]string{"127.0.0.1", "::1"}

// IsLocalHost will return true if address is a localhost address.
func (*DefaultCtx) isLocalHost(address string) bool {
for _, h := range localHosts {
if address == h {
return true
}
}
return false
}

// IsFromLocal will return true if request came from local.
func (c *DefaultCtx) IsFromLocal() bool {
return c.isLocalHost(c.fasthttp.RemoteIP().String())
return c.req.IsFromLocal()
}

// Bind You can bind body, cookie, headers etc. into the map, map slice, struct easily by using Binding method.
Expand Down
36 changes: 36 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package fiber

import (
"bytes"
"errors"
"mime/multipart"
"net"
"net/http"
"strconv"
"strings"

"github.com/gofiber/utils/v2"
Expand Down Expand Up @@ -210,6 +214,15 @@ func (r *Request) Hostname() string {
return addr
}

// Port returns the remote port of the request.
func (r *Request) Port() string {
tcpaddr, ok := r.ctx.Context().RemoteAddr().(*net.TCPAddr)
if !ok {
panic(errors.New("failed to type-assert to *net.TCPAddr"))
}
return strconv.Itoa(tcpaddr.Port)
}

// IP returns the remote IP address of the request.
// If ProxyHeader and IP Validation is configured, it will parse that header and return the first valid IP address.
// Please use Config.EnableTrustedProxyCheck to prevent header spoofing, in case when your app is behind the proxy.
Expand Down Expand Up @@ -354,6 +367,23 @@ func (r *Request) Is(extension string) bool {
)
}

var localHosts = [...]string{"127.0.0.1", "::1"}

// IsLocalHost will return true if address is a localhost address.
func isLocalHost(address string) bool {
for _, h := range localHosts {
if address == h {
return true
}
}
return false
}

// IsFromLocal will return true if request came from local.
func (r *Request) IsFromLocal() bool {
return isLocalHost(r.ctx.Context().RemoteIP().String())
}

// BodyRaw contains the raw body submitted in a POST request.
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting instead.
Expand Down Expand Up @@ -559,6 +589,12 @@ func (r *Request) XHR() bool {
return utils.EqualFold(r.fasthttp.Header.Peek(HeaderXRequestedWith), []byte("xmlhttprequest"))
}

// MultipartForm parse form entries from binary.
// This returns a map[string][]string, so given a key the value will be a string slice.
func (r *Request) MultipartForm() (*multipart.Form, error) {
return r.fasthttp.MultipartForm()
}

// Params is used to get the route parameters.
// Defaults to empty string "" if the param doesn't exist.
// If a default value is given, it will return that value if the param doesn't exist.
Expand Down

0 comments on commit 1f1f814

Please sign in to comment.