From ae958516ea905318975bed3ecb7665c0fa63a4ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 2 Feb 2024 15:58:33 +0100 Subject: [PATCH] feat: enable resolve_root_symlink by default (#546) * feat: enable resolve_root_symlink by default * Update docs/config.md Co-authored-by: Francis Lavoie * fix init --------- Co-authored-by: Francis Lavoie --- README.md | 1 + caddy/caddy.go | 26 +++++++++++++++++++++----- caddy/php-server.go | 6 ++++-- docs/config.md | 2 +- docs/laravel.md | 5 +---- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ae172120..696e6ee3 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ You can also run command-line scripts with: * [Create **standalone**, self-executable PHP apps](https://frankenphp.dev/docs/embed/) * [Create static binaries](https://frankenphp.dev/docs/static/) * [Compile from sources](https://frankenphp.dev/docs/compile/) +* [Laravel integration](https://frankenphp.dev/docs/laravel/) * [Known issues](https://frankenphp.dev/docs/known-issues/) * [Demo app (Symfony) and benchmarks](https://github.com/dunglas/frankenphp-demo) * [Go library documentation](https://pkg.go.dev/github.com/dunglas/frankenphp) diff --git a/caddy/caddy.go b/caddy/caddy.go index 4957783f..aa01af76 100644 --- a/caddy/caddy.go +++ b/caddy/caddy.go @@ -203,7 +203,7 @@ type FrankenPHPModule struct { // SplitPath sets the substrings for splitting the URI into two parts. The first matching substring will be used to split the "path info" from the path. The first piece is suffixed with the matching substring and will be assumed as the actual resource (CGI script) name. The second piece will be set to PATH_INFO for the CGI script to use. Default: `.php`. SplitPath []string `json:"split_path,omitempty"` // ResolveRootSymlink enables resolving the `root` directory to its actual value by evaluating a symbolic link, if one exists. - ResolveRootSymlink bool `json:"resolve_root_symlink,omitempty"` + ResolveRootSymlink *bool `json:"resolve_root_symlink,omitempty"` // Env sets an extra environment variable to the given value. Can be specified more than once for multiple environment variables. Env map[string]string `json:"env,omitempty"` logger *zap.Logger @@ -225,8 +225,9 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error { if frankenphp.EmbeddedAppPath == "" { f.Root = "{http.vars.root}" } else { + rrs := false f.Root = filepath.Join(frankenphp.EmbeddedAppPath, defaultDocumentRoot) - f.ResolveRootSymlink = false + f.ResolveRootSymlink = &rrs } } else { if frankenphp.EmbeddedAppPath != "" && filepath.IsLocal(f.Root) { @@ -238,6 +239,11 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error { f.SplitPath = []string{".php"} } + if f.ResolveRootSymlink == nil { + rrs := true + f.ResolveRootSymlink = &rrs + } + return nil } @@ -257,7 +263,7 @@ func (f FrankenPHPModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ ca fr, err := frankenphp.NewRequestWithContext( r, - frankenphp.WithRequestDocumentRoot(documentRoot, f.ResolveRootSymlink), + frankenphp.WithRequestDocumentRoot(documentRoot, *f.ResolveRootSymlink), frankenphp.WithRequestSplitPath(f.SplitPath), frankenphp.WithRequestEnv(env), ) @@ -298,9 +304,18 @@ func (f *FrankenPHPModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { case "resolve_root_symlink": if d.NextArg() { + if v, err := strconv.ParseBool(d.Val()); err == nil { + f.ResolveRootSymlink = &v + + if d.NextArg() { + return d.ArgErr() + } + } + return d.ArgErr() } - f.ResolveRootSymlink = true + rrs := true + f.ResolveRootSymlink = &rrs } } } @@ -444,7 +459,8 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) if phpsrv.Root == "" { phpsrv.Root = filepath.Join(frankenphp.EmbeddedAppPath, defaultDocumentRoot) fsrv.Root = phpsrv.Root - phpsrv.ResolveRootSymlink = false + rrs := false + phpsrv.ResolveRootSymlink = &rrs } else if filepath.IsLocal(fsrv.Root) { phpsrv.Root = filepath.Join(frankenphp.EmbeddedAppPath, phpsrv.Root) fsrv.Root = phpsrv.Root diff --git a/caddy/php-server.go b/caddy/php-server.go index 70b47dbc..a66c46ad 100644 --- a/caddy/php-server.go +++ b/caddy/php-server.go @@ -125,9 +125,11 @@ func cmdPHPServer(fs caddycmd.Flags) (int, error) { extensions := []string{"php"} tryFiles := []string{"{http.request.uri.path}", "{http.request.uri.path}/" + indexFile, indexFile} + rrs := true phpHandler := FrankenPHPModule{ - Root: root, - SplitPath: extensions, + Root: root, + SplitPath: extensions, + ResolveRootSymlink: &rrs, } // route to redirect to canonical path if index PHP file diff --git a/docs/config.md b/docs/config.md index b374b0b8..5abbea36 100644 --- a/docs/config.md +++ b/docs/config.md @@ -122,7 +122,7 @@ The `php_server` and the `php` directives have the following options: php_server [] { root # Sets the root folder to the site. Default: `root` directive. split_path # Sets the substrings for splitting the URI into two parts. The first matching substring will be used to split the "path info" from the path. The first piece is suffixed with the matching substring and will be assumed as the actual resource (CGI script) name. The second piece will be set to PATH_INFO for the CGI script to use. Default: `.php` - resolve_root_symlink # Enables resolving the `root` directory to its actual value by evaluating a symbolic link, if one exists. + resolve_root_symlink false # Disables resolving the `root` directory to its actual value by evaluating a symbolic link, if one exists (enabled by default). env # Sets an extra environment variable to the given value. Can be specified more than once for multiple environment variables. } ``` diff --git a/docs/laravel.md b/docs/laravel.md index 180484f8..67ebfbde 100644 --- a/docs/laravel.md +++ b/docs/laravel.md @@ -32,10 +32,7 @@ Alternatively, you can run your Laravel projects with FrankenPHP from your local # Enable compression (optional) encode zstd br gzip # Execute PHP files in the current directory and serve assets - php_server { - # Required for the public/storage/ dir - resolve_root_symlink - } + php_server } ```