Documentation ¶
Overview ¶
Package httputil is a Golang package which provides helper functions for HTTP utilities.
Index ¶
- Variables
- func GetFileName(targetURL string) (string, error)
- func GetFileNameFromResponse(resp *http.Response) (string, error)
- func GetFileNameFromURL(targetURL string) (string, error)
- func GetResp(uri string) (resp *http.Response, isSizeKnown bool, size uint64, isRangeSupported bool, ...)
- func GetRespOfRange(uri string, start, end uint64, isEndIgnored bool) (*http.Response, uint64, error)
- func GetRespOfRangeStart(uri string, start uint64) (*http.Response, uint64, error)
- func NewUploadFileRequest(method, uri, filePath, fieldName string, params map[string]string) (*http.Request, error)
- func SetRangeHeader(header http.Header, start, end uint64, isEndIgnored bool)
- func Size(uri string) (isSizeKnown bool, size uint64, isRangeSupported bool, err error)
- func SizeOfRange(uri string, start, end uint64, isEndIgnored bool) (uint64, error)
- func SizeOfRangeStart(uri string, start uint64) (uint64, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // status code is not 200 ErrNot200 = errors.New("status code is not 200") // content range is not set correctly ErrIncorrectRange = errors.New("incorrect range") // status code is not 200, 416 or 206 ErrNot200or416or206 = errors.New("status code is not 200, 416 or 206") // range header is not supported by the server ErrRangeNotSupported = errors.New("range header is not supported by the server") // request method is not HEAD or GET ErrMethodNotHeadOrGet = errors.New("request method is not HEAD or GET") )
Functions ¶
func GetFileName ¶
GetFileName returns the downloadable file name. It'll try to get the name in the HTTP response firstly. If it fails, then it'll try to parse the URL to get the file name.
func GetFileNameFromResponse ¶
GetFileNameFromResponse returns downloadable file name in the HTTP response.
Example ¶
package main import ( "fmt" "log" "net/http" "github.com/northbright/httputil" ) func main() { url := "https://github.com/northbright/plants/archive/master.zip" resp, err := http.Head(url) if err != nil { log.Printf("head error: %v", err) return } defer resp.Body.Close() fileName, _ := httputil.GetFileNameFromResponse(resp) fmt.Print(fileName) }
Output: plants-master.zip
func GetFileNameFromURL ¶
GetFileNameFromURL returns the downloadable file name from target URL.
Example ¶
package main import ( "fmt" "github.com/northbright/httputil" ) func main() { url := "https://github.com/northbright/plants/archive/master.zip" fileName, _ := httputil.GetFileNameFromURL(url) fmt.Print(fileName) }
Output: master.zip
func GetResp ¶ added in v1.1.0
func GetResp(uri string) (resp *http.Response, isSizeKnown bool, size uint64, isRangeSupported bool, err error)
GetResp returns: 1. the response. 2. if the size of content is known or not. 3. size of the content. 4. if range header is supported by the server.
Example ¶
package main import ( "fmt" "log" "github.com/northbright/httputil" ) func main() { uri := "https://golang.google.cn/dl/go1.19.3.darwin-arm64.pkg" resp, isSizeKnown, size, isRangeSupported, err := httputil.GetResp(uri) if err != nil { log.Printf("GetResp() error: %v", err) return } defer resp.Body.Close() fmt.Printf("is size known: %v\nsize: %d\nis range supported: %v", isSizeKnown, size, isRangeSupported) }
Output: is size known: true size: 145565374 is range supported: true
func GetRespOfRange ¶ added in v1.1.0
func GetRespOfRange(uri string, start, end uint64, isEndIgnored bool) (*http.Response, uint64, error)
GetRespOfRange returns the response and the size of the partial content. If isEndIgnored is true, the range header uses "bytes=start-" syntax.
Example ¶
package main import ( "fmt" "log" "github.com/northbright/httputil" ) func main() { uri := "https://golang.google.cn/dl/go1.19.3.darwin-arm64.pkg" // Get the response, size of the range of the content using "bytes=start-end" syntax. resp, l, err := httputil.GetRespOfRange(uri, 0, 99999999, false) if err != nil { log.Printf("httputil.GetRespOfRange() error: %v", err) return } defer resp.Body.Close() fmt.Printf("size of range: 0 - 99999999: %d\n", l) // Get response and len of range. resp2, l, err := httputil.GetRespOfRange(uri, 100000000, 0, true) if err != nil { log.Printf("httputil.GetRespOfRange() error: %v", err) return } defer resp2.Body.Close() fmt.Printf("size of range: 10000000-: %d\n", l) }
Output: size of range: 0 - 99999999: 100000000 size of range: 10000000-: 45565374
func GetRespOfRangeStart ¶ added in v1.1.0
GetRespOfRangeStart returns the response and the size of the partial content. If isEndIgnored is true, the range header uses "bytes=start-" syntax.
Example ¶
package main import ( "fmt" "log" "github.com/northbright/httputil" ) func main() { uri := "https://golang.google.cn/dl/go1.19.3.darwin-arm64.pkg" // Get the response, size of the range of the content using "bytes=start-" syntax. resp, l, err := httputil.GetRespOfRangeStart(uri, 100000000) if err != nil { log.Printf("httputil.SizeOfRange() error: %v", err) return } defer resp.Body.Close() fmt.Printf("size of range: 10000000-: %d\n", l) }
Output: size of range: 10000000-: 45565374
func NewUploadFileRequest ¶
func NewUploadFileRequest(method, uri, filePath, fieldName string, params map[string]string) (*http.Request, error)
NewUploadFileRequest() creates HTTP request to upload file.
Params:
method: HTTP request method. Available values: "POST", "PUT". uri: Request URL. filePath: Absolute path of file to be uploaded. fieldName: Field name(param name) of the uploaded file. Server side need this name to get uploaded file. params: Extra params to be set in the form.
func SetRangeHeader ¶ added in v1.1.0
SetRangeHeader adds the range key-value pair to the header. If isEndIgnored is true, the syntax is "bytes=start-".
func Size ¶ added in v1.2.0
Size returns: 1. if the size of content is known or not. 2. size of the content. 3. if range header is supported by the server.
Example ¶
package main import ( "fmt" "log" "github.com/northbright/httputil" ) func main() { uri := "https://golang.google.cn/dl/go1.19.3.darwin-arm64.pkg" isSizeKnown, size, isRangeSupported, err := httputil.Size(uri) if err != nil { log.Printf("Size() error: %v", err) return } fmt.Printf("is size known: %v\nsize: %d\nis range supported: %v", isSizeKnown, size, isRangeSupported) }
Output: is size known: true size: 145565374 is range supported: true
func SizeOfRange ¶ added in v1.2.0
SizeOfRange returns the size of the partial content. If isEndIgnored is true, the range header uses "bytes=start-" syntax.
Example ¶
package main import ( "fmt" "log" "github.com/northbright/httputil" ) func main() { uri := "https://golang.google.cn/dl/go1.19.3.darwin-arm64.pkg" // Get the size of the range of the content using "bytes=start-end" syntax. l, err := httputil.SizeOfRange(uri, 0, 99999999, false) if err != nil { log.Printf("httputil.SizeOfRange() error: %v", err) return } fmt.Printf("size of range: 0 - 99999999: %d\n", l) // Get the size of the range of the content using "bytes=start-" syntax. l, err = httputil.SizeOfRange(uri, 100000000, 0, true) if err != nil { log.Printf("httputil.SizeOfRange() error: %v", err) return } fmt.Printf("size of range: 10000000-: %d\n", l) }
Output: size of range: 0 - 99999999: 100000000 size of range: 10000000-: 45565374
func SizeOfRangeStart ¶ added in v1.2.0
SizeOfRangeStart returns the size of the partial content. The range header uses "bytes=start-" syntax.
Example ¶
package main import ( "fmt" "log" "github.com/northbright/httputil" ) func main() { uri := "https://golang.google.cn/dl/go1.19.3.darwin-arm64.pkg" // Get the size of the range of the content using "bytes=start-" syntax. l, err := httputil.SizeOfRangeStart(uri, 100000000) if err != nil { log.Printf("httputil.SizeOfRangeStart() error: %v", err) return } fmt.Printf("size of range: 10000000-: %d\n", l) }
Output: size of range: 10000000-: 45565374
Types ¶
This section is empty.