Skip to content

Commit ca00a1c

Browse files
committed
server: make publicHostMatcher backwards compatible with gorilla/mux
Fixes #713.
1 parent f9a9c17 commit ca00a1c

2 files changed

Lines changed: 68 additions & 5 deletions

File tree

‎fakestorage/server.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/http/httptest"
2121
"net/http/httputil"
2222
"net/textproto"
23+
"regexp"
2324
"strings"
2425
"sync"
2526

@@ -270,7 +271,11 @@ func (s *Server) buildMuxer() {
270271

271272
// publicHostMatcher matches incoming requests against the currently specified server publicHost.
272273
func (s *Server) publicHostMatcher(r *http.Request, rm *mux.RouteMatch) bool {
273-
return r.Host == s.publicHost
274+
if strings.Contains(s.publicHost, ":") {
275+
return r.Host == s.publicHost
276+
}
277+
matched, _ := regexp.MatchString("^"+regexp.QuoteMeta(s.publicHost), r.Host)
278+
return matched
274279
}
275280

276281
// Stop stops the server, closing all connections.

‎fakestorage/server_test.go‎

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func TestDownloadObject(t *testing.T) {
153153
}
154154

155155
func TestDownloadAfterPublicHostChange(t *testing.T) {
156-
server, err := NewServerWithOptions(Options{PublicHost: "127.0.0.1", InitialObjects: []Object{
156+
server, err := NewServerWithOptions(Options{PublicHost: "127.0.0.1:80", InitialObjects: []Object{
157157
{ObjectAttrs: ObjectAttrs{BucketName: "some-bucket", Name: "files/txt/text-01.txt"}, Content: []byte("something")},
158158
}})
159159
if err != nil {
@@ -173,10 +173,10 @@ func TestDownloadAfterPublicHostChange(t *testing.T) {
173173
}
174174
defer resp.Body.Close()
175175

176-
// This first request should fail because we have specified a PublicHost of 127.0.0.1, which does
177-
// not include the port generated by the test server.
176+
// First request fails because the port configured in PublicHost
177+
// doesn't match.
178178
if resp.StatusCode != http.StatusNotFound {
179-
t.Errorf("wrong status returned\nwant %d\ngot %d", http.StatusNotFound, resp.StatusCode)
179+
t.Fatalf("wrong status returned\nwant %d\ngot %d", http.StatusNotFound, resp.StatusCode)
180180
}
181181

182182
// Now set the public host to match the httptest.Server and try again.
@@ -202,6 +202,64 @@ func TestDownloadAfterPublicHostChange(t *testing.T) {
202202
}
203203
}
204204

205+
func TestDownloadPartialPublicHostMatch(t *testing.T) {
206+
server, err := NewServerWithOptions(Options{PublicHost: "127.0.0.1", InitialObjects: []Object{
207+
{ObjectAttrs: ObjectAttrs{BucketName: "some-bucket", Name: "files/txt/text-01.txt"}, Content: []byte("something")},
208+
}})
209+
if err != nil {
210+
t.Fatal(err)
211+
}
212+
213+
client := server.HTTPClient()
214+
requestURL := server.URL() + "/some-bucket/files/txt/text-01.txt"
215+
216+
req, err := http.NewRequest(http.MethodGet, requestURL, nil)
217+
if err != nil {
218+
t.Fatal(err)
219+
}
220+
resp, err := client.Do(req)
221+
if err != nil {
222+
t.Fatal(err)
223+
}
224+
defer resp.Body.Close()
225+
226+
if resp.StatusCode != http.StatusOK {
227+
t.Errorf("wrong status returned\nwant %d\ngot %d", http.StatusOK, resp.StatusCode)
228+
}
229+
}
230+
231+
func TestDownloadPartialHostValidationShouldntValidatePortPartially(t *testing.T) {
232+
server, err := NewServerWithOptions(Options{PublicHost: "127.0.0.1", InitialObjects: []Object{
233+
{ObjectAttrs: ObjectAttrs{BucketName: "some-bucket", Name: "files/txt/text-01.txt"}, Content: []byte("something")},
234+
}})
235+
if err != nil {
236+
t.Fatal(err)
237+
}
238+
239+
client := server.HTTPClient()
240+
requestURL := server.URL() + "/some-bucket/files/txt/text-01.txt"
241+
242+
serverURL, err := url.Parse(server.URL())
243+
if err != nil {
244+
t.Fatal(err)
245+
}
246+
server.publicHost = serverURL.Hostname() + ":" + serverURL.Port()[:2]
247+
248+
req, err := http.NewRequest(http.MethodGet, requestURL, nil)
249+
if err != nil {
250+
t.Fatal(err)
251+
}
252+
resp, err := client.Do(req)
253+
if err != nil {
254+
t.Fatal(err)
255+
}
256+
defer resp.Body.Close()
257+
258+
if resp.StatusCode != http.StatusNotFound {
259+
t.Errorf("wrong status returned\nwant %d\ngot %d", http.StatusNotFound, resp.StatusCode)
260+
}
261+
}
262+
205263
func testDownloadObject(t *testing.T, server *Server) {
206264
tests := []struct {
207265
name string

0 commit comments

Comments
 (0)