Skip to content

Commit e551e37

Browse files
committed
Updates:
- logging: print params if they're present, and add some color - Don't include Content-Type when returning 304
1 parent 4166509 commit e551e37

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

servefile.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,29 @@ func serveFile(ctx *Context, name string) {
5858
mtime := strconv.Itoa64(info.Mtime_ns)
5959
//set the last-modified header
6060
lm := time.SecondsToUTC(info.Mtime_ns / 1e9)
61-
ctx.SetHeader("Last-Modified", webTime(lm), true)
61+
62+
if ctx.Request.Headers.Get("If-Modified-Since") != "" {
63+
ims := ctx.Request.Headers.Get("If-Modified-Since")
64+
imstime, err := time.Parse(time.RFC1123, ims)
65+
if err == nil && imstime.Seconds() >= lm.Seconds() {
66+
ctx.NotModified()
67+
return
68+
}
69+
}
6270

6371
//generate a simple etag with heuristic MD5(filename, size, lastmod)
6472
etagparts := []string{name, size, mtime}
6573
etag := fmt.Sprintf(`"%s"`, getmd5(strings.Join(etagparts, "|")))
66-
ctx.SetHeader("ETag", etag, true)
74+
if ctx.Request.Headers.Get("If-None-Match") != "" {
75+
inm := ctx.Request.Headers.Get("If-None-Match")
76+
if inm == etag {
77+
ctx.NotModified()
78+
return
79+
}
80+
}
6781

82+
ctx.SetHeader("ETag", etag, true)
83+
ctx.SetHeader("Last-Modified", webTime(lm), true)
6884
//the first 1024 bytes of the file, used to detect content-type
6985
var firstChunk []byte
7086
ext := path.Ext(name)
@@ -82,24 +98,6 @@ func serveFile(ctx *Context, name string) {
8298
}
8399
}
84100

85-
if ctx.Request.Headers.Get("If-None-Match") != "" {
86-
inm := ctx.Request.Headers.Get("If-None-Match")
87-
if inm == etag {
88-
ctx.NotModified()
89-
return
90-
}
91-
92-
}
93-
94-
if ctx.Request.Headers.Get("If-Modified-Since") != "" {
95-
ims := ctx.Request.Headers.Get("If-Modified-Since")
96-
imstime, err := time.Parse(time.RFC1123, ims)
97-
if err == nil && imstime.Seconds() >= lm.Seconds() {
98-
ctx.NotModified()
99-
return
100-
}
101-
}
102-
103101
//set content-length
104102
ctx.SetHeader("Content-Length", size, true)
105103
if ctx.Request.Method != "HEAD" {

web.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,27 +290,25 @@ func requiresContext(handlerType reflect.Type) bool {
290290
}
291291

292292
func (s *Server) routeHandler(req *Request, c conn) {
293+
ctx := Context{req, s, c, false}
293294
requestPath := req.URL.Path
294295

295296
//log the request
296-
if len(req.URL.RawQuery) == 0 {
297-
s.Logger.Println(req.Method + " " + requestPath)
298-
} else {
299-
s.Logger.Println(req.Method + " " + requestPath + "?" + req.URL.RawQuery)
300-
}
297+
var logEntry bytes.Buffer
298+
logEntry.WriteString(fmt.Sprintf("\033[32;1m%s %s\033[0m", req.Method, requestPath))
301299

302300
//parse the form data (if it exists)
303301
perr := req.parseParams()
304302
if perr != nil {
305-
s.Logger.Printf("Failed to parse form data %q\n", perr.String())
303+
logEntry.WriteString(fmt.Sprintf("\nFailed to parse parms%q\n", perr.String()))
304+
} else if len(ctx.Params) > 0 {
305+
logEntry.WriteString(fmt.Sprintf("\n\033[37;1mParams: %v\033[0m\n", ctx.Params))
306306
}
307307

308-
ctx := Context{req, s, c, false}
308+
ctx.Logger.Print(logEntry.String())
309309

310310
//set some default headers
311-
ctx.SetHeader("Content-Type", "text/html; charset=utf-8", true)
312311
ctx.SetHeader("Server", "web.go", true)
313-
314312
tm := time.UTC()
315313
ctx.SetHeader("Date", webTime(tm), true)
316314

@@ -325,6 +323,8 @@ func (s *Server) routeHandler(req *Request, c conn) {
325323
return
326324
}
327325

326+
//Set the default content-type
327+
ctx.SetHeader("Content-Type", "text/html; charset=utf-8", true)
328328
for i := 0; i < len(s.routes); i++ {
329329
route := s.routes[i]
330330
cr := route.cr

web_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ func init() {
177177
})
178178

179179
Post("/parsejson", func(ctx *Context) string {
180-
var tmp = struct{ A string; B string }{}
180+
var tmp = struct {
181+
A string
182+
B string
183+
}{}
181184
json.NewDecoder(ctx.Request.Body).Decode(&tmp)
182185
return tmp.A + " " + tmp.B
183186
})
@@ -236,7 +239,7 @@ func buildTestRequest(method string, path string, body string, headers map[strin
236239
headers["Content-Length"] = []string{fmt.Sprintf("%d", len(body))}
237240
if headers["Content-Type"] == nil {
238241
headers["Content-Type"] = []string{"text/plain"}
239-
}
242+
}
240243
}
241244

242245
req := Request{Method: method,
@@ -678,7 +681,6 @@ func TestSecureCookieFcgi(t *testing.T) {
678681
}
679682
}
680683

681-
682684
//Disabled until issue 1375 is fixed
683685
/*
684686
func TestCloseServer(t *testing.T) {

0 commit comments

Comments
 (0)