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
04571ff3
Commit
04571ff3
authored
Jul 24, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
NewReplacer takes third argument for empty value string
parent
7adff28a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
51 deletions
+57
-51
middleware/log/log.go
middleware/log/log.go
+6
-5
middleware/proxy/proxy.go
middleware/proxy/proxy.go
+1
-1
middleware/replacer.go
middleware/replacer.go
+50
-45
No files found.
middleware/log/log.go
View file @
04571ff3
...
...
@@ -33,7 +33,7 @@ func (l Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
}
status
=
0
}
rep
:=
middleware
.
NewReplacer
(
r
,
responseRecorder
)
rep
:=
middleware
.
NewReplacer
(
r
,
responseRecorder
,
CommonLogEmptyValue
)
rule
.
Log
.
Println
(
rep
.
Replace
(
rule
.
Format
))
return
status
,
err
}
...
...
@@ -50,8 +50,9 @@ type Rule struct {
}
const
(
DefaultLogFilename
=
"access.log"
CommonLogFormat
=
`{remote} `
+
middleware
.
EmptyStringReplacer
+
` [{when}] "{method} {uri} {proto}" {status} {size}`
CombinedLogFormat
=
CommonLogFormat
+
` "{>Referer}" "{>User-Agent}"`
DefaultLogFormat
=
CommonLogFormat
DefaultLogFilename
=
"access.log"
CommonLogFormat
=
`{remote} `
+
CommonLogEmptyValue
+
` [{when}] "{method} {uri} {proto}" {status} {size}`
CommonLogEmptyValue
=
"-"
CombinedLogFormat
=
CommonLogFormat
+
` "{>Referer}" "{>User-Agent}"`
DefaultLogFormat
=
CommonLogFormat
)
middleware/proxy/proxy.go
View file @
04571ff3
...
...
@@ -89,7 +89,7 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if
replacer
==
nil
{
rHost
:=
r
.
Host
r
.
Host
=
requestHost
replacer
=
middleware
.
NewReplacer
(
r
,
nil
)
replacer
=
middleware
.
NewReplacer
(
r
,
nil
,
""
)
r
.
Host
=
rHost
}
for
header
,
values
:=
range
host
.
ExtraHeaders
{
...
...
middleware/replacer.go
View file @
04571ff3
...
...
@@ -16,57 +16,63 @@ type Replacer interface {
Replace
(
string
)
string
}
type
replacer
map
[
string
]
string
type
replacer
struct
{
replacements
map
[
string
]
string
emptyValue
string
}
// NewReplacer makes a new replacer based on r and rr.
// Do not create a new replacer until r and rr have all
// the needed values, because this function copies those
// values into the replacer.
func
NewReplacer
(
r
*
http
.
Request
,
rr
*
responseRecorder
)
Replacer
{
func
NewReplacer
(
r
*
http
.
Request
,
rr
*
responseRecorder
,
emptyValue
string
)
Replacer
{
rep
:=
replacer
{
"{method}"
:
r
.
Method
,
"{scheme}"
:
func
()
string
{
if
r
.
TLS
!=
nil
{
return
"https"
}
return
"http"
}(),
"{host}"
:
r
.
Host
,
"{path}"
:
r
.
URL
.
Path
,
"{query}"
:
r
.
URL
.
RawQuery
,
"{fragment}"
:
r
.
URL
.
Fragment
,
"{proto}"
:
r
.
Proto
,
"{remote}"
:
func
()
string
{
if
fwdFor
:=
r
.
Header
.
Get
(
"X-Forwarded-For"
);
fwdFor
!=
""
{
return
fwdFor
}
host
,
_
,
err
:=
net
.
SplitHostPort
(
r
.
RemoteAddr
)
if
err
!=
nil
{
return
r
.
RemoteAddr
}
return
host
}(),
"{port}"
:
func
()
string
{
_
,
port
,
err
:=
net
.
SplitHostPort
(
r
.
RemoteAddr
)
if
err
!=
nil
{
return
""
}
return
port
}(),
"{uri}"
:
r
.
RequestURI
,
"{when}"
:
func
()
string
{
return
time
.
Now
()
.
Format
(
timeFormat
)
}(),
replacements
:
map
[
string
]
string
{
"{method}"
:
r
.
Method
,
"{scheme}"
:
func
()
string
{
if
r
.
TLS
!=
nil
{
return
"https"
}
return
"http"
}(),
"{host}"
:
r
.
Host
,
"{path}"
:
r
.
URL
.
Path
,
"{query}"
:
r
.
URL
.
RawQuery
,
"{fragment}"
:
r
.
URL
.
Fragment
,
"{proto}"
:
r
.
Proto
,
"{remote}"
:
func
()
string
{
if
fwdFor
:=
r
.
Header
.
Get
(
"X-Forwarded-For"
);
fwdFor
!=
""
{
return
fwdFor
}
host
,
_
,
err
:=
net
.
SplitHostPort
(
r
.
RemoteAddr
)
if
err
!=
nil
{
return
r
.
RemoteAddr
}
return
host
}(),
"{port}"
:
func
()
string
{
_
,
port
,
err
:=
net
.
SplitHostPort
(
r
.
RemoteAddr
)
if
err
!=
nil
{
return
""
}
return
port
}(),
"{uri}"
:
r
.
URL
.
RequestURI
(),
"{when}"
:
func
()
string
{
return
time
.
Now
()
.
Format
(
timeFormat
)
}(),
},
emptyValue
:
emptyValue
,
}
if
rr
!=
nil
{
rep
[
"{status}"
]
=
strconv
.
Itoa
(
rr
.
status
)
rep
[
"{size}"
]
=
strconv
.
Itoa
(
rr
.
size
)
rep
[
"{latency}"
]
=
time
.
Since
(
rr
.
start
)
.
String
()
rep
.
replacements
[
"{status}"
]
=
strconv
.
Itoa
(
rr
.
status
)
rep
.
replacements
[
"{size}"
]
=
strconv
.
Itoa
(
rr
.
size
)
rep
.
replacements
[
"{latency}"
]
=
time
.
Since
(
rr
.
start
)
.
String
()
}
// Header placeholders
for
header
,
val
:=
range
r
.
Header
{
rep
[
headerReplacer
+
header
+
"}"
]
=
strings
.
Join
(
val
,
","
)
rep
.
replacements
[
headerReplacer
+
header
+
"}"
]
=
strings
.
Join
(
val
,
","
)
}
return
rep
...
...
@@ -75,9 +81,9 @@ func NewReplacer(r *http.Request, rr *responseRecorder) Replacer {
// Replace performs a replacement of values on s and returns
// the string with the replaced values.
func
(
r
replacer
)
Replace
(
s
string
)
string
{
for
placeholder
,
replacement
:=
range
r
{
for
placeholder
,
replacement
:=
range
r
.
replacements
{
if
replacement
==
""
{
replacement
=
EmptyStringReplacer
replacement
=
r
.
emptyValue
}
s
=
strings
.
Replace
(
s
,
placeholder
,
replacement
,
-
1
)
}
...
...
@@ -88,7 +94,7 @@ func (r replacer) Replace(s string) string {
endOffset
:=
idxStart
+
len
(
headerReplacer
)
idxEnd
:=
strings
.
Index
(
s
[
endOffset
:
],
"}"
)
if
idxEnd
>
-
1
{
s
=
s
[
:
idxStart
]
+
EmptyStringReplacer
+
s
[
endOffset
+
idxEnd
+
1
:
]
s
=
s
[
:
idxStart
]
+
r
.
emptyValue
+
s
[
endOffset
+
idxEnd
+
1
:
]
}
else
{
break
}
...
...
@@ -97,7 +103,6 @@ func (r replacer) Replace(s string) string {
}
const
(
timeFormat
=
"02/Jan/2006:15:04:05 -0700"
headerReplacer
=
"{>"
EmptyStringReplacer
=
"-"
timeFormat
=
"02/Jan/2006:15:04:05 -0700"
headerReplacer
=
"{>"
)
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