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
Łukasz Nowak
caddy
Commits
829a0f34
Commit
829a0f34
authored
Jan 03, 2016
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Preserve and clean up original host input in Caddyfile-JSON conversions
parent
bb80f991
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
11 deletions
+51
-11
caddy/caddyfile/json.go
caddy/caddyfile/json.go
+15
-10
caddy/caddyfile/json_test.go
caddy/caddyfile/json_test.go
+36
-1
No files found.
caddy/caddyfile/json.go
View file @
829a0f34
...
@@ -28,7 +28,7 @@ func ToJSON(caddyfile []byte) ([]byte, error) {
...
@@ -28,7 +28,7 @@ func ToJSON(caddyfile []byte) ([]byte, error) {
// Fill up host list
// Fill up host list
for
_
,
host
:=
range
sb
.
HostList
()
{
for
_
,
host
:=
range
sb
.
HostList
()
{
block
.
Hosts
=
append
(
block
.
Hosts
,
st
rings
.
TrimSuffix
(
host
,
":"
))
block
.
Hosts
=
append
(
block
.
Hosts
,
st
andardizeScheme
(
host
))
}
}
// Extract directives deterministically by sorting them
// Extract directives deterministically by sorting them
...
@@ -62,7 +62,6 @@ func ToJSON(caddyfile []byte) ([]byte, error) {
...
@@ -62,7 +62,6 @@ func ToJSON(caddyfile []byte) ([]byte, error) {
// but only one line at a time, to be used at the top-level of
// but only one line at a time, to be used at the top-level of
// a server block only (where the first token on each line is a
// a server block only (where the first token on each line is a
// directive) - not to be used at any other nesting level.
// directive) - not to be used at any other nesting level.
// goes to end of line
func
constructLine
(
d
*
parse
.
Dispenser
)
[]
interface
{}
{
func
constructLine
(
d
*
parse
.
Dispenser
)
[]
interface
{}
{
var
args
[]
interface
{}
var
args
[]
interface
{}
...
@@ -80,8 +79,8 @@ func constructLine(d *parse.Dispenser) []interface{} {
...
@@ -80,8 +79,8 @@ func constructLine(d *parse.Dispenser) []interface{} {
}
}
// constructBlock recursively processes tokens into a
// constructBlock recursively processes tokens into a
// JSON-encodable structure.
// JSON-encodable structure.
To be used in a directive's
//
goes to end of block
//
block. Goes to end of block.
func
constructBlock
(
d
*
parse
.
Dispenser
)
[][]
interface
{}
{
func
constructBlock
(
d
*
parse
.
Dispenser
)
[][]
interface
{}
{
block
:=
[][]
interface
{}{}
block
:=
[][]
interface
{}{}
...
@@ -110,15 +109,10 @@ func FromJSON(jsonBytes []byte) ([]byte, error) {
...
@@ -110,15 +109,10 @@ func FromJSON(jsonBytes []byte) ([]byte, error) {
result
+=
"
\n\n
"
result
+=
"
\n\n
"
}
}
for
i
,
host
:=
range
sb
.
Hosts
{
for
i
,
host
:=
range
sb
.
Hosts
{
if
hostname
,
port
,
err
:=
net
.
SplitHostPort
(
host
);
err
==
nil
{
if
port
==
"http"
||
port
==
"https"
{
host
=
port
+
"://"
+
hostname
}
}
if
i
>
0
{
if
i
>
0
{
result
+=
", "
result
+=
", "
}
}
result
+=
st
rings
.
TrimSuffix
(
host
,
":"
)
result
+=
st
andardizeScheme
(
host
)
}
}
result
+=
jsonToText
(
sb
.
Body
,
1
)
result
+=
jsonToText
(
sb
.
Body
,
1
)
}
}
...
@@ -170,6 +164,17 @@ func jsonToText(scope interface{}, depth int) string {
...
@@ -170,6 +164,17 @@ func jsonToText(scope interface{}, depth int) string {
return
result
return
result
}
}
// standardizeScheme turns an address like host:https into https://host,
// or "host:" into "host".
func
standardizeScheme
(
addr
string
)
string
{
if
hostname
,
port
,
err
:=
net
.
SplitHostPort
(
addr
);
err
==
nil
{
if
port
==
"http"
||
port
==
"https"
{
addr
=
port
+
"://"
+
hostname
}
}
return
strings
.
TrimSuffix
(
addr
,
":"
)
}
// Caddyfile encapsulates a slice of ServerBlocks.
// Caddyfile encapsulates a slice of ServerBlocks.
type
Caddyfile
[]
ServerBlock
type
Caddyfile
[]
ServerBlock
...
...
caddy/caddyfile/json_test.go
View file @
829a0f34
...
@@ -63,7 +63,7 @@ baz"
...
@@ -63,7 +63,7 @@ baz"
{
// 8
{
// 8
caddyfile
:
`http://host, https://host {
caddyfile
:
`http://host, https://host {
}`
,
}`
,
json
:
`[{"hosts":["h
ost:http","host:https
"],"body":[]}]`
,
// hosts in JSON are always host:port format (if port is specified), for consistency
json
:
`[{"hosts":["h
ttp://host","https://host
"],"body":[]}]`
,
// hosts in JSON are always host:port format (if port is specified), for consistency
},
},
{
// 9
{
// 9
caddyfile
:
`host {
caddyfile
:
`host {
...
@@ -124,3 +124,38 @@ func TestFromJSON(t *testing.T) {
...
@@ -124,3 +124,38 @@ func TestFromJSON(t *testing.T) {
}
}
}
}
}
}
func
TestStandardizeAddress
(
t
*
testing
.
T
)
{
// host:https should be converted to https://host
output
,
err
:=
ToJSON
([]
byte
(
`host:https`
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
expected
,
actual
:=
`[{"hosts":["https://host"],"body":[]}]`
,
string
(
output
);
expected
!=
actual
{
t
.
Errorf
(
"Expected:
\n
'%s'
\n
Actual:
\n
'%s'"
,
expected
,
actual
)
}
output
,
err
=
FromJSON
([]
byte
(
`[{"hosts":["https://host"],"body":[]}]`
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
expected
,
actual
:=
"https://host {
\n
}"
,
string
(
output
);
expected
!=
actual
{
t
.
Errorf
(
"Expected:
\n
'%s'
\n
Actual:
\n
'%s'"
,
expected
,
actual
)
}
// host: should be converted to just host
output
,
err
=
ToJSON
([]
byte
(
`host:`
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
expected
,
actual
:=
`[{"hosts":["host"],"body":[]}]`
,
string
(
output
);
expected
!=
actual
{
t
.
Errorf
(
"Expected:
\n
'%s'
\n
Actual:
\n
'%s'"
,
expected
,
actual
)
}
output
,
err
=
FromJSON
([]
byte
(
`[{"hosts":["host:"],"body":[]}]`
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
expected
,
actual
:=
"host {
\n
}"
,
string
(
output
);
expected
!=
actual
{
t
.
Errorf
(
"Expected:
\n
'%s'
\n
Actual:
\n
'%s'"
,
expected
,
actual
)
}
}
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