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
80dd95a4
Commit
80dd95a4
authored
Jun 28, 2016
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change outreq.Host instead of r.Host (possibly related to #874)
Also a few little formatting changes and comments.
parent
5a457192
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
11 deletions
+24
-11
caddyhttp/proxy/proxy.go
caddyhttp/proxy/proxy.go
+22
-10
caddyhttp/proxy/upstream.go
caddyhttp/proxy/upstream.go
+1
-0
caddytls/httphandler.go
caddytls/httphandler.go
+1
-1
No files found.
caddyhttp/proxy/proxy.go
View file @
80dd95a4
...
...
@@ -77,19 +77,21 @@ var tryDuration = 60 * time.Second
// ServeHTTP satisfies the httpserver.Handler interface.
func
(
p
Proxy
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
//
S
tart by selecting most specific matching upstream config
//
s
tart by selecting most specific matching upstream config
upstream
:=
p
.
match
(
r
)
if
upstream
==
nil
{
return
p
.
Next
.
ServeHTTP
(
w
,
r
)
}
// this replacer is used to fill in header field values
var
replacer
httpserver
.
Replacer
start
:=
time
.
Now
()
// outreq is the request that makes a roundtrip to the backend
outreq
:=
createUpstreamRequest
(
r
)
//
S
ince Select() should give us "up" hosts, keep retrying
//
s
ince 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
)
<
tryDuration
{
host
:=
upstream
.
Select
()
if
host
==
nil
{
...
...
@@ -99,7 +101,11 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
rr
.
Replacer
.
Set
(
"upstream"
,
host
.
Name
)
}
// for now, assume the backend's hostname is just a hostname; we'll
// handle extra information like scheme later
outreq
.
Host
=
host
.
Name
// set headers for request going upstream
if
host
.
UpstreamHeaders
!=
nil
{
if
replacer
==
nil
{
rHost
:=
r
.
Host
...
...
@@ -109,13 +115,15 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if
v
,
ok
:=
host
.
UpstreamHeaders
[
"Host"
];
ok
{
outreq
.
Host
=
replacer
.
Replace
(
v
[
len
(
v
)
-
1
])
}
//
M
odify headers for request that will be sent to the upstream host
//
m
odify headers for request that will be sent to the upstream host
upHeaders
:=
createHeadersByRules
(
host
.
UpstreamHeaders
,
r
.
Header
,
replacer
)
for
k
,
v
:=
range
upHeaders
{
outreq
.
Header
[
k
]
=
v
}
}
// prepare a function that will update response
// headers coming back downstream
var
downHeaderUpdateFn
respUpdateFn
if
host
.
DownstreamHeaders
!=
nil
{
if
replacer
==
nil
{
...
...
@@ -123,23 +131,27 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
replacer
=
httpserver
.
NewReplacer
(
r
,
nil
,
""
)
outreq
.
Host
=
rHost
}
//Creates a function that is used to update headers the response received by the reverse proxy
downHeaderUpdateFn
=
createRespHeaderUpdateFn
(
host
.
DownstreamHeaders
,
replacer
)
}
// a backend's name may contain more than just the host,
// so we parse it as a URL so we can isolate the host.
proxy
:=
host
.
ReverseProxy
if
baseURL
,
err
:=
url
.
Parse
(
host
.
Name
);
err
==
nil
{
r
.
Host
=
bas
eURL
.
Host
if
nameURL
,
err
:=
url
.
Parse
(
outreq
.
Host
);
err
==
nil
{
outreq
.
Host
=
nam
eURL
.
Host
if
proxy
==
nil
{
proxy
=
NewSingleHostReverseProxy
(
bas
eURL
,
host
.
WithoutPathPrefix
)
proxy
=
NewSingleHostReverseProxy
(
nam
eURL
,
host
.
WithoutPathPrefix
)
}
}
else
if
proxy
==
nil
{
return
http
.
StatusInternalServerError
,
err
}
// tell the proxy to serve the request
atomic
.
AddInt64
(
&
host
.
Conns
,
1
)
backendErr
:=
proxy
.
ServeHTTP
(
w
,
outreq
,
downHeaderUpdateFn
)
atomic
.
AddInt64
(
&
host
.
Conns
,
-
1
)
// if no errors, we're done here; otherwise failover
if
backendErr
==
nil
{
return
0
,
nil
}
...
...
@@ -186,9 +198,9 @@ func createUpstreamRequest(r *http.Request) *http.Request {
outreq
.
URL
.
Opaque
=
outreq
.
URL
.
RawPath
}
// Remove hop-by-hop headers to the backend.
Especially
// Remove hop-by-hop headers to the backend. Especially
// important is "Connection" because we want a persistent
// connection, regardless of what the client sent to us.
This
// connection, regardless of what the client sent to us. This
// is modifying the same underlying map from r (shallow
// copied above) so we only copy it if necessary.
for
_
,
h
:=
range
hopHeaders
{
...
...
caddyhttp/proxy/upstream.go
View file @
80dd95a4
...
...
@@ -158,6 +158,7 @@ func (u *staticUpstream) NewHost(host string) (*UpstreamHost, error) {
if
u
.
insecureSkipVerify
{
uh
.
ReverseProxy
.
Transport
=
InsecureTransport
}
return
uh
,
nil
}
...
...
caddytls/httphandler.go
View file @
80dd95a4
...
...
@@ -34,7 +34,7 @@ func HTTPChallengeHandler(w http.ResponseWriter, r *http.Request, altPort string
proxy
:=
httputil
.
NewSingleHostReverseProxy
(
upstream
)
proxy
.
Transport
=
&
http
.
Transport
{
TLSClientConfig
:
&
tls
.
Config
{
InsecureSkipVerify
:
true
},
// solver uses self-signed certs
TLSClientConfig
:
&
tls
.
Config
{
InsecureSkipVerify
:
true
},
}
proxy
.
ServeHTTP
(
w
,
r
)
...
...
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