fix mockArchiveServer and refactor isRemoteHTTPArchive

Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
pull/13225/head
Suleiman Dibirov 11 months ago
parent 5ee1d37c6e
commit a3d8cec9f2

@ -25,6 +25,7 @@ import (
"net/http/httptest" "net/http/httptest"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"syscall" "syscall"
"testing" "testing"
@ -67,8 +68,18 @@ func TestStripName(t *testing.T) {
} }
func mockArchiveServer(extensionToContentType map[string]string) *httptest.Server { func mockArchiveServer(extensionToContentType map[string]string) *httptest.Server {
// Extract and sort keys by length in descending order
extensions := make([]string, 0, len(extensionToContentType))
for ext := range extensionToContentType {
extensions = append(extensions, ext)
}
sort.Slice(extensions, func(i, j int) bool {
return len(extensions[i]) > len(extensions[j])
})
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for ext, contentType := range extensionToContentType { for _, ext := range extensions {
contentType := extensionToContentType[ext]
if strings.HasSuffix(r.URL.Path, ext) { if strings.HasSuffix(r.URL.Path, ext) {
w.Header().Add("Content-Type", contentType) w.Header().Add("Content-Type", contentType)
fmt.Fprintln(w, "test") fmt.Fprintln(w, "test")

@ -94,37 +94,51 @@ func isLocalReference(source string) bool {
// It works by checking whether the source looks like a URL and, if it does, running a // It works by checking whether the source looks like a URL and, if it does, running a
// HEAD operation to see if the remote resource is a file that we understand. // HEAD operation to see if the remote resource is a file that we understand.
func isRemoteHTTPArchive(source string) bool { func isRemoteHTTPArchive(source string) bool {
if strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://") { if !isHTTPURL(source) {
res, err := http.Head(source)
if err != nil {
// If we get an error at the network layer, we can't install it. So
// we return false.
return false return false
} }
// Next, we look for the content type or content disposition headers to see contentType, err := getRemoteContentType(source)
// if they have matching extractors. if err != nil {
contentType := res.Header.Get("content-type") return false
var foundSuffix string }
// Handle octet-stream specially by checking file extension
if contentType == "application/octet-stream" { if contentType == "application/octet-stream" {
foundSuffix = filepath.Ext(source) for suffix := range Extractors {
} else { if strings.HasSuffix(source, suffix) {
foundSuffix, _ = mediaTypeToExtension(contentType) return true
}
} }
if foundSuffix == "" {
// Media type not recognized
return false return false
} }
_, ok := Extractors[foundSuffix] // Check if we have an extractor for this media type
if suffix, ok := mediaTypeToExtension(contentType); ok {
return ok _, hasExtractor := Extractors[suffix]
return hasExtractor
} }
return false return false
} }
// isHTTPURL checks if the source is an HTTP or HTTPS URL
func isHTTPURL(source string) bool {
return strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://")
}
// getRemoteContentType performs a HEAD request and returns the content-type
func getRemoteContentType(url string) (string, error) {
res, err := http.Head(url)
if err != nil {
return "", err
}
defer res.Body.Close()
return res.Header.Get("content-type"), nil
}
// isPlugin checks if the directory contains a plugin.yaml file. // isPlugin checks if the directory contains a plugin.yaml file.
func isPlugin(dirname string) bool { func isPlugin(dirname string) bool {
_, err := os.Stat(filepath.Join(dirname, plugin.PluginFileName)) _, err := os.Stat(filepath.Join(dirname, plugin.PluginFileName))

Loading…
Cancel
Save