Skip to content

Commit 673c0e6

Browse files
hrabanhoisie
authored andcommitted
More flexible ContentType implementation
Fixes issue hoisie#76
1 parent 45d52ef commit 673c0e6

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

web.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,25 @@ func (ctx *Context) NotFound(message string) {
6464
ctx.ResponseWriter.Write([]byte(message))
6565
}
6666

67-
// Sets the content type by extension, as defined in the mime package.
67+
// ContentType sets the Content-Type header for an HTTP response.
6868
// For example, ctx.ContentType("json") sets the content-type to "application/json"
69-
func (ctx *Context) ContentType(ext string) {
70-
if !strings.HasPrefix(ext, ".") {
71-
ext = "." + ext
69+
// If the supplied value contains a slash (/) it is set as the Content-Type
70+
// verbatim. The return value is the content type as it was
71+
// set, or an empty string if none was found.
72+
func (ctx *Context) ContentType(val string) string {
73+
var ctype string
74+
if strings.ContainsRune(val, '/') {
75+
ctype = val
76+
} else {
77+
if !strings.HasPrefix(val, ".") {
78+
val = "." + val
79+
}
80+
ctype = mime.TypeByExtension(val)
7281
}
73-
ctype := mime.TypeByExtension(ext)
7482
if ctype != "" {
7583
ctx.Header().Set("Content-Type", ctype)
7684
}
85+
return ctype
7786
}
7887

7988
// SetHeader sets a response header. If `unique` is true, the current value

0 commit comments

Comments
 (0)