-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprotocol.go
More file actions
131 lines (112 loc) · 2.8 KB
/
protocol.go
File metadata and controls
131 lines (112 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package diecast
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/ghetzel/go-stockutil/maputil"
"github.com/ghetzel/go-stockutil/typeutil"
)
type Protocol interface {
Retrieve(*ProtocolRequest) (*ProtocolResponse, error)
}
type ProtocolConfig map[string]any
func (config ProtocolConfig) Get(key string, fallbacks ...any) typeutil.Variant {
var v = maputil.M(config).Get(key)
if v.IsNil() {
if len(fallbacks) > 0 {
return typeutil.V(fallbacks[0])
} else {
return typeutil.V(nil)
}
} else {
return v
}
}
type ProtocolRequest struct {
Verb string
URL *url.URL
Binding *Binding
Request *http.Request
Header *TemplateHeader
TemplateData map[string]any
TemplateFuncs FuncMap
DefaultTimeout time.Duration
AdditionalHeaders map[string]any
}
func (config *ProtocolRequest) ReadFile(filename string) ([]byte, error) {
if b := config.Binding; b != nil {
if s := b.server; s != nil {
return readFromFS(s.fs, filename)
}
}
return nil, fmt.Errorf("no such file or directory")
}
func (config *ProtocolRequest) Template(input any) (typeutil.Variant, error) {
// only do template evaluation if the input is a string that contains "{{" and "}}"
if vS := typeutil.String(input); strings.Contains(vS, `{{`) && strings.Contains(vS, `}}`) {
if len(config.TemplateFuncs) > 0 {
if v, err := EvalInline(vS, config.TemplateData, config.TemplateFuncs); err == nil {
return typeutil.V(v), nil
} else {
return typeutil.V(nil), err
}
}
}
return typeutil.V(input), nil
}
func (config *ProtocolRequest) Conf(proto string, key string, fallbacks ...any) typeutil.Variant {
if config.Binding != nil {
if config.Binding.server != nil {
if len(config.Binding.server.Protocols) > 0 {
if cnf, ok := config.Binding.server.Protocols[proto]; ok {
return cnf.Get(key, fallbacks...)
}
}
}
}
if len(fallbacks) > 0 {
return typeutil.V(fallbacks[0])
} else {
return typeutil.V(nil)
}
}
type ProtocolResponse struct {
MimeType string
StatusCode int
Raw any
data io.ReadCloser
}
func NewProtocolResponse(data io.ReadCloser) *ProtocolResponse {
return &ProtocolResponse{
MimeType: `application/octet-stream`,
StatusCode: http.StatusOK,
data: data,
}
}
func (config *ProtocolResponse) PeekLen() (int64, error) {
var buf = bytes.NewBuffer(nil)
if n, err := io.Copy(buf, config.data); err == nil {
config.data = io.NopCloser(buf)
return n, nil
} else {
return 0, err
}
}
func (config *ProtocolResponse) Read(b []byte) (int, error) {
if config.data == nil {
return 0, io.EOF
} else {
return config.data.Read(b)
}
}
func (config *ProtocolResponse) Close() error {
if config.data == nil {
return nil
} else {
return config.data.Close()
}
}