summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Titov <[email protected]>2020-02-03 15:56:25 +0300
committerGitHub <[email protected]>2020-02-03 15:56:25 +0300
commit48da9cfe2d8db778180e55d170f9a1f285cd382d (patch)
tree2a7fcb54e1cc52bb61bb833144624787a97005cb
parent648aac1b800fe506fe41f9143c2a4a2d95be408d (diff)
parent6e8554a8656df8b8f87c76166bf6f206a8495129 (diff)
Merge pull request #235 from marliotto/fix-request-uri-through-fcgi
fix REQUEST_URI for requests through FastCGI
-rw-r--r--service/http/fcgi_test.go46
-rw-r--r--service/http/request.go3
-rw-r--r--tests/http/request-uri.php10
3 files changed, 59 insertions, 0 deletions
diff --git a/service/http/fcgi_test.go b/service/http/fcgi_test.go
index 58fbe557..0cfc6e41 100644
--- a/service/http/fcgi_test.go
+++ b/service/http/fcgi_test.go
@@ -57,3 +57,49 @@ func Test_FCGI_Service_Echo(t *testing.T) {
assert.Equal(t, 201, w.Result().StatusCode)
assert.Equal(t, "WORLD", string(body))
}
+
+func Test_FCGI_Service_Request_Uri(t *testing.T) {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(ID, &Service{})
+
+ assert.NoError(t, c.Init(&testCfg{httpCfg: `{
+ "fcgi": {
+ "address": "tcp://0.0.0.0:6083"
+ },
+ "workers":{
+ "command": "php ../../tests/http/client.php request-uri pipes",
+ "pool": {"numWorkers": 1}
+ }
+ }`}))
+
+ s, st := c.Get(ID)
+ assert.NotNil(t, s)
+ assert.Equal(t, service.StatusOK, st)
+
+ // should do nothing
+ s.(*Service).Stop()
+
+ go func() { assert.NoError(t, c.Serve()) }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ fcgiConnFactory := gofast.SimpleConnFactory("tcp", "0.0.0.0:6083")
+
+ fcgiHandler := gofast.NewHandler(
+ gofast.BasicParamsMap(gofast.BasicSession),
+ gofast.SimpleClientFactory(fcgiConnFactory, 0),
+ )
+
+ w := httptest.NewRecorder()
+ req := httptest.NewRequest("GET", "http://site.local/hello-world", nil)
+ fcgiHandler.ServeHTTP(w, req)
+
+ body, err := ioutil.ReadAll(w.Result().Body)
+
+ assert.NoError(t, err)
+ assert.Equal(t, 200, w.Result().StatusCode)
+ assert.Equal(t, "http://site.local/hello-world", string(body))
+}
diff --git a/service/http/request.go b/service/http/request.go
index 5d91bfb6..630e26f6 100644
--- a/service/http/request.go
+++ b/service/http/request.go
@@ -170,6 +170,9 @@ func (r *Request) contentType() int {
// uri fetches full uri from request in a form of string (including https scheme if TLS connection is enabled).
func uri(r *http.Request) string {
+ if r.URL.Host != "" {
+ return r.URL.String()
+ }
if r.TLS != nil {
return fmt.Sprintf("https://%s%s", r.Host, r.URL.String())
}
diff --git a/tests/http/request-uri.php b/tests/http/request-uri.php
new file mode 100644
index 00000000..d4c87551
--- /dev/null
+++ b/tests/http/request-uri.php
@@ -0,0 +1,10 @@
+<?php
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+
+function handleRequest(ServerRequestInterface $req, ResponseInterface $resp): ResponseInterface
+{
+ $resp->getBody()->write($_SERVER['REQUEST_URI']);
+ return $resp;
+}