request_options.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // WithRequestResolvedDocumentRoot is similar to WithRequestDocumentRoot
  32. // but doesn't do any checks or resolving on the path to improve performance.
  33. func WithRequestResolvedDocumentRoot(documentRoot string) RequestOption {
  34. return func(o *FrankenPHPContext) error {
  35. o.documentRoot = documentRoot
  36. return nil
  37. }
  38. }
  39. // WithRequestSplitPath contains a list of split path strings.
  40. //
  41. // The path in the URL will be split into two, with the first piece ending
  42. // with the value of splitPath. The first piece will be assumed as the
  43. // actual resource (CGI script) name, and the second piece will be set to
  44. // PATH_INFO for the CGI script to use.
  45. //
  46. // Future enhancements should be careful to avoid CVE-2019-11043,
  47. // which can be mitigated with use of a try_files-like behavior
  48. // that 404s if the FastCGI path info is not found.
  49. func WithRequestSplitPath(splitPath []string) RequestOption {
  50. return func(o *FrankenPHPContext) error {
  51. o.splitPath = splitPath
  52. return nil
  53. }
  54. }
  55. type PreparedEnv = map[string]string
  56. func PrepareEnv(env map[string]string) PreparedEnv {
  57. preparedEnv := make(PreparedEnv, len(env))
  58. for k, v := range env {
  59. preparedEnv[k+"\x00"] = v
  60. }
  61. return preparedEnv
  62. }
  63. // WithRequestEnv set CGI-like environment variables that will be available in $_SERVER.
  64. // Values set with WithEnv always have priority over automatically populated values.
  65. func WithRequestEnv(env map[string]string) RequestOption {
  66. return WithRequestPreparedEnv(PrepareEnv(env))
  67. }
  68. func WithRequestPreparedEnv(env PreparedEnv) RequestOption {
  69. return func(o *FrankenPHPContext) error {
  70. o.env = env
  71. return nil
  72. }
  73. }
  74. // WithRequestLogger sets the logger associated with the current request
  75. func WithRequestLogger(logger *zap.Logger) RequestOption {
  76. return func(o *FrankenPHPContext) error {
  77. o.logger = logger
  78. return nil
  79. }
  80. }