1
// Copyright 2022 The CubeFS Authors.
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
30
"github.com/cubefs/cubefs/blobstore/util/version"
35
HeaderContentType = "Content-Type"
36
HeaderContentLength = "Content-Length"
37
HeaderContentRange = "Content-Range"
38
HeaderContentMD5 = "Content-MD5"
39
HeaderUA = "User-Agent"
42
HeaderTraceLog = "Trace-Log"
43
HeaderTraceTags = "Trace-Tags"
46
HeaderCrcEncoded = "X-Crc-Encoded"
47
HeaderAckCrcEncoded = "X-Ack-Crc-Encoded"
52
MIMEStream = "application/octet-stream"
53
MIMEJSON = "application/json"
54
MIMEXML = "application/xml"
55
MIMEPlain = "text/plain"
56
MIMEPOSTForm = "application/x-www-form-urlencoded"
57
MIMEMultipartPOSTForm = "multipart/form-data"
58
MIMEYAML = "application/x-yaml"
63
GzipEncodingType = "gzip"
66
// UserAgent user agent
67
var UserAgent = "Golang blobstore/rpc package"
70
// ValueGetter fill argument's field from url values or http params.
71
ValueGetter func(string) string
72
// Parser is the interface implemented by argument types
73
// that can parse themselves from url.Values.
75
Parse(ValueGetter) error
78
// priority of marshaler and unmarshaler (default is json).
79
// - - - - - - - - - - - - - - - - - - - - - -
80
// | | marshaler | unmarshaler |
82
// | ^ | MarshalerTo | UnmarshalerFrom |
83
// | | | Marshaler | Unmarshaler |
84
// | | | JSON Marshal| JSON Unmarshal |
86
// - - - - - - - - - - - - - - - - - - - - - -
89
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90
// | APP | Client | TCP | Server |
91
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
92
// | Request Type | marshaler | - - - - > | unmarshaler |
95
// | Response Type | unmarshaler | < - - - - | marshaler |
96
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98
// Marshaler is the interface implemented by types that
99
// can marshal themselves into bytes, second parameter
101
Marshaler interface {
102
Marshal() ([]byte, string, error)
104
// MarshalerTo is the interface implemented by types that
105
// can marshal themselves into writer, the first parameter
106
// is content type. (Not Recommended).
107
// The underlying writer is a *bytes.Buffer.
108
// Context.RespondWithReader is better than MarshalerTo on Server Side.
109
MarshalerTo interface {
110
MarshalTo(responseBody io.Writer) (string, error)
112
// Unmarshaler is the interface implemented by types
113
// that can unmarshal themselves from bytes.
114
Unmarshaler interface {
115
Unmarshal([]byte) error
117
// UnmarshalerFrom is the interface implemented by types
118
// that can unmarshal themselves from body reader.
119
// The body underlying implementation is a *io.LimitedReader.
120
UnmarshalerFrom interface {
121
UnmarshalFrom(requestBody io.Reader) error
124
// HTTPError interface of error with http status code
125
HTTPError interface {
126
// StatusCode http status code
128
// ErrorCode special defined code
130
// Error detail message
135
// ProgressHandler http progress handler
136
type ProgressHandler interface {
137
Handler(http.ResponseWriter, *http.Request, func(http.ResponseWriter, *http.Request))
140
// NoneBody no body of request of response.
141
var NoneBody Marshaler = noneBody{}
143
type noneBody struct{}
145
func (noneBody) Marshal() ([]byte, string, error) {
146
return []byte{}, "", nil
149
type marshalledBody struct {
155
func marshalObj(obj interface{}) (*marshalledBody, error) {
163
} else if o, ok := obj.(MarshalerTo); ok {
164
w := bytes.NewBuffer(nil)
165
ct, err = o.MarshalTo(w)
169
return &marshalledBody{
170
ContentLength: w.Len(),
175
} else if o, ok := obj.(Marshaler); ok {
176
buffer, ct, err = o.Marshal()
178
buffer, err = json.Marshal(obj)
183
return &marshalledBody{
184
ContentLength: len(buffer),
186
Body: bytes.NewReader(buffer),
190
func programVersion() string {
191
sp := strings.Fields(strings.TrimSpace(version.Version()))
192
if len(sp) == 0 || sp[0] == "develop" {
193
data, err := ioutil.ReadFile(os.Args[0])
197
return fmt.Sprintf("%x", md5.Sum(data))[:10]
206
hostname, _ := os.Hostname()
207
ua := fmt.Sprintf("%s/%s (%s/%s; %s) %s/%s",
208
path.Base(os.Args[0]),
214
fmt.Sprint(os.Getpid()),