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
4ab8f238
Commit
4ab8f238
authored
Dec 16, 2014
by
Daniel Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix proxy bug where it is confused by gzip encoding
parent
9b6aec5e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
15 deletions
+54
-15
proxy.go
pkg/apiserver/proxy.go
+29
-4
proxy_test.go
pkg/apiserver/proxy_test.go
+25
-11
No files found.
pkg/apiserver/proxy.go
View file @
4ab8f238
...
@@ -18,7 +18,9 @@ package apiserver
...
@@ -18,7 +18,9 @@ package apiserver
import
(
import
(
"bytes"
"bytes"
"compress/gzip"
"fmt"
"fmt"
"io"
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
"net/http/httputil"
"net/http/httputil"
...
@@ -225,17 +227,40 @@ func (t *proxyTransport) scan(n *html.Node, f func(*html.Node)) {
...
@@ -225,17 +227,40 @@ func (t *proxyTransport) scan(n *html.Node, f func(*html.Node)) {
// fixLinks modifies links in an HTML file such that they will be redirected through the proxy if needed.
// fixLinks modifies links in an HTML file such that they will be redirected through the proxy if needed.
func
(
t
*
proxyTransport
)
fixLinks
(
req
*
http
.
Request
,
resp
*
http
.
Response
)
(
*
http
.
Response
,
error
)
{
func
(
t
*
proxyTransport
)
fixLinks
(
req
*
http
.
Request
,
resp
*
http
.
Response
)
(
*
http
.
Response
,
error
)
{
defer
resp
.
Body
.
Close
()
origBody
:=
resp
.
Body
defer
origBody
.
Close
()
doc
,
err
:=
html
.
Parse
(
resp
.
Body
)
newContent
:=
&
bytes
.
Buffer
{}
var
reader
io
.
Reader
=
origBody
var
writer
io
.
Writer
=
newContent
encoding
:=
resp
.
Header
.
Get
(
"Content-Encoding"
)
switch
encoding
{
case
"gzip"
:
var
err
error
reader
,
err
=
gzip
.
NewReader
(
reader
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"errorf making gzip reader: %v"
,
err
)
}
gzw
:=
gzip
.
NewWriter
(
writer
)
defer
gzw
.
Close
()
writer
=
gzw
// TODO: support flate, other encodings.
case
""
:
// This is fine
default
:
// Some encoding we don't understand-- don't try to parse this
glog
.
Errorf
(
"Proxy encountered encoding %v for text/html; can't understand this so not fixing links."
,
encoding
)
return
resp
,
nil
}
doc
,
err
:=
html
.
Parse
(
reader
)
if
err
!=
nil
{
if
err
!=
nil
{
glog
.
Errorf
(
"Parse failed: %v"
,
err
)
glog
.
Errorf
(
"Parse failed: %v"
,
err
)
return
resp
,
err
return
resp
,
err
}
}
newContent
:=
&
bytes
.
Buffer
{}
t
.
scan
(
doc
,
func
(
n
*
html
.
Node
)
{
t
.
updateURLs
(
n
,
req
.
URL
)
})
t
.
scan
(
doc
,
func
(
n
*
html
.
Node
)
{
t
.
updateURLs
(
n
,
req
.
URL
)
})
if
err
:=
html
.
Render
(
newContent
,
doc
);
err
!=
nil
{
if
err
:=
html
.
Render
(
writer
,
doc
);
err
!=
nil
{
glog
.
Errorf
(
"Failed to render: %v"
,
err
)
glog
.
Errorf
(
"Failed to render: %v"
,
err
)
}
}
...
...
pkg/apiserver/proxy_test.go
View file @
4ab8f238
...
@@ -18,7 +18,9 @@ package apiserver
...
@@ -18,7 +18,9 @@ package apiserver
import
(
import
(
"bytes"
"bytes"
"compress/gzip"
"fmt"
"fmt"
"io"
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
...
@@ -132,17 +134,19 @@ func TestProxyTransport_fixLinks(t *testing.T) {
...
@@ -132,17 +134,19 @@ func TestProxyTransport_fixLinks(t *testing.T) {
func
TestProxy
(
t
*
testing
.
T
)
{
func
TestProxy
(
t
*
testing
.
T
)
{
table
:=
[]
struct
{
table
:=
[]
struct
{
method
string
method
string
path
string
path
string
reqBody
string
reqBody
string
respBody
string
respBody
string
reqNamespace
string
respContentType
string
reqNamespace
string
}{
}{
{
"GET"
,
"/some/dir"
,
""
,
"answer"
,
"default"
},
{
"GET"
,
"/some/dir"
,
""
,
"answer"
,
"text/css"
,
"default"
},
{
"POST"
,
"/some/other/dir"
,
"question"
,
"answer"
,
"default"
},
{
"GET"
,
"/some/dir"
,
""
,
"<html><head></head><body>answer</body></html>"
,
"text/html"
,
"default"
},
{
"PUT"
,
"/some/dir/id"
,
"different question"
,
"answer"
,
"default"
},
{
"POST"
,
"/some/other/dir"
,
"question"
,
"answer"
,
"text/css"
,
"default"
},
{
"DELETE"
,
"/some/dir/id"
,
""
,
"ok"
,
"default"
},
{
"PUT"
,
"/some/dir/id"
,
"different question"
,
"answer"
,
"text/css"
,
"default"
},
{
"GET"
,
"/some/dir/id"
,
""
,
"answer"
,
"other"
},
{
"DELETE"
,
"/some/dir/id"
,
""
,
"ok"
,
"text/css"
,
"default"
},
{
"GET"
,
"/some/dir/id"
,
""
,
"answer"
,
"text/css"
,
"other"
},
}
}
for
_
,
item
:=
range
table
{
for
_
,
item
:=
range
table
{
...
@@ -157,7 +161,17 @@ func TestProxy(t *testing.T) {
...
@@ -157,7 +161,17 @@ func TestProxy(t *testing.T) {
if
e
,
a
:=
item
.
path
,
req
.
URL
.
Path
;
e
!=
a
{
if
e
,
a
:=
item
.
path
,
req
.
URL
.
Path
;
e
!=
a
{
t
.
Errorf
(
"%v - expected %v, got %v"
,
item
.
method
,
e
,
a
)
t
.
Errorf
(
"%v - expected %v, got %v"
,
item
.
method
,
e
,
a
)
}
}
fmt
.
Fprint
(
w
,
item
.
respBody
)
w
.
Header
()
.
Set
(
"Content-Type"
,
item
.
respContentType
)
var
out
io
.
Writer
=
w
if
strings
.
Contains
(
req
.
Header
.
Get
(
"Accept-Encoding"
),
"gzip"
)
{
// The proxier can ask for gzip'd data; we need to provide it with that
// in order to test our processing of that data.
w
.
Header
()
.
Set
(
"Content-Encoding"
,
"gzip"
)
gzw
:=
gzip
.
NewWriter
(
w
)
out
=
gzw
defer
gzw
.
Close
()
}
fmt
.
Fprint
(
out
,
item
.
respBody
)
}))
}))
defer
proxyServer
.
Close
()
defer
proxyServer
.
Close
()
...
...
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