Permalink
Browse files

Update for weekly.2012-03-13

  • Loading branch information...
1 parent 05b6a77 commit 485f31dddb5d8d307cb525adb5f2d28e345e8740 @hoisie committed Mar 15, 2012
Showing with 96 additions and 116 deletions.
  1. +1 −6 Makefile
  2. +3 −8 Readme.md
  3. +11 −9 cookie.go
  4. +1 −1 examples/arcchallenge.go
  5. +1 −1 examples/logger.go
  6. +10 −11 fcgi.go
  7. +17 −26 request.go
  8. +14 −14 scgi.go
  9. +7 −9 servefile.go
  10. +1 −1 status.go
  11. +21 −23 web.go
  12. +9 −7 web_test.go
View
@@ -1,7 +1,4 @@
-include $(GOROOT)/src/Make.inc
-
-TARG=github.com/hoisie/web.go
-GOFMT=gofmt -s -spaces=true -tabindent=false -tabwidth=4
+GOFMT=gofmt -s -tabs=false -tabwidth=4
GOFILES=\
cookie.go\
@@ -12,8 +9,6 @@ GOFILES=\
status.go\
web.go\
-include $(GOROOT)/src/Make.pkg
-
format:
${GOFMT} -w ${GOFILES}
${GOFMT} -w web_test.go
View
@@ -18,12 +18,12 @@ Make sure you have the a working Go environment. See the [install instructions](
To install web.go, simply run:
- goinstall github.com/hoisie/web.go
+ go get github.com/hoisie/web.go
To compile it from source:
git clone git://github.com/hoisie/web.go.git
- cd web.go && make install
+ cd web.go && go build
## Example
@@ -40,14 +40,9 @@ To compile it from source:
web.Run("0.0.0.0:9999")
}
-
To run the application, put the code in a file called hello.go and run:
- 6g hello.go && 6l -o hello hello.6 && ./hello
-
-If you're running on a 32-bit OS, the commend is:
-
- 8g hello.go && 8l -o hello hello.8 && ./hello
+ go build hello.go
You can point your browser to http://localhost:9999/world .
View
@@ -7,13 +7,12 @@ package web
import (
"bytes"
"fmt"
- "http"
"io"
- "os"
+ "net/http"
+ "net/url"
"sort"
"strings"
"time"
- "url"
)
func sanitizeName(n string) string {
@@ -79,7 +78,7 @@ func isCookieNameValid(raw string) bool {
// to w. Each cookie is written on a separate "Set-Cookie: " line.
// This choice is made because HTTP parsers tend to have a limit on
// line-length, so it seems safer to place cookies on separate lines.
-func writeSetCookies(w io.Writer, kk []*http.Cookie) os.Error {
+func writeSetCookies(w io.Writer, kk []*http.Cookie) error {
if kk == nil {
return nil
}
@@ -95,8 +94,8 @@ func writeSetCookies(w io.Writer, kk []*http.Cookie) os.Error {
if len(c.Domain) > 0 {
fmt.Fprintf(&b, "; Domain=%s", url.QueryEscape(c.Domain))
}
- if len(c.Expires.Zone) > 0 {
- fmt.Fprintf(&b, "; Expires=%s", c.Expires.Format(time.RFC1123))
+ if c.Expires.Unix() > 0 {
+ fmt.Fprintf(&b, "; Expires=%s", c.Expires.UTC().Format(time.RFC1123))
}
if c.MaxAge >= 0 {
fmt.Fprintf(&b, "; Max-Age=%d", c.MaxAge)
@@ -122,7 +121,7 @@ func writeSetCookies(w io.Writer, kk []*http.Cookie) os.Error {
// to w. Each cookie is written on a separate "Cookie: " line.
// This choice is made because HTTP parsers tend to have a limit on
// line-length, so it seems safer to place cookies on separate lines.
-func writeCookies(w io.Writer, kk []*http.Cookie) os.Error {
+func writeCookies(w io.Writer, kk []*http.Cookie) error {
lines := make([]string, 0, len(kk))
var b bytes.Buffer
for _, c := range kk {
@@ -176,7 +175,7 @@ func readCookies(h http.Header) []*http.Cookie {
continue
}
attr, val := parts[i], ""
- var err os.Error
+ var err error
if j := strings.Index(attr, "="); j >= 0 {
attr, val = attr[:j], attr[j+1:]
val, err = url.QueryUnescape(val)
@@ -212,6 +211,9 @@ func readCookies(h http.Header) []*http.Cookie {
})
}
}
- h["Cookie"] = unparsedLines, len(unparsedLines) > 0
+ if len(unparsedLines) > 0 {
+ h["Cookie"] = unparsedLines
+ }
+
return cookies
}
@@ -1,10 +1,10 @@
package main
import (
+ "github.com/hoisie/web.go"
"rand"
"strconv"
"time"
- "github.com/hoisie/web.go"
)
var form = `<form action="say" method="POST"><input name="said"><input type="submit"></form>`
View
@@ -1,9 +1,9 @@
package main
import (
+ "github.com/hoisie/web.go"
"log"
"os"
- "github.com/hoisie/web.go"
)
func hello(val string) string { return "hello " + val }
View
@@ -1,14 +1,13 @@
package web
import (
- "bytes"
"bufio"
+ "bytes"
"encoding/binary"
"fmt"
- "http"
"io"
"net"
- "os"
+ "net/http"
"strings"
)
@@ -96,7 +95,7 @@ type fcgiConn struct {
wroteHeaders bool
}
-func (conn *fcgiConn) fcgiWrite(data []byte) (err os.Error) {
+func (conn *fcgiConn) fcgiWrite(data []byte) (err error) {
l := len(data)
// round to the nearest 8
padding := make([]byte, uint8(-l&7))
@@ -129,7 +128,7 @@ func (conn *fcgiConn) fcgiWrite(data []byte) (err os.Error) {
return err
}
-func (conn *fcgiConn) Write(data []byte) (n int, err os.Error) {
+func (conn *fcgiConn) Write(data []byte) (n int, err error) {
var buf bytes.Buffer
if !conn.wroteHeaders {
conn.wroteHeaders = true
@@ -238,11 +237,11 @@ func (s *Server) handleFcgiConnection(fd io.ReadWriteCloser) {
for {
var h fcgiHeader
err := binary.Read(br, binary.BigEndian, &h)
- if err == os.EOF {
+ if err == io.EOF {
break
}
if err != nil {
- s.Logger.Println("FCGI Error", err.String())
+ s.Logger.Println("FCGI Error", err.Error())
break
}
content := make([]byte, h.ContentLength)
@@ -282,9 +281,9 @@ func (s *Server) handleFcgiConnection(fd io.ReadWriteCloser) {
}
}
-func (s *Server) listenAndServeFcgi(addr string) os.Error {
+func (s *Server) listenAndServeFcgi(addr string) error {
var l net.Listener
- var err os.Error
+ var err error
//if the path begins with a "/", assume it's a unix address
if strings.HasPrefix(addr, "/") {
@@ -297,13 +296,13 @@ func (s *Server) listenAndServeFcgi(addr string) os.Error {
s.l = l
if err != nil {
- s.Logger.Println("FCGI listen error", err.String())
+ s.Logger.Println("FCGI listen error", err.Error())
return err
}
for {
fd, err := l.Accept()
if err != nil {
- s.Logger.Println("FCGI accept error", err.String())
+ s.Logger.Println("FCGI accept error", err.Error())
break
}
go s.handleFcgiConnection(fd)
View
@@ -2,18 +2,17 @@ package web
import (
"bytes"
- "fmt"
- "http"
+ "errors"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net"
- "os"
+ "net/http"
+ "net/url"
"reflect"
"strconv"
"strings"
- "url"
)
type filedata struct {
@@ -44,13 +43,6 @@ type Request struct {
RemotePort int
}
-type badStringError struct {
- what string
- str string
-}
-
-func (e *badStringError) String() string { return fmt.Sprintf("%s %q", e.what, e.str) }
-
func flattenParams(fullParams map[string][]string) map[string]string {
params := map[string]string{}
for name, lst := range fullParams {
@@ -137,12 +129,12 @@ func newRequestCgi(headers http.Header, body io.Reader) *Request {
return &req
}
-func parseForm(m map[string][]string, query string) (err os.Error) {
+func parseForm(m map[string][]string, query string) (err error) {
for _, kv := range strings.Split(query, "&") {
kvPair := strings.SplitN(kv, "=", 2)
var key, value string
- var e os.Error
+ var e error
key, e = url.QueryUnescape(kvPair[0])
if e == nil && len(kvPair) > 1 {
value, e = url.QueryUnescape(kvPair[1])
@@ -163,8 +155,7 @@ func parseForm(m map[string][]string, query string) (err os.Error) {
// ParseForm parses the request body as a form for POST requests, or the raw query for GET requests.
// It is idempotent.
-func (r *Request) parseParams() (err os.Error) {
-
+func (r *Request) parseParams() (err error) {
if r.Params != nil {
return
}
@@ -173,7 +164,7 @@ func (r *Request) parseParams() (err os.Error) {
switch r.Method {
case "POST":
if r.Body == nil {
- return os.NewError("missing form body")
+ return errors.New("missing form body")
}
ct := r.Headers.Get("Content-Type")
@@ -191,14 +182,14 @@ func (r *Request) parseParams() (err os.Error) {
}
boundary, ok := params["boundary"]
if !ok {
- return os.NewError("Missing Boundary")
+ return errors.New("Missing Boundary")
}
reader := multipart.NewReader(r.Body, boundary)
r.Files = make(map[string]filedata)
for {
part, err := reader.NextPart()
- if part == nil && err == os.EOF {
+ if part == nil && err == io.EOF {
break
}
@@ -231,7 +222,7 @@ func (r *Request) parseParams() (err os.Error) {
}
default:
- return &badStringError{"unknown Content-Type", ct}
+ return errors.New("unknown Content-Type: " + ct)
}
}
@@ -262,7 +253,7 @@ func (r *Request) HasFile(name string) bool {
return ok
}
-func writeTo(s string, val reflect.Value) os.Error {
+func writeTo(s string, val reflect.Value) error {
switch v := val; v.Kind() {
// if we're writing to an interace value, just set the byte data
// TODO: should we support writing to a pointer?
@@ -275,19 +266,19 @@ func writeTo(s string, val reflect.Value) os.Error {
v.SetBool(true)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- i, err := strconv.Atoi64(s)
+ i, err := strconv.ParseInt(s, 0, 64)
if err != nil {
return err
}
v.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- ui, err := strconv.Atoui64(s)
+ ui, err := strconv.ParseUint(s, 0, 64)
if err != nil {
return err
}
v.SetUint(ui)
case reflect.Float32, reflect.Float64:
- f, err := strconv.Atof64(s)
+ f, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
@@ -309,15 +300,15 @@ func matchName(key, name string) bool {
return strings.ToLower(key) == strings.ToLower(name)
}
-func (r *Request) writeToContainer(val reflect.Value) os.Error {
+func (r *Request) writeToContainer(val reflect.Value) error {
switch v := val; v.Kind() {
case reflect.Ptr:
return r.writeToContainer(reflect.Indirect(v))
case reflect.Interface:
return r.writeToContainer(v.Elem())
case reflect.Map:
if v.Type().Key().Kind() != reflect.String {
- return os.NewError("Invalid map type")
+ return errors.New("Invalid map type")
}
elemtype := v.Type().Elem()
for pk, pv := range r.Params {
@@ -342,7 +333,7 @@ func (r *Request) writeToContainer(val reflect.Value) os.Error {
}
default:
- return os.NewError("Invalid container type")
+ return errors.New("Invalid container type")
}
return nil
}
Oops, something went wrong.

0 comments on commit 485f31d

Please sign in to comment.