Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
6a0dec0e
Commit
6a0dec0e
authored
Feb 03, 2015
by
Anthony Yeh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewrite "Location" header in apiserver proxy.
So that redirects work correctly.
parent
52886de1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
34 deletions
+86
-34
proxy.go
pkg/apiserver/proxy.go
+37
-26
proxy_test.go
pkg/apiserver/proxy_test.go
+49
-8
No files found.
pkg/apiserver/proxy.go
View file @
6a0dec0e
...
@@ -185,6 +185,10 @@ func (t *proxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
...
@@ -185,6 +185,10 @@ func (t *proxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return
resp
,
nil
return
resp
,
nil
}
}
if
redirect
:=
resp
.
Header
.
Get
(
"Location"
);
redirect
!=
""
{
resp
.
Header
.
Set
(
"Location"
,
t
.
rewriteURL
(
redirect
,
req
.
URL
))
}
cType
:=
resp
.
Header
.
Get
(
"Content-Type"
)
cType
:=
resp
.
Header
.
Get
(
"Content-Type"
)
cType
=
strings
.
TrimSpace
(
strings
.
SplitN
(
cType
,
";"
,
2
)[
0
])
cType
=
strings
.
TrimSpace
(
strings
.
SplitN
(
cType
,
";"
,
2
)[
0
])
if
cType
!=
"text/html"
{
if
cType
!=
"text/html"
{
...
@@ -195,6 +199,38 @@ func (t *proxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
...
@@ -195,6 +199,38 @@ func (t *proxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return
t
.
fixLinks
(
req
,
resp
)
return
t
.
fixLinks
(
req
,
resp
)
}
}
// rewriteURL rewrites a single URL to go through the proxy, if the URL refers
// to the same host as sourceURL, which is the page on which the target URL
// occurred. If any error occurs (e.g. parsing), it returns targetURL.
func
(
t
*
proxyTransport
)
rewriteURL
(
targetURL
string
,
sourceURL
*
url
.
URL
)
string
{
url
,
err
:=
url
.
Parse
(
targetURL
)
if
err
!=
nil
{
return
targetURL
}
if
url
.
Host
!=
""
&&
url
.
Host
!=
sourceURL
.
Host
{
return
targetURL
}
url
.
Scheme
=
t
.
proxyScheme
url
.
Host
=
t
.
proxyHost
origPath
:=
url
.
Path
if
strings
.
HasPrefix
(
url
.
Path
,
"/"
)
{
// The path is rooted at the host. Just add proxy prepend.
url
.
Path
=
path
.
Join
(
t
.
proxyPathPrepend
,
url
.
Path
)
}
else
{
// The path is relative to sourceURL.
url
.
Path
=
path
.
Join
(
t
.
proxyPathPrepend
,
path
.
Dir
(
sourceURL
.
Path
),
url
.
Path
)
}
if
strings
.
HasSuffix
(
origPath
,
"/"
)
{
// Add back the trailing slash, which was stripped by path.Join().
url
.
Path
+=
"/"
}
return
url
.
String
()
}
// updateURLs checks and updates any of n's attributes that are listed in tagsToAttrs.
// updateURLs checks and updates any of n's attributes that are listed in tagsToAttrs.
// Any URLs found are, if they're relative, updated with the necessary changes to make
// Any URLs found are, if they're relative, updated with the necessary changes to make
// a visit to that URL also go through the proxy.
// a visit to that URL also go through the proxy.
...
@@ -212,32 +248,7 @@ func (t *proxyTransport) updateURLs(n *html.Node, sourceURL *url.URL) {
...
@@ -212,32 +248,7 @@ func (t *proxyTransport) updateURLs(n *html.Node, sourceURL *url.URL) {
if
!
attrs
.
Has
(
attr
.
Key
)
{
if
!
attrs
.
Has
(
attr
.
Key
)
{
continue
continue
}
}
url
,
err
:=
url
.
Parse
(
attr
.
Val
)
n
.
Attr
[
i
]
.
Val
=
t
.
rewriteURL
(
attr
.
Val
,
sourceURL
)
if
err
!=
nil
{
continue
}
// Is this URL referring to the same host as sourceURL?
if
url
.
Host
==
""
||
url
.
Host
==
sourceURL
.
Host
{
url
.
Scheme
=
t
.
proxyScheme
url
.
Host
=
t
.
proxyHost
origPath
:=
url
.
Path
if
strings
.
HasPrefix
(
url
.
Path
,
"/"
)
{
// The path is rooted at the host. Just add proxy prepend.
url
.
Path
=
path
.
Join
(
t
.
proxyPathPrepend
,
url
.
Path
)
}
else
{
// The path is relative to sourceURL.
url
.
Path
=
path
.
Join
(
t
.
proxyPathPrepend
,
path
.
Dir
(
sourceURL
.
Path
),
url
.
Path
)
}
if
strings
.
HasSuffix
(
origPath
,
"/"
)
{
// Add back the trailing slash, which was stripped by path.Join().
url
.
Path
+=
"/"
}
n
.
Attr
[
i
]
.
Val
=
url
.
String
()
}
}
}
}
}
...
...
pkg/apiserver/proxy_test.go
View file @
6a0dec0e
...
@@ -63,15 +63,18 @@ func TestProxyTransport(t *testing.T) {
...
@@ -63,15 +63,18 @@ func TestProxyTransport(t *testing.T) {
proxyHost
:
"foo.com"
,
proxyHost
:
"foo.com"
,
proxyPathPrepend
:
"/proxy/minion/minion1:8080"
,
proxyPathPrepend
:
"/proxy/minion/minion1:8080"
,
}
}
type
Item
struct
{
table
:=
map
[
string
]
struct
{
input
string
input
string
sourceURL
string
sourceURL
string
transport
*
proxyTransport
transport
*
proxyTransport
output
string
output
string
contentType
string
contentType
string
forwardedURI
string
forwardedURI
string
}{
redirect
string
redirectWant
string
}
table
:=
map
[
string
]
Item
{
"normal"
:
{
"normal"
:
{
input
:
`<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`
,
input
:
`<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`
,
sourceURL
:
"http://myminion.com/logs/log.log"
,
sourceURL
:
"http://myminion.com/logs/log.log"
,
...
@@ -136,9 +139,30 @@ func TestProxyTransport(t *testing.T) {
...
@@ -136,9 +139,30 @@ func TestProxyTransport(t *testing.T) {
contentType
:
"text/html"
,
contentType
:
"text/html"
,
forwardedURI
:
"/proxy/minion/minion1:10250/any/path/"
,
forwardedURI
:
"/proxy/minion/minion1:10250/any/path/"
,
},
},
"redirect rel"
:
{
sourceURL
:
"http://myminion.com/redirect"
,
transport
:
testTransport
,
redirect
:
"/redirected/target/"
,
redirectWant
:
"http://foo.com/proxy/minion/minion1:10250/redirected/target/"
,
forwardedURI
:
"/proxy/minion/minion1:10250/redirect"
,
},
"redirect abs same host"
:
{
sourceURL
:
"http://myminion.com/redirect"
,
transport
:
testTransport
,
redirect
:
"http://myminion.com/redirected/target/"
,
redirectWant
:
"http://foo.com/proxy/minion/minion1:10250/redirected/target/"
,
forwardedURI
:
"/proxy/minion/minion1:10250/redirect"
,
},
"redirect abs other host"
:
{
sourceURL
:
"http://myminion.com/redirect"
,
transport
:
testTransport
,
redirect
:
"http://example.com/redirected/target/"
,
redirectWant
:
"http://example.com/redirected/target/"
,
forwardedURI
:
"/proxy/minion/minion1:10250/redirect"
,
},
}
}
for
name
,
item
:=
range
table
{
testItem
:=
func
(
name
string
,
item
*
Item
)
{
// Canonicalize the html so we can diff.
// Canonicalize the html so we can diff.
item
.
input
=
fmtHTML
(
item
.
input
)
item
.
input
=
fmtHTML
(
item
.
input
)
item
.
output
=
fmtHTML
(
item
.
output
)
item
.
output
=
fmtHTML
(
item
.
output
)
...
@@ -156,34 +180,51 @@ func TestProxyTransport(t *testing.T) {
...
@@ -156,34 +180,51 @@ func TestProxyTransport(t *testing.T) {
}
}
// Send response.
// Send response.
if
item
.
redirect
!=
""
{
http
.
Redirect
(
w
,
r
,
item
.
redirect
,
http
.
StatusMovedPermanently
)
return
}
w
.
Header
()
.
Set
(
"Content-Type"
,
item
.
contentType
)
w
.
Header
()
.
Set
(
"Content-Type"
,
item
.
contentType
)
fmt
.
Fprint
(
w
,
item
.
input
)
fmt
.
Fprint
(
w
,
item
.
input
)
}))
}))
defer
server
.
Close
()
// Replace source URL with our test server address.
// Replace source URL with our test server address.
sourceURL
:=
parseURLOrDie
(
item
.
sourceURL
)
sourceURL
:=
parseURLOrDie
(
item
.
sourceURL
)
serverURL
:=
parseURLOrDie
(
server
.
URL
)
serverURL
:=
parseURLOrDie
(
server
.
URL
)
item
.
input
=
strings
.
Replace
(
item
.
input
,
sourceURL
.
Host
,
serverURL
.
Host
,
-
1
)
item
.
input
=
strings
.
Replace
(
item
.
input
,
sourceURL
.
Host
,
serverURL
.
Host
,
-
1
)
item
.
redirect
=
strings
.
Replace
(
item
.
redirect
,
sourceURL
.
Host
,
serverURL
.
Host
,
-
1
)
sourceURL
.
Host
=
serverURL
.
Host
sourceURL
.
Host
=
serverURL
.
Host
req
,
err
:=
http
.
NewRequest
(
"GET"
,
sourceURL
.
String
(),
nil
)
req
,
err
:=
http
.
NewRequest
(
"GET"
,
sourceURL
.
String
(),
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"%v: Unexpected error: %v"
,
name
,
err
)
t
.
Errorf
(
"%v: Unexpected error: %v"
,
name
,
err
)
continue
return
}
}
resp
,
err
:=
item
.
transport
.
RoundTrip
(
req
)
resp
,
err
:=
item
.
transport
.
RoundTrip
(
req
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"%v: Unexpected error: %v"
,
name
,
err
)
t
.
Errorf
(
"%v: Unexpected error: %v"
,
name
,
err
)
continue
return
}
if
item
.
redirect
!=
""
{
// Check that redirect URLs get rewritten properly.
if
got
,
want
:=
resp
.
Header
.
Get
(
"Location"
),
item
.
redirectWant
;
got
!=
want
{
t
.
Errorf
(
"%v: Location header = %q, want %q"
,
name
,
got
,
want
)
}
return
}
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"%v: Unexpected error: %v"
,
name
,
err
)
t
.
Errorf
(
"%v: Unexpected error: %v"
,
name
,
err
)
continue
return
}
}
if
e
,
a
:=
item
.
output
,
string
(
body
);
e
!=
a
{
if
e
,
a
:=
item
.
output
,
string
(
body
);
e
!=
a
{
t
.
Errorf
(
"%v: expected %v, but got %v"
,
name
,
e
,
a
)
t
.
Errorf
(
"%v: expected %v, but got %v"
,
name
,
e
,
a
)
}
}
server
.
Close
()
}
for
name
,
item
:=
range
table
{
testItem
(
name
,
&
item
)
}
}
}
}
...
...
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