MM-40450: Follow redirects in image proxy (#20516)
For the local proxy, we weren't following redirects. We remove the CheckRedirect function and use the default function, which is good enough for our purposes. https://mattermost.atlassian.net/browse/MM-40450 ```release-note NONE ```
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
5dfe275fe6
Коммит
b5dec4f4d9
@@ -64,9 +64,6 @@ func makeLocalBackend(proxy *ImageProxy) *LocalBackend {
|
||||
}
|
||||
|
||||
client := proxy.HTTPService.MakeClient(false)
|
||||
client.CheckRedirect = func(newreq *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
|
||||
return &LocalBackend{
|
||||
proxy: proxy,
|
||||
|
||||
@@ -193,6 +193,37 @@ func TestLocalBackend_GetImage(t *testing.T) {
|
||||
_, err = ioutil.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Redirect", func(t *testing.T) {
|
||||
var mock *httptest.Server
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/image.png":
|
||||
w.Header().Set("Location", mock.URL+"/image2.png")
|
||||
w.WriteHeader(http.StatusMovedPermanently)
|
||||
case "/image2.png":
|
||||
w.Header().Set("Cache-Control", "max-age=2592000, private")
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
w.Header().Set("Content-Length", "10")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("1111111111"))
|
||||
}
|
||||
})
|
||||
|
||||
mock = httptest.NewServer(handler)
|
||||
defer mock.Close()
|
||||
|
||||
proxy := makeTestLocalProxy()
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request, _ := http.NewRequest(http.MethodGet, "", nil)
|
||||
proxy.GetImage(recorder, request, mock.URL+"/image.png")
|
||||
resp := recorder.Result()
|
||||
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
assert.Equal(t, "10", resp.Header.Get("Content-Length"))
|
||||
})
|
||||
}
|
||||
|
||||
func TestLocalBackend_GetImageDirect(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user