Updating caching to match lab
BUG=None
TEST=GO111MODULE=off GOPATH="/usr/local/google/home/jaquesc/go:/usr/local/google/home/jaquesc/workplace/chromiumos/src/platform/dev" go run *.go gcp-creds=/usr/local/google/home/jaquesc/sa.json --port 12345
Change-Id: I945ca86fd22788812ce3168aba75273dd00088fe
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/3348170
Reviewed-by: Sergey Frolov <sfrolov@google.com>
Commit-Queue: Jaques Clapauch <jaquesc@google.com>
Tested-by: Jaques Clapauch <jaquesc@google.com>
Auto-Submit: Jaques Clapauch <jaquesc@google.com>
diff --git a/src/chromiumos/prototytpe/cache/cmd/cacheserver/cache.go b/src/chromiumos/prototytpe/cache/cmd/cacheserver/cache.go
index 11a6f66..4927a52 100644
--- a/src/chromiumos/prototytpe/cache/cmd/cacheserver/cache.go
+++ b/src/chromiumos/prototytpe/cache/cmd/cacheserver/cache.go
@@ -9,7 +9,6 @@
"fmt"
"io"
"log"
- "net/url"
"os"
"path"
"strings"
@@ -105,21 +104,17 @@
}
// parseGSURL retrieves the bucket and object from a GS URL.
-// URL expectation is of the form: "gs://bucket/object"
+// URL expectation is of the form: "bucket/object"
// This method does not exists in the GS client, so creating bespoke.
func (c *Cache) parseGSURL(gsUrl string) (string, string, error) {
- if !strings.HasPrefix(gsUrl, "gs://") {
- return "", "", fmt.Errorf("gs url must begin with 'gs://', instead have, %s", gsUrl)
+ if strings.HasPrefix(gsUrl, "gs://") {
+ return "", "", fmt.Errorf("gs url must not have \"gs://\" prefix")
}
-
- u, err := url.Parse(gsUrl)
- if err != nil {
- return "", "", err
+ r := strings.SplitN(gsUrl, "/", 2)
+ if len(r) != 2 {
+ return "", "", fmt.Errorf("gs url must contain both a bucket and object")
}
-
- // Host corresponds to bucket
- // Path corresponds to object (though we need to remove prepending '/')
- return u.Host, u.Path[1:], nil
+ return r[0], r[1], nil
}
// Close cleans up the cache (deletes files).
diff --git a/src/chromiumos/prototytpe/cache/cmd/cacheserver/handlers.go b/src/chromiumos/prototytpe/cache/cmd/cacheserver/handlers.go
index dee9290..240c513 100644
--- a/src/chromiumos/prototytpe/cache/cmd/cacheserver/handlers.go
+++ b/src/chromiumos/prototytpe/cache/cmd/cacheserver/handlers.go
@@ -11,11 +11,16 @@
"net/http"
"os"
"os/signal"
+ "strings"
"golang.org/x/sys/unix"
)
-const sourceURLKey = "source_url"
+const (
+ sourceURLKey = "source_url"
+ downloadPrefix = "/download/"
+ downloadLocalPrefix = "/download-local/"
+)
type HttpHandlers struct {
cache *Cache
@@ -44,8 +49,8 @@
cache: cache,
}
- http.HandleFunc("/v1/cache/gs/", h.cacheGSHandler)
- http.HandleFunc("/v1/cache/local/", h.cacheLocalHandler)
+ http.HandleFunc(downloadPrefix, h.cacheGSHandler)
+ http.HandleFunc(downloadLocalPrefix, h.cacheLocalHandler)
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
return err
@@ -64,21 +69,24 @@
}
}
-// getCacgeGSHandler handles GET requests to GS cache
+// getCacheGSHandler handles GET requests to GS cache
func (h *HttpHandlers) getCacheGSHandler(w http.ResponseWriter, r *http.Request) {
log.Print("got GET request for GS file")
- gsPath := r.URL.Query().Get(sourceURLKey)
+ gsPath := strings.TrimPrefix(r.URL.EscapedPath(), downloadPrefix)
if gsPath == "" {
- http.Error(w, fmt.Sprintf("URL must have an option with %s field", sourceURLKey), http.StatusUnprocessableEntity)
+ http.Error(w, "URL must have a path to download", http.StatusUnprocessableEntity)
+ return
}
localPath, err := h.cache.Get(gsPath)
if err != nil {
http.Error(w, fmt.Sprintf("Unable to cache %s, %v", gsPath, err), http.StatusBadRequest)
+ return
}
fr, err := os.OpenFile(localPath, os.O_RDONLY, 0644)
if err != nil {
http.Error(w, fmt.Sprintf("Unable to open local cache %s, %v", localPath, err), http.StatusInternalServerError)
+ return
}
defer fr.Close()