2017-04-20 17:40:52 -05:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-03-10 05:32:53 -06:00
|
|
|
package httputil
|
2017-03-07 10:11:08 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-08-05 04:26:59 -05:00
|
|
|
"io"
|
2017-03-09 05:47:06 -06:00
|
|
|
"net/http"
|
2020-10-09 03:15:51 -05:00
|
|
|
"unicode/utf8"
|
2017-03-09 05:47:06 -06:00
|
|
|
|
2023-05-09 17:46:49 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-03-07 10:11:08 -06:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnmarshalJSONRequest into the given interface pointer. Returns an error JSON response if
|
|
|
|
// there was a problem unmarshalling. Calling this function consumes the request body.
|
|
|
|
func UnmarshalJSONRequest(req *http.Request, iface interface{}) *util.JSONResponse {
|
2020-10-09 03:15:51 -05:00
|
|
|
// encoding/json allows invalid utf-8, matrix does not
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.6.1#api-standards
|
2022-08-05 04:26:59 -05:00
|
|
|
body, err := io.ReadAll(req.Body)
|
2020-10-09 03:15:51 -05:00
|
|
|
if err != nil {
|
2022-08-05 04:26:59 -05:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("io.ReadAll failed")
|
2023-05-09 17:46:49 -05:00
|
|
|
resp := spec.InternalServerError()
|
2020-10-09 03:15:51 -05:00
|
|
|
return &resp
|
|
|
|
}
|
|
|
|
|
2022-02-10 04:27:26 -06:00
|
|
|
return UnmarshalJSON(body, iface)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalJSON(body []byte, iface interface{}) *util.JSONResponse {
|
2020-10-09 03:15:51 -05:00
|
|
|
if !utf8.Valid(body) {
|
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 17:46:49 -05:00
|
|
|
JSON: spec.NotJSON("Body contains invalid UTF-8"),
|
2020-10-09 03:15:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(body, iface); err != nil {
|
2017-03-09 05:47:06 -06:00
|
|
|
// TODO: We may want to suppress the Error() return in production? It's useful when
|
|
|
|
// debugging because an error will be produced for both invalid/malformed JSON AND
|
|
|
|
// valid JSON with incorrect types for values.
|
2017-03-07 10:11:08 -06:00
|
|
|
return &util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 17:46:49 -05:00
|
|
|
JSON: spec.BadJSON("The request body could not be decoded into valid JSON. " + err.Error()),
|
2017-03-07 10:11:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|