2020-12-02 10:09:37 -05:00
|
|
|
package sendurl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-12-08 22:09:24 -05:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2020-12-02 10:09:37 -05:00
|
|
|
|
|
|
|
"gitlab.com/gitlab-org/labkit/mask"
|
|
|
|
|
2021-07-21 11:08:52 -04:00
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
|
2021-10-20 08:13:26 -04:00
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/httptransport"
|
2021-07-21 11:08:52 -04:00
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
|
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/senddata"
|
2020-12-02 10:09:37 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type entry struct{ senddata.Prefix }
|
|
|
|
|
|
|
|
type entryParams struct {
|
|
|
|
URL string
|
|
|
|
AllowRedirects bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var SendURL = &entry{"send-url:"}
|
|
|
|
|
|
|
|
var rangeHeaderKeys = []string{
|
|
|
|
"If-Match",
|
|
|
|
"If-Unmodified-Since",
|
|
|
|
"If-None-Match",
|
|
|
|
"If-Modified-Since",
|
|
|
|
"If-Range",
|
|
|
|
"Range",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep cache headers from the original response, not the proxied response. The
|
|
|
|
// original response comes from the Rails application, which should be the
|
|
|
|
// source of truth for caching.
|
|
|
|
var preserveHeaderKeys = map[string]bool{
|
|
|
|
"Cache-Control": true,
|
|
|
|
"Expires": true,
|
|
|
|
"Date": true, // Support for HTTP 1.0 proxies
|
|
|
|
"Pragma": true, // Support for HTTP 1.0 proxies
|
|
|
|
}
|
|
|
|
|
2021-10-20 08:13:26 -04:00
|
|
|
var httpTransport = httptransport.New()
|
2020-12-02 10:09:37 -05:00
|
|
|
|
|
|
|
var httpClient = &http.Client{
|
|
|
|
Transport: httpTransport,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-12-08 22:09:24 -05:00
|
|
|
sendURLRequests = promauto.NewCounterVec(
|
2020-12-02 10:09:37 -05:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "gitlab_workhorse_send_url_requests",
|
|
|
|
Help: "How many send URL requests have been processed",
|
|
|
|
},
|
|
|
|
[]string{"status"},
|
|
|
|
)
|
2020-12-08 22:09:24 -05:00
|
|
|
sendURLOpenRequests = promauto.NewGauge(
|
2020-12-02 10:09:37 -05:00
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "gitlab_workhorse_send_url_open_requests",
|
|
|
|
Help: "Describes how many send URL requests are open now",
|
|
|
|
},
|
|
|
|
)
|
2020-12-08 22:09:24 -05:00
|
|
|
sendURLBytes = promauto.NewCounter(
|
2020-12-02 10:09:37 -05:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "gitlab_workhorse_send_url_bytes",
|
|
|
|
Help: "How many bytes were passed with send URL",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
sendURLRequestsInvalidData = sendURLRequests.WithLabelValues("invalid-data")
|
|
|
|
sendURLRequestsRequestFailed = sendURLRequests.WithLabelValues("request-failed")
|
|
|
|
sendURLRequestsSucceeded = sendURLRequests.WithLabelValues("succeeded")
|
|
|
|
)
|
|
|
|
|
|
|
|
func (e *entry) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
|
|
|
|
var params entryParams
|
|
|
|
|
|
|
|
sendURLOpenRequests.Inc()
|
|
|
|
defer sendURLOpenRequests.Dec()
|
|
|
|
|
|
|
|
if err := e.Unpack(¶ms, sendData); err != nil {
|
|
|
|
helper.Fail500(w, r, fmt.Errorf("SendURL: unpack sendData: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithContextFields(r.Context(), log.Fields{
|
|
|
|
"url": mask.URL(params.URL),
|
|
|
|
"path": r.URL.Path,
|
|
|
|
}).Info("SendURL: sending")
|
|
|
|
|
|
|
|
if params.URL == "" {
|
|
|
|
sendURLRequestsInvalidData.Inc()
|
|
|
|
helper.Fail500(w, r, fmt.Errorf("SendURL: URL is empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new request and copy range headers
|
|
|
|
newReq, err := http.NewRequest("GET", params.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
sendURLRequestsInvalidData.Inc()
|
|
|
|
helper.Fail500(w, r, fmt.Errorf("SendURL: NewRequest: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newReq = newReq.WithContext(r.Context())
|
|
|
|
|
|
|
|
for _, header := range rangeHeaderKeys {
|
|
|
|
newReq.Header[header] = r.Header[header]
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute new request
|
|
|
|
var resp *http.Response
|
|
|
|
if params.AllowRedirects {
|
|
|
|
resp, err = httpClient.Do(newReq)
|
|
|
|
} else {
|
|
|
|
resp, err = httpTransport.RoundTrip(newReq)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
sendURLRequestsRequestFailed.Inc()
|
|
|
|
helper.Fail500(w, r, fmt.Errorf("SendURL: Do request: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent Go from adding a Content-Length header automatically
|
|
|
|
w.Header().Del("Content-Length")
|
|
|
|
|
|
|
|
// copy response headers and body, except the headers from preserveHeaderKeys
|
|
|
|
for key, value := range resp.Header {
|
|
|
|
if !preserveHeaderKeys[key] {
|
|
|
|
w.Header()[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
n, err := io.Copy(w, resp.Body)
|
|
|
|
sendURLBytes.Add(float64(n))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
sendURLRequestsRequestFailed.Inc()
|
2021-01-11 10:10:32 -05:00
|
|
|
log.WithRequest(r).WithError(fmt.Errorf("SendURL: Copy response: %v", err)).Error()
|
2020-12-02 10:09:37 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sendURLRequestsSucceeded.Inc()
|
|
|
|
}
|