mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 09:49:50 +00:00
Merge pull request #3514 from enisoc/apiserver-proxy-content-type
Allow Content-Type with charset in apiserver proxy.
This commit is contained in:
commit
aeab434400
@ -174,7 +174,9 @@ func (t *proxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Header.Get("Content-Type") != "text/html" {
|
cType := resp.Header.Get("Content-Type")
|
||||||
|
cType = strings.TrimSpace(strings.SplitN(cType, ";", 2)[0])
|
||||||
|
if cType != "text/html" {
|
||||||
// Do nothing, simply pass through
|
// Do nothing, simply pass through
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ func fmtHTML(in string) string {
|
|||||||
return string(out.Bytes())
|
return string(out.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProxyTransport_fixLinks(t *testing.T) {
|
func TestProxyTransport(t *testing.T) {
|
||||||
testTransport := &proxyTransport{
|
testTransport := &proxyTransport{
|
||||||
proxyScheme: "http",
|
proxyScheme: "http",
|
||||||
proxyHost: "foo.com",
|
proxyHost: "foo.com",
|
||||||
@ -65,40 +65,60 @@ func TestProxyTransport_fixLinks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
table := map[string]struct {
|
table := map[string]struct {
|
||||||
input string
|
input string
|
||||||
sourceURL string
|
sourceURL string
|
||||||
transport *proxyTransport
|
transport *proxyTransport
|
||||||
output string
|
output string
|
||||||
|
contentType string
|
||||||
}{
|
}{
|
||||||
"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",
|
||||||
transport: testTransport,
|
transport: testTransport,
|
||||||
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/logs/google.log">google.log</a></pre>`,
|
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/logs/google.log">google.log</a></pre>`,
|
||||||
|
contentType: "text/html",
|
||||||
|
},
|
||||||
|
"content-type charset": {
|
||||||
|
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
||||||
|
sourceURL: "http://myminion.com/logs/log.log",
|
||||||
|
transport: testTransport,
|
||||||
|
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/logs/google.log">google.log</a></pre>`,
|
||||||
|
contentType: "text/html; charset=utf-8",
|
||||||
|
},
|
||||||
|
"content-type passthrough": {
|
||||||
|
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
||||||
|
sourceURL: "http://myminion.com/logs/log.log",
|
||||||
|
transport: testTransport,
|
||||||
|
output: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
||||||
|
contentType: "text/plain",
|
||||||
},
|
},
|
||||||
"subdir": {
|
"subdir": {
|
||||||
input: `<a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a>`,
|
input: `<a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a>`,
|
||||||
sourceURL: "http://myminion.com/whatever/apt/somelog.log",
|
sourceURL: "http://myminion.com/whatever/apt/somelog.log",
|
||||||
transport: testTransport2,
|
transport: testTransport2,
|
||||||
output: `<a href="https://foo.com/proxy/minion/minion1:8080/whatever/apt/kubelet.log">kubelet.log</a><a href="https://foo.com/proxy/minion/minion1:8080/whatever/apt/google.log">google.log</a>`,
|
output: `<a href="https://foo.com/proxy/minion/minion1:8080/whatever/apt/kubelet.log">kubelet.log</a><a href="https://foo.com/proxy/minion/minion1:8080/whatever/apt/google.log">google.log</a>`,
|
||||||
|
contentType: "text/html",
|
||||||
},
|
},
|
||||||
"image": {
|
"image": {
|
||||||
input: `<pre><img src="kubernetes.jpg"/></pre>`,
|
input: `<pre><img src="kubernetes.jpg"/></pre>`,
|
||||||
sourceURL: "http://myminion.com/",
|
sourceURL: "http://myminion.com/",
|
||||||
transport: testTransport,
|
transport: testTransport,
|
||||||
output: `<pre><img src="http://foo.com/proxy/minion/minion1:10250/kubernetes.jpg"/></pre>`,
|
output: `<pre><img src="http://foo.com/proxy/minion/minion1:10250/kubernetes.jpg"/></pre>`,
|
||||||
|
contentType: "text/html",
|
||||||
},
|
},
|
||||||
"abs": {
|
"abs": {
|
||||||
input: `<script src="http://google.com/kubernetes.js"/>`,
|
input: `<script src="http://google.com/kubernetes.js"/>`,
|
||||||
sourceURL: "http://myminion.com/any/path/",
|
sourceURL: "http://myminion.com/any/path/",
|
||||||
transport: testTransport,
|
transport: testTransport,
|
||||||
output: `<script src="http://google.com/kubernetes.js"/>`,
|
output: `<script src="http://google.com/kubernetes.js"/>`,
|
||||||
|
contentType: "text/html",
|
||||||
},
|
},
|
||||||
"abs but same host": {
|
"abs but same host": {
|
||||||
input: `<script src="http://myminion.com/kubernetes.js"/>`,
|
input: `<script src="http://myminion.com/kubernetes.js"/>`,
|
||||||
sourceURL: "http://myminion.com/any/path/",
|
sourceURL: "http://myminion.com/any/path/",
|
||||||
transport: testTransport,
|
transport: testTransport,
|
||||||
output: `<script src="http://foo.com/proxy/minion/minion1:10250/kubernetes.js"/>`,
|
output: `<script src="http://foo.com/proxy/minion/minion1:10250/kubernetes.js"/>`,
|
||||||
|
contentType: "text/html",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,22 +126,28 @@ func TestProxyTransport_fixLinks(t *testing.T) {
|
|||||||
// 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)
|
||||||
req := &http.Request{
|
|
||||||
Method: "GET",
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
URL: parseURLOrDie(item.sourceURL),
|
w.Header().Set("Content-Type", item.contentType)
|
||||||
}
|
fmt.Fprint(w, item.input)
|
||||||
resp := &http.Response{
|
}))
|
||||||
Status: "200 OK",
|
// Replace source URL with our test server address.
|
||||||
StatusCode: http.StatusOK,
|
sourceURL := parseURLOrDie(item.sourceURL)
|
||||||
Body: ioutil.NopCloser(strings.NewReader(item.input)),
|
serverURL := parseURLOrDie(server.URL)
|
||||||
Close: true,
|
item.input = strings.Replace(item.input, sourceURL.Host, serverURL.Host, -1)
|
||||||
}
|
sourceURL.Host = serverURL.Host
|
||||||
updatedResp, err := item.transport.fixLinks(req, resp)
|
|
||||||
|
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
|
continue
|
||||||
}
|
}
|
||||||
body, err := ioutil.ReadAll(updatedResp.Body)
|
resp, err := item.transport.RoundTrip(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%v: Unexpected error: %v", name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
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
|
continue
|
||||||
@ -129,6 +155,7 @@ func TestProxyTransport_fixLinks(t *testing.T) {
|
|||||||
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user