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
7f546e52
Commit
7f546e52
authored
Jan 25, 2019
by
Max Heyer
Committed by
Toby Allen
Jan 25, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
httpserver: Implement {sever_port} placeholder (#2424)
parent
a7aeb979
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
0 deletions
+39
-0
caddyhttp/httpserver/replacer.go
caddyhttp/httpserver/replacer.go
+10
-0
caddyhttp/httpserver/replacer_test.go
caddyhttp/httpserver/replacer_test.go
+29
-0
No files found.
caddyhttp/httpserver/replacer.go
View file @
7f546e52
...
...
@@ -506,6 +506,16 @@ func (r *replacer) getSubstitution(key string) string {
return
cert
.
NotBefore
.
Format
(
"Jan 02 15:04:05 2006 MST"
)
}
return
r
.
emptyValue
case
"{server_port}"
:
_
,
port
,
err
:=
net
.
SplitHostPort
(
r
.
request
.
Host
)
if
err
!=
nil
{
if
r
.
request
.
TLS
!=
nil
{
return
"443"
}
else
{
return
"80"
}
}
return
port
default
:
// {labelN}
if
strings
.
HasPrefix
(
key
,
"{label"
)
{
...
...
caddyhttp/httpserver/replacer_test.go
View file @
7f546e52
...
...
@@ -127,6 +127,7 @@ func TestReplace(t *testing.T) {
{
"{label1} {label2} {label3} {label4}"
,
"localhost local - -"
},
{
"Label with missing number is {label} or {labelQQ}"
,
"Label with missing number is - or -"
},
{
"
\\
{ 'hostname': '{hostname}'
\\
}"
,
"{ 'hostname': '"
+
hostname
+
"' }"
},
{
"{server_port}"
,
"80"
},
}
for
_
,
c
:=
range
testCases
{
...
...
@@ -159,6 +160,33 @@ func TestReplace(t *testing.T) {
}
}
func
TestCustomServerPort
(
t
*
testing
.
T
)
{
w
:=
httptest
.
NewRecorder
()
recordRequest
:=
NewResponseRecorder
(
w
)
reader
:=
strings
.
NewReader
(
`{"username": "dennis"}`
)
request
,
err
:=
http
.
NewRequest
(
"POST"
,
"http://localhost.local:8000/?foo=bar"
,
reader
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to make request: %v"
,
err
)
}
ctx
:=
context
.
WithValue
(
request
.
Context
(),
OriginalURLCtxKey
,
*
request
.
URL
)
request
=
request
.
WithContext
(
ctx
)
repl
:=
NewReplacer
(
request
,
recordRequest
,
"-"
)
testCase
:=
struct
{
template
string
expect
string
}{
template
:
"{server_port}"
,
expect
:
"8000"
,
}
if
expected
,
actual
:=
testCase
.
expect
,
repl
.
Replace
(
testCase
.
template
);
expected
!=
actual
{
t
.
Errorf
(
"for template '%s', expected '%s', got '%s'"
,
testCase
.
template
,
expected
,
actual
)
}
}
func
TestTlsReplace
(
t
*
testing
.
T
)
{
w
:=
httptest
.
NewRecorder
()
recordRequest
:=
NewResponseRecorder
(
w
)
...
...
@@ -246,6 +274,7 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
{
"{tls_client_v_end}"
,
cVEnd
},
{
"{tls_client_v_remain}"
,
cVRemain
},
{
"{tls_client_v_start}"
,
cVStart
},
{
"{server_port}"
,
"443"
},
}
for
_
,
c
:=
range
testCases
{
...
...
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