Skip to content

Commit 485f31d

Browse files
committed
Update for weekly.2012-03-13
1 parent 05b6a77 commit 485f31d

File tree

12 files changed

+96
-116
lines changed

12 files changed

+96
-116
lines changed

Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
include $(GOROOT)/src/Make.inc
2-
3-
TARG=github.com/hoisie/web.go
4-
GOFMT=gofmt -s -spaces=true -tabindent=false -tabwidth=4
1+
GOFMT=gofmt -s -tabs=false -tabwidth=4
52

63
GOFILES=\
74
cookie.go\
@@ -12,8 +9,6 @@ GOFILES=\
129
status.go\
1310
web.go\
1411

15-
include $(GOROOT)/src/Make.pkg
16-
1712
format:
1813
${GOFMT} -w ${GOFILES}
1914
${GOFMT} -w web_test.go

Readme.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ Make sure you have the a working Go environment. See the [install instructions](
1818

1919
To install web.go, simply run:
2020

21-
goinstall github.com/hoisie/web.go
21+
go get github.com/hoisie/web.go
2222

2323
To compile it from source:
2424

2525
git clone git://github.com/hoisie/web.go.git
26-
cd web.go && make install
26+
cd web.go && go build
2727

2828
## Example
2929

@@ -40,14 +40,9 @@ To compile it from source:
4040
web.Run("0.0.0.0:9999")
4141
}
4242

43-
4443
To run the application, put the code in a file called hello.go and run:
4544

46-
6g hello.go && 6l -o hello hello.6 && ./hello
47-
48-
If you're running on a 32-bit OS, the commend is:
49-
50-
8g hello.go && 8l -o hello hello.8 && ./hello
45+
go build hello.go
5146

5247
You can point your browser to http://localhost:9999/world .
5348

cookie.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ package web
77
import (
88
"bytes"
99
"fmt"
10-
"http"
1110
"io"
12-
"os"
11+
"net/http"
12+
"net/url"
1313
"sort"
1414
"strings"
1515
"time"
16-
"url"
1716
)
1817

1918
func sanitizeName(n string) string {
@@ -79,7 +78,7 @@ func isCookieNameValid(raw string) bool {
7978
// to w. Each cookie is written on a separate "Set-Cookie: " line.
8079
// This choice is made because HTTP parsers tend to have a limit on
8180
// line-length, so it seems safer to place cookies on separate lines.
82-
func writeSetCookies(w io.Writer, kk []*http.Cookie) os.Error {
81+
func writeSetCookies(w io.Writer, kk []*http.Cookie) error {
8382
if kk == nil {
8483
return nil
8584
}
@@ -95,8 +94,8 @@ func writeSetCookies(w io.Writer, kk []*http.Cookie) os.Error {
9594
if len(c.Domain) > 0 {
9695
fmt.Fprintf(&b, "; Domain=%s", url.QueryEscape(c.Domain))
9796
}
98-
if len(c.Expires.Zone) > 0 {
99-
fmt.Fprintf(&b, "; Expires=%s", c.Expires.Format(time.RFC1123))
97+
if c.Expires.Unix() > 0 {
98+
fmt.Fprintf(&b, "; Expires=%s", c.Expires.UTC().Format(time.RFC1123))
10099
}
101100
if c.MaxAge >= 0 {
102101
fmt.Fprintf(&b, "; Max-Age=%d", c.MaxAge)
@@ -122,7 +121,7 @@ func writeSetCookies(w io.Writer, kk []*http.Cookie) os.Error {
122121
// to w. Each cookie is written on a separate "Cookie: " line.
123122
// This choice is made because HTTP parsers tend to have a limit on
124123
// line-length, so it seems safer to place cookies on separate lines.
125-
func writeCookies(w io.Writer, kk []*http.Cookie) os.Error {
124+
func writeCookies(w io.Writer, kk []*http.Cookie) error {
126125
lines := make([]string, 0, len(kk))
127126
var b bytes.Buffer
128127
for _, c := range kk {
@@ -176,7 +175,7 @@ func readCookies(h http.Header) []*http.Cookie {
176175
continue
177176
}
178177
attr, val := parts[i], ""
179-
var err os.Error
178+
var err error
180179
if j := strings.Index(attr, "="); j >= 0 {
181180
attr, val = attr[:j], attr[j+1:]
182181
val, err = url.QueryUnescape(val)
@@ -212,6 +211,9 @@ func readCookies(h http.Header) []*http.Cookie {
212211
})
213212
}
214213
}
215-
h["Cookie"] = unparsedLines, len(unparsedLines) > 0
214+
if len(unparsedLines) > 0 {
215+
h["Cookie"] = unparsedLines
216+
}
217+
216218
return cookies
217219
}

examples/arcchallenge.go

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

33
import (
4+
"github.com/hoisie/web.go"
45
"rand"
56
"strconv"
67
"time"
7-
"github.com/hoisie/web.go"
88
)
99

1010
var form = `<form action="say" method="POST"><input name="said"><input type="submit"></form>`

examples/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4+
"github.com/hoisie/web.go"
45
"log"
56
"os"
6-
"github.com/hoisie/web.go"
77
)
88

99
func hello(val string) string { return "hello " + val }

fcgi.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package web
22

33
import (
4-
"bytes"
54
"bufio"
5+
"bytes"
66
"encoding/binary"
77
"fmt"
8-
"http"
98
"io"
109
"net"
11-
"os"
10+
"net/http"
1211
"strings"
1312
)
1413

@@ -96,7 +95,7 @@ type fcgiConn struct {
9695
wroteHeaders bool
9796
}
9897

99-
func (conn *fcgiConn) fcgiWrite(data []byte) (err os.Error) {
98+
func (conn *fcgiConn) fcgiWrite(data []byte) (err error) {
10099
l := len(data)
101100
// round to the nearest 8
102101
padding := make([]byte, uint8(-l&7))
@@ -129,7 +128,7 @@ func (conn *fcgiConn) fcgiWrite(data []byte) (err os.Error) {
129128
return err
130129
}
131130

132-
func (conn *fcgiConn) Write(data []byte) (n int, err os.Error) {
131+
func (conn *fcgiConn) Write(data []byte) (n int, err error) {
133132
var buf bytes.Buffer
134133
if !conn.wroteHeaders {
135134
conn.wroteHeaders = true
@@ -238,11 +237,11 @@ func (s *Server) handleFcgiConnection(fd io.ReadWriteCloser) {
238237
for {
239238
var h fcgiHeader
240239
err := binary.Read(br, binary.BigEndian, &h)
241-
if err == os.EOF {
240+
if err == io.EOF {
242241
break
243242
}
244243
if err != nil {
245-
s.Logger.Println("FCGI Error", err.String())
244+
s.Logger.Println("FCGI Error", err.Error())
246245
break
247246
}
248247
content := make([]byte, h.ContentLength)
@@ -282,9 +281,9 @@ func (s *Server) handleFcgiConnection(fd io.ReadWriteCloser) {
282281
}
283282
}
284283

285-
func (s *Server) listenAndServeFcgi(addr string) os.Error {
284+
func (s *Server) listenAndServeFcgi(addr string) error {
286285
var l net.Listener
287-
var err os.Error
286+
var err error
288287

289288
//if the path begins with a "/", assume it's a unix address
290289
if strings.HasPrefix(addr, "/") {
@@ -297,13 +296,13 @@ func (s *Server) listenAndServeFcgi(addr string) os.Error {
297296
s.l = l
298297

299298
if err != nil {
300-
s.Logger.Println("FCGI listen error", err.String())
299+
s.Logger.Println("FCGI listen error", err.Error())
301300
return err
302301
}
303302
for {
304303
fd, err := l.Accept()
305304
if err != nil {
306-
s.Logger.Println("FCGI accept error", err.String())
305+
s.Logger.Println("FCGI accept error", err.Error())
307306
break
308307
}
309308
go s.handleFcgiConnection(fd)

request.go

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ package web
22

33
import (
44
"bytes"
5-
"fmt"
6-
"http"
5+
"errors"
76
"io"
87
"io/ioutil"
98
"mime"
109
"mime/multipart"
1110
"net"
12-
"os"
11+
"net/http"
12+
"net/url"
1313
"reflect"
1414
"strconv"
1515
"strings"
16-
"url"
1716
)
1817

1918
type filedata struct {
@@ -44,13 +43,6 @@ type Request struct {
4443
RemotePort int
4544
}
4645

47-
type badStringError struct {
48-
what string
49-
str string
50-
}
51-
52-
func (e *badStringError) String() string { return fmt.Sprintf("%s %q", e.what, e.str) }
53-
5446
func flattenParams(fullParams map[string][]string) map[string]string {
5547
params := map[string]string{}
5648
for name, lst := range fullParams {
@@ -137,12 +129,12 @@ func newRequestCgi(headers http.Header, body io.Reader) *Request {
137129
return &req
138130
}
139131

140-
func parseForm(m map[string][]string, query string) (err os.Error) {
132+
func parseForm(m map[string][]string, query string) (err error) {
141133
for _, kv := range strings.Split(query, "&") {
142134
kvPair := strings.SplitN(kv, "=", 2)
143135

144136
var key, value string
145-
var e os.Error
137+
var e error
146138
key, e = url.QueryUnescape(kvPair[0])
147139
if e == nil && len(kvPair) > 1 {
148140
value, e = url.QueryUnescape(kvPair[1])
@@ -163,8 +155,7 @@ func parseForm(m map[string][]string, query string) (err os.Error) {
163155

164156
// ParseForm parses the request body as a form for POST requests, or the raw query for GET requests.
165157
// It is idempotent.
166-
func (r *Request) parseParams() (err os.Error) {
167-
158+
func (r *Request) parseParams() (err error) {
168159
if r.Params != nil {
169160
return
170161
}
@@ -173,7 +164,7 @@ func (r *Request) parseParams() (err os.Error) {
173164
switch r.Method {
174165
case "POST":
175166
if r.Body == nil {
176-
return os.NewError("missing form body")
167+
return errors.New("missing form body")
177168
}
178169

179170
ct := r.Headers.Get("Content-Type")
@@ -191,14 +182,14 @@ func (r *Request) parseParams() (err os.Error) {
191182
}
192183
boundary, ok := params["boundary"]
193184
if !ok {
194-
return os.NewError("Missing Boundary")
185+
return errors.New("Missing Boundary")
195186
}
196187

197188
reader := multipart.NewReader(r.Body, boundary)
198189
r.Files = make(map[string]filedata)
199190
for {
200191
part, err := reader.NextPart()
201-
if part == nil && err == os.EOF {
192+
if part == nil && err == io.EOF {
202193
break
203194
}
204195

@@ -231,7 +222,7 @@ func (r *Request) parseParams() (err os.Error) {
231222

232223
}
233224
default:
234-
return &badStringError{"unknown Content-Type", ct}
225+
return errors.New("unknown Content-Type: " + ct)
235226
}
236227
}
237228

@@ -262,7 +253,7 @@ func (r *Request) HasFile(name string) bool {
262253
return ok
263254
}
264255

265-
func writeTo(s string, val reflect.Value) os.Error {
256+
func writeTo(s string, val reflect.Value) error {
266257
switch v := val; v.Kind() {
267258
// if we're writing to an interace value, just set the byte data
268259
// TODO: should we support writing to a pointer?
@@ -275,19 +266,19 @@ func writeTo(s string, val reflect.Value) os.Error {
275266
v.SetBool(true)
276267
}
277268
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
278-
i, err := strconv.Atoi64(s)
269+
i, err := strconv.ParseInt(s, 0, 64)
279270
if err != nil {
280271
return err
281272
}
282273
v.SetInt(i)
283274
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
284-
ui, err := strconv.Atoui64(s)
275+
ui, err := strconv.ParseUint(s, 0, 64)
285276
if err != nil {
286277
return err
287278
}
288279
v.SetUint(ui)
289280
case reflect.Float32, reflect.Float64:
290-
f, err := strconv.Atof64(s)
281+
f, err := strconv.ParseFloat(s, 64)
291282
if err != nil {
292283
return err
293284
}
@@ -309,15 +300,15 @@ func matchName(key, name string) bool {
309300
return strings.ToLower(key) == strings.ToLower(name)
310301
}
311302

312-
func (r *Request) writeToContainer(val reflect.Value) os.Error {
303+
func (r *Request) writeToContainer(val reflect.Value) error {
313304
switch v := val; v.Kind() {
314305
case reflect.Ptr:
315306
return r.writeToContainer(reflect.Indirect(v))
316307
case reflect.Interface:
317308
return r.writeToContainer(v.Elem())
318309
case reflect.Map:
319310
if v.Type().Key().Kind() != reflect.String {
320-
return os.NewError("Invalid map type")
311+
return errors.New("Invalid map type")
321312
}
322313
elemtype := v.Type().Elem()
323314
for pk, pv := range r.Params {
@@ -342,7 +333,7 @@ func (r *Request) writeToContainer(val reflect.Value) os.Error {
342333

343334
}
344335
default:
345-
return os.NewError("Invalid container type")
336+
return errors.New("Invalid container type")
346337
}
347338
return nil
348339
}

0 commit comments

Comments
 (0)