summaryrefslogtreecommitdiff
path: root/internal/datafs/reader.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2024-06-16 17:55:29 -0400
committerGitHub <noreply@github.com>2024-06-16 21:55:29 +0000
commitdeeb86fad7864d6216c1b10f52b2ea4d31435123 (patch)
tree5c9988b8f81f9f6fa07b64a7d0cd715eadf76b3d /internal/datafs/reader.go
parent33168dbb4d2080a49899f56576582c10d3f02c3b (diff)
feat: Add ability to override 'type' query parameter name (#2115)
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'internal/datafs/reader.go')
-rw-r--r--internal/datafs/reader.go36
1 files changed, 30 insertions, 6 deletions
diff --git a/internal/datafs/reader.go b/internal/datafs/reader.go
index f1af07cf..a7c00d34 100644
--- a/internal/datafs/reader.go
+++ b/internal/datafs/reader.go
@@ -8,6 +8,7 @@ import (
"io/fs"
"net/http"
"net/url"
+ "os"
"runtime"
"strings"
@@ -16,6 +17,17 @@ import (
"github.com/hairyhenderson/gomplate/v4/internal/iohelpers"
)
+// typeOverrideParam gets the query parameter used to override the content type
+// used to parse a given datasource - use GOMPLATE_TYPE_PARAM to use a different
+// parameter name.
+func typeOverrideParam() string {
+ if v := os.Getenv("GOMPLATE_TYPE_PARAM"); v != "" {
+ return v
+ }
+
+ return "type"
+}
+
// DataSourceReader reads content from a datasource
type DataSourceReader interface {
// ReadSource reads the content of a datasource, given an alias and optional
@@ -91,7 +103,25 @@ func (d *dsReader) ReadSource(ctx context.Context, alias string, args ...string)
return fc.contentType, fc.b, nil
}
+func removeQueryParam(u *url.URL, key string) *url.URL {
+ q := u.Query()
+ q.Del(key)
+ u.RawQuery = q.Encode()
+ return u
+}
+
func (d *dsReader) readFileContent(ctx context.Context, u *url.URL, hdr http.Header) (*content, error) {
+ // possible type hint in the type query param. Contrary to spec, we allow
+ // unescaped '+' characters to make it simpler to provide types like
+ // "application/array+json"
+ overrideType := typeOverrideParam()
+ mimeType := u.Query().Get(overrideType)
+ mimeType = strings.ReplaceAll(mimeType, " ", "+")
+
+ // now that we have the hint, remove it from the URL - we can't have it
+ // leaking into the filesystem layer
+ u = removeQueryParam(u, overrideType)
+
fsys, err := FSysForPath(ctx, u.String())
if err != nil {
return nil, fmt.Errorf("fsys for path %v: %w", u, err)
@@ -120,12 +150,6 @@ func (d *dsReader) readFileContent(ctx context.Context, u *url.URL, hdr http.Hea
return nil, fmt.Errorf("stat (url: %q, name: %q): %w", u, fname, err)
}
- // possible type hint in the type query param. Contrary to spec, we allow
- // unescaped '+' characters to make it simpler to provide types like
- // "application/array+json"
- mimeType := u.Query().Get("type")
- mimeType = strings.ReplaceAll(mimeType, " ", "+")
-
if mimeType == "" {
mimeType = fsimpl.ContentType(fi)
}