frankenphp.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. // Package frankenphp embeds PHP in Go projects and provides a SAPI for net/http.
  2. //
  3. // This is the core of the [FrankenPHP app server], and can be used in any Go program.
  4. //
  5. // [FrankenPHP app server]: https://frankenphp.dev
  6. package frankenphp
  7. //go:generate rm -Rf C-Thread-Pool/
  8. //go:generate git clone --branch=frankenphp --depth=1 git@github.com:dunglas/C-Thread-Pool.git
  9. //go:generate rm -Rf C-Thread-Pool/.git C-Thread-Pool/.circleci C-Thread-Pool/docs C-Thread-Pool/tests C-Thread-Pool/example.c
  10. // Use PHP includes corresponding to your PHP installation by running:
  11. //
  12. // export CGO_CFLAGS=$(php-config --includes)
  13. // export CGO_LDFLAGS=$(php-config --ldflags --libs)
  14. //
  15. // We also set these flags for hardening: https://github.com/docker-library/php/blob/master/8.2/bookworm/zts/Dockerfile#L57-L59
  16. // #cgo darwin pkg-config: libxml-2.0 sqlite3
  17. // #cgo CFLAGS: -Wall -Werror -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
  18. // #cgo CFLAGS: -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib
  19. // #cgo CFLAGS: -DTHREAD_NAME=frankenphp
  20. // #cgo linux CFLAGS: -D_GNU_SOURCE
  21. // #cgo CPPFLAGS: -fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
  22. // #cgo darwin LDFLAGS: -L/opt/homebrew/opt/libiconv/lib -liconv
  23. // #cgo linux LDFLAGS: -Wl,-O1 -lresolv
  24. // #cgo LDFLAGS: -pie -L/usr/local/lib -L/usr/lib -lphp -ldl -lm -lutil
  25. // #include <stdlib.h>
  26. // #include <stdint.h>
  27. // #include <php_variables.h>
  28. // #include <zend_llist.h>
  29. // #include <SAPI.h>
  30. // #include "frankenphp.h"
  31. import "C"
  32. import (
  33. "bytes"
  34. "context"
  35. "errors"
  36. "fmt"
  37. "io"
  38. "net/http"
  39. "runtime"
  40. "runtime/cgo"
  41. "strconv"
  42. "strings"
  43. "sync"
  44. "unsafe"
  45. "go.uber.org/zap"
  46. // debug on Linux
  47. //_ "github.com/ianlancetaylor/cgosymbolizer"
  48. )
  49. type key int
  50. var contextKey key
  51. var (
  52. InvalidRequestError = errors.New("not a FrankenPHP request")
  53. AlreaydStartedError = errors.New("FrankenPHP is already started")
  54. InvalidPHPVersionError = errors.New("FrankenPHP is only compatible with PHP 8.2+")
  55. ZendSignalsError = errors.New("Zend Signals are enabled, recompile PHP with --disable-zend-signals")
  56. NotEnoughThreads = errors.New("the number of threads must be superior to the number of workers")
  57. MainThreadCreationError = errors.New("error creating the main thread")
  58. RequestContextCreationError = errors.New("error during request context creation")
  59. RequestStartupError = errors.New("error during PHP request startup")
  60. ScriptExecutionError = errors.New("error during PHP script execution")
  61. requestChan chan *http.Request
  62. done chan struct{}
  63. shutdownWG sync.WaitGroup
  64. loggerMu sync.RWMutex
  65. logger *zap.Logger
  66. )
  67. type syslogLevel int
  68. const (
  69. emerg syslogLevel = iota // system is unusable
  70. alert // action must be taken immediately
  71. crit // critical conditions
  72. err // error conditions
  73. warning // warning conditions
  74. notice // normal but significant condition
  75. info // informational
  76. debug // debug-level messages
  77. )
  78. func (l syslogLevel) String() string {
  79. switch l {
  80. case emerg:
  81. return "emerg"
  82. case alert:
  83. return "alert"
  84. case crit:
  85. return "crit"
  86. case err:
  87. return "err"
  88. case warning:
  89. return "warning"
  90. case notice:
  91. return "notice"
  92. case debug:
  93. return "debug"
  94. default:
  95. return "info"
  96. }
  97. }
  98. // FrankenPHPContext provides contextual information about the Request to handle.
  99. type FrankenPHPContext struct {
  100. // The root directory of the PHP application.
  101. DocumentRoot string
  102. // The path in the URL will be split into two, with the first piece ending
  103. // with the value of SplitPath. The first piece will be assumed as the
  104. // actual resource (CGI script) name, and the second piece will be set to
  105. // PATH_INFO for the CGI script to use.
  106. //
  107. // Future enhancements should be careful to avoid CVE-2019-11043,
  108. // which can be mitigated with use of a try_files-like behavior
  109. // that 404s if the fastcgi path info is not found.
  110. SplitPath []string
  111. // Path declared as root directory will be resolved to its absolute value
  112. // after the evaluation of any symbolic links.
  113. // Due to the nature of PHP opcache, root directory path is cached: when
  114. // using a symlinked directory as root this could generate errors when
  115. // symlink is changed without php-fpm being restarted; enabling this
  116. // directive will set $_SERVER['DOCUMENT_ROOT'] to the real directory path.
  117. ResolveRootSymlink bool
  118. // CGI-like environment variables that will be available in $_SERVER.
  119. // This map is populated automatically, exisiting key are never replaced.
  120. Env map[string]string
  121. // The logger associated with the current request
  122. Logger *zap.Logger
  123. populated bool
  124. authPassword string
  125. // Whether the request is already closed by us
  126. closed sync.Once
  127. responseWriter http.ResponseWriter
  128. done chan interface{}
  129. currentWorkerRequest cgo.Handle
  130. }
  131. func clientHasClosed(r *http.Request) bool {
  132. select {
  133. case <-r.Context().Done():
  134. return true
  135. default:
  136. return false
  137. }
  138. }
  139. // NewRequestWithContext creates a new FrankenPHP request context.
  140. func NewRequestWithContext(r *http.Request, documentRoot string, l *zap.Logger) *http.Request {
  141. if l == nil {
  142. l = getLogger()
  143. }
  144. ctx := context.WithValue(r.Context(), contextKey, &FrankenPHPContext{
  145. DocumentRoot: documentRoot,
  146. SplitPath: []string{".php"},
  147. Env: make(map[string]string),
  148. Logger: l,
  149. })
  150. return r.WithContext(ctx)
  151. }
  152. // FromContext extracts the FrankenPHPContext from a context.
  153. func FromContext(ctx context.Context) (fctx *FrankenPHPContext, ok bool) {
  154. fctx, ok = ctx.Value(contextKey).(*FrankenPHPContext)
  155. return
  156. }
  157. type PHPVersion struct {
  158. MajorVersion int
  159. MinorVersion int
  160. ReleaseVersion int
  161. ExtraVersion string
  162. Version string
  163. VersionID int
  164. }
  165. type PHPConfig struct {
  166. Version PHPVersion
  167. ZTS bool
  168. ZendSignals bool
  169. ZendMaxExecutionTimers bool
  170. }
  171. // Version returns infos about the PHP version.
  172. func Version() PHPVersion {
  173. cVersion := C.frankenphp_get_version()
  174. return PHPVersion{
  175. int(cVersion.major_version),
  176. int(cVersion.minor_version),
  177. int(cVersion.release_version),
  178. C.GoString(cVersion.extra_version),
  179. C.GoString(cVersion.version),
  180. int(cVersion.version_id),
  181. }
  182. }
  183. func Config() PHPConfig {
  184. cConfig := C.frankenphp_get_config()
  185. return PHPConfig{
  186. Version: Version(),
  187. ZTS: bool(cConfig.zts),
  188. ZendSignals: bool(cConfig.zend_signals),
  189. ZendMaxExecutionTimers: bool(cConfig.zend_max_execution_timers),
  190. }
  191. }
  192. // Init starts the PHP runtime and the configured workers.
  193. func Init(options ...Option) error {
  194. if requestChan != nil {
  195. return AlreaydStartedError
  196. }
  197. opt := &opt{}
  198. for _, o := range options {
  199. if err := o(opt); err != nil {
  200. return err
  201. }
  202. }
  203. if opt.logger == nil {
  204. l, err := zap.NewDevelopment()
  205. if err != nil {
  206. return err
  207. }
  208. loggerMu.Lock()
  209. logger = l
  210. loggerMu.Unlock()
  211. } else {
  212. loggerMu.Lock()
  213. logger = opt.logger
  214. loggerMu.Unlock()
  215. }
  216. maxProcs := runtime.GOMAXPROCS(0)
  217. var numWorkers int
  218. for i, w := range opt.workers {
  219. if w.num <= 0 {
  220. // https://github.com/dunglas/frankenphp/issues/126
  221. opt.workers[i].num = maxProcs * 2
  222. }
  223. numWorkers += opt.workers[i].num
  224. }
  225. if opt.numThreads <= 0 {
  226. if numWorkers >= maxProcs {
  227. // Start at least as many threads as workers, and keep a free thread to handle requests in non-worker mode
  228. opt.numThreads = numWorkers + 1
  229. } else {
  230. opt.numThreads = maxProcs
  231. }
  232. } else if opt.numThreads <= numWorkers {
  233. return NotEnoughThreads
  234. }
  235. config := Config()
  236. if config.Version.MajorVersion < 8 || (config.Version.MajorVersion == 8 && config.Version.MinorVersion < 2) {
  237. return InvalidPHPVersionError
  238. }
  239. if config.ZTS {
  240. if !config.ZendMaxExecutionTimers && runtime.GOOS == "linux" {
  241. logger.Warn(`Zend Max Execution Timers are not enabled, timeouts (e.g. "max_execution_time") are disabled, recompile PHP with the "--enable-zend-max-execution-timers" configuration option to fix this issue`)
  242. }
  243. } else {
  244. opt.numThreads = 1
  245. logger.Warn(`ZTS is not enabled, only 1 thread will be available, recompile PHP using the "--enable-zts" configuration option or performance will be degraded`)
  246. }
  247. shutdownWG.Add(1)
  248. done = make(chan struct{})
  249. requestChan = make(chan *http.Request)
  250. if C.frankenphp_init(C.int(opt.numThreads)) != 0 {
  251. return MainThreadCreationError
  252. }
  253. if err := initWorkers(opt.workers); err != nil {
  254. return err
  255. }
  256. logger.Debug("FrankenPHP started")
  257. return nil
  258. }
  259. // Shutdown stops the workers and the PHP runtime.
  260. func Shutdown() {
  261. stopWorkers()
  262. close(done)
  263. shutdownWG.Wait()
  264. requestChan = nil
  265. // Always reset the WaitGroup to ensure we're in a clean state
  266. workersReadyWG = sync.WaitGroup{}
  267. logger.Debug("FrankenPHP shut down")
  268. }
  269. //export go_shutdown
  270. func go_shutdown() {
  271. shutdownWG.Done()
  272. }
  273. func getLogger() *zap.Logger {
  274. loggerMu.RLock()
  275. defer loggerMu.RUnlock()
  276. return logger
  277. }
  278. func updateServerContext(request *http.Request, create bool, mrh C.uintptr_t) error {
  279. fc, ok := FromContext(request.Context())
  280. if !ok {
  281. return InvalidRequestError
  282. }
  283. var cAuthUser, cAuthPassword *C.char
  284. if fc.authPassword != "" {
  285. cAuthPassword = C.CString(fc.authPassword)
  286. }
  287. if authUser := fc.Env["REMOTE_USER"]; authUser != "" {
  288. cAuthUser = C.CString(authUser)
  289. }
  290. cMethod := C.CString(request.Method)
  291. cQueryString := C.CString(request.URL.RawQuery)
  292. contentLengthStr := request.Header.Get("Content-Length")
  293. contentLength := 0
  294. if contentLengthStr != "" {
  295. var err error
  296. contentLength, err = strconv.Atoi(contentLengthStr)
  297. if err != nil {
  298. return fmt.Errorf("invalid Content-Length header: %w", err)
  299. }
  300. }
  301. contentType := request.Header.Get("Content-Type")
  302. var cContentType *C.char
  303. if contentType != "" {
  304. cContentType = C.CString(contentType)
  305. }
  306. var cPathTranslated *C.char
  307. if pathTranslated := fc.Env["PATH_TRANSLATED"]; pathTranslated != "" {
  308. cPathTranslated = C.CString(pathTranslated)
  309. }
  310. cRequestUri := C.CString(request.URL.RequestURI())
  311. var rh cgo.Handle
  312. if fc.responseWriter == nil {
  313. mrh = C.uintptr_t(cgo.NewHandle(request))
  314. } else {
  315. rh = cgo.NewHandle(request)
  316. }
  317. ret := C.frankenphp_update_server_context(
  318. C.bool(create),
  319. C.uintptr_t(rh),
  320. mrh,
  321. cMethod,
  322. cQueryString,
  323. C.zend_long(contentLength),
  324. cPathTranslated,
  325. cRequestUri,
  326. cContentType,
  327. cAuthUser,
  328. cAuthPassword,
  329. C.int(request.ProtoMajor*1000+request.ProtoMinor),
  330. )
  331. if ret > 0 {
  332. return RequestContextCreationError
  333. }
  334. return nil
  335. }
  336. // ServeHTTP executes a PHP script according to the given context.
  337. func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error {
  338. shutdownWG.Add(1)
  339. defer shutdownWG.Done()
  340. fc, ok := FromContext(request.Context())
  341. if !ok {
  342. return InvalidRequestError
  343. }
  344. if err := populateEnv(request); err != nil {
  345. return err
  346. }
  347. fc.responseWriter = responseWriter
  348. fc.done = make(chan interface{})
  349. rc := requestChan
  350. // Detect if a worker is available to handle this request
  351. if nil == fc.responseWriter {
  352. fc.Env["FRANKENPHP_WORKER"] = "1"
  353. } else if v, ok := workersRequestChans.Load(fc.Env["SCRIPT_FILENAME"]); ok {
  354. fc.Env["FRANKENPHP_WORKER"] = "1"
  355. rc = v.(chan *http.Request)
  356. }
  357. select {
  358. case <-done:
  359. case rc <- request:
  360. <-fc.done
  361. }
  362. return nil
  363. }
  364. //export go_fetch_request
  365. func go_fetch_request() C.uintptr_t {
  366. select {
  367. case <-done:
  368. return 0
  369. case r := <-requestChan:
  370. return C.uintptr_t(cgo.NewHandle(r))
  371. }
  372. }
  373. func maybeCloseContext(fc *FrankenPHPContext) {
  374. fc.closed.Do(func() {
  375. close(fc.done)
  376. })
  377. }
  378. //export go_execute_script
  379. func go_execute_script(rh unsafe.Pointer) {
  380. handle := cgo.Handle(rh)
  381. defer handle.Delete()
  382. request := handle.Value().(*http.Request)
  383. fc, ok := FromContext(request.Context())
  384. if !ok {
  385. panic(InvalidRequestError)
  386. }
  387. defer maybeCloseContext(fc)
  388. if err := updateServerContext(request, true, 0); err != nil {
  389. panic(err)
  390. }
  391. if C.frankenphp_request_startup() < 0 {
  392. panic(RequestStartupError)
  393. }
  394. cFileName := C.CString(fc.Env["SCRIPT_FILENAME"])
  395. defer C.free(unsafe.Pointer(cFileName))
  396. if C.frankenphp_execute_script(cFileName) < 0 {
  397. panic(ScriptExecutionError)
  398. }
  399. C.frankenphp_clean_server_context()
  400. C.frankenphp_request_shutdown()
  401. }
  402. //export go_ub_write
  403. func go_ub_write(rh C.uintptr_t, cString *C.char, length C.int) (C.size_t, C.bool) {
  404. r := cgo.Handle(rh).Value().(*http.Request)
  405. fc, _ := FromContext(r.Context())
  406. var writer io.Writer
  407. if fc.responseWriter == nil {
  408. var b bytes.Buffer
  409. // log the output of the worker
  410. writer = &b
  411. } else {
  412. writer = fc.responseWriter
  413. }
  414. i, _ := writer.Write([]byte(C.GoStringN(cString, length)))
  415. if fc.responseWriter == nil {
  416. fc.Logger.Info(writer.(*bytes.Buffer).String())
  417. }
  418. return C.size_t(i), C.bool(clientHasClosed(r))
  419. }
  420. //export go_register_variables
  421. func go_register_variables(rh C.uintptr_t, trackVarsArray *C.zval) {
  422. var env map[string]string
  423. r := cgo.Handle(rh).Value().(*http.Request)
  424. env = r.Context().Value(contextKey).(*FrankenPHPContext).Env
  425. le := len(env) * 2
  426. cArr := (**C.char)(C.malloc(C.size_t(le) * C.size_t(unsafe.Sizeof((*C.char)(nil)))))
  427. defer C.free(unsafe.Pointer(cArr))
  428. variables := unsafe.Slice(cArr, le)
  429. var i int
  430. for k, v := range env {
  431. variables[i] = C.CString(k)
  432. i++
  433. variables[i] = C.CString(v)
  434. i++
  435. }
  436. C.frankenphp_register_bulk_variables(cArr, C.size_t(le), trackVarsArray)
  437. for _, v := range variables {
  438. C.free(unsafe.Pointer(v))
  439. }
  440. }
  441. func addHeader(fc *FrankenPHPContext, cString *C.char, length C.int) {
  442. parts := strings.SplitN(C.GoStringN(cString, length), ": ", 2)
  443. if len(parts) != 2 {
  444. fc.Logger.Debug("invalid header", zap.String("header", parts[0]))
  445. return
  446. }
  447. fc.responseWriter.Header().Add(parts[0], parts[1])
  448. }
  449. //export go_write_headers
  450. func go_write_headers(rh C.uintptr_t, status C.int, headers *C.zend_llist) {
  451. r := cgo.Handle(rh).Value().(*http.Request)
  452. fc := r.Context().Value(contextKey).(*FrankenPHPContext)
  453. if fc.responseWriter == nil {
  454. return
  455. }
  456. current := headers.head
  457. for current != nil {
  458. h := (*C.sapi_header_struct)(unsafe.Pointer(&(current.data)))
  459. addHeader(fc, h.header, C.int(h.header_len))
  460. current = current.next
  461. }
  462. fc.responseWriter.WriteHeader(int(status))
  463. if status >= 100 && status < 200 {
  464. // Clear headers, it's not automatically done by ResponseWriter.WriteHeader() for 1xx responses
  465. h := fc.responseWriter.Header()
  466. for k := range h {
  467. delete(h, k)
  468. }
  469. }
  470. }
  471. //export go_sapi_flush
  472. func go_sapi_flush(rh C.uintptr_t) bool {
  473. r := cgo.Handle(rh).Value().(*http.Request)
  474. fc := r.Context().Value(contextKey).(*FrankenPHPContext)
  475. if fc.responseWriter == nil || clientHasClosed(r) {
  476. return true
  477. }
  478. if r.ProtoMajor == 1 {
  479. if _, err := r.Body.Read(nil); err != nil {
  480. // Don't flush until the whole body has been read to prevent https://github.com/golang/go/issues/15527
  481. return false
  482. }
  483. }
  484. if err := http.NewResponseController(fc.responseWriter).Flush(); err != nil {
  485. fc.Logger.Error("the current responseWriter is not a flusher", zap.Error(err))
  486. }
  487. return false
  488. }
  489. //export go_read_post
  490. func go_read_post(rh C.uintptr_t, cBuf *C.char, countBytes C.size_t) (readBytes C.size_t) {
  491. r := cgo.Handle(rh).Value().(*http.Request)
  492. p := unsafe.Slice((*byte)(unsafe.Pointer(cBuf)), countBytes)
  493. var err error
  494. for readBytes < countBytes && err == nil {
  495. var n int
  496. n, err = r.Body.Read(p[readBytes:])
  497. readBytes += C.size_t(n)
  498. }
  499. if err != nil && err != io.EOF {
  500. // invalid Read on closed Body may happen because of https://github.com/golang/go/issues/15527
  501. fc, _ := FromContext(r.Context())
  502. fc.Logger.Error("error while reading the request body", zap.Error(err))
  503. }
  504. return
  505. }
  506. //export go_read_cookies
  507. func go_read_cookies(rh C.uintptr_t) *C.char {
  508. r := cgo.Handle(rh).Value().(*http.Request)
  509. cookies := r.Cookies()
  510. if len(cookies) == 0 {
  511. return nil
  512. }
  513. cookieString := make([]string, len(cookies))
  514. for _, cookie := range r.Cookies() {
  515. cookieString = append(cookieString, cookie.String())
  516. }
  517. cCookie := C.CString(strings.Join(cookieString, "; "))
  518. // freed in frankenphp_request_shutdown()
  519. return cCookie
  520. }
  521. //export go_log
  522. func go_log(message *C.char, level C.int) {
  523. l := getLogger()
  524. m := C.GoString(message)
  525. var le syslogLevel
  526. if level < C.int(emerg) || level > C.int(debug) {
  527. le = info
  528. } else {
  529. le = syslogLevel(level)
  530. }
  531. switch le {
  532. case emerg, alert, crit, err:
  533. l.Error(m, zap.Stringer("syslog_level", syslogLevel(level)))
  534. case warning:
  535. l.Warn(m, zap.Stringer("syslog_level", syslogLevel(level)))
  536. case debug:
  537. l.Debug(m, zap.Stringer("syslog_level", syslogLevel(level)))
  538. default:
  539. l.Info(m, zap.Stringer("syslog_level", syslogLevel(level)))
  540. }
  541. }
  542. // ExecuteScriptCLI executes the PHP script passed as parameter.
  543. // It returns the exit status code of the script.
  544. func ExecuteScriptCLI(script string, args []string) int {
  545. cScript := C.CString(script)
  546. defer C.free(unsafe.Pointer(cScript))
  547. argc := C.int(len(args))
  548. argv := make([]*C.char, argc)
  549. for i, arg := range args {
  550. argv[i] = C.CString(arg)
  551. }
  552. return int(C.frankenphp_execute_script_cli(cScript, argc, (**C.char)(unsafe.Pointer(&argv[0]))))
  553. }