Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
3441cdef
Commit
3441cdef
authored
Apr 01, 2016
by
Pieter Raubenheimer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Etag header
parent
8a2f2f8d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
0 deletions
+24
-0
middleware/fileserver.go
middleware/fileserver.go
+5
-0
middleware/fileserver_test.go
middleware/fileserver_test.go
+19
-0
No files found.
middleware/fileserver.go
View file @
3441cdef
package
middleware
import
(
"fmt"
"net/http"
"os"
"path"
...
...
@@ -128,6 +129,10 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
return
http
.
StatusNotFound
,
nil
}
// Add ETag header
e
:=
fmt
.
Sprintf
(
"
\"
%x-%x
\"
"
,
d
.
ModTime
()
.
Unix
(),
d
.
Size
())
w
.
Header
()
.
Set
(
"Etag"
,
e
)
// Note: Errors generated by ServeContent are written immediately
// to the response. This usually only happens if seeking fails (rare).
http
.
ServeContent
(
w
,
r
,
d
.
Name
(),
d
.
ModTime
(),
f
)
...
...
middleware/fileserver_test.go
View file @
3441cdef
...
...
@@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"
)
var
testDir
=
filepath
.
Join
(
os
.
TempDir
(),
"caddy_testdir"
)
...
...
@@ -44,6 +45,7 @@ func TestServeHTTP(t *testing.T) {
expectedStatus
int
expectedBodyContent
string
expectedEtag
string
}{
// Test 0 - access without any path
{
...
...
@@ -60,12 +62,14 @@ func TestServeHTTP(t *testing.T) {
url
:
"https://foo/file1.html"
,
expectedStatus
:
http
.
StatusOK
,
expectedBodyContent
:
testFiles
[
"file1.html"
],
expectedEtag
:
"
\"
1e240-13
\"
"
,
},
// Test 3 - access folder with index file with trailing slash
{
url
:
"https://foo/dirwithindex/"
,
expectedStatus
:
http
.
StatusOK
,
expectedBodyContent
:
testFiles
[
filepath
.
Join
(
"dirwithindex"
,
"index.html"
)],
expectedEtag
:
"
\"
1e240-20
\"
"
,
},
// Test 4 - access folder with index file without trailing slash
{
...
...
@@ -105,6 +109,7 @@ func TestServeHTTP(t *testing.T) {
url
:
"https://foo/dirwithindex/index.html"
,
expectedStatus
:
http
.
StatusOK
,
expectedBodyContent
:
testFiles
[
filepath
.
Join
(
"dirwithindex"
,
"index.html"
)],
expectedEtag
:
"
\"
1e240-20
\"
"
,
},
// Test 11 - send a request with query params
{
...
...
@@ -143,6 +148,7 @@ func TestServeHTTP(t *testing.T) {
responseRecorder
:=
httptest
.
NewRecorder
()
request
,
err
:=
http
.
NewRequest
(
"GET"
,
test
.
url
,
strings
.
NewReader
(
""
))
status
,
err
:=
fileserver
.
ServeHTTP
(
responseRecorder
,
request
)
etag
:=
responseRecorder
.
Header
()
.
Get
(
"Etag"
)
// check if error matches expectations
if
err
!=
nil
{
...
...
@@ -154,6 +160,11 @@ func TestServeHTTP(t *testing.T) {
t
.
Errorf
(
getTestPrefix
(
i
)
+
"Expected status %d, found %d"
,
test
.
expectedStatus
,
status
)
}
// check etag
if
test
.
expectedEtag
!=
etag
{
t
.
Errorf
(
getTestPrefix
(
i
)
+
"Expected Etag header %d, found %d"
,
test
.
expectedEtag
,
etag
)
}
// check body content
if
!
strings
.
Contains
(
responseRecorder
.
Body
.
String
(),
test
.
expectedBodyContent
)
{
t
.
Errorf
(
getTestPrefix
(
i
)
+
"Expected body to contain %q, found %q"
,
test
.
expectedBodyContent
,
responseRecorder
.
Body
.
String
())
...
...
@@ -173,6 +184,8 @@ func beforeServeHTTPTest(t *testing.T) {
}
}
fixedTime
:=
time
.
Unix
(
123456
,
0
)
for
relFile
,
fileContent
:=
range
testFiles
{
absFile
:=
filepath
.
Join
(
testDir
,
relFile
)
...
...
@@ -197,6 +210,12 @@ func beforeServeHTTPTest(t *testing.T) {
return
}
f
.
Close
()
// and set the last modified time
err
=
os
.
Chtimes
(
absFile
,
fixedTime
,
fixedTime
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to set file time to %s. Error was: %v"
,
fixedTime
,
err
)
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment