本站源代码
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

140 líneas
3.4KB

  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package runtime
  15. import (
  16. "bufio"
  17. "io"
  18. "net/http"
  19. "strings"
  20. "github.com/go-openapi/swag"
  21. )
  22. // CanHaveBody returns true if this method can have a body
  23. func CanHaveBody(method string) bool {
  24. mn := strings.ToUpper(method)
  25. return mn == "POST" || mn == "PUT" || mn == "PATCH" || mn == "DELETE"
  26. }
  27. // IsSafe returns true if this is a request with a safe method
  28. func IsSafe(r *http.Request) bool {
  29. mn := strings.ToUpper(r.Method)
  30. return mn == "GET" || mn == "HEAD"
  31. }
  32. // AllowsBody returns true if the request allows for a body
  33. func AllowsBody(r *http.Request) bool {
  34. mn := strings.ToUpper(r.Method)
  35. return mn != "HEAD"
  36. }
  37. // HasBody returns true if this method needs a content-type
  38. func HasBody(r *http.Request) bool {
  39. // happy case: we have a content length set
  40. if r.ContentLength > 0 {
  41. return true
  42. }
  43. if r.Header.Get(http.CanonicalHeaderKey("content-length")) != "" {
  44. // in this case, no Transfer-Encoding should be present
  45. // we have a header set but it was explicitly set to 0, so we assume no body
  46. return false
  47. }
  48. rdr := newPeekingReader(r.Body)
  49. r.Body = rdr
  50. return rdr.HasContent()
  51. }
  52. func newPeekingReader(r io.ReadCloser) *peekingReader {
  53. if r == nil {
  54. return nil
  55. }
  56. return &peekingReader{
  57. underlying: bufio.NewReader(r),
  58. orig: r,
  59. }
  60. }
  61. type peekingReader struct {
  62. underlying interface {
  63. Buffered() int
  64. Peek(int) ([]byte, error)
  65. Read([]byte) (int, error)
  66. }
  67. orig io.ReadCloser
  68. }
  69. func (p *peekingReader) HasContent() bool {
  70. if p == nil {
  71. return false
  72. }
  73. if p.underlying.Buffered() > 0 {
  74. return true
  75. }
  76. b, err := p.underlying.Peek(1)
  77. if err != nil {
  78. return false
  79. }
  80. return len(b) > 0
  81. }
  82. func (p *peekingReader) Read(d []byte) (int, error) {
  83. if p == nil {
  84. return 0, io.EOF
  85. }
  86. return p.underlying.Read(d)
  87. }
  88. func (p *peekingReader) Close() error {
  89. p.underlying = nil
  90. if p.orig != nil {
  91. return p.orig.Close()
  92. }
  93. return nil
  94. }
  95. // JSONRequest creates a new http request with json headers set
  96. func JSONRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
  97. req, err := http.NewRequest(method, urlStr, body)
  98. if err != nil {
  99. return nil, err
  100. }
  101. req.Header.Add(HeaderContentType, JSONMime)
  102. req.Header.Add(HeaderAccept, JSONMime)
  103. return req, nil
  104. }
  105. // Gettable for things with a method GetOK(string) (data string, hasKey bool, hasValue bool)
  106. type Gettable interface {
  107. GetOK(string) ([]string, bool, bool)
  108. }
  109. // ReadSingleValue reads a single value from the source
  110. func ReadSingleValue(values Gettable, name string) string {
  111. vv, _, hv := values.GetOK(name)
  112. if hv {
  113. return vv[len(vv)-1]
  114. }
  115. return ""
  116. }
  117. // ReadCollectionValue reads a collection value from a string data source
  118. func ReadCollectionValue(values Gettable, name, collectionFormat string) []string {
  119. v := ReadSingleValue(values, name)
  120. return swag.SplitByFormat(v, collectionFormat)
  121. }
上海开阖软件有限公司 沪ICP备12045867号-1