mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-17 04:32:15 +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_ROOT_PATH=/
|
||||||
API_TRACE_HEADER=Gotenberg-Trace
|
API_TRACE_HEADER=Gotenberg-Trace
|
||||||
API_DISABLE_HEALTH_CHECK_LOGGING=false
|
API_DISABLE_HEALTH_CHECK_LOGGING=false
|
||||||
CHROMIUM_USER_AGENT=
|
|
||||||
CHROMIUM_INCOGNITO=false
|
CHROMIUM_INCOGNITO=false
|
||||||
CHROMIUM_IGNORE_CERTIFICATE_ERRORS=false
|
CHROMIUM_IGNORE_CERTIFICATE_ERRORS=false
|
||||||
CHROMIUM_DISABLE_WEB_SECURITY=false
|
CHROMIUM_DISABLE_WEB_SECURITY=false
|
||||||
@@ -77,7 +76,6 @@ run: ## Start a Gotenberg container
|
|||||||
--api-root-path=$(API_ROOT_PATH) \
|
--api-root-path=$(API_ROOT_PATH) \
|
||||||
--api-trace-header=$(API_TRACE_HEADER) \
|
--api-trace-header=$(API_TRACE_HEADER) \
|
||||||
--api-disable-health-check-logging=$(API_DISABLE_HEALTH_CHECK_LOGGING) \
|
--api-disable-health-check-logging=$(API_DISABLE_HEALTH_CHECK_LOGGING) \
|
||||||
--chromium-user-agent=$(CHROMIUM_USER_AGENT) \
|
|
||||||
--chromium-incognito=$(CHROMIUM_INCOGNITO) \
|
--chromium-incognito=$(CHROMIUM_INCOGNITO) \
|
||||||
--chromium-ignore-certificate-errors=$(CHROMIUM_IGNORE_CERTIFICATE_ERRORS) \
|
--chromium-ignore-certificate-errors=$(CHROMIUM_IGNORE_CERTIFICATE_ERRORS) \
|
||||||
--chromium-disable-web-security=$(CHROMIUM_DISABLE_WEB_SECURITY) \
|
--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.
|
// Options are the available options for converting HTML document to PDF.
|
||||||
type Options struct {
|
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
|
// WaitDelay is the duration to wait when loading an HTML document before
|
||||||
// converting it to PDF.
|
// converting it to PDF.
|
||||||
// Optional.
|
// Optional.
|
||||||
@@ -143,6 +147,7 @@ type Options struct {
|
|||||||
// DefaultOptions returns the default values for Options.
|
// DefaultOptions returns the default values for Options.
|
||||||
func DefaultOptions() Options {
|
func DefaultOptions() Options {
|
||||||
return Options{
|
return Options{
|
||||||
|
UserAgent: "",
|
||||||
WaitDelay: 0,
|
WaitDelay: 0,
|
||||||
WaitWindowStatus: "",
|
WaitWindowStatus: "",
|
||||||
ExtraHTTPHeaders: nil,
|
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.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")
|
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
|
return fs
|
||||||
}(),
|
}(),
|
||||||
New: func() gotenberg.Module { return new(Chromium) },
|
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),
|
chromedp.UserDataDir(userProfileDirPath),
|
||||||
)
|
)
|
||||||
|
|
||||||
if mod.userAgent != "" {
|
if mod.userAgent != "" && options.UserAgent == "" {
|
||||||
|
// Deprecated.
|
||||||
args = append(args, chromedp.UserAgent(mod.userAgent))
|
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))
|
args = append(args, chromedp.ProxyServer(mod.proxyServer))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if options.UserAgent != "" {
|
||||||
|
args = append(args, chromedp.UserAgent(options.UserAgent))
|
||||||
|
}
|
||||||
|
|
||||||
allocatorCtx, cancel := chromedp.NewExecAllocator(ctx, args...)
|
allocatorCtx, cancel := chromedp.NewExecAllocator(ctx, args...)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
|||||||
@@ -250,6 +250,12 @@ func TestChromium_PDF(t *testing.T) {
|
|||||||
denyList: regexp.MustCompile("file:///tests/*"),
|
denyList: regexp.MustCompile("file:///tests/*"),
|
||||||
expectErr: true,
|
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",
|
URL: "file:///tests/test/testdata/chromium/html/sample4/index.html",
|
||||||
options: Options{
|
options: Options{
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ func FormDataChromiumPDFOptions(ctx *api.Context) (*api.FormData, Options) {
|
|||||||
defaultOptions := DefaultOptions()
|
defaultOptions := DefaultOptions()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
userAgent string
|
||||||
waitDelay time.Duration
|
waitDelay time.Duration
|
||||||
waitWindowStatus string
|
waitWindowStatus string
|
||||||
extraHTTPHeaders map[string]string
|
extraHTTPHeaders map[string]string
|
||||||
@@ -38,6 +39,7 @@ func FormDataChromiumPDFOptions(ctx *api.Context) (*api.FormData, Options) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
form := ctx.FormData().
|
form := ctx.FormData().
|
||||||
|
String("userAgent", &userAgent, defaultOptions.UserAgent).
|
||||||
Duration("waitDelay", &waitDelay, defaultOptions.WaitDelay).
|
Duration("waitDelay", &waitDelay, defaultOptions.WaitDelay).
|
||||||
String("waitWindowStatus", &waitWindowStatus, defaultOptions.WaitWindowStatus).
|
String("waitWindowStatus", &waitWindowStatus, defaultOptions.WaitWindowStatus).
|
||||||
Custom("extraHttpHeaders", func(value string) error {
|
Custom("extraHttpHeaders", func(value string) error {
|
||||||
@@ -69,6 +71,7 @@ func FormDataChromiumPDFOptions(ctx *api.Context) (*api.FormData, Options) {
|
|||||||
Bool("preferCssPageSize", &preferCSSPageSize, defaultOptions.PreferCSSPageSize)
|
Bool("preferCssPageSize", &preferCSSPageSize, defaultOptions.PreferCSSPageSize)
|
||||||
|
|
||||||
options := Options{
|
options := Options{
|
||||||
|
UserAgent: userAgent,
|
||||||
WaitDelay: waitDelay,
|
WaitDelay: waitDelay,
|
||||||
WaitWindowStatus: waitWindowStatus,
|
WaitWindowStatus: waitWindowStatus,
|
||||||
ExtraHTTPHeaders: extraHTTPHeaders,
|
ExtraHTTPHeaders: extraHTTPHeaders,
|
||||||
|
|||||||
Reference in New Issue
Block a user