request_options.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package frankenphp
  2. import (
  3. "path/filepath"
  4. "go.uber.org/zap"
  5. )
  6. // RequestOption instances allow to configure a FrankenPHP Request.
  7. type RequestOption func(h *FrankenPHPContext) error
  8. // WithRequestDocumentRoot sets the root directory of the PHP application.
  9. // if resolveSymlink is true, oath declared as root directory will be resolved
  10. // to its absolute value after the evaluation of any symbolic links.
  11. // Due to the nature of PHP opcache, root directory path is cached: when
  12. // using a symlinked directory as root this could generate errors when
  13. // symlink is changed without PHP being restarted; enabling this
  14. // directive will set $_SERVER['DOCUMENT_ROOT'] to the real directory path.
  15. func WithRequestDocumentRoot(documentRoot string, resolveSymlink bool) RequestOption {
  16. return func(o *FrankenPHPContext) error {
  17. // make sure file root is absolute
  18. root, err := filepath.Abs(documentRoot)
  19. if err != nil {
  20. return err
  21. }
  22. if resolveSymlink {
  23. if root, err = filepath.EvalSymlinks(root); err != nil {
  24. return err
  25. }
  26. }
  27. o.documentRoot = root
  28. return nil
  29. }
  30. }
  31. // The path in the URL will be split into two, with the first piece ending
  32. // with the value of SplitPath. The first piece will be assumed as the
  33. // actual resource (CGI script) name, and the second piece will be set to
  34. // PATH_INFO for the CGI script to use.
  35. //
  36. // Future enhancements should be careful to avoid CVE-2019-11043,
  37. // which can be mitigated with use of a try_files-like behavior
  38. // that 404s if the fastcgi path info is not found.
  39. func WithRequestSplitPath(splitPath []string) RequestOption {
  40. return func(o *FrankenPHPContext) error {
  41. o.splitPath = splitPath
  42. return nil
  43. }
  44. }
  45. // WithEnv set CGI-like environment variables that will be available in $_SERVER.
  46. // Values set with WithEnv always have priority over automatically populated values.
  47. func WithRequestEnv(env map[string]string) RequestOption {
  48. return func(o *FrankenPHPContext) error {
  49. o.env = env
  50. return nil
  51. }
  52. }
  53. // WithLogger sets the logger associated with the current request
  54. func WithRequestLogger(logger *zap.Logger) RequestOption {
  55. return func(o *FrankenPHPContext) error {
  56. o.logger = logger
  57. return nil
  58. }
  59. }