-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
222 lines (192 loc) · 4.94 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
package webkitgtk
import (
"fmt"
"io"
"math"
"net/http"
"net/url"
"os"
"strconv"
"syscall"
"unsafe"
)
type uriSchemeRequestBody struct {
stream ptr
closed bool
}
func newUriSchemeRequestBody(stream ptr) *uriSchemeRequestBody {
return &uriSchemeRequestBody{stream: stream}
}
// Read implements io.Reader
func (r *uriSchemeRequestBody) Read(p []byte) (int, error) {
if r.closed {
return 0, io.ErrClosedPipe
}
var content unsafe.Pointer
var contentLen int
if p != nil {
content = unsafe.Pointer(&p[0])
contentLen = len(p)
}
var n int
var gErr *gError
if !lib.g.InputStreamReadAll(r.stream, ptr(content), contentLen, &n, 0, gErr) {
return 0, gErr.toError("stream read failed")
}
if n == 0 {
return 0, io.EOF
}
return n, nil
}
// Close implements io.Closer
func (r *uriSchemeRequestBody) Close() error {
if r.closed {
return nil
}
r.closed = true
var err error
var gErr *gError
if !lib.g.InputStreamClose(r.stream, 0, gErr) {
err = gErr.toError("stream close failed")
}
lib.g.ObjectUnref(r.stream)
r.stream = 0
return err
}
type uriSchemeRequest struct {
pointer ptr
reqBody *uriSchemeRequestBody
}
type uriSchemeResponseWriter struct {
request *uriSchemeRequest
header http.Header
code int
writer io.WriteCloser
writerErr error
finished bool
}
func (r *uriSchemeRequest) toResponseWriter() *uriSchemeResponseWriter {
return &uriSchemeResponseWriter{
request: r,
header: http.Header{},
code: math.MinInt,
}
}
func (rw *uriSchemeResponseWriter) Close() error {
if rw.code == math.MinInt {
rw.WriteHeader(http.StatusNotImplemented)
}
if rw.finished {
return nil
}
rw.finished = true
if rw.writer != nil {
return rw.writer.Close()
}
return nil
}
func (rw *uriSchemeResponseWriter) Header() http.Header {
return rw.header
}
func (rw *uriSchemeResponseWriter) WriteHeader(code int) {
if rw.finished || rw.code != math.MinInt {
return
}
rw.code = code
//////////////
contentLength := int64(-1)
if sLen := rw.Header().Get("Content-Length"); sLen != "" {
if pLen, _ := strconv.ParseInt(sLen, 10, 64); pLen > 0 {
contentLength = pLen
}
}
rFD, w, err := rw.newPipe()
if err != nil {
rw.finishWithError(http.StatusInternalServerError, fmt.Errorf("unable to open pipe: %s", err))
return
}
rw.writer = w
stream := lib.g.UnixInputStreamNew(rFD, true)
if err := rw.finishWithResponse(code, rw.Header(), stream, contentLength); err != nil {
rw.finishWithError(http.StatusInternalServerError, fmt.Errorf("unable to finish request: %s", err))
return
}
}
func (rw *uriSchemeResponseWriter) Write(buf []byte) (n int, err error) {
if rw.finished {
return 0, fmt.Errorf("write after finish")
}
rw.WriteHeader(http.StatusOK)
if rw.writerErr != nil {
return 0, rw.writerErr
}
return rw.writer.Write(buf)
}
func (rw *uriSchemeResponseWriter) newPipe() (r int, f *os.File, err error) {
var p [2]int
e := syscall.Pipe2(p[0:], 0)
if e != nil {
return 0, nil, fmt.Errorf("pipe2: %s", e)
}
return p[0], os.NewFile(uintptr(p[1]), "|1"), nil
}
func (rw *uriSchemeResponseWriter) finishWithResponse(code int, header http.Header, stream ptr, streamLength int64) error {
resp := lib.webkit.UriSchemeResponseNew(stream, streamLength)
defer lib.g.ObjectUnref(resp)
lib.webkit.UriSchemeResponseSetStatus(resp, code, "")
lib.webkit.UriSchemeResponseSetContentType(resp, header.Get("Content-Type"))
headers := lib.soup.MessageHeadersNew(1)
for name, values := range header {
for _, value := range values {
lib.soup.MessageHeadersAppend(headers, name, value)
}
}
lib.webkit.UriSchemeResponseSetHttpHeaders(resp, headers)
lib.webkit.UriSchemeRequestFinishWithResponse(rw.request.pointer, resp)
return nil
}
type uriSchemeResponseNopCloser struct {
io.Writer
}
func (uriSchemeResponseNopCloser) Close() error { return nil }
func (rw *uriSchemeResponseWriter) finishWithError(code int, err error) {
if rw.writer != nil {
rw.writer.Close()
rw.writer = &uriSchemeResponseNopCloser{io.Discard}
}
rw.writerErr = err
msg := err.Error()
gErr := lib.g.ErrorNewLiteral(1, msg, code, msg)
defer lib.g.ErrorFree(gErr)
lib.webkit.UriSchemeRequestFinishError(rw.request.pointer, gErr)
}
func (r *uriSchemeRequest) toHttpRequest() (*http.Request, error) {
var req http.Request
req.RequestURI = lib.webkit.UriSchemeRequestGetUri(r.pointer)
reqUrl, err := url.ParseRequestURI(req.RequestURI)
if err != nil {
return nil, err
}
req.URL = reqUrl
req.Method = lib.webkit.UriSchemeRequestGetHttpMethod(r.pointer)
req.Body = http.NoBody
reqBody := lib.webkit.UriSchemeRequestGetHttpBody(r.pointer)
if reqBody != 0 {
r.reqBody = newUriSchemeRequestBody(reqBody)
req.Body = r.reqBody
}
return &req, nil
}
func newUriSchemeRequest(pointer ptr) *uriSchemeRequest {
req := &uriSchemeRequest{pointer: pointer}
lib.g.ObjectRef(req.pointer)
return req
}
func (r *uriSchemeRequest) Close() error {
if r.reqBody != nil {
return r.reqBody.Close()
}
lib.g.ObjectUnref(r.pointer)
r.pointer = 0
return nil
}