Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue: #345 - fixes resp == nil: #348

Merged
merged 2 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,28 @@ func (proxy *ProxyHttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
if err != nil {
ctx.Error = err
resp = proxy.filterResponse(nil, ctx)
if resp == nil {
ctx.Logf("error read response %v %v:", r.URL.Host, err.Error())
http.Error(w, err.Error(), 500)
return
}

}
if resp != nil {
ctx.Logf("Received response %v", resp.Status)
}
ctx.Logf("Received response %v", resp.Status)
}
origBody := resp.Body
resp = proxy.filterResponse(resp, ctx)

if resp == nil {
var errorString string
if ctx.Error != nil {
errorString = "error read response " + r.URL.Host + " : " + ctx.Error.Error()
ctx.Logf(errorString)
http.Error(w, ctx.Error.Error(), 500)
} else {
errorString = "error read response " + r.URL.Host
ctx.Logf(errorString)
http.Error(w, errorString, 500)
}
return
}
origBody := resp.Body
defer origBody.Close()
ctx.Logf("Copying response to client %v [%d]", resp.Status, resp.StatusCode)
// http.ResponseWriter will take care of filling the correct response length
Expand Down
57 changes: 54 additions & 3 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package goproxy_test
import (
"bufio"
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"fmt"
"github.com/elazarl/goproxy"
goproxy_image "github.com/elazarl/goproxy/ext/image"
"image"
"io"
"io/ioutil"
Expand All @@ -16,11 +19,10 @@ import (
"net/url"
"os"
"os/exec"
"regexp"
"strings"
"testing"

"github.com/elazarl/goproxy"
goproxy_image "github.com/elazarl/goproxy/ext/image"
"time"
)

var acceptAllCerts = &tls.Config{InsecureSkipVerify: true}
Expand Down Expand Up @@ -919,3 +921,52 @@ func TestHttpsMitmURLRewrite(t *testing.T) {
}
}
}

func returnNil(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
return nil
}

func TestSimpleHttpRequest(t *testing.T) {
proxy := goproxy.NewProxyHttpServer()

var server *http.Server
go func() {
fmt.Println("serving end proxy server at localhost:5000")
server = &http.Server{
Addr: "localhost:5000",
Handler: proxy,
}
err := server.ListenAndServe()
if err == nil {
t.Error("Error shutdown should always return error", err)
}
}()

time.Sleep(1 * time.Second)
u, _ := url.Parse("http://localhost:5000")
tr := &http.Transport{
Proxy: http.ProxyURL(u),
// Disable HTTP/2.
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
client := http.Client{Transport: tr}

resp, err := client.Get("http://google.de")
if err != nil || resp.StatusCode != 200 {
t.Error("Error while requesting google with http", err)
}
resp, err = client.Get("http://google20012312031.de")
fmt.Println(resp)
if resp == nil {
t.Error("Error while requesting random string with http", resp)
}
proxy.OnResponse(goproxy.UrlMatches(regexp.MustCompile(".*"))).DoFunc(returnNil)

resp, err = client.Get("http://google20012312031.de")
fmt.Println(resp)
if resp == nil {
t.Error("Error while requesting random string with http", resp)
}

server.Shutdown(context.TODO())
}