Exemple #1
0
func EndpointFromUrl(id string, u string) (*VulcanEndpoint, error) {
	url, err := netutils.ParseUrl(u)
	if err != nil {
		return nil, err
	}
	return &VulcanEndpoint{Url: url, Id: id}, nil
}
func ParseUrl(in string) (*HttpEndpoint, error) {
	url, err := netutils.ParseUrl(in)
	if err != nil {
		return nil, err
	}
	return &HttpEndpoint{url: url, id: fmt.Sprintf("%s://%s", url.Scheme, url.Host)}, nil
}
Exemple #3
0
func NewRedirectFallback(r Redirect) (*RedirectFallback, error) {
	u, err := netutils.ParseUrl(r.URL)
	if err != nil {
		return nil, err
	}
	return &RedirectFallback{u: u}, nil
}
func (r *RoundRobin) FindEndpointByUrl(url string) *WeightedEndpoint {
	out, err := netutils.ParseUrl(url)
	if err != nil {
		return nil
	}
	found, _ := r.findEndpointByUrl(out)
	return found
}
func NewUpstreamFromString(in string) (*Upstream, error) {
	//To ensure that upstream is correct url
	parsedUrl, err := netutils.ParseUrl(in)
	if err != nil {
		return nil, err
	}
	return NewUpstreamFromUrl(parsedUrl)
}
Exemple #6
0
// Test scenario when middleware redirects the request
func (s *LocSuite) TestMiddlewareRedirectsRequest(c *C) {
	server1 := NewTestServer(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hi, I'm endpoint 1"))
	})
	defer server1.Close()

	server2 := NewTestServer(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hi, I'm endpoint 2"))
	})
	defer server2.Close()

	rr1, rr2 := s.newRoundRobin(server1.URL), s.newRoundRobin(server2.URL)

	loc1, err := NewLocationWithOptions("loc1", rr1, Options{})
	c.Assert(err, IsNil)

	loc2, err := NewLocationWithOptions("loc2", rr2, Options{})
	c.Assert(err, IsNil)

	m := hostroute.NewHostRouter()
	route1 := exproute.NewExpRouter()
	c.Assert(route1.AddLocation(`TrieRoute("/loc1")`, loc1), IsNil)

	route2 := exproute.NewExpRouter()
	c.Assert(route2.AddLocation(`TrieRoute("/loc2")`, loc2), IsNil)

	m.SetRouter("localhost1", route1)
	m.SetRouter("localhost2", route2)

	p, err := vulcan.NewProxy(m)
	c.Assert(err, IsNil)

	proxy := httptest.NewServer(p)
	defer proxy.Close()

	redirect := &MiddlewareWrapper{
		OnRequest: func(r Request) (*http.Response, error) {
			u, err := netutils.ParseUrl("http://localhost2/loc2")
			if err != nil {
				return nil, err
			}
			return nil, &errors.RedirectError{URL: u}
		},
		OnResponse: func(r Request, a Attempt) {
		},
	}

	loc1.GetMiddlewareChain().Add("redir", 0, redirect)

	response, bodyBytes, err := MakeRequest(proxy.URL+"/loc1", Opts{Host: "localhost1"})
	c.Assert(err, IsNil)
	c.Assert(response.StatusCode, Equals, http.StatusOK)
	c.Assert(string(bodyBytes), Equals, "Hi, I'm endpoint 2")
}
Exemple #7
0
func NewWebhookSideEffect(w Webhook) (*WebhookSideEffect, error) {
	if w.Method == "" {
		return nil, fmt.Errorf("Supply method")
	}
	_, err := netutils.ParseUrl(w.URL)
	if err != nil {
		return nil, err
	}

	return &WebhookSideEffect{w: w}, nil
}
Exemple #8
0
func (c *ProxyController) DrainUpstreamConnections(w http.ResponseWriter, r *http.Request, params map[string]string) (interface{}, error) {
	upstream, err := c.backend.GetUpstream(params["id"])
	if err != nil {
		return nil, err
	}
	if upstream == nil {
		return nil, api.NotFoundError{Description: "Upstream not found"}
	}

	timeoutS, err := api.GetStringField(r, "timeout")
	if err != nil {
		return nil, err
	}

	timeout, err := strconv.Atoi(timeoutS)
	if err != nil {
		return nil, err
	}

	endpoints := make([]*url.URL, len(upstream.Endpoints))
	for i, e := range upstream.Endpoints {
		u, err := netutils.ParseUrl(e.Url)
		if err != nil {
			return nil, err
		}
		endpoints[i] = u
	}

	connections, err := c.connWatcher.DrainConnections(time.Duration(timeout)*time.Second, endpoints...)
	if err != nil {
		return nil, err
	}
	return api.Response{
		"Connections": connections,
	}, err
}