From a05dd1d0d3cc8ab772e580ef6957725fb0056192 Mon Sep 17 00:00:00 2001 From: Nicholas Jackson Date: Sun, 17 Mar 2024 18:14:23 -0700 Subject: [PATCH] feat: move Secure, Stale, Subdomains, and XHR to Request --- ctx.go | 29 ++++++++--------------------- request.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/ctx.go b/ctx.go index 76708d7d94f..5dddc5aa807 100644 --- a/ctx.go +++ b/ctx.go @@ -1095,9 +1095,9 @@ func (*DefaultCtx) SaveFileToStorage(fileheader *multipart.FileHeader, path stri return nil } -// Secure returns whether a secure connection was established. +// Secure is an alias of [Request.Secure]. func (c *DefaultCtx) Secure() bool { - return c.Protocol() == schemeHTTPS + return c.Req().Secure() } // Send sets the HTTP response body without copying it. @@ -1228,26 +1228,14 @@ func (c *DefaultCtx) setCanonical(key, val string) { c.fasthttp.Response.Header.SetCanonical(utils.UnsafeBytes(key), utils.UnsafeBytes(val)) } -// Subdomains returns a string slice of subdomains in the domain name of the request. -// The subdomain offset, which defaults to 2, is used for determining the beginning of the subdomain segments. +// Subdomains is an alias of [Request.Subdomains]. func (c *DefaultCtx) Subdomains(offset ...int) []string { - o := 2 - if len(offset) > 0 { - o = offset[0] - } - subdomains := strings.Split(c.Host(), ".") - l := len(subdomains) - o - // Check index to avoid slice bounds out of range panic - if l < 0 { - l = len(subdomains) - } - subdomains = subdomains[:l] - return subdomains + return c.Req().Subdomains(offset...) } -// Stale is not implemented yet, pull requests are welcome! +// Stale is an alias of [Request.Stale]. func (c *DefaultCtx) Stale() bool { - return !c.Fresh() + return c.Req().Stale() } // Status sets the HTTP status for the response. @@ -1330,10 +1318,9 @@ func (c *DefaultCtx) WriteString(s string) (int, error) { return len(s), nil } -// XHR returns a Boolean property, that is true, if the request's X-Requested-With header field is XMLHttpRequest, -// indicating that the request was issued by a client library (such as jQuery). +// XHR is an alias of [Request.XHR]. func (c *DefaultCtx) XHR() bool { - return utils.EqualFold(c.app.getBytes(c.Get(HeaderXRequestedWith)), []byte("xmlhttprequest")) + return c.Req().XHR() } // configDependentPaths set paths for route recognition and prepared paths for the user, diff --git a/request.go b/request.go index f3171617a05..97b6f465f54 100644 --- a/request.go +++ b/request.go @@ -431,3 +431,37 @@ func (r *Request) Fresh() bool { } return true } + +// Secure returns whether a secure connection was established. +func (r *Request) Secure() bool { + return r.Protocol() == schemeHTTPS +} + +// Stale is the opposite of [Request.Fresh] and returns true when the response +// to this request is no longer "fresh" in the client's cache. +func (r *Request) Stale() bool { + return !r.Fresh() +} + +// Subdomains returns a string slice of subdomains in the domain name of the request. +// The subdomain offset, which defaults to 2, is used for determining the beginning of the subdomain segments. +func (r *Request) Subdomains(offset ...int) []string { + o := 2 + if len(offset) > 0 { + o = offset[0] + } + subdomains := strings.Split(r.Host(), ".") + l := len(subdomains) - o + // Check index to avoid slice bounds out of range panic + if l < 0 { + l = len(subdomains) + } + subdomains = subdomains[:l] + return subdomains +} + +// XHR returns a Boolean property, that is true, if the request's X-Requested-With header field is XMLHttpRequest, +// indicating that the request was issued by a client library (such as jQuery). +func (r *Request) XHR() bool { + return utils.EqualFold(r.Get(HeaderXRequestedWith), "xmlhttprequest") +}