From 4525cb38e43fb39177fd375e127ee2a4dcae65f5 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Sun, 21 Nov 2021 14:11:39 +0100 Subject: [PATCH] feat(chromium): deprecate --chromium-user-agent property, add userAgent form field --- Makefile | 2 -- pkg/modules/chromium/chromium.go | 17 ++++++++++++++++- pkg/modules/chromium/chromium_test.go | 6 ++++++ pkg/modules/chromium/routes.go | 3 +++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 0a70edb5..f64ff6a1 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,6 @@ API_WRITE_TIMEOUT=30s API_ROOT_PATH=/ API_TRACE_HEADER=Gotenberg-Trace API_DISABLE_HEALTH_CHECK_LOGGING=false -CHROMIUM_USER_AGENT= CHROMIUM_INCOGNITO=false CHROMIUM_IGNORE_CERTIFICATE_ERRORS=false CHROMIUM_DISABLE_WEB_SECURITY=false @@ -77,7 +76,6 @@ run: ## Start a Gotenberg container --api-root-path=$(API_ROOT_PATH) \ --api-trace-header=$(API_TRACE_HEADER) \ --api-disable-health-check-logging=$(API_DISABLE_HEALTH_CHECK_LOGGING) \ - --chromium-user-agent=$(CHROMIUM_USER_AGENT) \ --chromium-incognito=$(CHROMIUM_INCOGNITO) \ --chromium-ignore-certificate-errors=$(CHROMIUM_IGNORE_CERTIFICATE_ERRORS) \ --chromium-disable-web-security=$(CHROMIUM_DISABLE_WEB_SECURITY) \ diff --git a/pkg/modules/chromium/chromium.go b/pkg/modules/chromium/chromium.go index f4c1e40d..2ee35e74 100644 --- a/pkg/modules/chromium/chromium.go +++ b/pkg/modules/chromium/chromium.go @@ -61,6 +61,10 @@ type Chromium struct { // Options are the available options for converting HTML document to PDF. type Options struct { + // UserAgent overrides the default User-Agent header. + // Optional. + UserAgent string + // WaitDelay is the duration to wait when loading an HTML document before // converting it to PDF. // Optional. @@ -143,6 +147,7 @@ type Options struct { // DefaultOptions returns the default values for Options. func DefaultOptions() Options { return Options{ + UserAgent: "", WaitDelay: 0, WaitWindowStatus: "", ExtraHTTPHeaders: nil, @@ -194,6 +199,11 @@ func (mod Chromium) Descriptor() gotenberg.ModuleDescriptor { fs.String("chromium-deny-list", "^file:///[^tmp].*", "Set the denied URLs for Chromium using a regular expression") fs.Bool("chromium-disable-routes", false, "Disable the routes") + err := fs.MarkDeprecated("chromium-user-agent", "use the userAgent form field instead") + if err != nil { + panic(fmt.Errorf("create deprecated flags for chromium module: %v", err)) + } + return fs }(), New: func() gotenberg.Module { return new(Chromium) }, @@ -302,7 +312,8 @@ func (mod Chromium) PDF(ctx context.Context, logger *zap.Logger, URL, outputPath chromedp.UserDataDir(userProfileDirPath), ) - if mod.userAgent != "" { + if mod.userAgent != "" && options.UserAgent == "" { + // Deprecated. args = append(args, chromedp.UserAgent(mod.userAgent)) } @@ -328,6 +339,10 @@ func (mod Chromium) PDF(ctx context.Context, logger *zap.Logger, URL, outputPath args = append(args, chromedp.ProxyServer(mod.proxyServer)) } + if options.UserAgent != "" { + args = append(args, chromedp.UserAgent(options.UserAgent)) + } + allocatorCtx, cancel := chromedp.NewExecAllocator(ctx, args...) defer cancel() diff --git a/pkg/modules/chromium/chromium_test.go b/pkg/modules/chromium/chromium_test.go index 4161096e..f63bd0f1 100644 --- a/pkg/modules/chromium/chromium_test.go +++ b/pkg/modules/chromium/chromium_test.go @@ -250,6 +250,12 @@ func TestChromium_PDF(t *testing.T) { denyList: regexp.MustCompile("file:///tests/*"), expectErr: true, }, + { + URL: "file:///tests/test/testdata/chromium/html/sample4/index.html", + options: Options{ + UserAgent: "foo", + }, + }, { URL: "file:///tests/test/testdata/chromium/html/sample4/index.html", options: Options{ diff --git a/pkg/modules/chromium/routes.go b/pkg/modules/chromium/routes.go index 5f4b557c..97058438 100644 --- a/pkg/modules/chromium/routes.go +++ b/pkg/modules/chromium/routes.go @@ -26,6 +26,7 @@ func FormDataChromiumPDFOptions(ctx *api.Context) (*api.FormData, Options) { defaultOptions := DefaultOptions() var ( + userAgent string waitDelay time.Duration waitWindowStatus string extraHTTPHeaders map[string]string @@ -38,6 +39,7 @@ func FormDataChromiumPDFOptions(ctx *api.Context) (*api.FormData, Options) { ) form := ctx.FormData(). + String("userAgent", &userAgent, defaultOptions.UserAgent). Duration("waitDelay", &waitDelay, defaultOptions.WaitDelay). String("waitWindowStatus", &waitWindowStatus, defaultOptions.WaitWindowStatus). Custom("extraHttpHeaders", func(value string) error { @@ -69,6 +71,7 @@ func FormDataChromiumPDFOptions(ctx *api.Context) (*api.FormData, Options) { Bool("preferCssPageSize", &preferCSSPageSize, defaultOptions.PreferCSSPageSize) options := Options{ + UserAgent: userAgent, WaitDelay: waitDelay, WaitWindowStatus: waitWindowStatus, ExtraHTTPHeaders: extraHTTPHeaders,