request.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package restful
  2. // Copyright 2013 Ernest Micklei. All rights reserved.
  3. // Use of this source code is governed by a license
  4. // that can be found in the LICENSE file.
  5. import (
  6. "compress/zlib"
  7. "net/http"
  8. )
  9. var defaultRequestContentType string
  10. // Request is a wrapper for a http Request that provides convenience methods
  11. type Request struct {
  12. Request *http.Request
  13. pathParameters map[string]string
  14. attributes map[string]interface{} // for storing request-scoped values
  15. selectedRoutePath string // root path + route path that matched the request, e.g. /meetings/{id}/attendees
  16. }
  17. func NewRequest(httpRequest *http.Request) *Request {
  18. return &Request{
  19. Request: httpRequest,
  20. pathParameters: map[string]string{},
  21. attributes: map[string]interface{}{},
  22. } // empty parameters, attributes
  23. }
  24. // If ContentType is missing or */* is given then fall back to this type, otherwise
  25. // a "Unable to unmarshal content of type:" response is returned.
  26. // Valid values are restful.MIME_JSON and restful.MIME_XML
  27. // Example:
  28. // restful.DefaultRequestContentType(restful.MIME_JSON)
  29. func DefaultRequestContentType(mime string) {
  30. defaultRequestContentType = mime
  31. }
  32. // PathParameter accesses the Path parameter value by its name
  33. func (r *Request) PathParameter(name string) string {
  34. return r.pathParameters[name]
  35. }
  36. // PathParameters accesses the Path parameter values
  37. func (r *Request) PathParameters() map[string]string {
  38. return r.pathParameters
  39. }
  40. // QueryParameter returns the (first) Query parameter value by its name
  41. func (r *Request) QueryParameter(name string) string {
  42. return r.Request.FormValue(name)
  43. }
  44. // 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.
  45. func (r *Request) BodyParameter(name string) (string, error) {
  46. err := r.Request.ParseForm()
  47. if err != nil {
  48. return "", err
  49. }
  50. return r.Request.PostFormValue(name), nil
  51. }
  52. // HeaderParameter returns the HTTP Header value of a Header name or empty if missing
  53. func (r *Request) HeaderParameter(name string) string {
  54. return r.Request.Header.Get(name)
  55. }
  56. // ReadEntity checks the Accept header and reads the content into the entityPointer.
  57. func (r *Request) ReadEntity(entityPointer interface{}) (err error) {
  58. contentType := r.Request.Header.Get(HEADER_ContentType)
  59. contentEncoding := r.Request.Header.Get(HEADER_ContentEncoding)
  60. // check if the request body needs decompression
  61. if ENCODING_GZIP == contentEncoding {
  62. gzipReader := currentCompressorProvider.AcquireGzipReader()
  63. defer currentCompressorProvider.ReleaseGzipReader(gzipReader)
  64. gzipReader.Reset(r.Request.Body)
  65. r.Request.Body = gzipReader
  66. } else if ENCODING_DEFLATE == contentEncoding {
  67. zlibReader, err := zlib.NewReader(r.Request.Body)
  68. if err != nil {
  69. return err
  70. }
  71. r.Request.Body = zlibReader
  72. }
  73. // lookup the EntityReader, use defaultRequestContentType if needed and provided
  74. entityReader, ok := entityAccessRegistry.accessorAt(contentType)
  75. if !ok {
  76. if len(defaultRequestContentType) != 0 {
  77. entityReader, ok = entityAccessRegistry.accessorAt(defaultRequestContentType)
  78. }
  79. if !ok {
  80. return NewError(http.StatusBadRequest, "Unable to unmarshal content of type:"+contentType)
  81. }
  82. }
  83. return entityReader.Read(r, entityPointer)
  84. }
  85. // SetAttribute adds or replaces the attribute with the given value.
  86. func (r *Request) SetAttribute(name string, value interface{}) {
  87. r.attributes[name] = value
  88. }
  89. // Attribute returns the value associated to the given name. Returns nil if absent.
  90. func (r Request) Attribute(name string) interface{} {
  91. return r.attributes[name]
  92. }
  93. // SelectedRoutePath root path + route path that matched the request, e.g. /meetings/{id}/attendees
  94. func (r Request) SelectedRoutePath() string {
  95. return r.selectedRoutePath
  96. }