diff options
Diffstat (limited to 'pkg/api/handlers/utils/handler.go')
-rw-r--r-- | pkg/api/handlers/utils/handler.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/pkg/api/handlers/utils/handler.go b/pkg/api/handlers/utils/handler.go index f2ce26f1a..32b8c5b0a 100644 --- a/pkg/api/handlers/utils/handler.go +++ b/pkg/api/handlers/utils/handler.go @@ -5,9 +5,12 @@ import ( "fmt" "io" "net/http" + "net/url" "os" "strings" + "github.com/gorilla/mux" + "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -20,6 +23,14 @@ func IsLibpodRequest(r *http.Request) bool { // WriteResponse encodes the given value as JSON or string and renders it for http client func WriteResponse(w http.ResponseWriter, code int, value interface{}) { + // RFC2616 explicitly states that the following status codes "MUST NOT + // include a message-body": + switch code { + case http.StatusNoContent, http.StatusNotModified: // 204, 304 + w.WriteHeader(code) + return + } + switch v := value.(type) { case string: w.Header().Set("Content-Type", "text/plain; charset=us-ascii") @@ -59,3 +70,18 @@ func FilterMapToString(filters map[string][]string) (string, error) { } return string(f), nil } + +func getVar(r *http.Request, k string) string { + val := mux.Vars(r)[k] + safeVal, err := url.PathUnescape(val) + if err != nil { + logrus.Error(errors.Wrapf(err, "failed to unescape mux key %s, value %s", k, val)) + return val + } + return safeVal +} + +// GetName extracts the name from the mux +func GetName(r *http.Request) string { + return getVar(r, "name") +} |