Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cgi.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ func go_register_variables(threadIndex C.uintptr_t, trackVarsArray *C.zval) {
thread := phpThreads[threadIndex]
fc := thread.getRequestContext()

addKnownVariablesToServer(thread, fc, trackVarsArray)
addHeadersToServer(fc, trackVarsArray)
if fc.request != nil {
addKnownVariablesToServer(thread, fc, trackVarsArray)
addHeadersToServer(fc, trackVarsArray)
}

// The Prepared Environment is registered last and can overwrite any previous values
addPreparedEnvToServer(fc, trackVarsArray)
Expand Down Expand Up @@ -280,6 +282,10 @@ func go_update_request_info(threadIndex C.uintptr_t, info *C.sapi_request_info)
fc := thread.getRequestContext()
request := fc.request

if request == nil {
return C.bool(fc.worker != nil)
}

authUser, authPassword, ok := request.BasicAuth()
if ok {
if authPassword != "" {
Expand Down
17 changes: 13 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ func fromContext(ctx context.Context) (fctx *frankenPHPContext, ok bool) {
return
}

// NewRequestWithContext creates a new FrankenPHP request context.
func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Request, error) {
fc := &frankenPHPContext{
func newFrankenPHPContext() *frankenPHPContext {
return &frankenPHPContext{
done: make(chan any),
startedAt: time.Now(),
request: r,
}
}

// NewRequestWithContext creates a new FrankenPHP request context.
func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Request, error) {
fc := newFrankenPHPContext()
fc.request = r

for _, o := range opts {
if err := o(fc); err != nil {
return nil, err
Expand Down Expand Up @@ -132,6 +137,10 @@ func (fc *frankenPHPContext) validate() bool {
}

func (fc *frankenPHPContext) clientHasClosed() bool {
if fc.request == nil {
return false
}

select {
case <-fc.request.Context().Done():
return true
Expand Down
10 changes: 7 additions & 3 deletions frankenphp.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func Init(options ...Option) error {
registerExtensions()

// add registered external workers
for _, ew := range externalWorkers {
for _, ew := range extensionWorkers {
options = append(options, WithWorkers(ew.Name(), ew.FileName(), ew.GetMinThreads(), WithWorkerEnv(ew.Env())))
}

Expand Down Expand Up @@ -527,8 +527,12 @@ func go_read_post(threadIndex C.uintptr_t, cBuf *C.char, countBytes C.size_t) (r

//export go_read_cookies
func go_read_cookies(threadIndex C.uintptr_t) *C.char {
cookies := phpThreads[threadIndex].getRequestContext().request.Header.Values("Cookie")
cookie := strings.Join(cookies, "; ")
request := phpThreads[threadIndex].getRequestContext().request
if request == nil {
return nil
}

cookie := strings.Join(request.Header.Values("Cookie"), "; ")
if cookie == "" {
return nil
}
Expand Down
145 changes: 0 additions & 145 deletions threadFramework.go

This file was deleted.

30 changes: 23 additions & 7 deletions threadworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ type workerThread struct {
dummyContext *frankenPHPContext
workerContext *frankenPHPContext
backoff *exponentialBackoff
externalWorker WorkerExtension
externalWorker Worker
isBootingScript bool // true if the worker has not reached frankenphp_handle_request yet
}

func convertToWorkerThread(thread *phpThread, worker *worker) {
externalWorker := externalWorkers[worker.name]
externalWorker := extensionWorkers[worker.name]

thread.setHandler(&workerThread{
state: thread.state,
Expand Down Expand Up @@ -204,7 +204,11 @@ func (handler *workerThread) waitForWorkerRequest() (bool, any) {
handler.workerContext = fc
handler.state.markAsWaiting(false)

logger.LogAttrs(ctx, slog.LevelDebug, "request handling started", slog.String("worker", handler.worker.name), slog.Int("thread", handler.thread.threadIndex), slog.String("url", fc.request.RequestURI))
if fc.request == nil {
logger.LogAttrs(ctx, slog.LevelDebug, "request handling started", slog.String("worker", handler.worker.name), slog.Int("thread", handler.thread.threadIndex))
} else {
logger.LogAttrs(ctx, slog.LevelDebug, "request handling started", slog.String("worker", handler.worker.name), slog.Int("thread", handler.thread.threadIndex), slog.String("url", fc.request.RequestURI))
}

return true, fc.handlerParameters
}
Expand All @@ -217,10 +221,18 @@ func go_frankenphp_worker_handle_request_start(threadIndex C.uintptr_t) (C.bool,
hasRequest, parameters := handler.waitForWorkerRequest()

if parameters != nil {
p := PHPValue(parameters)
handler.thread.Pin(p)
var ptr unsafe.Pointer

switch p := parameters.(type) {
case unsafe.Pointer:
ptr = p

return C.bool(hasRequest), p
default:
ptr = PHPValue(ptr)
}
handler.thread.Pin(ptr)

return C.bool(hasRequest), ptr
}

return C.bool(hasRequest), nil
Expand All @@ -239,7 +251,11 @@ func go_frankenphp_finish_worker_request(threadIndex C.uintptr_t, retval *C.zval
fc.closeContext()
thread.handler.(*workerThread).workerContext = nil

fc.logger.LogAttrs(context.Background(), slog.LevelDebug, "request handling finished", slog.String("worker", fc.scriptFilename), slog.Int("thread", thread.threadIndex), slog.String("url", fc.request.RequestURI))
if fc.request == nil {
fc.logger.LogAttrs(context.Background(), slog.LevelDebug, "request handling finished", slog.String("worker", fc.scriptFilename), slog.Int("thread", thread.threadIndex))
} else {
fc.logger.LogAttrs(context.Background(), slog.LevelDebug, "request handling finished", slog.String("worker", fc.scriptFilename), slog.Int("thread", thread.threadIndex), slog.String("url", fc.request.RequestURI))
}
}

// when frankenphp_finish_request() is directly called from PHP
Expand Down
3 changes: 2 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package frankenphp
*/
import "C"
import (
"fmt"
"strconv"
"unsafe"
)
Expand Down Expand Up @@ -284,7 +285,7 @@ func phpValue(value any) *C.zval {
case []any:
return (*C.zval)(PHPPackedArray(v))
default:
C.__zval_null__(&zval)
panic(fmt.Sprintf("unsupported Go type %T", v))
}

return &zval
Expand Down
2 changes: 1 addition & 1 deletion worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func initWorkers(opt []workerOpt) error {
// create a pipe from the external worker to the main worker
// note: this is locked to the initial thread size the external worker requested
if workerThread, ok := thread.handler.(*workerThread); ok && workerThread.externalWorker != nil {
go startExternalWorkerPipe(w, workerThread.externalWorker, thread)
go startWorker(w, workerThread.externalWorker, thread)
}
workersReady.Done()
}()
Expand Down
Loading