Skip to content

Commit d308dd3

Browse files
committed
remove err condtion after filepath.Join statement
1 parent 36aa408 commit d308dd3

File tree

2 files changed

+1
-169
lines changed

2 files changed

+1
-169
lines changed

README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,11 @@ type spaHandler struct {
251251
// file located at the index path on the SPA handler will be served. This
252252
// is suitable behavior for serving an SPA (single page application).
253253
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
254-
var err error
255254
// Join internally call path.Clean to prevent directory traversal
256255
path = filepath.Join(h.staticPath, path)
257-
if err != nil {
258-
// if we failed to get the absolute path respond with a 400 bad request
259-
// and stop
260-
http.Error(w, err.Error(), http.StatusBadRequest)
261-
return
262-
}
263256

264257
// check whether a file exists at the given path
265-
_, err = os.Stat(path)
258+
_, err := os.Stat(path)
266259
if os.IsNotExist(err) {
267260
// file does not exist, serve index.html
268261
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))

mux_httpserver_test.go

Lines changed: 0 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -8,95 +8,9 @@ import (
88
"io/ioutil"
99
"net/http"
1010
"net/http/httptest"
11-
"os"
12-
"path/filepath"
13-
"runtime"
14-
"strings"
1511
"testing"
1612
)
1713

18-
// spaHandler implements the http.Handler interface, so we can use it
19-
// to respond to HTTP requests. The path to the static directory and
20-
// path to the index file within that static directory are used to
21-
// serve the SPA in the given static directory.
22-
type spaHandler struct {
23-
staticPath string
24-
indexPath string
25-
}
26-
27-
// FilepathAbsServeHTTP inspects the URL path to locate a file within the static dir
28-
// on the SPA handler. If a file is found, it will be served. If not, the
29-
// file located at the index path on the SPA handler will be served. This
30-
// is suitable behavior for serving an SPA (single page application).
31-
// This is a negative test case where `filepath.Abs` will return path value like `D:\`
32-
// if our route is `/`. As per docs: Abs returns an absolute representation of path.
33-
// If the path is not absolute it will be joined with the current working directory to turn
34-
// it into an absolute path. The absolute path name for a given file is not guaranteed to
35-
// be unique. Abs calls Clean on the result.
36-
func (h spaHandler) FilepathAbsServeHTTP(w http.ResponseWriter, r *http.Request) {
37-
// get the absolute path to prevent directory traversal
38-
path, err := filepath.Abs(r.URL.Path)
39-
if err != nil {
40-
// if we failed to get the absolute path respond with a 400 bad request
41-
// and stop
42-
http.Error(w, err.Error(), http.StatusBadRequest)
43-
return
44-
}
45-
46-
// prepend the path with the path to the static directory
47-
path = filepath.Join(h.staticPath, path)
48-
49-
// check whether a file exists at the given path
50-
_, err = os.Stat(path)
51-
52-
if os.IsNotExist(err) {
53-
// file does not exist, serve index.html
54-
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
55-
return
56-
} else if err != nil {
57-
// if we got an error (that wasn't that the file doesn't exist) stating the
58-
// file, return a 500 internal server error and stop
59-
http.Error(w, err.Error(), http.StatusInternalServerError)
60-
return
61-
}
62-
63-
// otherwise, use http.FileServer to serve the static dir
64-
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
65-
}
66-
67-
// ServeHTTP inspects the URL path to locate a file within the static dir
68-
// on the SPA handler. If a file is found, it will be served. If not, the
69-
// file located at the index path on the SPA handler will be served. This
70-
// is suitable behavior for serving an SPA (single page application).
71-
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
72-
var err error
73-
// internally calls path.Clean path to prevent directory traversal
74-
path := filepath.Join(h.staticPath, r.URL.Path)
75-
if err != nil {
76-
// if we failed to get the absolute path respond with a 400 bad request
77-
// and stop
78-
http.Error(w, err.Error(), http.StatusBadRequest)
79-
return
80-
}
81-
82-
// check whether a file exists at the given path
83-
_, err = os.Stat(path)
84-
85-
if os.IsNotExist(err) {
86-
// file does not exist, serve index.html
87-
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
88-
return
89-
} else if err != nil {
90-
// if we got an error (that wasn't that the file doesn't exist) stating the
91-
// file, return a 500 internal server error and stop
92-
http.Error(w, err.Error(), http.StatusInternalServerError)
93-
return
94-
}
95-
96-
// otherwise, use http.FileServer to serve the static dir
97-
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
98-
}
99-
10014
func TestSchemeMatchers(t *testing.T) {
10115
router := NewRouter()
10216
router.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
@@ -134,78 +48,3 @@ func TestSchemeMatchers(t *testing.T) {
13448
assertResponseBody(t, s, "hello https world")
13549
})
13650
}
137-
138-
func TestServeHttpFilepathAbs(t *testing.T) {
139-
// create a diretory name `build`
140-
os.Mkdir("build", 0700)
141-
142-
// create a file `index.html` and write below content
143-
htmlContent := []byte(`<html><head><title>hello</title></head><body>world</body></html>`)
144-
err := ioutil.WriteFile("./build/index.html", htmlContent, 0700)
145-
if err != nil {
146-
t.Fatal(err)
147-
}
148-
149-
// make new request
150-
req, err := http.NewRequest("GET", "/", nil)
151-
if err != nil {
152-
t.Fatal(err)
153-
}
154-
155-
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
156-
rr := httptest.NewRecorder()
157-
spa := spaHandler{staticPath: "./build", indexPath: "index.html"}
158-
spa.FilepathAbsServeHTTP(rr, req)
159-
160-
status := rr.Code
161-
if runtime.GOOS != "windows" && status != http.StatusOK {
162-
t.Errorf("handler returned wrong status code: got %v want %v",
163-
status, http.StatusOK)
164-
} else if runtime.GOOS == "windows" && rr.Code != http.StatusInternalServerError {
165-
t.Errorf("handler returned wrong status code in case of windows: got %v want %v",
166-
status, http.StatusOK)
167-
}
168-
169-
// Check the response body is what we expect.
170-
if runtime.GOOS != "windows" && rr.Body.String() != string(htmlContent) {
171-
t.Errorf("handler returned unexpected body: got %v want %v",
172-
rr.Body.String(), string(htmlContent))
173-
} else if runtime.GOOS == "windows" && !strings.Contains(rr.Body.String(), "syntax is incorrect.") {
174-
t.Errorf("handler returned unexpected body in case of windows: got %v want %v",
175-
rr.Body.String(), string(htmlContent))
176-
}
177-
}
178-
179-
func TestServeHttpFilepathJoin(t *testing.T) {
180-
// create a diretory name `build`
181-
os.Mkdir("build", 0700)
182-
183-
// create a file `index.html` and write below content
184-
htmlContent := []byte(`<html><head><title>hello</title></head><body>world</body></html>`)
185-
err := ioutil.WriteFile("./build/index.html", htmlContent, 0700)
186-
if err != nil {
187-
t.Fatal(err)
188-
}
189-
190-
// make new request
191-
req, err := http.NewRequest("GET", "/", nil)
192-
if err != nil {
193-
t.Fatal(err)
194-
}
195-
196-
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
197-
rr := httptest.NewRecorder()
198-
spa := spaHandler{staticPath: "./build", indexPath: "index.html"}
199-
spa.ServeHTTP(rr, req)
200-
201-
if status := rr.Code; status != http.StatusOK {
202-
t.Errorf("handler returned wrong status code: got %v want %v",
203-
status, http.StatusOK)
204-
}
205-
206-
// Check the response body is what we expect.
207-
if rr.Body.String() != string(htmlContent) {
208-
t.Errorf("handler returned unexpected body: got %v want %v",
209-
rr.Body.String(), string(htmlContent))
210-
}
211-
}

0 commit comments

Comments
 (0)