request_options.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. type PreparedEnv = map[string]string
  46. func PrepareEnv(env map[string]string) PreparedEnv {
  47. preparedEnv := make(PreparedEnv, len(env))
  48. for k, v := range env {
  49. preparedEnv[k+"\x00"] = v
  50. }
  51. return preparedEnv
  52. }
  53. // WithRequestEnv set CGI-like environment variables that will be available in $_SERVER.
  54. // Values set with WithEnv always have priority over automatically populated values.
  55. func WithRequestEnv(env map[string]string) RequestOption {
  56. return WithRequestPreparedEnv(PrepareEnv(env))
  57. }
  58. func WithRequestPreparedEnv(env PreparedEnv) RequestOption {
  59. return func(o *FrankenPHPContext) error {
  60. o.env = env
  61. return nil
  62. }
  63. }
  64. // WithRequestLogger sets the logger associated with the current request
  65. func WithRequestLogger(logger *zap.Logger) RequestOption {
  66. return func(o *FrankenPHPContext) error {
  67. o.logger = logger
  68. return nil
  69. }
  70. }