podman
139 строк · 3.3 Кб
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
15package runtime
16
17import (
18"bufio"
19"io"
20"net/http"
21"strings"
22
23"github.com/go-openapi/swag"
24)
25
26// CanHaveBody returns true if this method can have a body
27func CanHaveBody(method string) bool {
28mn := strings.ToUpper(method)
29return mn == "POST" || mn == "PUT" || mn == "PATCH" || mn == "DELETE"
30}
31
32// IsSafe returns true if this is a request with a safe method
33func IsSafe(r *http.Request) bool {
34mn := strings.ToUpper(r.Method)
35return mn == "GET" || mn == "HEAD"
36}
37
38// AllowsBody returns true if the request allows for a body
39func AllowsBody(r *http.Request) bool {
40mn := strings.ToUpper(r.Method)
41return mn != "HEAD"
42}
43
44// HasBody returns true if this method needs a content-type
45func HasBody(r *http.Request) bool {
46// happy case: we have a content length set
47if r.ContentLength > 0 {
48return true
49}
50
51if r.Header.Get("content-length") != "" {
52// in this case, no Transfer-Encoding should be present
53// we have a header set but it was explicitly set to 0, so we assume no body
54return false
55}
56
57rdr := newPeekingReader(r.Body)
58r.Body = rdr
59return rdr.HasContent()
60}
61
62func newPeekingReader(r io.ReadCloser) *peekingReader {
63if r == nil {
64return nil
65}
66return &peekingReader{
67underlying: bufio.NewReader(r),
68orig: r,
69}
70}
71
72type peekingReader struct {
73underlying interface {
74Buffered() int
75Peek(int) ([]byte, error)
76Read([]byte) (int, error)
77}
78orig io.ReadCloser
79}
80
81func (p *peekingReader) HasContent() bool {
82if p == nil {
83return false
84}
85if p.underlying.Buffered() > 0 {
86return true
87}
88b, err := p.underlying.Peek(1)
89if err != nil {
90return false
91}
92return len(b) > 0
93}
94
95func (p *peekingReader) Read(d []byte) (int, error) {
96if p == nil {
97return 0, io.EOF
98}
99return p.underlying.Read(d)
100}
101
102func (p *peekingReader) Close() error {
103p.underlying = nil
104if p.orig != nil {
105return p.orig.Close()
106}
107return nil
108}
109
110// JSONRequest creates a new http request with json headers set
111func JSONRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
112req, err := http.NewRequest(method, urlStr, body)
113if err != nil {
114return nil, err
115}
116req.Header.Add(HeaderContentType, JSONMime)
117req.Header.Add(HeaderAccept, JSONMime)
118return req, nil
119}
120
121// Gettable for things with a method GetOK(string) (data string, hasKey bool, hasValue bool)
122type Gettable interface {
123GetOK(string) ([]string, bool, bool)
124}
125
126// ReadSingleValue reads a single value from the source
127func ReadSingleValue(values Gettable, name string) string {
128vv, _, hv := values.GetOK(name)
129if hv {
130return vv[len(vv)-1]
131}
132return ""
133}
134
135// ReadCollectionValue reads a collection value from a string data source
136func ReadCollectionValue(values Gettable, name, collectionFormat string) []string {
137v := ReadSingleValue(values, name)
138return swag.SplitByFormat(v, collectionFormat)
139}
140