Skip to content

Commit 2edb3f5

Browse files
committed
Fix empty URL path component returned if request path is '/'
1 parent b2d26f4 commit 2edb3f5

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

server.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ func (s *Server) GetFile(fileUUID string) (r io.ReadSeeker, contentType string,
220220
}
221221

222222
func (s *Server) acceptFile(w http.ResponseWriter, r *http.Request) {
223-
splittenPath := strings.Split(r.URL.Path, "/")
224-
225223
if s.Conf.UploadAuth.Callback != nil && !s.Conf.UploadAuth.Callback(r) {
226224
w.WriteHeader(http.StatusForbidden)
227225
w.Write([]byte("403 forbidden"))
@@ -291,6 +289,10 @@ func (s *Server) acceptFile(w http.ResponseWriter, r *http.Request) {
291289
resURL.Scheme = "http"
292290
}
293291
resURL.Host = r.Host
292+
splittenPath := strings.Split(r.URL.Path, "/")
293+
if r.URL.Path == "/" {
294+
splittenPath = nil
295+
}
294296
splittenPath = append(splittenPath, fileUUID)
295297
resURL.Path = strings.Join(splittenPath, "/")
296298

@@ -352,8 +354,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
352354
w.Header().Set("Access-Control-Allow-Origin", s.Conf.AllowedOrigins)
353355
if r.Method == http.MethodPost {
354356
s.acceptFile(w, r)
355-
} else if
356-
r.Method == http.MethodGet ||
357+
} else if r.Method == http.MethodGet ||
357358
r.Method == http.MethodHead {
358359

359360
s.serveFile(w, r)

server_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ func TestBasicSubmit(t *testing.T) {
3939
}
4040
}
4141

42+
func TestBasicSubmit_NoPath(t *testing.T) {
43+
serv := initServ(filedrop.Default)
44+
ts := httptest.NewServer(serv)
45+
defer cleanServ(serv)
46+
defer ts.Close()
47+
c := ts.Client()
48+
49+
url := string(doPOST(t, c, ts.URL+"", "text/plain", strings.NewReader(file)))
50+
51+
t.Log("File URL:", url)
52+
53+
// First match is in http://
54+
if strings.Count(url, "//") > 1 {
55+
t.Error("URL path includes empty components")
56+
}
57+
58+
fileBody := doGET(t, c, url)
59+
if string(fileBody) != file {
60+
t.Log("Got different file!")
61+
sentHash := sha256.Sum256([]byte(file))
62+
t.Log("Sent:", hex.EncodeToString(sentHash[:]))
63+
recvHash := sha256.Sum256(fileBody)
64+
t.Log("Received:", hex.EncodeToString(recvHash[:]))
65+
t.FailNow()
66+
}
67+
}
68+
4269
func TestHeadAndGet(t *testing.T) {
4370
serv := initServ(filedrop.Default)
4471
ts := httptest.NewServer(serv)

0 commit comments

Comments
 (0)