podman

Форк
0
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

15
package runtime
16

17
import (
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
27
func CanHaveBody(method string) bool {
28
	mn := strings.ToUpper(method)
29
	return mn == "POST" || mn == "PUT" || mn == "PATCH" || mn == "DELETE"
30
}
31

32
// IsSafe returns true if this is a request with a safe method
33
func IsSafe(r *http.Request) bool {
34
	mn := strings.ToUpper(r.Method)
35
	return mn == "GET" || mn == "HEAD"
36
}
37

38
// AllowsBody returns true if the request allows for a body
39
func AllowsBody(r *http.Request) bool {
40
	mn := strings.ToUpper(r.Method)
41
	return mn != "HEAD"
42
}
43

44
// HasBody returns true if this method needs a content-type
45
func HasBody(r *http.Request) bool {
46
	// happy case: we have a content length set
47
	if r.ContentLength > 0 {
48
		return true
49
	}
50

51
	if 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
54
		return false
55
	}
56

57
	rdr := newPeekingReader(r.Body)
58
	r.Body = rdr
59
	return rdr.HasContent()
60
}
61

62
func newPeekingReader(r io.ReadCloser) *peekingReader {
63
	if r == nil {
64
		return nil
65
	}
66
	return &peekingReader{
67
		underlying: bufio.NewReader(r),
68
		orig:       r,
69
	}
70
}
71

72
type peekingReader struct {
73
	underlying interface {
74
		Buffered() int
75
		Peek(int) ([]byte, error)
76
		Read([]byte) (int, error)
77
	}
78
	orig io.ReadCloser
79
}
80

81
func (p *peekingReader) HasContent() bool {
82
	if p == nil {
83
		return false
84
	}
85
	if p.underlying.Buffered() > 0 {
86
		return true
87
	}
88
	b, err := p.underlying.Peek(1)
89
	if err != nil {
90
		return false
91
	}
92
	return len(b) > 0
93
}
94

95
func (p *peekingReader) Read(d []byte) (int, error) {
96
	if p == nil {
97
		return 0, io.EOF
98
	}
99
	return p.underlying.Read(d)
100
}
101

102
func (p *peekingReader) Close() error {
103
	p.underlying = nil
104
	if p.orig != nil {
105
		return p.orig.Close()
106
	}
107
	return nil
108
}
109

110
// JSONRequest creates a new http request with json headers set
111
func JSONRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
112
	req, err := http.NewRequest(method, urlStr, body)
113
	if err != nil {
114
		return nil, err
115
	}
116
	req.Header.Add(HeaderContentType, JSONMime)
117
	req.Header.Add(HeaderAccept, JSONMime)
118
	return req, nil
119
}
120

121
// Gettable for things with a method GetOK(string) (data string, hasKey bool, hasValue bool)
122
type Gettable interface {
123
	GetOK(string) ([]string, bool, bool)
124
}
125

126
// ReadSingleValue reads a single value from the source
127
func ReadSingleValue(values Gettable, name string) string {
128
	vv, _, hv := values.GetOK(name)
129
	if hv {
130
		return vv[len(vv)-1]
131
	}
132
	return ""
133
}
134

135
// ReadCollectionValue reads a collection value from a string data source
136
func ReadCollectionValue(values Gettable, name, collectionFormat string) []string {
137
	v := ReadSingleValue(values, name)
138
	return swag.SplitByFormat(v, collectionFormat)
139
}
140

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.