Skip to content

Commit

Permalink
httptransport: add "robots.txt" endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Feb 13, 2024
1 parent 7de8cea commit 201ed2b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
26 changes: 26 additions & 0 deletions httptransport/robotshandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package httptransport

import (
"net/http"
"strings"
"time"

"github.com/quay/clair/v4/cmd"
)

var startup = time.Now()

const robotstxt = "User-agent: *\nDisallow: /\n"

// RobotsHandler provides a "robots.txt" endpoint.
var robotsHandler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := w.Header()
h.Set("X-Content-Type-Options", "nosniff")
h.Set("Content-Type", "text/plain; charset=utf-8")
h.Set("Cache-Control", "no-store")
d := cmd.CommitDate
if d.IsZero() {
d = startup
}
http.ServeContent(w, r, "robots.txt", d, strings.NewReader(robotstxt))
})
26 changes: 26 additions & 0 deletions httptransport/robotshandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package httptransport

import (
"bytes"
"net/http"
"net/http/httptest"
"net/http/httputil"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestRobotsTXT(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/robots.txt", nil)
w := httptest.NewRecorder()
robotsHandler.ServeHTTP(w, r)
res, err := httputil.DumpResponse(w.Result(), false)
if err != nil {
t.Error(err)
}
t.Logf("response:\n%s", string(res))

if got, want := w.Body.Bytes(), []byte(robotstxt); !bytes.Equal(got, want) {
t.Error(cmp.Diff(string(got), string(want)))
}
}

0 comments on commit 201ed2b

Please sign in to comment.