Skip to content

Commit a05dd1d

Browse files
committed
feat: move Secure, Stale, Subdomains, and XHR to Request
1 parent 02cf7d6 commit a05dd1d

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

ctx.go

+8-21
Original file line numberDiff line numberDiff line change
@@ -1095,9 +1095,9 @@ func (*DefaultCtx) SaveFileToStorage(fileheader *multipart.FileHeader, path stri
10951095
return nil
10961096
}
10971097

1098-
// Secure returns whether a secure connection was established.
1098+
// Secure is an alias of [Request.Secure].
10991099
func (c *DefaultCtx) Secure() bool {
1100-
return c.Protocol() == schemeHTTPS
1100+
return c.Req().Secure()
11011101
}
11021102

11031103
// Send sets the HTTP response body without copying it.
@@ -1228,26 +1228,14 @@ func (c *DefaultCtx) setCanonical(key, val string) {
12281228
c.fasthttp.Response.Header.SetCanonical(utils.UnsafeBytes(key), utils.UnsafeBytes(val))
12291229
}
12301230

1231-
// Subdomains returns a string slice of subdomains in the domain name of the request.
1232-
// The subdomain offset, which defaults to 2, is used for determining the beginning of the subdomain segments.
1231+
// Subdomains is an alias of [Request.Subdomains].
12331232
func (c *DefaultCtx) Subdomains(offset ...int) []string {
1234-
o := 2
1235-
if len(offset) > 0 {
1236-
o = offset[0]
1237-
}
1238-
subdomains := strings.Split(c.Host(), ".")
1239-
l := len(subdomains) - o
1240-
// Check index to avoid slice bounds out of range panic
1241-
if l < 0 {
1242-
l = len(subdomains)
1243-
}
1244-
subdomains = subdomains[:l]
1245-
return subdomains
1233+
return c.Req().Subdomains(offset...)
12461234
}
12471235

1248-
// Stale is not implemented yet, pull requests are welcome!
1236+
// Stale is an alias of [Request.Stale].
12491237
func (c *DefaultCtx) Stale() bool {
1250-
return !c.Fresh()
1238+
return c.Req().Stale()
12511239
}
12521240

12531241
// Status sets the HTTP status for the response.
@@ -1330,10 +1318,9 @@ func (c *DefaultCtx) WriteString(s string) (int, error) {
13301318
return len(s), nil
13311319
}
13321320

1333-
// XHR returns a Boolean property, that is true, if the request's X-Requested-With header field is XMLHttpRequest,
1334-
// indicating that the request was issued by a client library (such as jQuery).
1321+
// XHR is an alias of [Request.XHR].
13351322
func (c *DefaultCtx) XHR() bool {
1336-
return utils.EqualFold(c.app.getBytes(c.Get(HeaderXRequestedWith)), []byte("xmlhttprequest"))
1323+
return c.Req().XHR()
13371324
}
13381325

13391326
// configDependentPaths set paths for route recognition and prepared paths for the user,

request.go

+34
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,37 @@ func (r *Request) Fresh() bool {
431431
}
432432
return true
433433
}
434+
435+
// Secure returns whether a secure connection was established.
436+
func (r *Request) Secure() bool {
437+
return r.Protocol() == schemeHTTPS
438+
}
439+
440+
// Stale is the opposite of [Request.Fresh] and returns true when the response
441+
// to this request is no longer "fresh" in the client's cache.
442+
func (r *Request) Stale() bool {
443+
return !r.Fresh()
444+
}
445+
446+
// Subdomains returns a string slice of subdomains in the domain name of the request.
447+
// The subdomain offset, which defaults to 2, is used for determining the beginning of the subdomain segments.
448+
func (r *Request) Subdomains(offset ...int) []string {
449+
o := 2
450+
if len(offset) > 0 {
451+
o = offset[0]
452+
}
453+
subdomains := strings.Split(r.Host(), ".")
454+
l := len(subdomains) - o
455+
// Check index to avoid slice bounds out of range panic
456+
if l < 0 {
457+
l = len(subdomains)
458+
}
459+
subdomains = subdomains[:l]
460+
return subdomains
461+
}
462+
463+
// XHR returns a Boolean property, that is true, if the request's X-Requested-With header field is XMLHttpRequest,
464+
// indicating that the request was issued by a client library (such as jQuery).
465+
func (r *Request) XHR() bool {
466+
return utils.EqualFold(r.Get(HeaderXRequestedWith), "xmlhttprequest")
467+
}

0 commit comments

Comments
 (0)