mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 02:14:02 +03:00
FILE / ScuroNeko/mtg
ipblocklist/files/http.go
Исходный файл и его история в репозитории.
64 lines
1.2 KiB
Go
64 lines
1.2 KiB
Go
package files
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
type httpFile struct {
|
|
http *http.Client
|
|
url string
|
|
}
|
|
|
|
func (h httpFile) Open(ctx context.Context) (io.ReadCloser, error) {
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, h.url, nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
response, err := h.http.Do(request)
|
|
if err != nil {
|
|
if response != nil {
|
|
io.Copy(io.Discard, response.Body) // nolint: errcheck
|
|
response.Body.Close()
|
|
}
|
|
|
|
return nil, fmt.Errorf("cannot get url %s: %w", h.url, err)
|
|
}
|
|
|
|
if response.StatusCode >= http.StatusBadRequest {
|
|
return nil, fmt.Errorf("unexpected status code %d", response.StatusCode)
|
|
}
|
|
|
|
return response.Body, nil
|
|
}
|
|
|
|
func (h httpFile) String() string {
|
|
return h.url
|
|
}
|
|
|
|
func NewHTTP(client *http.Client, endpoint string) (File, error) {
|
|
if client == nil {
|
|
return nil, ErrBadHTTPClient
|
|
}
|
|
|
|
parsed, err := url.Parse(endpoint)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("incorrect url %s: %w", endpoint, err)
|
|
}
|
|
|
|
switch parsed.Scheme {
|
|
case "http", "https":
|
|
default:
|
|
return nil, fmt.Errorf("unsupported url %s", endpoint)
|
|
}
|
|
|
|
return httpFile{
|
|
http: client,
|
|
url: endpoint,
|
|
}, nil
|
|
}
|