oxy icon indicating copy to clipboard operation
oxy copied to clipboard

Empty body with chunked encoding will return 500 Internal server error

Open vincentob opened this issue 2 years ago • 0 comments

Hi guys,

I found that oxy/buffer can't handle response with chunked encoding and empty body. I can reproduce it by adding the testcase below:

func TestEmptyChunkedResponse(t *testing.T) {
	srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) {
		h := w.(http.Hijacker)
		conn, _, _ := h.Hijack()
		_, _ = fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n")
		_ = conn.Close()
	})
	t.Cleanup(srv.Close)

	fwd := forward.New(false)

	rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		req.URL = testutils.ParseURI(srv.URL)
		fwd.ServeHTTP(w, req)
	})
	st, err := New(rdr)
	require.NoError(t, err)
	proxy := httptest.NewServer(st)

	t.Cleanup(proxy.Close)

	re, body, err := testutils.Get(proxy.URL)
	require.NoError(t, err)
	assert.Equal(t, "", string(body))
	assert.Equal(t, http.StatusOK, re.StatusCode)
}

I have test proxys like nginx and traefik with curl, they all looks fine. But if i use oxy/buffer, it will return 500 Internal Server Error

vincentob avatar Oct 15 '23 16:10 vincentob