Skip to content

Commit c331ea6

Browse files
committed
fix: set correct document uri
Signed-off-by: Nico Braun <rainbowstack@gmail.com>
1 parent 56dcf3e commit c331ea6

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ OPTIONS:
2020
-s, --silent don't show any output, unless failed
2121
--status-min int fail if status is below (default 200)
2222
--status-max int fail if status is above (default 299)
23+
--debug dump the fcgi params
2324
```
2425

2526
The fcqi `REQUEST_URI` and `PATH_INFO` are derived from the request path.
@@ -43,6 +44,6 @@ curl -fsSL github.com/wolf-gmbh/fcgi-probe/releases/latest/download/fcgi-probe_L
4344

4445
### From Source
4546

46-
```
47+
```bash
4748
go install github.com/wolf-gmbh/fcgi-probe@latest
4849
```

main.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package main
22

33
import (
44
"bytes"
5+
"encoding/json"
6+
"errors"
57
"fmt"
68
"io"
7-
"log"
89
"net/http"
910
"net/http/httptest"
1011
"net/url"
@@ -24,6 +25,7 @@ type RequestOptions struct {
2425
statusMax int
2526
silent bool
2627
headers []string
28+
debug bool
2729
}
2830

2931
func main() {
@@ -55,10 +57,11 @@ func run() error {
5557
pflag.BoolVarP(&opts.silent, "silent", "s", opts.silent, "don't show any output, unless failed")
5658
pflag.IntVar(&opts.statusMin, "status-min", opts.statusMin, "fail if status is below")
5759
pflag.IntVar(&opts.statusMax, "status-max", opts.statusMax, "fail if status is above")
60+
pflag.BoolVar(&opts.debug, "debug", opts.debug, "dump the fcgi params")
5861
pflag.Parse()
5962

6063
if len(pflag.Args()) != 1 {
61-
log.Fatal("need exactly 1 argument")
64+
return errors.New("need exactly 1 argument")
6265
}
6366

6467
res, err := fpmRequest(pflag.Arg(0), opts)
@@ -109,7 +112,7 @@ func fpmRequest(rawUrl string, opts RequestOptions) (*http.Response, error) {
109112

110113
req := gofast.NewRequest(httpReq)
111114

112-
pipe, err := fpmHandler(opts.docroot, opts.index)(client, req)
115+
pipe, err := fpmHandler(opts.docroot, opts.index, opts.debug)(client, req)
113116
if err != nil {
114117
return nil, fmt.Errorf("fcgi request: %w", err)
115118
}
@@ -126,28 +129,29 @@ func fpmRequest(rawUrl string, opts RequestOptions) (*http.Response, error) {
126129
return rec.Result(), nil
127130
}
128131

129-
func fpmHandler(docroot, index string) gofast.SessionHandler {
132+
func fpmHandler(docroot, index string, debug bool) gofast.SessionHandler {
130133
return gofast.Chain(gofast.MapHeader, func(inner gofast.SessionHandler) gofast.SessionHandler {
131134
return func(client gofast.Client, req *gofast.Request) (*gofast.ResponsePipe, error) {
135+
if req.Raw.URL.Path == "" {
136+
req.Raw.URL.Path = "/"
137+
}
138+
132139
req.Params["QUERY_STRING"] = req.Raw.URL.RawQuery
133140
req.Params["REQUEST_METHOD"] = req.Raw.Method
134141
req.Params["CONTENT_TYPE"] = req.Raw.Header.Get("content-type")
135142
req.Params["CONTENT_LENGTH"] = fmt.Sprintf("%d", req.Raw.ContentLength)
136143

137-
req.Params["SCRIPT_NAME"] = req.Raw.URL.Path
138-
req.Params["SCRIPT_FILENAME"] = fmt.Sprintf("%s/%s", docroot, index) // custom for fpm
139-
req.Params["REQUEST_URI"] = req.Raw.RequestURI
140-
req.Params["DOCUMENT_URI"] = "$document_uri;"
144+
req.Params["SCRIPT_FILENAME"] = fmt.Sprintf("%s/%s", docroot, index)
141145
req.Params["DOCUMENT_ROOT"] = docroot
142-
req.Params["SERVER_PROTOCOL"] = "http"
143-
req.Params["REQUEST_SCHEME"] = req.Raw.URL.Scheme
144-
req.Params["HTTPS"] = "$https if_not_empty;"
145-
146-
req.Params["GATEWAY_INTERFACE"] = "CGI/1.1;"
147-
req.Params["SERVER_SOFTWARE"] = "fastcgi-probe"
146+
req.Params["DOCUMENT_URI"] = "/" + index
147+
req.Params["REQUEST_URI"] = req.Raw.URL.Path
148+
req.Params["SCRIPT_NAME"] = req.Raw.URL.Path
148149

149-
// PHP only, required if PHP was built with --enable-force-cgi-redirect
150-
req.Params["REDIRECT_STATUS"] = "200;"
150+
if debug {
151+
if b, err := json.MarshalIndent(req.Params, "", " "); err == nil {
152+
fmt.Println(string(b))
153+
}
154+
}
151155

152156
return inner(client, req)
153157
}

testdata/nginx/default.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ server {
2121
include fastcgi_params;
2222
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
2323
fastcgi_index index.php;
24-
include fastcgi_params;
2524
fastcgi_pass php-fpm:9000;
2625
}
2726

0 commit comments

Comments
 (0)