Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
6db4771a
Commit
6db4771a
authored
Jun 29, 2016
by
Matt Holt
Committed by
GitHub
Jun 29, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #907 from abiosoft/fastcgi_env_placeholder
Support for placeholders in fastcgi env vars.
parents
2e84fe45
b1cd0bfe
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
67 additions
and
21 deletions
+67
-21
caddyhttp/fastcgi/fastcgi.go
caddyhttp/fastcgi/fastcgi.go
+3
-1
caddyhttp/fastcgi/fastcgi_test.go
caddyhttp/fastcgi/fastcgi_test.go
+58
-19
caddyhttp/httpserver/replacer.go
caddyhttp/httpserver/replacer.go
+5
-0
caddyhttp/httpserver/replacer_test.go
caddyhttp/httpserver/replacer_test.go
+1
-1
No files found.
caddyhttp/fastcgi/fastcgi.go
View file @
6db4771a
...
...
@@ -254,9 +254,11 @@ func (h Handler) buildEnv(r *http.Request, rule Rule, fpath string) (map[string]
env
[
"HTTPS"
]
=
"on"
}
replacer
:=
httpserver
.
NewReplacer
(
r
,
nil
,
""
)
// Add env variables from config
for
_
,
envVar
:=
range
rule
.
EnvVars
{
env
[
envVar
[
0
]]
=
envVar
[
1
]
// replace request placeholders in environment variables
env
[
envVar
[
0
]]
=
replacer
.
Replace
(
envVar
[
1
])
}
// Add all HTTP headers to env variables
...
...
caddyhttp/fastcgi/fastcgi_test.go
View file @
6db4771a
...
...
@@ -123,38 +123,77 @@ func TestBuildEnv(t *testing.T) {
t
.
Error
(
"Unexpected error:"
,
err
.
Error
())
}
r
:=
http
.
Request
{
Method
:
"GET"
,
URL
:
url
,
Proto
:
"HTTP/1.1"
,
ProtoMajor
:
1
,
ProtoMinor
:
1
,
Host
:
"localhost:2015"
,
RemoteAddr
:
"[2b02:1810:4f2d:9400:70ab:f822:be8a:9093]:51688"
,
RequestURI
:
"/fgci_test.php"
,
var
newReq
=
func
()
*
http
.
Request
{
return
&
http
.
Request
{
Method
:
"GET"
,
URL
:
url
,
Proto
:
"HTTP/1.1"
,
ProtoMajor
:
1
,
ProtoMinor
:
1
,
Host
:
"localhost:2015"
,
RemoteAddr
:
"[2b02:1810:4f2d:9400:70ab:f822:be8a:9093]:51688"
,
RequestURI
:
"/fgci_test.php"
,
}
}
fpath
:=
"/fgci_test.php"
var
envExpected
=
map
[
string
]
string
{
"REMOTE_ADDR"
:
"2b02:1810:4f2d:9400:70ab:f822:be8a:9093"
,
"REMOTE_PORT"
:
"51688"
,
"SERVER_PROTOCOL"
:
"HTTP/1.1"
,
"QUERY_STRING"
:
"test=blabla"
,
"REQUEST_METHOD"
:
"GET"
,
"HTTP_HOST"
:
"localhost:2015"
,
var
newEnv
=
func
()
map
[
string
]
string
{
return
map
[
string
]
string
{
"REMOTE_ADDR"
:
"2b02:1810:4f2d:9400:70ab:f822:be8a:9093"
,
"REMOTE_PORT"
:
"51688"
,
"SERVER_PROTOCOL"
:
"HTTP/1.1"
,
"QUERY_STRING"
:
"test=blabla"
,
"REQUEST_METHOD"
:
"GET"
,
"HTTP_HOST"
:
"localhost:2015"
,
}
}
// request
var
r
*
http
.
Request
// expected environment variables
var
envExpected
map
[
string
]
string
// 1. Test for full canonical IPv6 address
testBuildEnv
(
&
r
,
rule
,
fpath
,
envExpected
)
r
=
newReq
()
testBuildEnv
(
r
,
rule
,
fpath
,
envExpected
)
// 2. Test for shorthand notation of IPv6 address
r
=
newReq
()
r
.
RemoteAddr
=
"[::1]:51688"
envExpected
=
newEnv
()
envExpected
[
"REMOTE_ADDR"
]
=
"::1"
testBuildEnv
(
&
r
,
rule
,
fpath
,
envExpected
)
testBuildEnv
(
r
,
rule
,
fpath
,
envExpected
)
// 3. Test for IPv4 address
r
=
newReq
()
r
.
RemoteAddr
=
"192.168.0.10:51688"
envExpected
=
newEnv
()
envExpected
[
"REMOTE_ADDR"
]
=
"192.168.0.10"
testBuildEnv
(
&
r
,
rule
,
fpath
,
envExpected
)
testBuildEnv
(
r
,
rule
,
fpath
,
envExpected
)
// 4. Test for environment variable
r
=
newReq
()
rule
.
EnvVars
=
[][
2
]
string
{
{
"HTTP_HOST"
,
"localhost:2016"
},
{
"REQUEST_METHOD"
,
"POST"
},
}
envExpected
=
newEnv
()
envExpected
[
"HTTP_HOST"
]
=
"localhost:2016"
envExpected
[
"REQUEST_METHOD"
]
=
"POST"
testBuildEnv
(
r
,
rule
,
fpath
,
envExpected
)
// 5. Test for environment variable placeholders
r
=
newReq
()
rule
.
EnvVars
=
[][
2
]
string
{
{
"HTTP_HOST"
,
"{host}"
},
{
"CUSTOM_URI"
,
"custom_uri{uri}"
},
{
"CUSTOM_QUERY"
,
"custom=true&{query}"
},
}
envExpected
=
newEnv
()
envExpected
[
"HTTP_HOST"
]
=
"localhost:2015"
envExpected
[
"CUSTOM_URI"
]
=
"custom_uri/fgci_test.php?test=blabla"
envExpected
[
"CUSTOM_QUERY"
]
=
"custom=true&test=blabla"
testBuildEnv
(
r
,
rule
,
fpath
,
envExpected
)
}
caddyhttp/httpserver/replacer.go
View file @
6db4771a
...
...
@@ -127,6 +127,11 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
// Replace performs a replacement of values on s and returns
// the string with the replaced values.
func
(
r
*
replacer
)
Replace
(
s
string
)
string
{
// Do not attempt replacements if no placeholder is found.
if
!
strings
.
ContainsAny
(
s
,
"{}"
)
{
return
s
}
// Make response placeholders now
if
r
.
responseRecorder
!=
nil
{
r
.
replacements
[
"{status}"
]
=
strconv
.
Itoa
(
r
.
responseRecorder
.
status
)
...
...
caddyhttp/httpserver/replacer_test.go
View file @
6db4771a
...
...
@@ -32,7 +32,7 @@ func TestNewReplacer(t *testing.T) {
if
got
,
want
:=
v
.
replacements
[
"{status}"
],
""
;
got
!=
want
{
t
.
Errorf
(
"Expected status to NOT be set before Replace() is called; was: %s"
,
got
)
}
rep
.
Replace
(
"
foobar
"
)
rep
.
Replace
(
"
{foobar}
"
)
if
got
,
want
:=
v
.
replacements
[
"{status}"
],
"200"
;
got
!=
want
{
t
.
Errorf
(
"Expected status to be %s, was: %s"
,
want
,
got
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment