/
api-response.go
241 lines (211 loc) · 6.82 KB
/
api-response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Minio Cloud Storage, (C) 2015, 2016 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package s3
import (
"encoding/xml"
"net/http"
"net/url"
"path"
"strconv"
"time"
"github.com/emicklei/go-restful"
log "github.com/sirupsen/logrus"
. "github.com/opensds/multi-cloud/api/pkg/s3/datatype"
. "github.com/opensds/multi-cloud/s3/error"
"github.com/opensds/multi-cloud/s3/pkg/helper"
)
const (
timeFormatAMZ = "2006-01-02T15:04:05.000Z" // Reply date format
)
func GetFinalError(err error, errorCode int32) error {
if errorCode != int32(ErrNoErr) {
return S3ErrorCode(errorCode)
} else {
return err
}
}
// WriteSuccessResponse write success headers and response if any.
func WriteSuccessResponse(response *restful.Response, data []byte) {
if data == nil {
response.WriteHeader(http.StatusOK)
return
}
response.AddHeader("Content-Length", strconv.Itoa(len(data)))
response.WriteHeader(http.StatusOK)
response.Write(data)
response.Flush()
}
// writeErrorResponse write error headers
// w http.ResponseWriter, r *http.Request
func WriteErrorResponse(response *restful.Response, request *restful.Request, err error) {
WriteErrorResponseHeaders(response, err)
WriteErrorResponseNoHeader(response, request, err, request.Request.URL.Path)
}
func WriteErrorResponseWithResource(response *restful.Response, request *restful.Request, err error, resource string) {
WriteErrorResponseHeaders(response, err)
WriteErrorResponseNoHeader(response, request, err, resource)
}
func WriteErrorResponseHeaders(response *restful.Response, err error) {
var status int
apiErrorCode, ok := err.(S3Error)
if ok {
status = apiErrorCode.HttpStatusCode()
} else {
status = http.StatusInternalServerError
}
response.WriteHeader(status)
}
func WriteErrorResponseNoHeader(response *restful.Response, request *restful.Request, err error, resource string) {
// HEAD should have no body, do not attempt to write to it
if request.Request.Method == "HEAD" {
return
}
// Generate error response.
errorResponse := ApiErrorResponse{}
apiErrorCode, ok := err.(S3Error)
if ok {
errorResponse.AwsErrorCode = apiErrorCode.AwsErrorCode()
errorResponse.Message = apiErrorCode.Description()
} else {
errorResponse.AwsErrorCode = "InternalError"
errorResponse.Message = "We encountered an internal error, please try again."
}
errorResponse.Resource = resource
errorResponse.HostId = helper.CONFIG.InstanceId
encodedErrorResponse := EncodeResponse(errorResponse)
response.Write(encodedErrorResponse)
response.ResponseWriter.(http.Flusher).Flush()
}
// getLocation get URL location.
func GetLocation(r *http.Request) string {
return path.Clean(r.URL.Path) // Clean any trailing slashes.
}
// writeSuccessNoContent write success headers with http status 204
func WriteSuccessNoContent(response *restful.Response) {
response.ResponseWriter.WriteHeader(http.StatusNoContent)
}
func WriteApiErrorResponse(response *restful.Response, request *restful.Request, status int, awsErrCode, message string) {
// write header
response.WriteHeader(status)
// HEAD should have no body, do not attempt to write to it
if request.Request.Method == "HEAD" {
return
}
errorResponse := ApiErrorResponse{}
errorResponse.AwsErrorCode = awsErrCode
errorResponse.Message = message
errorResponse.Resource = request.Request.URL.Path
errorResponse.HostId = helper.CONFIG.InstanceId
encodedErrorResponse := EncodeResponse(errorResponse)
response.Write(encodedErrorResponse)
response.ResponseWriter.(http.Flusher).Flush()
}
// GenerateCopyObjectResponse
func GenerateCopyObjectResponse(etag string, lastModified time.Time) CopyObjectResponse {
return CopyObjectResponse{
ETag: "\"" + etag + "\"",
LastModified: lastModified.UTC().Format(timeFormatAMZ),
}
}
func GenerateCopyObjectPartResponse(etag string, lastModified int64) CopyObjectPartResponse {
return CopyObjectPartResponse{
LastModified: time.Unix(lastModified, 0).UTC().Format(timeFormatAMZ),
ETag: "\"" + etag + "\"",
}
}
// GenerateInitiateMultipartUploadResponse
func GenerateInitiateMultipartUploadResponse(bucket, key, uploadID string) InitiateMultipartUploadResponse {
return InitiateMultipartUploadResponse{
Bucket: bucket,
Key: key,
UploadID: uploadID,
}
}
// GenerateCompleteMultipartUploadResponse
func GenerateCompleteMultipartUploadResponse(bucket, key, location, etag string) CompleteMultipartUploadResponse {
return CompleteMultipartUploadResponse{
Location: location,
Bucket: bucket,
Key: key,
ETag: etag,
}
}
// APIErrorResponse - error response format
type ApiErrorResponse struct {
XMLName xml.Name `xml:"Error" json:"-"`
AwsErrorCode string `xml:"Code"`
Message string
Key string
BucketName string
Resource string
RequestId string
HostId string
}
// Parse bucket url queries for ?uploads
func parseListUploadsQuery(query url.Values) (request ListUploadsRequest, err error) {
request.Delimiter = query.Get("delimiter")
request.EncodingType = query.Get("encoding-type")
if query.Get("max-uploads") == "" {
request.MaxUploads = MaxUploadsList
} else {
request.MaxUploads, err = strconv.Atoi(query.Get("max-uploads"))
if err != nil {
return
}
if request.MaxUploads > MaxUploadsList || request.MaxUploads < 1 {
err = ErrInvalidMaxUploads
return
}
}
request.KeyMarker = query.Get("key-marker")
request.Prefix = query.Get("prefix")
request.UploadIdMarker = query.Get("upload-id-marker")
return
}
// Parse object url queries
func parseListObjectPartsQuery(query url.Values) (request ListPartsRequest, err error) {
request.EncodingType = query.Get("encoding-type")
request.UploadId = query.Get("uploadId")
if request.UploadId == "" {
err = ErrNoSuchUpload
return
}
if query.Get("max-parts") == "" {
request.MaxParts = MaxPartsList
} else {
request.MaxParts, err = strconv.Atoi(query.Get("max-parts"))
if err != nil {
log.Errorln("failed to convert from string to integer. err:", err)
return
}
if request.MaxParts > MaxPartsList || request.MaxParts < 1 {
err = ErrInvalidMaxParts
return
}
}
if query.Get("part-number-marker") != "" {
request.PartNumberMarker, err = strconv.Atoi(query.Get("part-number-marker"))
if err != nil {
err = ErrInvalidPartNumberMarker
return
}
if request.PartNumberMarker < 0 {
err = ErrInvalidPartNumberMarker
return
}
}
return
}