Example #1
0
func canHash(req *http.Request) (headResp *http.Response, etag string, hashingOk bool) {
	if req.Method != "GET" {
		return
	}
	if vias, ok := req.Header["Via"]; ok {
		for _, v := range vias {
			if v == proxy.MyVia {
				// TODO: We should error out here probably
				return
			}
		}
	}

	headReq := proxy.SanitizeRequest(req)
	headReq.Method = "HEAD"
	headReq.Body = nil
	headReq.ContentLength = 0
	headResp, err := http.DefaultTransport.RoundTrip(headReq)
	if err != nil {
		log.Printf("Head request to %s failed: %v", req.URL, err)
		return
	}
	headResp.Body.Close()
	etag = headResp.Header.Get("ETag")
	// TODO: Maybe check that it is well-formed?
	if etag == "" || etag[0] == 'W' {
		// We require strong ETags to be sure that the hasher received the same entity.
		return
	}
	// FIXME: Cache-Control
	return headResp, etag, true
}
Example #2
0
func directProxy(rw http.ResponseWriter, req *http.Request) {
	r := proxy.SanitizeRequest(req)
	resp, err := http.DefaultTransport.RoundTrip(r)
	if err != nil {
		http.Error(rw, fmt.Sprintf("Proxy error: %v", err), http.StatusInternalServerError)
		return
	}
	defer resp.Body.Close()

	proxy.SanitizeResponseHeaders(rw, resp)
	io.Copy(rw, resp.Body)
}