From 9ca364d8753a5f2c7529c5b3dd7178bfd51effc6 Mon Sep 17 00:00:00 2001 From: clsr Date: Fri, 25 Aug 2017 17:04:42 +0200 Subject: Add support for the draft/cnp-select "select" request/response parameter --- response.go | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'response.go') diff --git a/response.go b/response.go index bf23f20..3ecb961 100644 --- a/response.go +++ b/response.go @@ -76,7 +76,7 @@ func (r *Response) Name() string { // SetName sets the name response parameter. // -// An error is raised if the name includes characters not valid in a filename +// Returns an error if the name includes characters not valid in a filename // (slash, null byte). func (r *Response) SetName(name string) error { return setFilename(&r.Message, "name", name) @@ -93,7 +93,7 @@ func (r *Response) Type() string { // SetType sets the type response parameter. // -// An error is raised if typ is not a valid format for a MIME type. +// Returns an error if typ is not a valid format for a MIME type. func (r *Response) SetType(typ string) error { return setType(&r.Message, "type", typ) } @@ -131,11 +131,11 @@ func (r *Response) Modified() time.Time { // SetModified sets the modified response parameter. // -// If the time response parameter is empty, it's set to the current time. -// If t is the zero time value, the modified parameter is unset. +// If t is the zero time value, the modified parameter is unset. Otherwise, if +// the time response parameter is empty, it's set to the current time. func (r *Response) SetModified(t time.Time) { setTime(&r.Message, "modified", t) - if r.Time().IsZero() { + if !t.IsZero() && r.Time().IsZero() { r.SetTime(time.Now()) } } @@ -159,14 +159,15 @@ func (r *Response) Location() (host, path string, err error) { // SetLocation sets the location response parameter to host and path. // -// If the host or path are invalid +// Returns an error if the host or path are invalid. func (r *Response) SetLocation(host, path string) error { + err := ErrorInvalid{"invalid response: invalid location parameter"} if strings.ContainsRune(host, '/') { - return ErrorInvalid{"invalid response: invalid location parameter"} + return err } l := host + path if err := validateRequestIntent(l); err != nil { - return ErrorInvalid{"invalid response: invalid location parameter"} + return err } r.SetParam("location", l) return nil @@ -208,8 +209,23 @@ func (r *Response) SetReason(reason string) error { return nil } +// Select retrieves the select response parameter. +// +// If the parameter isn't a valid selector, empty strings are returned. +func (r *Response) Select() (selector, query string) { + selector, query, _ = getSelect(&r.Message, "select") + return +} + +// SetSelect sets the select response parameter. +// +// If the selector name is empty, the select parameter is unset. +func (r *Response) SetSelect(selector, query string) error { + return setSelect(&r.Message, "select", selector, query) +} + // Validate validates the response intent and header parameter value format -// (length, name, type, time, modified, location, reason) +// (length, name, type, time, modified, location, reason, select) func (r *Response) Validate() error { if !responseIntents[r.Intent()] { return ErrorInvalid{"invalid response: unknown response intent"} @@ -237,6 +253,9 @@ func (r *Response) Validate() error { if !responseErrorReasons[r.Param("reason")] { return ErrorInvalid{"invalid response: unknown error reason"} } + if _, _, err := getSelect(&r.Message, "select"); err != nil { + return err + } return nil } -- cgit