76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||
|
|
||
|
package transport
|
||
|
|
||
|
import (
|
||
|
_ "errors"
|
||
|
"io"
|
||
|
_ "os"
|
||
|
"net/url"
|
||
|
"net/http"
|
||
|
"context"
|
||
|
"path/filepath"
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
type HTTPReader struct {
|
||
|
*HTTP
|
||
|
get *HTTPConnection
|
||
|
}
|
||
|
|
||
|
func NewHTTPReader(u *url.URL, ctx context.Context) (h *HTTPReader, err error) {
|
||
|
h = &HTTPReader {
|
||
|
HTTP: &HTTP {
|
||
|
ctx: ctx,
|
||
|
uri: u,
|
||
|
path: filepath.Join(u.Hostname(), u.RequestURI()),
|
||
|
Client: http.DefaultClient,
|
||
|
},
|
||
|
get: NewHTTPConnection(http.DefaultClient),
|
||
|
}
|
||
|
|
||
|
h.extension()
|
||
|
h.DetectGzip()
|
||
|
err = h.get.NewGetRequest(h.ctx, h.uri.String())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (h *HTTPReader) Signature() (documentSignature string) {
|
||
|
if h.get.Response() != nil {
|
||
|
documentSignature = h.get.Response().Header.Get("Signature")
|
||
|
if documentSignature == "" {
|
||
|
signatureResp, signatureErr := h.Client.Get(fmt.Sprintf("%s.sig", h.uri.String()))
|
||
|
if signatureErr == nil {
|
||
|
defer signatureResp.Body.Close()
|
||
|
readSignatureBody, readSignatureErr := io.ReadAll(signatureResp.Body)
|
||
|
if readSignatureErr == nil {
|
||
|
documentSignature = string(readSignatureBody)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return documentSignature
|
||
|
}
|
||
|
|
||
|
func (h *HTTPReader) ContentType() (contenttype string) {
|
||
|
contenttype = h.get.Response().Header.Get("Content-Type")
|
||
|
switch contenttype {
|
||
|
case "application/octet-stream":
|
||
|
return h.exttype
|
||
|
default:
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (h *HTTPReader) Reader() io.ReadCloser {
|
||
|
return h.get
|
||
|
}
|
||
|
|
||
|
func (h *HTTPReader) GetRequest() *http.Request {
|
||
|
return h.get.Request()
|
||
|
}
|
||
|
|
||
|
func (h *HTTPReader) GetResponse() *http.Response {
|
||
|
return h.get.Response()
|
||
|
}
|