Skip to content

Commit c21e884

Browse files
committed
Stop setting a 'Content-Type' header for custom HTTP handlers
Previously, when using web.Handle, the `Content-Type` HTTP header was set by default to `text/html; charset=utf-8`. This does not play nicely well with Go's FileHandler. If a `Content-Type` header is set, Go's FileHandler will not overwrite it. This breaks serving static assets with a FileHandler. This resolves hoisie#158
1 parent 6e587a8 commit c21e884

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,6 @@ func (s *Server) routeHandler(req *http.Request, w http.ResponseWriter) (unused
335335
}
336336
}
337337

338-
//Set the default content-type
339-
ctx.SetHeader("Content-Type", "text/html; charset=utf-8", true)
340-
341338
for i := 0; i < len(s.routes); i++ {
342339
route := s.routes[i]
343340
cr := route.cr
@@ -361,6 +358,9 @@ func (s *Server) routeHandler(req *http.Request, w http.ResponseWriter) (unused
361358
return
362359
}
363360

361+
// set the default content-type
362+
ctx.SetHeader("Content-Type", "text/html; charset=utf-8", true)
363+
364364
var args []reflect.Value
365365
handlerType := route.handler.Type()
366366
if requiresContext(handlerType) {

web.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (ctx *Context) WriteString(content string) {
4343
// Once it has been called, any return value from the handler will
4444
// not be written to the response.
4545
func (ctx *Context) Abort(status int, body string) {
46+
ctx.SetHeader("Content-Type", "text/html; charset=utf-8", true)
4647
ctx.ResponseWriter.WriteHeader(status)
4748
ctx.ResponseWriter.Write([]byte(body))
4849
}
@@ -250,7 +251,7 @@ func Match(method string, route string, handler interface{}) {
250251
mainServer.addRoute(route, method, handler)
251252
}
252253

253-
// Add a custom HTTP handler for a path. This will have no effect when running as FCGI or SCGI.
254+
// Add a custom http.Handler. Will have no effect when running as FCGI or SCGI.
254255
func Handle(route string, method string, httpHandler http.Handler) {
255256
mainServer.Handle(route, method, httpHandler)
256257
}

web_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,24 @@ func TestNoColorOutput(t *testing.T) {
591591
}
592592
}
593593

594+
type TestHandler struct{}
595+
596+
func (t *TestHandler) ServeHTTP(c http.ResponseWriter, req *http.Request) {
597+
}
598+
599+
// When a custom HTTP handler is used, the Content-Type header should not be set to a default.
600+
// Go's FileHandler does not replace the Content-Type header if it is already set.
601+
func TestCustomHandlerContentType(t *testing.T) {
602+
s := NewServer()
603+
s.Handle("/testHandler", "GET", &TestHandler{})
604+
req := buildTestRequest("GET", "/testHandler", "", nil, nil)
605+
c := scgiConn{wroteHeaders: false, req: req, headers: make(map[string][]string), fd: nil}
606+
s.Process(&c, req)
607+
if c.headers["Content-Type"] != nil {
608+
t.Fatalf("A default Content-Type should not be present when using a custom HTTP handler")
609+
}
610+
}
611+
594612
func BuildBasicAuthCredentials(user string, pass string) string {
595613
s := user + ":" + pass
596614
return "Basic " + base64.StdEncoding.EncodeToString([]byte(s))

0 commit comments

Comments
 (0)