Просмотр исходного кода

fix: prevent crash when calling apache_request_headers() in non-HTTP context

Kévin Dunglas 1 год назад
Родитель
Сommit
b71dae9b03
3 измененных файлов с 19 добавлено и 2 удалено
  1. 1 1
      frankenphp.c
  2. 16 1
      frankenphp.go
  3. 2 0
      testdata/request-headers.php

+ 1 - 1
frankenphp.c

@@ -250,7 +250,7 @@ PHP_FUNCTION(frankenphp_request_headers) {
 
   frankenphp_server_context *ctx = SG(server_context);
   struct go_apache_request_headers_return headers =
-      go_apache_request_headers(ctx->current_request);
+      go_apache_request_headers(ctx->current_request, ctx->main_request);
 
   array_init_size(return_value, headers.r1);
 

+ 16 - 1
frankenphp.go

@@ -582,7 +582,18 @@ func go_register_variables(rh C.uintptr_t, trackVarsArray *C.zval) {
 }
 
 //export go_apache_request_headers
-func go_apache_request_headers(rh C.uintptr_t) (*C.go_string, C.size_t, C.uintptr_t) {
+func go_apache_request_headers(rh, mrh C.uintptr_t) (*C.go_string, C.size_t, C.uintptr_t) {
+	if rh == 0 {
+		// worker mode, not handling a request
+		mr := cgo.Handle(mrh).Value().(*http.Request)
+		mfc := mr.Context().Value(contextKey).(*FrankenPHPContext)
+
+		if c := mfc.logger.Check(zap.DebugLevel, "apache_request_headers() called in non-HTTP context"); c != nil {
+			c.Write(zap.String("worker", mfc.scriptFilename))
+		}
+
+		return nil, 0, 0
+	}
 	r := cgo.Handle(rh).Value().(*http.Request)
 
 	pinner := &runtime.Pinner{}
@@ -613,6 +624,10 @@ func go_apache_request_headers(rh C.uintptr_t) (*C.go_string, C.size_t, C.uintpt
 
 //export go_apache_request_cleanup
 func go_apache_request_cleanup(rh C.uintptr_t) {
+	if rh == 0 {
+		return
+	}
+
 	h := cgo.Handle(rh)
 	p := h.Value().(*runtime.Pinner)
 	p.Unpin()

+ 2 - 0
testdata/request-headers.php

@@ -1,5 +1,7 @@
 <?php
 
+apache_request_headers();
+
 require_once __DIR__.'/_executor.php';
 
 return function() {