Skip to content

Commit

Permalink
Merge pull request #19 from tilezen/zerebubuth/use-http-compliant-tim…
Browse files Browse the repository at this point in the history
…estamp-format

Use HTTP compliant timestamp format string
  • Loading branch information
zerebubuth authored Feb 13, 2017
2 parents de848db + 28d3f3c commit 093bddb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
6 changes: 5 additions & 1 deletion tapalcatl_server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,11 @@ func MetatileHandler(p Parser, metatileSize int, mimeMap map[string]string, stor
headers := rw.Header()
headers.Set("Content-Type", parseResult.ContentType)
if lastMod := storageResp.LastModified; lastMod != nil {
lastModifiedFormatted := lastMod.Format(time.RFC1123Z)
// important! we must format times in an HTTP-compliant way, which
// apparently doesn't match any existing Go time format string, so the
// recommended way is to switch to UTC and use the format string that
// the net/http package exposes.
lastModifiedFormatted := lastMod.UTC().Format(http.TimeFormat)
headers.Set("Last-Modified", lastModifiedFormatted)
reqState.storageMetadata.hasLastModified = true
}
Expand Down
4 changes: 2 additions & 2 deletions tapalcatl_server/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func TestHandlerHit(t *testing.T) {
}

etag := "1234"
lastModifiedStr := "Thu, 17 Nov 2016 12:27:00 +0000"
lastModified, err := time.Parse(time.RFC1123Z, lastModifiedStr)
lastModifiedStr := "Thu, 17 Nov 2016 12:27:00 GMT"
lastModified, err := time.Parse(http.TimeFormat, lastModifiedStr)
if err != nil {
t.Fatalf("Couldn't parse time %s: %s", lastModifiedStr, err)
}
Expand Down
3 changes: 2 additions & 1 deletion tapalcatl_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (h *handlerConfig) Set(line string) error {
// try and parse a range of different date formats which are allowed by HTTP.
func parseHTTPDates(date string) (*time.Time, error) {
time_layouts := []string{
http.TimeFormat,
time.RFC1123, time.RFC1123Z,
time.RFC822, time.RFC822Z,
time.RFC850, time.ANSIC,
Expand All @@ -103,7 +104,7 @@ func parseHTTPDates(date string) (*time.Time, error) {
}

// give the error for our preferred format
_, err = time.Parse(time.RFC1123, date)
_, err = time.Parse(http.TimeFormat, date)
return nil, err
}

Expand Down
9 changes: 6 additions & 3 deletions tapalcatl_server/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/tilezen/tapalcatl"
"net/http"
"testing"
"time"
)
Expand Down Expand Up @@ -95,9 +96,11 @@ func TestS3Storage(t *testing.T) {
if lastMod == nil {
t.Fatalf("Missing last modified from storage")
}
// should be formatted in RFC 822 / 1123 format
expLastModStr := "Thu, 17 Nov 2016 12:27:00 +0000"
lastModStr := lastMod.Format(time.RFC1123Z)
// should be formatted in HTTP standard way, which means the GMT on the end
// is intentional, and shouldn't be UTC or +0000 or Z, despite all of those
// being better choices.
expLastModStr := "Thu, 17 Nov 2016 12:27:00 GMT"
lastModStr := lastMod.UTC().Format(http.TimeFormat)
if expLastModStr != lastModStr {
t.Fatalf("Expected Last-Modified to be %#v, but got %#v", expLastModStr, lastModStr)
}
Expand Down

0 comments on commit 093bddb

Please sign in to comment.