File size: 3,173 Bytes
8baf122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package plugins

import (
	"fmt"
	"net/url"
	"os"
	"time"

	"github.com/valyala/fasthttp"
)

var (
	ErrPluginNotFound = fmt.Errorf("plugin not found")
)

// pluginDownloadClient is a fasthttp client with a larger read buffer to handle
// responses with large headers.
var pluginDownloadClient = &fasthttp.Client{
	ReadBufferSize: 64 * 1024, // 64KB, matches the bifrost HTTP server setting
}

// DownloadPlugin downloads a plugin from a URL and returns the local file path
func DownloadPlugin(pluginURL string, extension string) (string, error) {
	req := fasthttp.AcquireRequest()
	defer fasthttp.ReleaseRequest(req)
	response := fasthttp.AcquireResponse()
	defer fasthttp.ReleaseResponse(response)

	req.Header.SetMethod(fasthttp.MethodGet)
	req.Header.Set("Accept", "application/octet-stream")
	req.Header.Set("Accept-Encoding", "gzip")
	req.Header.Set("Accept-Language", "en-US,en;q=0.9")

	const maxRedirects = 5
	currentURL := pluginURL
	for i := 0; i <= maxRedirects; i++ {
		req.SetRequestURI(currentURL)
		if i > 0 {
			response.Reset()
		}

		if err := pluginDownloadClient.DoTimeout(req, response, 120*time.Second); err != nil {
			return "", err
		}

		statusCode := response.StatusCode()
		if statusCode == fasthttp.StatusOK {
			break
		}
		if statusCode >= 300 && statusCode < 400 {
			if i == maxRedirects {
				return "", fmt.Errorf("too many redirects downloading plugin")
			}
			location := string(response.Header.Peek("Location"))
			if location == "" {
				return "", fmt.Errorf("redirect response missing Location header: HTTP %d", statusCode)
			}
			loc, err := url.Parse(location)
			if err != nil {
				return "", fmt.Errorf("invalid Location header %q: %w", location, err)
			}
			base, err := url.Parse(currentURL)
			if err != nil {
				return "", fmt.Errorf("invalid request URL %q: %w", currentURL, err)
			}
			currentURL = base.ResolveReference(loc).String()
			continue
		}
		return "", fmt.Errorf("failed to download plugin: HTTP %d", statusCode)
	}

	// Decompress the response body if it was gzip/deflate compressed
	// BodyUncompressed handles both gzip and deflate encodings based on Content-Encoding header
	body, err := response.BodyUncompressed()
	if err != nil {
		return "", fmt.Errorf("failed to decompress response body: %w", err)
	}

	// Create a unique temporary file for the plugin
	tempFile, err := os.CreateTemp(os.TempDir(), "bifrost-plugin-*"+extension)
	if err != nil {
		return "", fmt.Errorf("failed to create temporary file: %w", err)
	}
	tempPath := tempFile.Name()

	// Write the downloaded body to the temporary file
	_, err = tempFile.Write(body)
	if err != nil {
		tempFile.Close()
		os.Remove(tempPath)
		return "", fmt.Errorf("failed to write plugin to temporary file: %w", err)
	}

	// Close the file
	err = tempFile.Close()
	if err != nil {
		os.Remove(tempPath)
		return "", fmt.Errorf("failed to close temporary file: %w", err)
	}

	// Set file permissions to be executable (for .so files)
	if extension == ".so" {
		err = os.Chmod(tempPath, 0755)
		if err != nil {
			os.Remove(tempPath)
			return "", fmt.Errorf("failed to set executable permissions on plugin: %w", err)
		}
	}

	return tempPath, nil
}