mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-25 01:09:04 +01:00
Exported NewReplacer and NewRecorder
This commit is contained in:
parent
261beb046e
commit
04996b2850
2 changed files with 9 additions and 9 deletions
|
@ -14,13 +14,13 @@ type responseRecorder struct {
|
||||||
size int
|
size int
|
||||||
}
|
}
|
||||||
|
|
||||||
// newResponseRecorder makes and returns a new responseRecorder,
|
// NewResponseRecorder makes and returns a new responseRecorder,
|
||||||
// which captures the HTTP Status code from the ResponseWriter
|
// which captures the HTTP Status code from the ResponseWriter
|
||||||
// and also the length of the response body written through it.
|
// and also the length of the response body written through it.
|
||||||
// Because a status is not set unless WriteHeader is called
|
// Because a status is not set unless WriteHeader is called
|
||||||
// explicitly, this constructor initializes with a status code
|
// explicitly, this constructor initializes with a status code
|
||||||
// of 200 to cover the default case.
|
// of 200 to cover the default case.
|
||||||
func newResponseRecorder(w http.ResponseWriter) *responseRecorder {
|
func NewResponseRecorder(w http.ResponseWriter) *responseRecorder {
|
||||||
return &responseRecorder{
|
return &responseRecorder{
|
||||||
ResponseWriter: w,
|
ResponseWriter: w,
|
||||||
status: http.StatusOK,
|
status: http.StatusOK,
|
||||||
|
|
|
@ -10,14 +10,14 @@ import (
|
||||||
// replacer is a type which can replace placeholder
|
// replacer is a type which can replace placeholder
|
||||||
// substrings in a string with actual values from a
|
// substrings in a string with actual values from a
|
||||||
// http.Request and responseRecorder. Always use
|
// http.Request and responseRecorder. Always use
|
||||||
// newReplacer to get one of these.
|
// NewReplacer to get one of these.
|
||||||
type replacer map[string]string
|
type replacer map[string]string
|
||||||
|
|
||||||
// newReplacer makes a new replacer based on r and rw.
|
// NewReplacer makes a new replacer based on r and rw.
|
||||||
// Do not create a new replacer until r and rw have all
|
// Do not create a new replacer until r and rw have all
|
||||||
// the needed values, because this function copies those
|
// the needed values, because this function copies those
|
||||||
// values into the replacer.
|
// values into the replacer.
|
||||||
func newReplacer(r *http.Request, rw *responseRecorder) replacer {
|
func NewReplacer(r *http.Request, rw *responseRecorder) replacer {
|
||||||
rep := replacer{
|
rep := replacer{
|
||||||
"{method}": r.Method,
|
"{method}": r.Method,
|
||||||
"{scheme}": func() string {
|
"{scheme}": func() string {
|
||||||
|
@ -62,10 +62,10 @@ func newReplacer(r *http.Request, rw *responseRecorder) replacer {
|
||||||
|
|
||||||
// replace performs a replacement of values on s and returns
|
// replace performs a replacement of values on s and returns
|
||||||
// the string with the replaced values.
|
// the string with the replaced values.
|
||||||
func (r replacer) replace(s string) string {
|
func (r replacer) Replace(s string) string {
|
||||||
for placeholder, replacement := range r {
|
for placeholder, replacement := range r {
|
||||||
if replacement == "" {
|
if replacement == "" {
|
||||||
replacement = emptyStringReplacer
|
replacement = EmptyStringReplacer
|
||||||
}
|
}
|
||||||
s = strings.Replace(s, placeholder, replacement, -1)
|
s = strings.Replace(s, placeholder, replacement, -1)
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ func (r replacer) replace(s string) string {
|
||||||
endOffset := idxStart + len(headerReplacer)
|
endOffset := idxStart + len(headerReplacer)
|
||||||
idxEnd := strings.Index(s[endOffset:], "}")
|
idxEnd := strings.Index(s[endOffset:], "}")
|
||||||
if idxEnd > -1 {
|
if idxEnd > -1 {
|
||||||
s = s[:idxStart] + emptyStringReplacer + s[endOffset+idxEnd+1:]
|
s = s[:idxStart] + EmptyStringReplacer + s[endOffset+idxEnd+1:]
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -87,5 +87,5 @@ func (r replacer) replace(s string) string {
|
||||||
const (
|
const (
|
||||||
timeFormat = "02/Jan/2006:15:04:05 -0700"
|
timeFormat = "02/Jan/2006:15:04:05 -0700"
|
||||||
headerReplacer = "{>"
|
headerReplacer = "{>"
|
||||||
emptyStringReplacer = "-"
|
EmptyStringReplacer = "-"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue