Grab downloader

This commit is contained in:
Lorenzo Bolla
2021-10-08 10:43:52 +02:00
parent f93bc6ef0f
commit 894192851e
38 changed files with 4240 additions and 1 deletions
+92
View File
@@ -0,0 +1,92 @@
package grabtest
import (
"errors"
"net/http"
"time"
)
type HandlerOption func(*handler) error
func StatusCodeStatic(code int) HandlerOption {
return func(h *handler) error {
return StatusCode(func(req *http.Request) int {
return code
})(h)
}
}
func StatusCode(f StatusCodeFunc) HandlerOption {
return func(h *handler) error {
if f == nil {
return errors.New("status code function cannot be nil")
}
h.statusCodeFunc = f
return nil
}
}
func MethodWhitelist(methods ...string) HandlerOption {
return func(h *handler) error {
h.methodWhitelist = methods
return nil
}
}
func HeaderBlacklist(headers ...string) HandlerOption {
return func(h *handler) error {
h.headerBlacklist = headers
return nil
}
}
func ContentLength(n int) HandlerOption {
return func(h *handler) error {
if n < 0 {
return errors.New("content length must be zero or greater")
}
h.contentLength = n
return nil
}
}
func AcceptRanges(enabled bool) HandlerOption {
return func(h *handler) error {
h.acceptRanges = enabled
return nil
}
}
func LastModified(t time.Time) HandlerOption {
return func(h *handler) error {
h.lastModified = t.UTC()
return nil
}
}
func TimeToFirstByte(d time.Duration) HandlerOption {
return func(h *handler) error {
if d < 1 {
return errors.New("time to first byte must be greater than zero")
}
h.ttfb = d
return nil
}
}
func RateLimiter(bps int) HandlerOption {
return func(h *handler) error {
if bps < 1 {
return errors.New("bytes per second must be greater than zero")
}
h.rateLimiter = time.NewTicker(time.Second / time.Duration(bps))
return nil
}
}
func AttachmentFilename(filename string) HandlerOption {
return func(h *handler) error {
h.attachmentFilename = filename
return nil
}
}