jx/internal/source/http.go

84 lines
1.7 KiB
Go
Raw Normal View History

2024-04-18 00:07:12 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package source
import (
_ "context"
_ "encoding/json"
2024-05-12 08:20:51 +00:00
_ "fmt"
2024-04-18 00:07:12 +00:00
_ "gopkg.in/yaml.v3"
"net/url"
2024-05-12 08:20:51 +00:00
_ "net/http"
2024-04-18 00:07:12 +00:00
_ "path/filepath"
"decl/internal/resource"
"decl/internal/iofilter"
"decl/internal/signature"
2024-05-12 08:20:51 +00:00
"decl/internal/transport"
"decl/internal/codec"
2024-04-18 00:07:12 +00:00
_ "os"
"io"
"errors"
"crypto/sha256"
)
type HTTP struct {
Endpoint string `yaml:"endpoint" json:"endpoint"`
2024-05-12 08:20:51 +00:00
transport *transport.Reader `yaml:"-" json:"-"`
2024-04-18 00:07:12 +00:00
}
func NewHTTP() *HTTP {
return &HTTP{}
}
func init() {
SourceTypes.Register([]string{"http","https"}, func(u *url.URL) DocSource {
t := NewHTTP()
t.Endpoint = u.String()
2024-05-12 08:20:51 +00:00
t.transport,_ = transport.NewReader(u)
2024-04-18 00:07:12 +00:00
return t
})
}
func (d *HTTP) Type() string { return "http" }
func (h *HTTP) ExtractResources(filter ResourceSelector) ([]*resource.Document, error) {
documents := make([]*resource.Document, 0, 100)
2024-05-12 08:20:51 +00:00
defer h.transport.Close()
documentSignature := h.transport.Signature()
2024-04-21 06:13:17 +00:00
2024-04-18 00:07:12 +00:00
hash := sha256.New()
2024-05-12 08:20:51 +00:00
sumReadData := iofilter.NewReader(h.transport, func(p []byte, readn int, readerr error) (n int, err error) {
2024-04-18 00:07:12 +00:00
hash.Write(p)
return
})
decoder := codec.NewYAMLDecoder(sumReadData)
2024-04-18 00:07:12 +00:00
index := 0
for {
2024-04-22 06:11:17 +00:00
doc := resource.NewDocument()
2024-04-18 00:07:12 +00:00
e := decoder.Decode(doc)
if errors.Is(e, io.EOF) {
break
}
if e != nil {
return documents, e
}
if validationErr := doc.Validate(); validationErr != nil {
return documents, validationErr
}
2024-04-22 06:11:17 +00:00
documents = append(documents, doc)
2024-04-18 00:07:12 +00:00
index++
}
2024-04-19 07:52:10 +00:00
if documentSignature != "" {
sig := &signature.Ident{}
2024-04-22 06:11:17 +00:00
sigErr := sig.VerifySum(hash.Sum(nil), []byte(documentSignature))
if sigErr != nil {
return documents, sigErr
}
2024-04-18 00:07:12 +00:00
}
return documents, nil
}