Ejemplo n.º 1
0
Archivo: http.go Proyecto: erkl/relay
func scrubRequest(req *heat.Request) error {
	// Only ever send HTTP/1.1 requests.
	req.Major = 1
	req.Minor = 1

	// Work out the request body size.
	size, err := heat.RequestBodySize(req)
	if err != nil {
		return err
	}

	scrubHeaderFields(&req.Fields, size)
	return nil
}
Ejemplo n.º 2
0
Archivo: http.go Proyecto: erkl/relay
func (p *Proxy) proxy(req *heat.Request) (*heat.Response, error) {
	if req.Body != nil {
		defer req.Body.Close()
	}

	// Extract the destination URL.
	u, err := url.ParseRequestURI(req.URI)
	if err != nil {
		return statusResponse(400, "Invalid URI in request."), nil
	}

	// Make sure the request's URI is absolute.
	if !u.IsAbs() {
		return statusResponse(400, "Request URI must be absolute."), nil
	}

	// Clean the request.
	err = scrubRequest(req)
	if err != nil {
		return statusResponse(500, "Could not scrub request."), nil
	}

	// Update the request to reflect the actual destination.
	req.URI = u.RequestURI()
	req.Scheme = u.Scheme
	req.Remote = u.Host

	// Issue the actual request.
	resp, err := p.RoundTrip(req)
	if err != nil {
		return statusResponse(500, "Round-trip to upstream failed: %s.", err), nil
	}

	// Clean the response.
	err = scrubResponse(resp, req.Method)
	if err != nil {
		return statusResponse(500, "Could not scrub response."), nil
	}

	return resp, nil
}