elazarl / goproxy

An HTTP proxy library for Go
BSD 3-Clause "New" or "Revised" License
5.99k stars 1.09k forks source link

ERR_INCOMPLETE_CHUNKED_ENCODING issue #280

Open scopdrag opened 6 years ago

scopdrag commented 6 years ago

Hi All,

Getting "ERR_INCOMPLETE_CHUNKED_ENCODING issue" issue with many of the sites. e.g: http://www.sustainablesites.org/

I am using following code to expose the proxy server. I am using this proxy server with emulators (Android), When I test these websites without proxy it works fine.

Using this for proxy setttings:

Android/sdk/emulator/emulator @Nexus_5X_API_24 -http-proxy http://127.0.0.1:32001

But after setting proxy it throws me ERR_INCOMPLETE_CHUNKED_ENCODING error

Please find the screenshot attached below.

your prompt attention will be greatly appreciated @insp3ctre , @fredcy @fastest963 @mlbright @sandrogauci @rtuin @jnormington

` package main import ( "log" "net/http" "fmt" "strings" "io/ioutil" "bytes" "gopkg.in/elazarl/goproxy.v1" ) func main() { proxy := goproxy.NewProxyHttpServer() proxy.OnRequest(goproxy.UrlHasPrefix("10.0.2.2")).DoFunc( func(r http.Request, ctx goproxy.ProxyCtx) (http.Request, http.Response) { url := r.URL.String() url = strings.Replace(url, "10.0.2.2", "localhost", 1) fresp, ferr := http.Get(url) println(ferr) return r, fresp

    })

proxy.OnRequest().HandleConnectFunc(func(host string, ctx *goproxy.ProxyCtx) 
                 (*goproxy.ConnectAction, string) {
    fmt.Println(ctx.Req.Proto)
    return goproxy.OkConnect, host
})

proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {

    readBody, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // handle error
    }
    resp.Body.Close()
    // use readBody

    resp.Body = ioutil.NopCloser(bytes.NewReader([]byte(string(readBody))))
    fmt.Println(resp.Body)

    resp.Uncompressed = true
    resp.Header.Del("Content-Length")
    resp.Header.Del("Content-Encoding")
    resp.Header.Del("Transfer-Encoding")
    resp.Header.Set("Content-Encoding", "identity")
    resp.Header.Del("Vary")
    resp.Header.Del("Link")
    resp.Header.Del("Content-Type")
    return resp
})

log.Fatalln(http.ListenAndServe(":32001", proxy))

}` screenshot_1523569845

Thanks,

rtuin commented 6 years ago

@scopdrag what happens if you try the basic example proxy and make the request? https://github.com/elazarl/goproxy/tree/master/examples/goproxy-basic

Also, have you tried to request other sites with your proxy? What happens then?

scopdrag commented 6 years ago

Hey @rtuin,

Thanks for your kind attention.

Tried with basic proxy. Getting same issue. Yes i have tried with other sites too . Mainly this issue has been found in wordpress sites (Served using MAMP server). However same is working in desktop browser fine.

package main

import (
    "github.com/elazarl/goproxy"
    "log"
    "flag"
    "net/http"
)

func main() {
    verbose := flag.Bool("v", false, "should every proxy request be logged to stdout")
    addr := flag.String("addr", ":32001", "proxy listen address")
    flag.Parse()
    proxy := goproxy.NewProxyHttpServer()
    proxy.Verbose = *verbose
    log.Fatal(http.ListenAndServe(*addr, proxy))
}
rtuin commented 6 years ago

So the outcome depends on the browser? This usually tells us that the response body isn't 100% what the HTTP headers say it is. For example, it may tell that the Content-Length value is > then the length in the body.