request.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. selectedRoute *Route // is nil when no route was matched
  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. //
  29. // restful.DefaultRequestContentType(restful.MIME_JSON)
  30. func DefaultRequestContentType(mime string) {
  31. defaultRequestContentType = mime
  32. }
  33. // PathParameter accesses the Path parameter value by its name
  34. func (r *Request) PathParameter(name string) string {
  35. return r.pathParameters[name]
  36. }
  37. // PathParameters accesses the Path parameter values
  38. func (r *Request) PathParameters() map[string]string {
  39. return r.pathParameters
  40. }
  41. // QueryParameter returns the (first) Query parameter value by its name
  42. func (r *Request) QueryParameter(name string) string {
  43. return r.Request.URL.Query().Get(name)
  44. }
  45. // QueryParameters returns the all the query parameters values by name
  46. func (r *Request) QueryParameters(name string) []string {
  47. return r.Request.URL.Query()[name]
  48. }
  49. // 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.
  50. func (r *Request) BodyParameter(name string) (string, error) {
  51. err := r.Request.ParseForm()
  52. if err != nil {
  53. return "", err
  54. }
  55. return r.Request.PostFormValue(name), nil
  56. }
  57. // HeaderParameter returns the HTTP Header value of a Header name or empty if missing
  58. func (r *Request) HeaderParameter(name string) string {
  59. return r.Request.Header.Get(name)
  60. }
  61. // ReadEntity checks the Accept header and reads the content into the entityPointer.
  62. func (r *Request) ReadEntity(entityPointer interface{}) (err error) {
  63. contentType := r.Request.Header.Get(HEADER_ContentType)
  64. contentEncoding := r.Request.Header.Get(HEADER_ContentEncoding)
  65. // check if the request body needs decompression
  66. if ENCODING_GZIP == contentEncoding {
  67. gzipReader := currentCompressorProvider.AcquireGzipReader()
  68. defer currentCompressorProvider.ReleaseGzipReader(gzipReader)
  69. gzipReader.Reset(r.Request.Body)
  70. r.Request.Body = gzipReader
  71. } else if ENCODING_DEFLATE == contentEncoding {
  72. zlibReader, err := zlib.NewReader(r.Request.Body)
  73. if err != nil {
  74. return err
  75. }
  76. r.Request.Body = zlibReader
  77. }
  78. // lookup the EntityReader, use defaultRequestContentType if needed and provided
  79. entityReader, ok := entityAccessRegistry.accessorAt(contentType)
  80. if !ok {
  81. if len(defaultRequestContentType) != 0 {
  82. entityReader, ok = entityAccessRegistry.accessorAt(defaultRequestContentType)
  83. }
  84. if !ok {
  85. return NewError(http.StatusBadRequest, "Unable to unmarshal content of type:"+contentType)
  86. }
  87. }
  88. return entityReader.Read(r, entityPointer)
  89. }
  90. // SetAttribute adds or replaces the attribute with the given value.
  91. func (r *Request) SetAttribute(name string, value interface{}) {
  92. r.attributes[name] = value
  93. }
  94. // Attribute returns the value associated to the given name. Returns nil if absent.
  95. func (r Request) Attribute(name string) interface{} {
  96. return r.attributes[name]
  97. }
  98. // SelectedRoutePath root path + route path that matched the request, e.g. /meetings/{id}/attendees
  99. // If no route was matched then return an empty string.
  100. func (r Request) SelectedRoutePath() string {
  101. if r.selectedRoute == nil {
  102. return ""
  103. }
  104. // skip creating an accessor
  105. return r.selectedRoute.Path
  106. }
  107. // SelectedRoute returns a reader to access the selected Route by the container
  108. // Returns nil if no route was matched.
  109. func (r Request) SelectedRoute() RouteReader {
  110. if r.selectedRoute == nil {
  111. return nil
  112. }
  113. return routeAccessor{route: r.selectedRoute}
  114. }