Skip to content

Commit

Permalink
set Content-Type correctly
Browse files Browse the repository at this point in the history
all headers need to be set before `w.WriteHeader(status code)` is called
  • Loading branch information
individual-it committed Jun 2, 2021
1 parent b74a2b1 commit 373e0f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/fix-content-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: set Content-Type header correctly for ocs requests

Before this fix the `Content-Type` header was guessed by `w.Write` because `WriteHeader` was called to early. Now the `Content-Type` is set correctly and to the same values as in ownCloud 10

https://github.com/owncloud/ocis/issues/1779
6 changes: 3 additions & 3 deletions internal/http/services/owncloud/ocs/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,19 @@ func WriteOCSResponse(w http.ResponseWriter, r *http.Request, res Response, err
version := APIVersion(r.Context())
m := statusCodeMapper(version)
statusCode := m(res.OCS.Meta)
w.WriteHeader(statusCode)
if version == "v2.php" && statusCode == http.StatusOK {
res.OCS.Meta.StatusCode = statusCode
}

var encoder func(Response) ([]byte, error)
if r.URL.Query().Get("format") == "json" {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
encoder = encodeJSON
} else {
w.Header().Set("Content-Type", "application/xml")
w.Header().Set("Content-Type", "text/xml; charset=utf-8")
encoder = encodeXML
}
w.WriteHeader(statusCode)
encoded, err := encoder(res)
if err != nil {
appctx.GetLogger(r.Context()).Error().Err(err).Msg("error encoding ocs response")
Expand Down

0 comments on commit 373e0f3

Please sign in to comment.