frankenphp.go 17 KB

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