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
a14fce0b
Commit
a14fce0b
authored
Apr 03, 2016
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #707 from jupiter/caching-headers
Add Etag header
parents
25b567b3
93d982a5
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 @
a14fce0b
package
middleware
package
middleware
import
(
import
(
"fmt"
"net/http"
"net/http"
"os"
"os"
"path"
"path"
...
@@ -128,6 +129,10 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
...
@@ -128,6 +129,10 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
return
http
.
StatusNotFound
,
nil
return
http
.
StatusNotFound
,
nil
}
}
// Add ETag header
e
:=
fmt
.
Sprintf
(
`W/"%x-%x"`
,
d
.
ModTime
()
.
Unix
(),
d
.
Size
())
w
.
Header
()
.
Set
(
"ETag"
,
e
)
// Note: Errors generated by ServeContent are written immediately
// Note: Errors generated by ServeContent are written immediately
// to the response. This usually only happens if seeking fails (rare).
// to the response. This usually only happens if seeking fails (rare).
http
.
ServeContent
(
w
,
r
,
d
.
Name
(),
d
.
ModTime
(),
f
)
http
.
ServeContent
(
w
,
r
,
d
.
Name
(),
d
.
ModTime
(),
f
)
...
...
middleware/fileserver_test.go
View file @
a14fce0b
...
@@ -8,6 +8,7 @@ import (
...
@@ -8,6 +8,7 @@ import (
"path/filepath"
"path/filepath"
"strings"
"strings"
"testing"
"testing"
"time"
)
)
var
testDir
=
filepath
.
Join
(
os
.
TempDir
(),
"caddy_testdir"
)
var
testDir
=
filepath
.
Join
(
os
.
TempDir
(),
"caddy_testdir"
)
...
@@ -44,6 +45,7 @@ func TestServeHTTP(t *testing.T) {
...
@@ -44,6 +45,7 @@ func TestServeHTTP(t *testing.T) {
expectedStatus
int
expectedStatus
int
expectedBodyContent
string
expectedBodyContent
string
expectedEtag
string
}{
}{
// Test 0 - access without any path
// Test 0 - access without any path
{
{
...
@@ -60,12 +62,14 @@ func TestServeHTTP(t *testing.T) {
...
@@ -60,12 +62,14 @@ func TestServeHTTP(t *testing.T) {
url
:
"https://foo/file1.html"
,
url
:
"https://foo/file1.html"
,
expectedStatus
:
http
.
StatusOK
,
expectedStatus
:
http
.
StatusOK
,
expectedBodyContent
:
testFiles
[
"file1.html"
],
expectedBodyContent
:
testFiles
[
"file1.html"
],
expectedEtag
:
`W/"1e240-13"`
,
},
},
// Test 3 - access folder with index file with trailing slash
// Test 3 - access folder with index file with trailing slash
{
{
url
:
"https://foo/dirwithindex/"
,
url
:
"https://foo/dirwithindex/"
,
expectedStatus
:
http
.
StatusOK
,
expectedStatus
:
http
.
StatusOK
,
expectedBodyContent
:
testFiles
[
filepath
.
Join
(
"dirwithindex"
,
"index.html"
)],
expectedBodyContent
:
testFiles
[
filepath
.
Join
(
"dirwithindex"
,
"index.html"
)],
expectedEtag
:
`W/"1e240-20"`
,
},
},
// Test 4 - access folder with index file without trailing slash
// Test 4 - access folder with index file without trailing slash
{
{
...
@@ -105,6 +109,7 @@ func TestServeHTTP(t *testing.T) {
...
@@ -105,6 +109,7 @@ func TestServeHTTP(t *testing.T) {
url
:
"https://foo/dirwithindex/index.html"
,
url
:
"https://foo/dirwithindex/index.html"
,
expectedStatus
:
http
.
StatusOK
,
expectedStatus
:
http
.
StatusOK
,
expectedBodyContent
:
testFiles
[
filepath
.
Join
(
"dirwithindex"
,
"index.html"
)],
expectedBodyContent
:
testFiles
[
filepath
.
Join
(
"dirwithindex"
,
"index.html"
)],
expectedEtag
:
`W/"1e240-20"`
,
},
},
// Test 11 - send a request with query params
// Test 11 - send a request with query params
{
{
...
@@ -143,6 +148,7 @@ func TestServeHTTP(t *testing.T) {
...
@@ -143,6 +148,7 @@ func TestServeHTTP(t *testing.T) {
responseRecorder
:=
httptest
.
NewRecorder
()
responseRecorder
:=
httptest
.
NewRecorder
()
request
,
err
:=
http
.
NewRequest
(
"GET"
,
test
.
url
,
strings
.
NewReader
(
""
))
request
,
err
:=
http
.
NewRequest
(
"GET"
,
test
.
url
,
strings
.
NewReader
(
""
))
status
,
err
:=
fileserver
.
ServeHTTP
(
responseRecorder
,
request
)
status
,
err
:=
fileserver
.
ServeHTTP
(
responseRecorder
,
request
)
etag
:=
responseRecorder
.
Header
()
.
Get
(
"Etag"
)
// check if error matches expectations
// check if error matches expectations
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -154,6 +160,11 @@ func TestServeHTTP(t *testing.T) {
...
@@ -154,6 +160,11 @@ func TestServeHTTP(t *testing.T) {
t
.
Errorf
(
getTestPrefix
(
i
)
+
"Expected status %d, found %d"
,
test
.
expectedStatus
,
status
)
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
// check body content
if
!
strings
.
Contains
(
responseRecorder
.
Body
.
String
(),
test
.
expectedBodyContent
)
{
if
!
strings
.
Contains
(
responseRecorder
.
Body
.
String
(),
test
.
expectedBodyContent
)
{
t
.
Errorf
(
getTestPrefix
(
i
)
+
"Expected body to contain %q, found %q"
,
test
.
expectedBodyContent
,
responseRecorder
.
Body
.
String
())
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) {
...
@@ -173,6 +184,8 @@ func beforeServeHTTPTest(t *testing.T) {
}
}
}
}
fixedTime
:=
time
.
Unix
(
123456
,
0
)
for
relFile
,
fileContent
:=
range
testFiles
{
for
relFile
,
fileContent
:=
range
testFiles
{
absFile
:=
filepath
.
Join
(
testDir
,
relFile
)
absFile
:=
filepath
.
Join
(
testDir
,
relFile
)
...
@@ -197,6 +210,12 @@ func beforeServeHTTPTest(t *testing.T) {
...
@@ -197,6 +210,12 @@ func beforeServeHTTPTest(t *testing.T) {
return
return
}
}
f
.
Close
()
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