Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Feb 12, 2024
1 parent a3fb06d commit 9b05728
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 51 deletions.
5 changes: 1 addition & 4 deletions caddy/caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"path/filepath"
"strconv"
"strings"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
Expand Down Expand Up @@ -248,8 +247,6 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
return nil
}

var requestURIkey = strings.Clone("REQUEST_URI\x00")

// ServeHTTP implements caddyhttp.MiddlewareHandler.
// TODO: Expose TLS versions as env vars, as Apache's mod_ssl: https://github.com/caddyserver/caddy/blob/master/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go#L298
func (f FrankenPHPModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
Expand All @@ -259,7 +256,7 @@ func (f FrankenPHPModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ ca
documentRoot := repl.ReplaceKnown(f.Root, "")

env := make(map[string]string, len(f.Env)+1)
env[requestURIkey] = origReq.URL.RequestURI()
env["REQUEST_URI\x00"] = origReq.URL.RequestURI()
for k, v := range f.Env {
env[k] = repl.ReplaceKnown(v, "")
}
Expand Down
4 changes: 1 addition & 3 deletions caddy/go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/dunglas/frankenphp/caddy

go 1.21

toolchain go1.22.0
go 1.22

replace github.com/dunglas/frankenphp => ../

Expand Down
44 changes: 14 additions & 30 deletions cgi.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ var knownServerKeys = map[string]struct{}{
}

func setKnownServerVariable(p *runtime.Pinner, cArr *[27]C.go_string, serverKey serverKey, val string) {
// TODO: remove this guard clause when upgrading to Go 1.22
if val == "" {
return
}
Expand All @@ -70,20 +69,6 @@ func setKnownServerVariable(p *runtime.Pinner, cArr *[27]C.go_string, serverKey
cArr[serverKey].data = (*C.char)(unsafe.Pointer(valData))
}

// TODO: remove this when upgrading to Go 1.22
var (
gatewayInterfaceValue = strings.Clone("CGI/1.1")
serverSoftwareValue = strings.Clone("FrankenPHP")
httpValue = strings.Clone("http")
httpsValue = strings.Clone("https")
httpPortValue = strings.Clone("80")
httpsPortValue = strings.Clone("443")
onValue = strings.Clone("on")
http10Value = strings.Clone("HTTP/1.0")
http11Value = strings.Clone("HTTP/1.1")
http2Value = strings.Clone("HTTP/2")
)

// computeKnownVariables returns a set of CGI environment variables for the request.
//
// TODO: handle this case https://github.com/caddyserver/caddy/issues/3718
Expand Down Expand Up @@ -111,7 +96,7 @@ func computeKnownVariables(request *http.Request, p *runtime.Pinner) (cArr [27]C
if raOK {
setKnownServerVariable(p, &cArr, remoteAddr, ra)
} else {
setKnownServerVariable(p, &cArr, remoteAddr, strings.Clone(ip))
setKnownServerVariable(p, &cArr, remoteAddr, ip)
}

if rh, ok := fc.env["REMOTE_HOST\x00"]; ok {
Expand All @@ -124,7 +109,7 @@ func computeKnownVariables(request *http.Request, p *runtime.Pinner) (cArr [27]C
}
}

setKnownServerVariable(p, &cArr, remotePort, strings.Clone(port))
setKnownServerVariable(p, &cArr, remotePort, port)
setKnownServerVariable(p, &cArr, documentRoot, fc.documentRoot)
setKnownServerVariable(p, &cArr, pathInfo, fc.pathInfo)
setKnownServerVariable(p, &cArr, phpSelf, request.URL.Path)
Expand All @@ -134,14 +119,14 @@ func computeKnownVariables(request *http.Request, p *runtime.Pinner) (cArr [27]C

var rs string
if request.TLS == nil {
rs = httpValue
rs = "http"
} else {
rs = httpsValue
rs = "https"

if h, ok := fc.env["HTTPS\x00"]; ok {
setKnownServerVariable(p, &cArr, https, h)
} else {
setKnownServerVariable(p, &cArr, https, onValue)
setKnownServerVariable(p, &cArr, https, "on")
}

// and pass the protocol details in a manner compatible with apache's mod_ssl
Expand Down Expand Up @@ -170,9 +155,9 @@ func computeKnownVariables(request *http.Request, p *runtime.Pinner) (cArr [27]C
// https://tools.ietf.org/html/rfc3875#section-4.1.15
switch rs {
case "https":
reqPort = httpsPortValue
reqPort = "443"
case "http":
reqPort = httpPortValue
reqPort = "80"
}
}

Expand All @@ -186,19 +171,18 @@ func computeKnownVariables(request *http.Request, p *runtime.Pinner) (cArr [27]C
// the parent environment from interfering.

// These values can not be override
proto := http10Value
var proto string
switch request.Proto {
case http2Value:
proto = http2Value

case http11Value:
proto = http11Value
case "HTTP/1.1", "HTTP/2":
proto = request.Proto
default:
proto = "HTTP/1.0"
}

setKnownServerVariable(p, &cArr, contentLength, request.Header.Get("Content-Length"))
setKnownServerVariable(p, &cArr, gatewayInterface, gatewayInterfaceValue)
setKnownServerVariable(p, &cArr, gatewayInterface, "CGI/1.1")
setKnownServerVariable(p, &cArr, serverProtocol, proto)
setKnownServerVariable(p, &cArr, serverSoftware, serverSoftwareValue)
setKnownServerVariable(p, &cArr, serverSoftware, "FrankenPHP")
setKnownServerVariable(p, &cArr, httpHost, request.Host) // added here, since not always part of headers

return
Expand Down
1 change: 1 addition & 0 deletions frankenphp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ func BenchmarkServerSuperGlobal(b *testing.B) {
req := httptest.NewRequest("GET", "http://example.com/server-variable.php", nil)
w := httptest.NewRecorder()

b.ResetTimer()
for i := 0; i < b.N; i++ {
handler(w, req)
}
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/dunglas/frankenphp

go 1.21

toolchain go1.22.0
go 1.22

retract v1.0.0-rc.1 // Human error

Expand Down
4 changes: 1 addition & 3 deletions request_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package frankenphp

import (
"path/filepath"
"strings"

"go.uber.org/zap"
)
Expand Down Expand Up @@ -55,11 +54,10 @@ func WithRequestSplitPath(splitPath []string) RequestOption {

type PreparedEnv = map[string]string

// TODO: remove the calls to strings.Clone when upgrading to Go 1.22
func PrepareEnv(env map[string]string) PreparedEnv {
preparedEnv := make(PreparedEnv, len(env))
for k, v := range env {
preparedEnv[strings.Clone(k+"\x00")] = strings.Clone(v)
preparedEnv[k+"\x00"] = v
}

return preparedEnv
Expand Down
9 changes: 1 addition & 8 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"path/filepath"
"runtime/cgo"
"strings"
"sync"

"go.uber.org/zap"
Expand All @@ -31,12 +30,6 @@ func initWorkers(opt []workerOpt) error {
return nil
}

// TODO: remove this when upgrading to Go 1.22
var (
frankenphpWorkerKey = strings.Clone("FRANKENPHP_WORKER\x00")
frankenphpWorkerValue = strings.Clone("1\x00")
)

func startWorkers(fileName string, nbWorkers int, env PreparedEnv) error {
absFileName, err := filepath.Abs(fileName)
if err != nil {
Expand All @@ -60,7 +53,7 @@ func startWorkers(fileName string, nbWorkers int, env PreparedEnv) error {
env = make(PreparedEnv, 1)
}

env[frankenphpWorkerKey] = frankenphpWorkerValue
env["FRANKENPHP_WORKER\x00"] = "1\x00"

l := getLogger()
for i := 0; i < nbWorkers; i++ {
Expand Down

0 comments on commit 9b05728

Please sign in to comment.