File tree 1 file changed +14
-5
lines changed
1 file changed +14
-5
lines changed Original file line number Diff line number Diff line change @@ -64,16 +64,25 @@ func (ctx *Context) NotFound(message string) {
64
64
ctx .ResponseWriter .Write ([]byte (message ))
65
65
}
66
66
67
- // Sets the content type by extension, as defined in the mime package .
67
+ // ContentType sets the Content-Type header for an HTTP response .
68
68
// 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 )
72
81
}
73
- ctype := mime .TypeByExtension (ext )
74
82
if ctype != "" {
75
83
ctx .Header ().Set ("Content-Type" , ctype )
76
84
}
85
+ return ctype
77
86
}
78
87
79
88
// SetHeader sets a response header. If `unique` is true, the current value
You can’t perform that action at this time.
0 commit comments