mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-10 01:22:14 +01:00
feat(chromium): deprecate --chromium-user-agent property, add userAgent form field
This commit is contained in:
2
Makefile
2
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) \
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user