@@ -2,9 +2,10 @@ package main
22
33import (
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
2931func 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 }
0 commit comments