Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Last-Modified/If-Modified-Since headers #756

Merged
merged 3 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Bugfixes

* Properly handle modification headers (`If-Modified-Since`, `Last-Modified`) for static resources. [#756](https://github.com/elastic/package-registry/pull/756)

### Added

### Deprecated
Expand Down
93 changes: 93 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,99 @@ func TestStatics(t *testing.T) {

}

func TestStaticsModifiedTime(t *testing.T) {
const ifModifiedSinceHeader = "If-Modified-Since"
const lastModifiedHeader = "Last-Modified"

tests := []struct {
title string
endpoint string
headers map[string]string
code int
}{
{
title: "No cache headers",
endpoint: "/package/example/1.0.0/img/kibana-envoyproxy.jpg",
code: 200,
},
{
title: "Doesn't exist",
endpoint: "/package/none/1.0.0/img/foo.jpg",
code: 404,
},
{
title: "Cached entry",
endpoint: "/package/example/1.0.0/img/kibana-envoyproxy.jpg",
headers: map[string]string{
// Assuming that the file hasn't been modified in the future.
ifModifiedSinceHeader: time.Now().UTC().Format(http.TimeFormat),
},
code: 304,
},
{
title: "Old cached entry",
endpoint: "/package/example/1.0.0/img/kibana-envoyproxy.jpg",
headers: map[string]string{
ifModifiedSinceHeader: time.Time{}.Format(http.TimeFormat),
},
code: 200,
},

// From zip
{
title: "No cache headers from zip",
endpoint: "/package/example/1.0.1/img/kibana-envoyproxy.jpg",
code: 200,
},
{
title: "Cached entry from zip",
endpoint: "/package/example/1.0.1/img/kibana-envoyproxy.jpg",
headers: map[string]string{
// Assuming that the file hasn't been modified in the future.
ifModifiedSinceHeader: time.Now().UTC().Format(http.TimeFormat),
},
code: 304,
},
{
title: "Old cached entry from zip",
endpoint: "/package/example/1.0.1/img/kibana-envoyproxy.jpg",
headers: map[string]string{
ifModifiedSinceHeader: time.Time{}.Format(http.TimeFormat),
},
code: 200,
},
}

indexer := NewCombinedIndexer(
packages.NewFileSystemIndexer("./testdata/package"),
packages.NewZipFileSystemIndexer("./testdata/local-storage"),
)
err := indexer.Init(context.Background())
require.NoError(t, err)

router := mux.NewRouter()
router.HandleFunc(staticRouterPath, staticHandler(indexer, testCacheTime))

for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
req, err := http.NewRequest("GET", test.endpoint, nil)
require.NoError(t, err)

for k, v := range test.headers {
req.Header.Add(k, v)
}

recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, req)

assert.Equal(t, test.code, recorder.Code)
if test.code >= 0 && test.code < 400 {
assert.NotEmpty(t, recorder.Header().Values(lastModifiedHeader))
}
})
}
}

func TestZippedArtifacts(t *testing.T) {
indexer := packages.NewZipFileSystemIndexer("./testdata/local-storage")

Expand Down
12 changes: 9 additions & 3 deletions packages/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log"
"net/http"
"os"
"time"

"go.elastic.co/apm"

Expand Down Expand Up @@ -59,17 +58,24 @@ func ServeFile(w http.ResponseWriter, r *http.Request, p *Package, name string)
return
}

f, err := fs.Open(name)
stat, err := fs.Stat(name)
if os.IsNotExist(err) {
http.Error(w, "resource not found", http.StatusNotFound)
return
}
if err != nil {
log.Printf("failed to check file modification time (%s) in package: %v", name, err)
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}

f, err := fs.Open(name)
if err != nil {
log.Printf("failed to open file (%s) in package: %v", name, err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
defer f.Close()

http.ServeContent(w, r, name, time.Time{}, f)
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
http.ServeContent(w, r, name, stat.ModTime(), f)
}