Skip to content

Commit 045bd47

Browse files
committed
Merge branch 'master' of git://github.com/hoisie/web.go
Conflicts: web.go
2 parents 207f520 + b0acb23 commit 045bd47

File tree

10 files changed

+563
-301
lines changed

10 files changed

+563
-301
lines changed

Makefile

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@
55
include $(GOROOT)/src/Make.$(GOARCH)
66

77
TARG=web
8+
GOFMT=gofmt -spaces=true -tabindent=false -tabwidth=4
9+
810
GOFILES=\
911
fcgi.go\
12+
request.go\
1013
scgi.go\
1114
servefile.go\
1215
web.go\
1316

1417
include $(GOROOT)/src/Make.pkg
1518

1619
format:
17-
gofmt -spaces=true -tabindent=false -tabwidth=4 -w fcgi.go
18-
gofmt -spaces=true -tabindent=false -tabwidth=4 -w scgi.go
19-
gofmt -spaces=true -tabindent=false -tabwidth=4 -w servefile.go
20-
gofmt -spaces=true -tabindent=false -tabwidth=4 -w web.go
21-
gofmt -spaces=true -tabindent=false -tabwidth=4 -w web_test.go
20+
${GOFMT} -w fcgi.go
21+
${GOFMT} -w request.go
22+
${GOFMT} -w scgi.go
23+
${GOFMT} -w servefile.go
24+
${GOFMT} -w web.go
25+
${GOFMT} -w web_test.go
26+
${GOFMT} -w examples/hello.go
27+
${GOFMT} -w examples/arcchallenge.go

examples/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ALL=hello arcchallenge
55
all: $(ALL)
66

77
clean:
8-
rm -rf *.6 $(ALL)
8+
rm -rf *.[68] $(ALL)
99

1010
%: %.go
1111
$(GC) $*.go

examples/arcchallenge.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ import (
44
"web"
55
)
66

7-
var input = ""
8-
9-
var tmpl = `<form action="say" method="POST"><input name="said"><input type="submit"></form>`
7+
var form = `<form action="say" method="POST"><input name="said"><input type="submit"></form>`
108

119
func main() {
12-
web.Get("/said", func() string { return tmpl })
10+
web.Get("/said", func() string { return form })
1311
web.Post("/say", func(ctx *web.Context) string {
14-
input = ctx.Request.Form["said"][0]
12+
ctx.Session.Data["said"] = ctx.Request.Params["said"][0]
1513
return `<a href="/final">Click Here</a>`
1614
})
17-
web.Get("/final", func() string { return "You said " + input })
15+
web.Get("/final", func(ctx *web.Context) string { return "You said " + ctx.Session.Data["said"].(string) })
1816
web.Run("0.0.0.0:9999")
1917
}

examples/hello.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package main
22

33
import (
4-
"fmt"
54
"web"
65
)
76

8-
func hello(val string) string { return fmt.Sprintf("hello %s", val) }
7+
func hello(val string) string { return "hello " + val }
98

109
func main() {
1110
web.Get("/(.*)", hello)

fcgi.go

Lines changed: 48 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,32 @@ import (
55
"bufio"
66
"encoding/binary"
77
"fmt"
8-
"http"
98
"io"
109
"log"
1110
"net"
1211
"os"
1312
)
1413

1514
const (
16-
FcgiBeginRequest = iota + 1
17-
FcgiAbortRequest
18-
FcgiEndRequest
19-
FcgiParams
20-
FcgiStdin
21-
FcgiStdout
22-
FcgiStderr
23-
FcgiData
24-
FcgiGetValues
25-
FcgiGetValuesResult
26-
FcgiUnknownType
27-
FcgiMaxType = FcgiUnknownType
15+
fcgiBeginRequest = iota + 1
16+
fcgiAbortRequest
17+
fcgiEndRequest
18+
fcgiParams
19+
fcgiStdin
20+
fcgiStdout
21+
fcgiStderr
22+
fcgiData
23+
fcgiGetValues
24+
fcgiGetValuesResult
25+
fcgiUnknownType
26+
fcgiMaxType = fcgiUnknownType
2827
)
2928

3029
const (
31-
FcgiRequestComplete = iota
32-
FcgiCantMpxConn
33-
FcgiOverloaded
34-
FcgiUnknownRole
30+
fcgiRequestComplete = iota
31+
fcgiCantMpxConn
32+
fcgiOverloaded
33+
fcgiUnknownRole
3534
)
3635

3736
type fcgiHeader struct {
@@ -76,13 +75,13 @@ func newFcgiRecord(typ int, requestId int, data []byte) []byte {
7675
return record.Bytes()
7776
}
7877

79-
type fcgiEndRequest struct {
78+
type fcgiEndReq struct {
8079
appStatus uint32
8180
protocolStatus uint8
8281
reserved [3]uint8
8382
}
8483

85-
func (er fcgiEndRequest) bytes() []byte {
84+
func (er fcgiEndReq) bytes() []byte {
8685
buf := make([]byte, 8)
8786
binary.BigEndian.PutUint32(buf, er.appStatus)
8887
buf[4] = er.protocolStatus
@@ -92,7 +91,7 @@ func (er fcgiEndRequest) bytes() []byte {
9291
type fcgiConn struct {
9392
requestId uint16
9493
fd io.ReadWriteCloser
95-
headers map[string]string
94+
headers map[string][]string
9695
wroteHeaders bool
9796
}
9897

@@ -102,7 +101,7 @@ func (conn *fcgiConn) fcgiWrite(data []byte) (err os.Error) {
102101
padding := make([]byte, uint8(-l&7))
103102
hdr := fcgiHeader{
104103
Version: 1,
105-
Type: FcgiStdout,
104+
Type: fcgiStdout,
106105
RequestId: conn.requestId,
107106
ContentLength: uint16(l),
108107
PaddingLength: uint8(len(padding)),
@@ -134,7 +133,9 @@ func (conn *fcgiConn) Write(data []byte) (n int, err os.Error) {
134133
if !conn.wroteHeaders {
135134
conn.wroteHeaders = true
136135
for k, v := range conn.headers {
137-
buf.WriteString(k + ": " + v + "\r\n")
136+
for _, i := range v {
137+
buf.WriteString(k + ": " + i + "\r\n")
138+
}
138139
}
139140
buf.WriteString("\r\n")
140141
conn.fcgiWrite(buf.Bytes())
@@ -162,17 +163,30 @@ func (conn *fcgiConn) StartResponse(status int) {
162163
conn.fcgiWrite(buf.Bytes())
163164
}
164165

165-
func (conn *fcgiConn) SetHeader(hdr string, val string) {
166-
conn.headers[hdr] = val
166+
func (conn *fcgiConn) SetHeader(hdr string, val string, unique bool) {
167+
if _, contains := conn.headers[hdr]; !contains {
168+
conn.headers[hdr] = []string{val}
169+
return
170+
}
171+
172+
if unique {
173+
//just overwrite the first value
174+
conn.headers[hdr][0] = val
175+
} else {
176+
newHeaders := make([]string, len(conn.headers)+1)
177+
copy(newHeaders, conn.headers[hdr])
178+
newHeaders[len(newHeaders)-1] = val
179+
conn.headers[hdr] = newHeaders
180+
}
167181
}
168182

169183
func (conn *fcgiConn) complete() {
170-
content := fcgiEndRequest{appStatus: 200, protocolStatus: FcgiRequestComplete}.bytes()
184+
content := fcgiEndReq{appStatus: 200, protocolStatus: fcgiRequestComplete}.bytes()
171185
l := len(content)
172186

173187
hdr := fcgiHeader{
174188
Version: 1,
175-
Type: FcgiEndRequest,
189+
Type: fcgiEndRequest,
176190
RequestId: uint16(conn.requestId),
177191
ContentLength: uint16(l),
178192
PaddingLength: 0,
@@ -218,46 +232,13 @@ func readFcgiParams(data []byte, storage map[string]string) {
218232
}
219233
}
220234

221-
func buildRequest(headers map[string]string) *Request {
222-
method, _ := headers["REQUEST_METHOD"]
223-
host, _ := headers["HTTP_HOST"]
224-
path, _ := headers["REQUEST_URI"]
225-
port, _ := headers["SERVER_PORT"]
226-
proto, _ := headers["SERVER_PROTOCOL"]
227-
rawurl := "http://" + host + ":" + port + path
228-
229-
url, _ := http.ParseURL(rawurl)
230-
useragent, _ := headers["USER_AGENT"]
231-
232-
httpheader := map[string]string{}
233-
if method == "POST" {
234-
if ctype, ok := headers["CONTENT_TYPE"]; ok {
235-
httpheader["Content-Type"] = ctype
236-
}
237-
238-
if clength, ok := headers["CONTENT_LENGTH"]; ok {
239-
httpheader["Content-Length"] = clength
240-
}
241-
}
242-
243-
req := Request{Method: method,
244-
RawURL: rawurl,
245-
URL: url,
246-
Proto: proto,
247-
Host: host,
248-
UserAgent: useragent,
249-
Header: httpheader,
250-
}
251-
252-
return &req
253-
}
254-
255235
func handleFcgiConnection(fd io.ReadWriteCloser) {
256236
br := bufio.NewReader(fd)
257237
var req *Request
258238
var fc *fcgiConn
259239
var body bytes.Buffer
260240
headers := map[string]string{}
241+
261242
for {
262243
var h fcgiHeader
263244
err := binary.Read(br, binary.BigEndian, &h)
@@ -278,32 +259,26 @@ func handleFcgiConnection(fd io.ReadWriteCloser) {
278259
}
279260

280261
switch h.Type {
281-
case FcgiBeginRequest:
282-
fc = &fcgiConn{h.RequestId, fd, make(map[string]string), false}
262+
case fcgiBeginRequest:
263+
fc = &fcgiConn{h.RequestId, fd, make(map[string][]string), false}
283264

284-
case FcgiParams:
265+
case fcgiParams:
285266
if h.ContentLength > 0 {
286267
readFcgiParams(content, headers)
287-
} else if h.ContentLength == 0 {
288-
req = buildRequest(headers)
289268
}
290-
case FcgiStdin:
269+
case fcgiStdin:
291270
if h.ContentLength > 0 {
292271
body.Write(content)
293272
} else if h.ContentLength == 0 {
294-
if req == nil {
295-
log.Stderrf("Invalid fcgi request params\n")
296-
return
297-
}
298-
req.Body = &body
273+
req = newRequestCgi(headers, &body)
299274
routeHandler(req, fc)
300275
fc.complete()
301276
}
302-
case FcgiData:
277+
case fcgiData:
303278
if h.ContentLength > 0 {
304279
body.Write(content)
305280
}
306-
case FcgiAbortRequest:
281+
case fcgiAbortRequest:
307282
}
308283
}
309284
}

0 commit comments

Comments
 (0)