forked from johannesboyne/gofakes3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing.go
200 lines (177 loc) · 5.85 KB
/
routing.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
package gofakes3
import (
"encoding/base64"
"fmt"
"net/http"
"strings"
)
// routeBase is a http.HandlerFunc that dispatches top level routes for
// GoFakeS3.
//
// URLs are assumed to break down into two common path segments, in the
// following format:
// /<bucket>/<object>
//
// The operation for most of the core functionality is built around HTTP
// verbs, but outside the core functionality, the clean separation starts
// to degrade, especially around multipart uploads.
//
func (g *GoFakeS3) routeBase(w http.ResponseWriter, r *http.Request) {
var (
path = strings.Trim(r.URL.Path, "/")
parts = strings.SplitN(path, "/", 2)
bucket = parts[0]
query = r.URL.Query()
object = ""
err error
)
hdr := w.Header()
id := fmt.Sprintf("%016X", g.nextRequestID())
hdr.Set("x-amz-id-2", base64.StdEncoding.EncodeToString([]byte(id+id+id+id))) // x-amz-id-2 is 48 bytes of random stuff
hdr.Set("x-amz-request-id", id)
hdr.Set("Server", "AmazonS3")
if len(parts) == 2 {
object = parts[1]
}
if uploadID := UploadID(query.Get("uploadId")); uploadID != "" {
err = g.routeMultipartUpload(bucket, object, uploadID, w, r)
} else if _, ok := query["uploads"]; ok {
err = g.routeMultipartUploadBase(bucket, object, w, r)
} else if _, ok := query["versioning"]; ok {
err = g.routeVersioning(bucket, w, r)
} else if _, ok := query["versions"]; ok {
err = g.routeVersions(bucket, w, r)
} else if versionID := versionFromQuery(query["versionId"]); versionID != "" {
err = g.routeVersion(bucket, object, VersionID(versionID), w, r)
} else if bucket != "" && object != "" {
err = g.routeObject(bucket, object, w, r)
} else if bucket != "" {
err = g.routeBucket(bucket, w, r)
} else if r.Method == "GET" {
err = g.listBuckets(w, r)
} else {
http.NotFound(w, r)
return
}
if err != nil {
g.httpError(w, r, err)
}
}
// routeObject oandles URLs that contain both a bucket path segment and an
// object path segment.
func (g *GoFakeS3) routeObject(bucket, object string, w http.ResponseWriter, r *http.Request) (err error) {
switch r.Method {
case "GET":
return g.getObject(bucket, object, "", w, r)
case "HEAD":
return g.headObject(bucket, object, "", w, r)
case "PUT":
return g.createObject(bucket, object, w, r)
case "DELETE":
return g.deleteObject(bucket, object, w, r)
default:
return ErrMethodNotAllowed
}
}
// routeBucket handles URLs that contain only a bucket path segment, not an
// object path segment.
func (g *GoFakeS3) routeBucket(bucket string, w http.ResponseWriter, r *http.Request) (err error) {
switch r.Method {
case "GET":
if _, ok := r.URL.Query()["location"]; ok {
return g.getBucketLocation(bucket, w, r)
} else {
return g.listBucket(bucket, w, r)
}
case "PUT":
return g.createBucket(bucket, w, r)
case "DELETE":
return g.deleteBucket(bucket, w, r)
case "HEAD":
return g.headBucket(bucket, w, r)
case "POST":
if _, ok := r.URL.Query()["delete"]; ok {
return g.deleteMulti(bucket, w, r)
} else {
return g.createObjectBrowserUpload(bucket, w, r)
}
default:
return ErrMethodNotAllowed
}
}
// routeMultipartUploadBase operates on routes that contain '?uploads' in the
// query string. These routes may or may not have a value for bucket or object;
// this is validated and handled in the target handler functions.
func (g *GoFakeS3) routeMultipartUploadBase(bucket, object string, w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
return g.listMultipartUploads(bucket, w, r)
case "POST":
return g.initiateMultipartUpload(bucket, object, w, r)
default:
return ErrMethodNotAllowed
}
}
// routeVersioningBase operates on routes that contain '?versioning' in the
// query string. These routes may or may not have a value for bucket; this is
// validated and handled in the target handler functions.
func (g *GoFakeS3) routeVersioning(bucket string, w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
return g.getBucketVersioning(bucket, w, r)
case "PUT":
return g.putBucketVersioning(bucket, w, r)
default:
return ErrMethodNotAllowed
}
}
// routeVersions operates on routes that contain '?versions' in the query string.
func (g *GoFakeS3) routeVersions(bucket string, w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
return g.listBucketVersions(bucket, w, r)
default:
return ErrMethodNotAllowed
}
}
// routeVersion operates on routes that contain '?versionId=<id>' in the
// query string.
func (g *GoFakeS3) routeVersion(bucket, object string, versionID VersionID, w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
return g.getObject(bucket, object, versionID, w, r)
case "HEAD":
return g.headObject(bucket, object, versionID, w, r)
case "DELETE":
return g.deleteObjectVersion(bucket, object, versionID, w, r)
default:
return ErrMethodNotAllowed
}
}
// routeMultipartUpload operates on routes that contain '?uploadId=<id>' in the
// query string.
func (g *GoFakeS3) routeMultipartUpload(bucket, object string, uploadID UploadID, w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
return g.listMultipartUploadParts(bucket, object, uploadID, w, r)
case "PUT":
return g.putMultipartUploadPart(bucket, object, uploadID, w, r)
case "DELETE":
return g.abortMultipartUpload(bucket, object, uploadID, w, r)
case "POST":
return g.completeMultipartUpload(bucket, object, uploadID, w, r)
default:
return ErrMethodNotAllowed
}
}
func versionFromQuery(qv []string) string {
// The versionId subresource may be the string 'null'; this has been
// observed coming in via Boto. The S3 documentation for the "DELETE
// object" endpoint describes a 'null' version explicitly, but we don't
// want backend implementers to have to special-case this string, so
// let's hide it in here:
if len(qv) > 0 && qv[0] != "" && qv[0] != "null" {
return qv[0]
}
return ""
}