76 lines
1.7 KiB
Go
76 lines
1.7 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"
|
|
)
|
|
|
|
// An HTTPReadWriter supports reading the HTTP response using the io.ReadWriteCloser interface.
|
|
type HTTPReadWriter struct {
|
|
*HTTP
|
|
post *HTTPConnection
|
|
}
|
|
|
|
func NewHTTPReadWriter(u *url.URL, ctx context.Context) (h *HTTPReadWriter, err error) {
|
|
h = &HTTPReadWriter {
|
|
HTTP: &HTTP {
|
|
ctx: ctx,
|
|
uri: u,
|
|
path: filepath.Join(u.Hostname(), u.RequestURI()),
|
|
},
|
|
post: NewHTTPConnection(http.DefaultClient),
|
|
}
|
|
h.extension()
|
|
h.DetectGzip()
|
|
err = h.post.NewPostRequest(h.ctx, h.uri.String())
|
|
return
|
|
}
|
|
|
|
/*
|
|
func (h *HTTP) 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 *HTTP) ContentType() (contenttype string) {
|
|
contenttype = h.get.Response().Header.Get("Content-Type")
|
|
switch contenttype {
|
|
case "application/octet-stream":
|
|
return h.exttype
|
|
default:
|
|
}
|
|
return
|
|
}
|
|
*/
|
|
|
|
func (h *HTTPReadWriter) ReadWriter() io.ReadWriteCloser {
|
|
return h.post
|
|
}
|
|
|
|
func (h *HTTPReadWriter) PostRequest() *http.Request {
|
|
return h.post.Request()
|
|
}
|
|
|
|
func (h *HTTPReadWriter) PostResponse() *http.Response {
|
|
return h.post.Response()
|
|
}
|