forked from 30x/libgozerian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
197 lines (168 loc) · 3.69 KB
/
request.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
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"github.com/30x/gozerian/pipeline"
)
/*
#include <stdlib.h>
*/
import "C"
const (
bodyBufSize = 32767
)
/*
* This represents a single request. The request, in turn, drives HTTP.
* It is assumed that all function calls for a single request happen in the same
* goroutine (that will be the case for an Nginx worker). However, request
* processing itself may happen in a different goroutine.
*/
const (
commandQueueSize = 100
bodyQueueSize = 2
)
type request struct {
req *http.Request
resp *httpResponse
origHeaders http.Header
origURL *url.URL
origBody io.ReadCloser
id uint32
msgID string
pipe pipeline.Pipe
pd pipeline.Definition
cmds chan command
bodies chan []byte
proxying bool
}
func newRequest(id uint32, pd pipeline.Definition) *request {
r := request{
id: id,
proxying: true,
pd: pd,
}
return &r
}
func (r *request) Commands() chan command {
return r.cmds
}
func (r *request) Bodies() chan []byte {
return r.bodies
}
func (r *request) Headers() http.Header {
return r.req.Header
}
func (r *request) ResponseWritten() {
r.proxying = false
}
func (r *request) StartRead() {
}
func (r *request) begin(rawHeaders string) error {
r.cmds = make(chan command, commandQueueSize)
r.bodies = make(chan []byte, bodyQueueSize)
go r.startRequest(rawHeaders)
return nil
}
func (r *request) pollNB() string {
select {
case cmd := <-r.cmds:
return cmd.String()
default:
return ""
}
}
func (r *request) poll() string {
cmd := <-r.cmds
return cmd.String()
}
func (r *request) startRequest(rawHeaders string) {
req, err := parseHTTPHeaders(rawHeaders, true)
if err != nil {
r.cmds <- createErrorCommand(err)
return
}
// Save headers for later
r.origHeaders = copyHeaders(req.Header)
r.origURL = req.URL
r.req = req
resp := &httpResponse{
handler: r,
}
r.resp = resp
req.Body = &requestBody{
handler: r,
}
r.origBody = req.Body
// Call handlers. They may write the request body or headers, or start
// to write out a response.
r.msgID = makeMessageID()
r.pipe = r.pd.CreatePipe()
r.req = r.pipe.PrepareRequest(r.msgID, r.req)
r.pipe.RequestHandlerFunc()(resp, req)
// It's possible that not everything was cleaned up here.
if r.proxying {
r.flush()
} else {
r.resp.flush(http.StatusOK)
}
// This signals that everything is done.
r.cmds <- command{id: DONE}
}
func readAndSend(handler commandHandler, body io.ReadCloser) {
defer body.Close()
buf := make([]byte, bodyBufSize)
len, _ := body.Read(buf)
for len > 0 {
sendBodyChunk(handler, buf[:len])
len, _ = body.Read(buf)
}
}
func sendBodyChunk(handler commandHandler, chunk []byte) {
if len(chunk) == 0 {
return
}
chunkID := allocateChunk(chunk)
cmd := command{
id: WBOD,
msg: fmt.Sprintf("%x", chunkID),
}
handler.Commands() <- cmd
}
func allocateChunk(chunk []byte) int32 {
chunkLen := uint32(len(chunk))
chunkPtr := C.malloc(C.size_t(chunkLen))
copy((*[1 << 30]byte)(chunkPtr)[:], chunk[:])
chunkID := GoStoreChunk(chunkPtr, chunkLen)
return chunkID
}
func (r *request) flush() {
if r.origURL.String() != r.req.URL.String() {
uriCmd := command{
id: WURI,
msg: r.req.URL.String(),
}
r.cmds <- uriCmd
}
if !reflect.DeepEqual(r.origHeaders, r.req.Header) {
hdrCmd := command{
id: WHDR,
msg: serializeHeaders(r.req.Header),
}
r.cmds <- hdrCmd
}
if r.req.Body != r.origBody {
readAndSend(r, r.req.Body)
}
}
func copyHeaders(hdr http.Header) http.Header {
newHeaders := http.Header{}
for k, v := range hdr {
newVal := make([]string, len(v))
copy(newVal, v)
newHeaders[k] = newVal
}
return newHeaders
}