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
d227bec0
Commit
d227bec0
authored
Oct 29, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move common function into existing file
parent
a3f0fff7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
92 additions
and
103 deletions
+92
-103
middleware/header.go
middleware/header.go
+0
-33
middleware/header_test.go
middleware/header_test.go
+0
-70
middleware/middleware.go
middleware/middleware.go
+28
-0
middleware/middleware_test.go
middleware/middleware_test.go
+64
-0
No files found.
middleware/header.go
deleted
100644 → 0
View file @
a3f0fff7
package
middleware
import
(
"net/http"
"time"
)
// currentTime returns time.Now() everytime it's called. It's used for mocking in tests.
var
currentTime
=
func
()
time
.
Time
{
return
time
.
Now
()
}
// SetLastModifiedHeader checks if the provided modTime is valid and if it is sets it
// as a Last-Modified header to the ResponseWriter. If the modTime is in the future
// the current time is used instead.
func
SetLastModifiedHeader
(
w
http
.
ResponseWriter
,
modTime
time
.
Time
)
{
if
modTime
.
IsZero
()
||
modTime
.
Equal
(
time
.
Unix
(
0
,
0
))
{
// the time does not appear to be valid. Don't put it in the response
return
}
// RFC 2616 - Section 14.29 - Last-Modified:
// An origin server MUST NOT send a Last-Modified date which is later than the
// server's time of message origination. In such cases, where the resource's last
// modification would indicate some time in the future, the server MUST replace
// that date with the message origination date.
now
:=
currentTime
()
if
modTime
.
After
(
now
)
{
modTime
=
now
}
w
.
Header
()
.
Set
(
"Last-Modified"
,
modTime
.
UTC
()
.
Format
(
http
.
TimeFormat
))
}
middleware/header_test.go
deleted
100644 → 0
View file @
a3f0fff7
package
middleware
import
(
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func
TestSetLastModified
(
t
*
testing
.
T
)
{
nowTime
:=
time
.
Now
()
// ovewrite the function to return reliable time
originalGetCurrentTimeFunc
:=
currentTime
currentTime
=
func
()
time
.
Time
{
return
nowTime
}
defer
func
()
{
currentTime
=
originalGetCurrentTimeFunc
}()
pastTime
:=
nowTime
.
Truncate
(
1
*
time
.
Hour
)
futureTime
:=
nowTime
.
Add
(
1
*
time
.
Hour
)
tests
:=
[]
struct
{
inputModTime
time
.
Time
expectedIsHeaderSet
bool
expectedLastModified
string
}{
{
inputModTime
:
pastTime
,
expectedIsHeaderSet
:
true
,
expectedLastModified
:
pastTime
.
UTC
()
.
Format
(
http
.
TimeFormat
),
},
{
inputModTime
:
nowTime
,
expectedIsHeaderSet
:
true
,
expectedLastModified
:
nowTime
.
UTC
()
.
Format
(
http
.
TimeFormat
),
},
{
inputModTime
:
futureTime
,
expectedIsHeaderSet
:
true
,
expectedLastModified
:
nowTime
.
UTC
()
.
Format
(
http
.
TimeFormat
),
},
{
inputModTime
:
time
.
Time
{},
expectedIsHeaderSet
:
false
,
},
}
for
i
,
test
:=
range
tests
{
responseRecorder
:=
httptest
.
NewRecorder
()
errorPrefix
:=
fmt
.
Sprintf
(
"Test [%d]: "
,
i
)
SetLastModifiedHeader
(
responseRecorder
,
test
.
inputModTime
)
actualLastModifiedHeader
:=
responseRecorder
.
Header
()
.
Get
(
"Last-Modified"
)
if
test
.
expectedIsHeaderSet
&&
actualLastModifiedHeader
==
""
{
t
.
Fatalf
(
errorPrefix
+
"Expected to find Last-Modified header, but found nothing"
)
}
if
!
test
.
expectedIsHeaderSet
&&
actualLastModifiedHeader
!=
""
{
t
.
Fatalf
(
errorPrefix
+
"Did not expect to find Last-Modified header, but found one [%s]."
,
actualLastModifiedHeader
)
}
if
test
.
expectedLastModified
!=
actualLastModifiedHeader
{
t
.
Errorf
(
errorPrefix
+
"Expected Last-Modified content [%s], found [%s}"
,
test
.
expectedLastModified
,
actualLastModifiedHeader
)
}
}
}
middleware/middleware.go
View file @
d227bec0
...
...
@@ -4,6 +4,7 @@ package middleware
import
(
"net/http"
"path"
"time"
)
type
(
...
...
@@ -78,3 +79,30 @@ func IndexFile(root http.FileSystem, fpath string, indexFiles []string) (string,
}
return
""
,
false
}
// SetLastModifiedHeader checks if the provided modTime is valid and if it is sets it
// as a Last-Modified header to the ResponseWriter. If the modTime is in the future
// the current time is used instead.
func
SetLastModifiedHeader
(
w
http
.
ResponseWriter
,
modTime
time
.
Time
)
{
if
modTime
.
IsZero
()
||
modTime
.
Equal
(
time
.
Unix
(
0
,
0
))
{
// the time does not appear to be valid. Don't put it in the response
return
}
// RFC 2616 - Section 14.29 - Last-Modified:
// An origin server MUST NOT send a Last-Modified date which is later than the
// server's time of message origination. In such cases, where the resource's last
// modification would indicate some time in the future, the server MUST replace
// that date with the message origination date.
now
:=
currentTime
()
if
modTime
.
After
(
now
)
{
modTime
=
now
}
w
.
Header
()
.
Set
(
"Last-Modified"
,
modTime
.
UTC
()
.
Format
(
http
.
TimeFormat
))
}
// currentTime returns time.Now() everytime it's called. It's used for mocking in tests.
var
currentTime
=
func
()
time
.
Time
{
return
time
.
Now
()
}
middleware/middleware_test.go
View file @
d227bec0
package
middleware
import
(
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func
TestIndexfile
(
t
*
testing
.
T
)
{
...
...
@@ -42,3 +45,64 @@ func TestIndexfile(t *testing.T) {
}
}
}
func
TestSetLastModified
(
t
*
testing
.
T
)
{
nowTime
:=
time
.
Now
()
// ovewrite the function to return reliable time
originalGetCurrentTimeFunc
:=
currentTime
currentTime
=
func
()
time
.
Time
{
return
nowTime
}
defer
func
()
{
currentTime
=
originalGetCurrentTimeFunc
}()
pastTime
:=
nowTime
.
Truncate
(
1
*
time
.
Hour
)
futureTime
:=
nowTime
.
Add
(
1
*
time
.
Hour
)
tests
:=
[]
struct
{
inputModTime
time
.
Time
expectedIsHeaderSet
bool
expectedLastModified
string
}{
{
inputModTime
:
pastTime
,
expectedIsHeaderSet
:
true
,
expectedLastModified
:
pastTime
.
UTC
()
.
Format
(
http
.
TimeFormat
),
},
{
inputModTime
:
nowTime
,
expectedIsHeaderSet
:
true
,
expectedLastModified
:
nowTime
.
UTC
()
.
Format
(
http
.
TimeFormat
),
},
{
inputModTime
:
futureTime
,
expectedIsHeaderSet
:
true
,
expectedLastModified
:
nowTime
.
UTC
()
.
Format
(
http
.
TimeFormat
),
},
{
inputModTime
:
time
.
Time
{},
expectedIsHeaderSet
:
false
,
},
}
for
i
,
test
:=
range
tests
{
responseRecorder
:=
httptest
.
NewRecorder
()
errorPrefix
:=
fmt
.
Sprintf
(
"Test [%d]: "
,
i
)
SetLastModifiedHeader
(
responseRecorder
,
test
.
inputModTime
)
actualLastModifiedHeader
:=
responseRecorder
.
Header
()
.
Get
(
"Last-Modified"
)
if
test
.
expectedIsHeaderSet
&&
actualLastModifiedHeader
==
""
{
t
.
Fatalf
(
errorPrefix
+
"Expected to find Last-Modified header, but found nothing"
)
}
if
!
test
.
expectedIsHeaderSet
&&
actualLastModifiedHeader
!=
""
{
t
.
Fatalf
(
errorPrefix
+
"Did not expect to find Last-Modified header, but found one [%s]."
,
actualLastModifiedHeader
)
}
if
test
.
expectedLastModified
!=
actualLastModifiedHeader
{
t
.
Errorf
(
errorPrefix
+
"Expected Last-Modified content [%s], found [%s}"
,
test
.
expectedLastModified
,
actualLastModifiedHeader
)
}
}
}
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