-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhandler.go
225 lines (192 loc) · 5 KB
/
handler.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package handler
import (
"bytes"
"fmt"
"net/http"
"strconv"
incominghandler "github.com/dev-wasm/dev-wasm-go/lib/wasi/http/incoming-handler"
"github.com/dev-wasm/dev-wasm-go/lib/wasi/http/types"
"go.bytecodealliance.org/cm"
)
var h = &handler{
handler: http.DefaultServeMux,
}
func OK[Shape, T, Err any](val cm.Result[Shape, T, Err]) *T {
return (&val).OK()
}
func OKOrPanic[Shape, T, Err any](val cm.Result[Shape, T, Err]) *T {
if !val.IsOK() {
panic(fmt.Sprintf("a value is not OK as expected: %v", val))
}
return (&val).OK()
}
func Some[T any](val cm.Option[T]) *T {
return (&val).Some()
}
func theHandler(req types.IncomingRequest, res types.ResponseOutparam) {
h.Handle(req, res)
}
func init() {
incominghandler.Exports.Handle = theHandler
}
func HandleFunc(pattern string, fn http.HandlerFunc) {
http.DefaultServeMux.HandleFunc(pattern, fn)
}
type handler struct {
handler http.Handler
}
type wasmResponseWriter struct {
header http.Header
code int
body bytes.Buffer
}
func (w *wasmResponseWriter) Header() http.Header {
return w.header
}
func (w *wasmResponseWriter) WriteHeader(code int) {
w.code = code
}
func (w *wasmResponseWriter) Write(data []byte) (int, error) {
if w.code == 0 {
w.code = http.StatusOK
}
return w.body.Write(data)
}
func schemeToString(scheme *types.Scheme) string {
if scheme.HTTP() {
return "http"
}
if scheme.HTTPS() {
return "https"
}
return *scheme.Other()
}
func methodToString(method types.Method) string {
if method.Get() {
return "GET"
}
if method.Put() {
return "PUT"
}
if method.Post() {
return "POST"
}
if method.Patch() {
return "PATCH"
}
if method.Connect() {
return "CONNECT"
}
if method.Delete() {
return "DELETE"
}
if method.Head() {
return "HEAD"
}
if method.Options() {
return "OPTIONS"
}
panic("unsupported method")
}
func (h *handler) HandleError(msg string, req types.IncomingRequest, responseOut types.ResponseOutparam) {
headers := []cm.Tuple[types.FieldKey, types.FieldValue]{}
hdrs := cm.ToList(headers)
res := types.FieldsFromList(hdrs)
response := types.NewOutgoingResponse(*res.OK())
response.SetStatusCode(500)
body := OK(response.Body())
resResult := cm.OK[cm.Result[types.ErrorCodeShape, types.OutgoingResponse, types.ErrorCode]](response)
types.ResponseOutparamSet(responseOut, resResult)
out := OK(body.Write())
OKOrPanic(out.BlockingWriteAndFlush(cm.ToList([]uint8(msg))))
types.OutgoingBodyFinish(*body, cm.None[types.Fields]())
}
func (h *handler) Handle(req types.IncomingRequest, responseOut types.ResponseOutparam) {
defer func() {
if r := recover(); r != nil {
msg := "unknown panic"
switch t := r.(type) {
case string:
msg = t
case error:
msg = t.Error()
default:
// pass
}
h.HandleError(msg, req, responseOut)
}
}()
scheme := Some(req.Scheme())
authority := Some(req.Authority())
path := Some(req.PathWithQuery())
urlString := fmt.Sprintf("%s://%s%s", schemeToString(scheme), *authority, *path)
method := req.Method()
requestBody := OK(req.Consume())
bodyStream := OK(requestBody.Stream())
var buff []byte
for {
readResult := bodyStream.BlockingRead(1024 * 1024)
if readResult.IsErr() {
if readResult.Err().Closed() {
break
} else {
h.HandleError("Reading body failed!", req, responseOut)
return
}
} else {
bytes := readResult.OK().Slice()
buff = append(buff, bytes...)
}
}
bodyStream.ResourceDrop()
requestBody.ResourceDrop()
fields := req.Headers()
header := http.Header{}
for _, tuple := range fields.Entries().Slice() {
header[string(tuple.F0)] = append(header[string(tuple.F0)], string(tuple.F1.Slice()))
}
fields.ResourceDrop()
req.ResourceDrop()
goReq, err := http.NewRequest(methodToString(method), urlString, bytes.NewBuffer(buff))
if err != nil {
h.HandleError(err.Error(), req, responseOut)
return
}
goReq.Header = header
if length, ok := header["Content-Length"]; ok {
if contentLength, err := strconv.Atoi(length[0]); err == nil {
goReq.ContentLength = int64(contentLength)
}
}
goRes := wasmResponseWriter{
header: http.Header{},
code: -1,
body: bytes.Buffer{},
}
h.handler.ServeHTTP(&goRes, goReq)
headers := []cm.Tuple[types.FieldKey, types.FieldValue]{}
for key, val := range goRes.header {
for ix := range val {
headers = append(headers, cm.Tuple[types.FieldKey, types.FieldValue]{
F0: types.FieldKey(key),
F1: types.FieldValue(cm.ToList([]uint8(val[ix]))),
})
}
}
f := OK(types.FieldsFromList(cm.ToList(headers)))
res := types.NewOutgoingResponse(*f)
res.SetStatusCode(types.StatusCode(goRes.code))
body := OK(res.Body())
result := cm.OK[cm.Result[types.ErrorCodeShape, types.OutgoingResponse, types.ErrorCode]](res)
types.ResponseOutparamSet(responseOut, result)
stream := OK(body.Write())
stream.BlockingWriteAndFlush(cm.ToList([]byte(goRes.body.Bytes())))
stream.ResourceDrop()
types.OutgoingBodyFinish(*body, cm.None[types.Fields]())
}
func ListenAndServe(handler http.Handler) error {
if handler != nil {
h.handler = handler
}
return nil
}