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 --- request.go | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'request.go') diff --git a/request.go b/request.go index 8c5a34e..0fb27a2 100644 --- a/request.go +++ b/request.go @@ -29,7 +29,7 @@ func NewRequest(host, pth string, body []byte) (*Request, error) { return req, nil } -// NewRequestURL creates a new Request from a URL and body data. +// NewRequestURL creates a new Request from a URL and optional body data. func NewRequestURL(urlStr string, body []byte) (*Request, error) { // XXX: handle //example.com/path URLs if strings.HasPrefix(urlStr, "//") { @@ -163,7 +163,7 @@ func (r *Request) Name() string { // SetName sets the name request 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 *Request) SetName(name string) error { return setFilename(&r.Message, "name", name) @@ -180,7 +180,7 @@ func (r *Request) Type() string { // SetType sets the type request 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 *Request) SetType(typ string) error { return setType(&r.Message, "type", typ) } @@ -204,8 +204,23 @@ func (r *Request) SetIfModified(t time.Time) { setTime(&r.Message, "if_modified", t) } +// Select retrieves the select request parameter. +// +// If the parameter isn't a valid selector, empty strings are returned. +func (r *Request) Select() (selector, query string) { + selector, query, _ = getSelect(&r.Message, "select") + return +} + +// SetSelect sets the select request parameter. +// +// If the selector name is empty, the select parameter is unset. +func (r *Request) SetSelect(selector, query string) error { + return setSelect(&r.Message, "select", selector, query) +} + // Validate validates the request header intent and parameter value format -// (length, name, type, if_modified) +// (length, name, type, if_modified, select) func (r *Request) Validate() error { if err := validateRequestIntent(r.Intent()); err != nil { return err @@ -222,6 +237,9 @@ func (r *Request) Validate() error { if _, err := getTime(&r.Message, "if_modified"); err != nil { return err } + if _, _, err := getSelect(&r.Message, "select"); err != nil { + return err + } return nil } @@ -250,7 +268,9 @@ func (r *Request) Write(w io.Writer) error { return r.Message.Write(w) } -// Clean cleans a CNP intent path. +// Clean cleans a CNP request intent path. +// +// This works the same as path.Clean(), but preserves a trailing slash. func Clean(s string) string { c := path.Clean(s) if len(s) > 0 && len(c) > 0 && s[len(s)-1] == '/' && c[len(c)-1] != '/' { -- cgit