-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix fasthttpadaptor Content-Type detection
Make this in line with how net/http handles Content-Type.
- Loading branch information
1 parent
ac4cc17
commit ef51a7e
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,11 +85,26 @@ func NewFastHTTPHandler(h http.Handler) fasthttp.RequestHandler { | |
h.ServeHTTP(&w, r.WithContext(ctx)) | ||
|
||
ctx.SetStatusCode(w.StatusCode()) | ||
haveContentType := false | ||
for k, vv := range w.Header() { | ||
if k == fasthttp.HeaderContentType { | ||
haveContentType = true | ||
} | ||
|
||
for _, v := range vv { | ||
ctx.Response.Header.Set(k, v) | ||
} | ||
} | ||
if !haveContentType { | ||
// From net/http.ResponseWriter.Write: | ||
// If the Header does not contain a Content-Type line, Write adds a Content-Type set | ||
// to the result of passing the initial 512 bytes of written data to DetectContentType. | ||
l := 512 | ||
if len(w.body) < 512 { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Quons
|
||
l = len(w.body) | ||
} | ||
ctx.Response.Header.Set(fasthttp.HeaderContentType, http.DetectContentType(w.body[:l])) | ||
} | ||
ctx.Write(w.body) //nolint:errcheck | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Deduplication
if len(w.body) < l {