Skip to content

Commit 3177142

Browse files
author
Michael Hoisie
committed
Update for release 2010-03-04.
- Replace strings.Bytes with []byte - Ran the new gofmt - Move the status code text into its own file - Add a test for fastcgi cookies - Cleaned up test file
1 parent 5a3a5b7 commit 3177142

File tree

7 files changed

+149
-104
lines changed

7 files changed

+149
-104
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
*.8
33
*.o
44
*.so
5+
*.out
6+
*.go~
57
*.cgo?.*
68
_cgo_*
79
_obj
10+
_test

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ GOFILES=\
1212
request.go\
1313
scgi.go\
1414
servefile.go\
15+
status.go\
1516
web.go\
1617

1718
include $(GOROOT)/src/Make.pkg
@@ -21,6 +22,7 @@ format:
2122
${GOFMT} -w request.go
2223
${GOFMT} -w scgi.go
2324
${GOFMT} -w servefile.go
25+
${GOFMT} -w status.go
2426
${GOFMT} -w web.go
2527
${GOFMT} -w web_test.go
2628
${GOFMT} -w examples/hello.go

fcgi.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ func newFcgiRecord(typ int, requestId int, data []byte) []byte {
6060
// round to the nearest 8
6161
padding := make([]byte, uint8(-l&7))
6262
hdr := fcgiHeader{
63-
Version: 1,
64-
Type: uint8(typ),
65-
RequestId: uint16(requestId),
63+
Version: 1,
64+
Type: uint8(typ),
65+
RequestId: uint16(requestId),
6666
ContentLength: uint16(l),
6767
PaddingLength: uint8(len(padding)),
6868
}
@@ -100,9 +100,9 @@ func (conn *fcgiConn) fcgiWrite(data []byte) (err os.Error) {
100100
// round to the nearest 8
101101
padding := make([]byte, uint8(-l&7))
102102
hdr := fcgiHeader{
103-
Version: 1,
104-
Type: fcgiStdout,
105-
RequestId: conn.requestId,
103+
Version: 1,
104+
Type: fcgiStdout,
105+
RequestId: conn.requestId,
106106
ContentLength: uint16(l),
107107
PaddingLength: uint8(len(padding)),
108108
}
@@ -179,9 +179,9 @@ func (conn *fcgiConn) complete() {
179179
l := len(content)
180180

181181
hdr := fcgiHeader{
182-
Version: 1,
183-
Type: fcgiEndRequest,
184-
RequestId: uint16(conn.requestId),
182+
Version: 1,
183+
Type: fcgiEndRequest,
184+
RequestId: uint16(conn.requestId),
185185
ContentLength: uint16(l),
186186
PaddingLength: 0,
187187
}

request.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ func (e *badStringError) String() string { return fmt.Sprintf("%s %q", e.what, e
4444

4545
func newRequest(hr *http.Request) *Request {
4646
req := Request{
47-
Method: hr.Method,
48-
RawURL: hr.RawURL,
49-
URL: hr.URL,
50-
Proto: hr.Proto,
47+
Method: hr.Method,
48+
RawURL: hr.RawURL,
49+
URL: hr.URL,
50+
Proto: hr.Proto,
5151
ProtoMajor: hr.ProtoMajor,
5252
ProtoMinor: hr.ProtoMinor,
53-
Headers: hr.Header,
54-
Body: hr.Body,
55-
Close: hr.Close,
56-
Host: hr.Host,
57-
Referer: hr.Referer,
58-
UserAgent: hr.UserAgent,
59-
Params: hr.Form,
53+
Headers: hr.Header,
54+
Body: hr.Body,
55+
Close: hr.Close,
56+
Host: hr.Host,
57+
Referer: hr.Referer,
58+
UserAgent: hr.UserAgent,
59+
Params: hr.Form,
6060
}
6161
return &req
6262
}
@@ -89,14 +89,14 @@ func newRequestCgi(headers map[string]string, body io.Reader) *Request {
8989
}
9090

9191
req := Request{
92-
Method: method,
93-
RawURL: rawurl,
94-
URL: url,
95-
Proto: proto,
96-
Host: host,
92+
Method: method,
93+
RawURL: rawurl,
94+
URL: url,
95+
Proto: proto,
96+
Host: host,
9797
UserAgent: useragent,
98-
Body: body,
99-
Headers: httpheader,
98+
Body: body,
99+
Headers: httpheader,
100100
}
101101

102102
return &req
@@ -163,8 +163,8 @@ func (r *Request) parseParams() (err os.Error) {
163163
if b, err = ioutil.ReadAll(r.Body); err != nil {
164164
return err
165165
}
166-
parts := bytes.Split(b, strings.Bytes("--"+boundary+"--\r\n"), 0)
167-
parts = bytes.Split(parts[0], strings.Bytes("--"+boundary+"\r\n"), 0)
166+
parts := bytes.Split(b, []byte("--"+boundary+"--\r\n"), 0)
167+
parts = bytes.Split(parts[0], []byte("--"+boundary+"\r\n"), 0)
168168
for _, data := range parts {
169169
if len(data) < 2 {
170170
continue

status.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2010 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package web
6+
7+
import "http"
8+
9+
var statusText = map[int]string{
10+
http.StatusContinue: "Continue",
11+
http.StatusSwitchingProtocols: "Switching Protocols",
12+
13+
http.StatusOK: "OK",
14+
http.StatusCreated: "Created",
15+
http.StatusAccepted: "Accepted",
16+
http.StatusNonAuthoritativeInfo: "Non-Authoritative Information",
17+
http.StatusNoContent: "No Content",
18+
http.StatusResetContent: "Reset Content",
19+
http.StatusPartialContent: "Partial Content",
20+
21+
http.StatusMultipleChoices: "Multiple Choices",
22+
http.StatusMovedPermanently: "Moved Permanently",
23+
http.StatusFound: "Found",
24+
http.StatusSeeOther: "See Other",
25+
http.StatusNotModified: "Not Modified",
26+
http.StatusUseProxy: "Use Proxy",
27+
http.StatusTemporaryRedirect: "Temporary Redirect",
28+
29+
http.StatusBadRequest: "Bad Request",
30+
http.StatusUnauthorized: "Unauthorized",
31+
http.StatusPaymentRequired: "Payment Required",
32+
http.StatusForbidden: "Forbidden",
33+
http.StatusNotFound: "Not Found",
34+
http.StatusMethodNotAllowed: "Method Not Allowed",
35+
http.StatusNotAcceptable: "Not Acceptable",
36+
http.StatusProxyAuthRequired: "Proxy Authentication Required",
37+
http.StatusRequestTimeout: "Request Timeout",
38+
http.StatusConflict: "Conflict",
39+
http.StatusGone: "Gone",
40+
http.StatusLengthRequired: "Length Required",
41+
http.StatusPreconditionFailed: "Precondition Failed",
42+
http.StatusRequestEntityTooLarge: "Request Entity Too Large",
43+
http.StatusRequestURITooLong: "Request URI Too Long",
44+
http.StatusUnsupportedMediaType: "Unsupported Media Type",
45+
http.StatusRequestedRangeNotSatisfiable: "Requested Range Not Satisfiable",
46+
http.StatusExpectationFailed: "Expectation Failed",
47+
48+
http.StatusInternalServerError: "Internal Server Error",
49+
http.StatusNotImplemented: "Not Implemented",
50+
http.StatusBadGateway: "Bad Gateway",
51+
http.StatusServiceUnavailable: "Service Unavailable",
52+
http.StatusGatewayTimeout: "Gateway Timeout",
53+
http.StatusHTTPVersionNotSupported: "HTTP Version Not Supported",
54+
}

web.go

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (ctx *Context) Write(data []byte) (n int, err os.Error) {
5252
return ctx.conn.Write(data)
5353
}
5454
func (ctx *Context) WriteString(content string) {
55-
ctx.Write(strings.Bytes(content))
55+
ctx.Write([]byte(content))
5656
}
5757

5858
func (ctx *Context) Abort(status int, body string) {
@@ -88,10 +88,10 @@ func (ctx *Context) SetCookie(name string, value string, age int64) {
8888
func SetCookieSecret(key string) { secret = key }
8989

9090
func getCookieSig(val []byte, timestamp string) string {
91-
hm := hmac.NewSHA1(strings.Bytes(secret))
91+
hm := hmac.NewSHA1([]byte(secret))
9292

9393
hm.Write(val)
94-
hm.Write(strings.Bytes(timestamp))
94+
hm.Write([]byte(timestamp))
9595

9696
hex := fmt.Sprintf("%02x", hm.Sum())
9797
return hex
@@ -105,7 +105,7 @@ func (ctx *Context) SetSecureCookie(name string, val string, age int64) {
105105
}
106106
var buf bytes.Buffer
107107
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
108-
encoder.Write(strings.Bytes(val))
108+
encoder.Write([]byte(val))
109109
encoder.Close()
110110
vs := buf.String()
111111
vb := buf.Bytes()
@@ -133,7 +133,7 @@ func (ctx *Context) GetSecureCookie(name string) (string, bool) {
133133
timestamp := parts[1]
134134
sig := parts[2]
135135

136-
if getCookieSig(strings.Bytes(val), timestamp) != sig {
136+
if getCookieSig([]byte(val), timestamp) != sig {
137137
return "", false
138138
}
139139

@@ -303,10 +303,10 @@ func routeHandler(req *Request, c conn) {
303303
sval, ok := ret[0].(*reflect.StringValue)
304304

305305
if ok && !ctx.responseStarted {
306-
outbytes := strings.Bytes(sval.Get())
307-
ctx.SetHeader("Content-Length", strconv.Itoa(len(outbytes)), true)
306+
content := []byte(sval.Get())
307+
ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true)
308308
ctx.StartResponse(200)
309-
ctx.Write(outbytes)
309+
ctx.Write(content)
310310
}
311311

312312
return
@@ -407,51 +407,3 @@ func Urlencode(data map[string]string) string {
407407
s := buf.String()
408408
return s[0 : len(s)-1]
409409
}
410-
411-
//copied from go's http package, because it's not public
412-
var statusText = map[int]string{
413-
http.StatusContinue: "Continue",
414-
http.StatusSwitchingProtocols: "Switching Protocols",
415-
416-
http.StatusOK: "OK",
417-
http.StatusCreated: "Created",
418-
http.StatusAccepted: "Accepted",
419-
http.StatusNonAuthoritativeInfo: "Non-Authoritative Information",
420-
http.StatusNoContent: "No Content",
421-
http.StatusResetContent: "Reset Content",
422-
http.StatusPartialContent: "Partial Content",
423-
424-
http.StatusMultipleChoices: "Multiple Choices",
425-
http.StatusMovedPermanently: "Moved Permanently",
426-
http.StatusFound: "Found",
427-
http.StatusSeeOther: "See Other",
428-
http.StatusNotModified: "Not Modified",
429-
http.StatusUseProxy: "Use Proxy",
430-
http.StatusTemporaryRedirect: "Temporary Redirect",
431-
432-
http.StatusBadRequest: "Bad Request",
433-
http.StatusUnauthorized: "Unauthorized",
434-
http.StatusPaymentRequired: "Payment Required",
435-
http.StatusForbidden: "Forbidden",
436-
http.StatusNotFound: "Not Found",
437-
http.StatusMethodNotAllowed: "Method Not Allowed",
438-
http.StatusNotAcceptable: "Not Acceptable",
439-
http.StatusProxyAuthRequired: "Proxy Authentication Required",
440-
http.StatusRequestTimeout: "Request Timeout",
441-
http.StatusConflict: "Conflict",
442-
http.StatusGone: "Gone",
443-
http.StatusLengthRequired: "Length Required",
444-
http.StatusPreconditionFailed: "Precondition Failed",
445-
http.StatusRequestEntityTooLarge: "Request Entity Too Large",
446-
http.StatusRequestURITooLong: "Request URI Too Long",
447-
http.StatusUnsupportedMediaType: "Unsupported Media Type",
448-
http.StatusRequestedRangeNotSatisfiable: "Requested Range Not Satisfiable",
449-
http.StatusExpectationFailed: "Expectation Failed",
450-
451-
http.StatusInternalServerError: "Internal Server Error",
452-
http.StatusNotImplemented: "Not Implemented",
453-
http.StatusBadGateway: "Bad Gateway",
454-
http.StatusServiceUnavailable: "Service Unavailable",
455-
http.StatusGatewayTimeout: "Gateway Timeout",
456-
http.StatusHTTPVersionNotSupported: "HTTP Version Not Supported",
457-
}

0 commit comments

Comments
 (0)