...

Source file src/github.com/emicklei/go-restful/v3/request.go

Documentation: github.com/emicklei/go-restful/v3

     1  package restful
     2  
     3  // Copyright 2013 Ernest Micklei. All rights reserved.
     4  // Use of this source code is governed by a license
     5  // that can be found in the LICENSE file.
     6  
     7  import (
     8  	"compress/zlib"
     9  	"net/http"
    10  )
    11  
    12  var defaultRequestContentType string
    13  
    14  // Request is a wrapper for a http Request that provides convenience methods
    15  type Request struct {
    16  	Request        *http.Request
    17  	pathParameters map[string]string
    18  	attributes     map[string]interface{} // for storing request-scoped values
    19  	selectedRoute  *Route                 // is nil when no route was matched
    20  }
    21  
    22  func NewRequest(httpRequest *http.Request) *Request {
    23  	return &Request{
    24  		Request:        httpRequest,
    25  		pathParameters: map[string]string{},
    26  		attributes:     map[string]interface{}{},
    27  	} // empty parameters, attributes
    28  }
    29  
    30  // If ContentType is missing or */* is given then fall back to this type, otherwise
    31  // a "Unable to unmarshal content of type:" response is returned.
    32  // Valid values are restful.MIME_JSON and restful.MIME_XML
    33  // Example:
    34  //
    35  //	restful.DefaultRequestContentType(restful.MIME_JSON)
    36  func DefaultRequestContentType(mime string) {
    37  	defaultRequestContentType = mime
    38  }
    39  
    40  // PathParameter accesses the Path parameter value by its name
    41  func (r *Request) PathParameter(name string) string {
    42  	return r.pathParameters[name]
    43  }
    44  
    45  // PathParameters accesses the Path parameter values
    46  func (r *Request) PathParameters() map[string]string {
    47  	return r.pathParameters
    48  }
    49  
    50  // QueryParameter returns the (first) Query parameter value by its name
    51  func (r *Request) QueryParameter(name string) string {
    52  	return r.Request.URL.Query().Get(name)
    53  }
    54  
    55  // QueryParameters returns the all the query parameters values by name
    56  func (r *Request) QueryParameters(name string) []string {
    57  	return r.Request.URL.Query()[name]
    58  }
    59  
    60  // BodyParameter parses the body of the request (once for typically a POST or a PUT) and returns the value of the given name or an error.
    61  func (r *Request) BodyParameter(name string) (string, error) {
    62  	err := r.Request.ParseForm()
    63  	if err != nil {
    64  		return "", err
    65  	}
    66  	return r.Request.PostFormValue(name), nil
    67  }
    68  
    69  // HeaderParameter returns the HTTP Header value of a Header name or empty if missing
    70  func (r *Request) HeaderParameter(name string) string {
    71  	return r.Request.Header.Get(name)
    72  }
    73  
    74  // ReadEntity checks the Accept header and reads the content into the entityPointer.
    75  func (r *Request) ReadEntity(entityPointer interface{}) (err error) {
    76  	contentType := r.Request.Header.Get(HEADER_ContentType)
    77  	contentEncoding := r.Request.Header.Get(HEADER_ContentEncoding)
    78  
    79  	// check if the request body needs decompression
    80  	if ENCODING_GZIP == contentEncoding {
    81  		gzipReader := currentCompressorProvider.AcquireGzipReader()
    82  		defer currentCompressorProvider.ReleaseGzipReader(gzipReader)
    83  		gzipReader.Reset(r.Request.Body)
    84  		r.Request.Body = gzipReader
    85  	} else if ENCODING_DEFLATE == contentEncoding {
    86  		zlibReader, err := zlib.NewReader(r.Request.Body)
    87  		if err != nil {
    88  			return err
    89  		}
    90  		r.Request.Body = zlibReader
    91  	}
    92  
    93  	// lookup the EntityReader, use defaultRequestContentType if needed and provided
    94  	entityReader, ok := entityAccessRegistry.accessorAt(contentType)
    95  	if !ok {
    96  		if len(defaultRequestContentType) != 0 {
    97  			entityReader, ok = entityAccessRegistry.accessorAt(defaultRequestContentType)
    98  		}
    99  		if !ok {
   100  			return NewError(http.StatusBadRequest, "Unable to unmarshal content of type:"+contentType)
   101  		}
   102  	}
   103  	return entityReader.Read(r, entityPointer)
   104  }
   105  
   106  // SetAttribute adds or replaces the attribute with the given value.
   107  func (r *Request) SetAttribute(name string, value interface{}) {
   108  	r.attributes[name] = value
   109  }
   110  
   111  // Attribute returns the value associated to the given name. Returns nil if absent.
   112  func (r Request) Attribute(name string) interface{} {
   113  	return r.attributes[name]
   114  }
   115  
   116  // SelectedRoutePath root path + route path that matched the request, e.g. /meetings/{id}/attendees
   117  // If no route was matched then return an empty string.
   118  func (r Request) SelectedRoutePath() string {
   119  	if r.selectedRoute == nil {
   120  		return ""
   121  	}
   122  	// skip creating an accessor
   123  	return r.selectedRoute.Path
   124  }
   125  
   126  // SelectedRoute returns a reader to access the selected Route by the container
   127  // Returns nil if no route was matched.
   128  func (r Request) SelectedRoute() RouteReader {
   129  	if r.selectedRoute == nil {
   130  		return nil
   131  	}
   132  	return routeAccessor{route: r.selectedRoute}
   133  }
   134  

View as plain text