aboutsummaryrefslogtreecommitdiff
path: root/clientapi/httputil
diff options
context:
space:
mode:
authorruben <code@rbn.im>2019-05-21 22:56:55 +0200
committerBrendan Abolivier <babolivier@matrix.org>2019-05-21 21:56:55 +0100
commit74827428bd3e11faab65f12204449c1b9469b0ae (patch)
tree0decafa542436a0667ed2d3e3cfd4df0f03de1e5 /clientapi/httputil
parent4d588f7008afe5600219ac0930c2eee2de5c447b (diff)
use go module for dependencies (#594)
Diffstat (limited to 'clientapi/httputil')
-rw-r--r--clientapi/httputil/httputil.go46
-rw-r--r--clientapi/httputil/parse.go39
2 files changed, 85 insertions, 0 deletions
diff --git a/clientapi/httputil/httputil.go b/clientapi/httputil/httputil.go
new file mode 100644
index 00000000..11785f51
--- /dev/null
+++ b/clientapi/httputil/httputil.go
@@ -0,0 +1,46 @@
+// 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.
+
+package httputil
+
+import (
+ "encoding/json"
+ "net/http"
+
+ "github.com/matrix-org/dendrite/clientapi/jsonerror"
+ "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 {
+ if err := json.NewDecoder(req.Body).Decode(iface); err != nil {
+ // 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.
+ return &util.JSONResponse{
+ Code: http.StatusBadRequest,
+ JSON: jsonerror.BadJSON("The request body could not be decoded into valid JSON. " + err.Error()),
+ }
+ }
+ return nil
+}
+
+// LogThenError logs the given error then returns a matrix-compliant 500 internal server error response.
+// This should be used to log fatal errors which require investigation. It should not be used
+// to log client validation errors, etc.
+func LogThenError(req *http.Request, err error) util.JSONResponse {
+ util.GetLogger(req.Context()).WithError(err).Error("request failed")
+ return jsonerror.InternalServerError()
+}
diff --git a/clientapi/httputil/parse.go b/clientapi/httputil/parse.go
new file mode 100644
index 00000000..ee603341
--- /dev/null
+++ b/clientapi/httputil/parse.go
@@ -0,0 +1,39 @@
+// 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.
+
+package httputil
+
+import (
+ "fmt"
+ "net/http"
+ "strconv"
+ "time"
+)
+
+// ParseTSParam takes a req (typically from an application service) and parses a Time object
+// from the req if it exists in the query parameters. If it doesn't exist, the
+// current time is returned.
+func ParseTSParam(req *http.Request) (time.Time, error) {
+ // Use the ts parameter's value for event time if present
+ tsStr := req.URL.Query().Get("ts")
+ if tsStr == "" {
+ return time.Now(), nil
+ }
+
+ // The parameter exists, parse into a Time object
+ ts, err := strconv.ParseInt(tsStr, 10, 64)
+ if err != nil {
+ return time.Time{}, fmt.Errorf("Param 'ts' is no valid int (%s)", err.Error())
+ }
+
+ return time.Unix(ts/1000, 0), nil
+}