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
264e5b79
Commit
264e5b79
authored
May 03, 2015
by
Nimi Wariboko Jr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use the provided Replacer tools in order to proxy string interpolation.
parent
a28d5585
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
64 deletions
+14
-64
middleware/proxy/proxy.go
middleware/proxy/proxy.go
+14
-64
No files found.
middleware/proxy/proxy.go
View file @
264e5b79
...
...
@@ -4,11 +4,8 @@ package proxy
import
(
"errors"
"github.com/mholt/caddy/middleware"
"net"
"net/http"
"net/url"
"regexp"
"strings"
"sync/atomic"
"time"
)
...
...
@@ -34,6 +31,7 @@ type UpstreamHostDownFunc func(*UpstreamHost) bool
// An UpstreamHost represents a single proxy upstream
type
UpstreamHost
struct
{
// The hostname of this upstream host
Name
string
ReverseProxy
*
ReverseProxy
Conns
int64
...
...
@@ -52,81 +50,27 @@ func (uh *UpstreamHost) Down() bool {
return
uh
.
CheckDown
(
uh
)
}
//https://github.com/mgutz/str
var
tRe
=
regexp
.
MustCompile
(
`([\-\[\]()*\s])`
)
var
tRe2
=
regexp
.
MustCompile
(
`\$`
)
var
openDelim
=
tRe2
.
ReplaceAllString
(
tRe
.
ReplaceAllString
(
"{{"
,
"
\\
$1"
),
"
\\
$"
)
var
closDelim
=
tRe2
.
ReplaceAllString
(
tRe
.
ReplaceAllString
(
"}}"
,
"
\\
$1"
),
"
\\
$"
)
var
templateDelim
=
regexp
.
MustCompile
(
openDelim
+
`(.+?)`
+
closDelim
)
type
requestVars
struct
{
Host
string
RemoteIp
string
Scheme
string
Upstream
string
UpstreamHost
string
}
func
templateWithDelimiters
(
s
string
,
vars
requestVars
)
string
{
matches
:=
templateDelim
.
FindAllStringSubmatch
(
s
,
-
1
)
for
_
,
submatches
:=
range
matches
{
match
:=
submatches
[
0
]
key
:=
submatches
[
1
]
found
:=
true
repl
:=
""
switch
key
{
case
"http_host"
:
repl
=
vars
.
Host
case
"remote_addr"
:
repl
=
vars
.
RemoteIp
case
"scheme"
:
repl
=
vars
.
Scheme
case
"upstream"
:
repl
=
vars
.
Upstream
case
"upstream_host"
:
repl
=
vars
.
UpstreamHost
default
:
found
=
false
}
if
found
{
s
=
strings
.
Replace
(
s
,
match
,
repl
,
-
1
)
}
}
return
s
}
// ServeHTTP satisfies the middleware.Handler interface.
func
(
p
Proxy
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
_
,
upstream
:=
range
p
.
Upstreams
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
upstream
.
From
())
{
vars
:=
requestVars
{
Host
:
r
.
Host
,
Scheme
:
"http"
,
}
if
clientIP
,
_
,
err
:=
net
.
SplitHostPort
(
r
.
RemoteAddr
);
err
==
nil
{
vars
.
RemoteIp
=
clientIP
}
if
fFor
:=
r
.
Header
.
Get
(
"X-Forwarded-For"
);
fFor
!=
""
{
vars
.
RemoteIp
=
fFor
}
if
r
.
TLS
!=
nil
{
vars
.
Scheme
=
"https"
}
var
replacer
middleware
.
Replacer
start
:=
time
.
Now
()
requestHost
:=
r
.
Host
// Since Select() should give us "up" hosts, keep retrying
// hosts until timeout (or until we get a nil host).
start
:=
time
.
Now
()
for
time
.
Now
()
.
Sub
(
start
)
<
(
60
*
time
.
Second
)
{
host
:=
upstream
.
Select
()
if
host
==
nil
{
return
http
.
StatusBadGateway
,
errUnreachable
}
proxy
:=
host
.
ReverseProxy
vars
.
Upstream
=
host
.
Name
r
.
Host
=
host
.
Name
if
baseUrl
,
err
:=
url
.
Parse
(
host
.
Name
);
err
==
nil
{
vars
.
Upstream
Host
=
baseUrl
.
Host
r
.
Host
=
baseUrl
.
Host
if
proxy
==
nil
{
proxy
=
NewSingleHostReverseProxy
(
baseUrl
)
}
...
...
@@ -136,12 +80,18 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
var
extraHeaders
http
.
Header
if
host
.
ExtraHeaders
!=
nil
{
extraHeaders
=
make
(
http
.
Header
)
if
replacer
==
nil
{
rHost
:=
r
.
Host
r
.
Host
=
requestHost
replacer
=
middleware
.
NewReplacer
(
r
,
nil
)
r
.
Host
=
rHost
}
for
header
,
values
:=
range
host
.
ExtraHeaders
{
for
_
,
value
:=
range
values
{
extraHeaders
.
Add
(
header
,
templateWithDelimiters
(
value
,
vars
))
replacer
.
Replace
(
value
))
if
header
==
"Host"
{
r
.
Host
=
templateWithDelimiters
(
value
,
vars
)
r
.
Host
=
replacer
.
Replace
(
value
)
}
}
}
...
...
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