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

Using upstream hostname for request (#294) #301

Merged
merged 6 commits into from
May 31, 2017

Conversation

mitchelldavis
Copy link
Contributor

No description provided.

Copy link
Contributor

@magiconair magiconair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable. A couple of comments;

  1. Please roll the two tests into one. You can define a routing table with two entries and then use t.Run subtests to test the two cases.

  2. For boolean flags the =true or =false is redundant since the existence or non-existence of the option is sufficient. In route.go you can use _, t.UseUpstreamHostame = r.Opts["..."] to achieve this.

  3. I think the name UseUpstreamHostname is too long. Can you think of something shorter and less verbose, e.g. fwdhost, usehost, upstreamhost, ... I don't have better ideas either.

@mitchelldavis
Copy link
Contributor Author

  • So if the two tests are redundant, should I roll them into one or just get rid of the second?
  • I set UseUpstreamHostname on line 73 in route.go. Is that not correct?
  • usehost works for me and the only one I can come up with otherwise is resetHost.

@magiconair
Copy link
Contributor

You can replace the tests with this:

func TestProxyUsehost(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, r.Host)
	}))

	proxy := httptest.NewServer(&HTTPProxy{
		Transport: &http.Transport{
			Dial: func(network, addr string) (net.Conn, error) {
				addr = server.URL[len("http://"):]
				return net.Dial(network, addr)
			},
		},
		Lookup: func(r *http.Request) *route.Target {
			routes := "route add mock /usehost http://a.com/ opts \"usehost\"\n"
			routes += "route add mock / http://a.com/"
			tbl, _ := route.NewTable(routes)
			return tbl.Lookup(r, "", route.Picker["rr"], route.Matcher["prefix"])
		},
	})
	defer proxy.Close()

	check := func(uri, host string) {
		resp, body := mustGet(proxy.URL + uri)
		if got, want := resp.StatusCode, http.StatusOK; got != want {
			t.Fatalf("got status %d want %d", got, want)
		}
		if got, want := string(body), host; got != want {
			t.Fatalf("got body %q want %q", got, want)
		}
	}

	proxyHost := proxy.URL[len("http://"):]
	t.Run("use host", func(t *testing.T) { check("/usehost", "a.com") })
	t.Run("no host", func(t *testing.T) { check("/", proxyHost) })
}
  1. I mean to use usehost instead of usehost=(true|false). This changes on how you set the t.UseHost flag since checking for an empty string is not sufficient.

  2. usehost it is then. Pls also use t.UseHost internally.

Thx a lot

Copy link
Contributor

@magiconair magiconair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some small comments. The rest looks good.

route/route.go Outdated
@@ -71,7 +71,11 @@ func (r *Route) addTarget(service string, targetURL *url.URL, fixedWeight float6
if r.Opts != nil {
t.StripPath = r.Opts["strip"]
t.TLSSkipVerify = r.Opts["tlsskipverify"] == "true"
t.UseUpstreamHostname = r.Opts["useupstreamhostname"] == "true"
// If the 'usehost' key exists in the map, we set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant. Just use what I wrote initially

_, t.UseHost = r.Opts["usehost"]

route/target.go Outdated
@@ -21,10 +21,10 @@ type Target struct {
// TLS connections.
TLSSkipVerify bool

// UseUpstreamHostname causes the origin host header to be
// UseHost causes the origin host header to be
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UseHost determines whether the the origin or the target hostname
is used for the upstream request

route/route.go Outdated
@@ -73,9 +73,7 @@ func (r *Route) addTarget(service string, targetURL *url.URL, fixedWeight float6
t.TLSSkipVerify = r.Opts["tlsskipverify"] == "true"
// If the 'usehost' key exists in the map, we set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you drop the comment, too please? Don't want to be overly pick but it really is redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No Worries. It looks like I need to rerun the build anyways. :-D

@magiconair
Copy link
Contributor

I'll let this sit for another day or two before I merge it. usehost is concise but also ambiguous. I'll see whether I can come up with something better. Thx for your patience.

@mitchelldavis
Copy link
Contributor Author

@magiconair, how about usedesthost?

@magiconair
Copy link
Contributor

How about host=dst?

@mitchelldavis
Copy link
Contributor Author

ooooo.... that's way better. Default will be src?

@magiconair
Copy link
Contributor

Yes. I think that makes more sense and provides some flexibility for the future. boolean flags are always somewhat limiting unless you can be absolutely sure that there will ever only be two choices.

@magiconair
Copy link
Contributor

Can you make that change?

@mitchelldavis
Copy link
Contributor Author

I'm on it!

@magiconair
Copy link
Contributor

Two small changes and then we can merge it.

@mitchelldavis
Copy link
Contributor Author

Thats a bummer. It looks like an unstable test. :-(

@magiconair magiconair merged commit b14a199 into fabiolb:master May 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants