) (bool, error)
func roundTrip(w http.ResponseWriter, req *http.Request) {
+ fifos.SinkReq <- fmt.Sprintf("%s %s", req.Method, req.URL.String())
host := strings.TrimSuffix(req.URL.Host, ":443")
for _, round := range []Round{
rounds.RoundNoHead,
- rounds.RoundLog,
rounds.RoundDenySpy,
rounds.RoundRedditOld,
rounds.RoundHabrImage,
}
}
+ reqFlags := []string{}
+ unauthorized := false
+Retry:
resp, err := transport.RoundTrip(req)
if err != nil {
fifos.SinkErr <- fmt.Sprintf("%s\t%s", req.URL.Host, err.Error())
return
}
+ if resp.StatusCode == http.StatusUnauthorized {
+ resp.Body.Close()
+ authCacheM.Lock()
+ if unauthorized {
+ delete(authCache, req.URL.Host)
+ } else {
+ unauthorized = true
+ if creds, ok := authCache[req.URL.Host]; ok {
+ authCacheM.Unlock()
+ req.SetBasicAuth(creds[0], creds[1])
+ goto Retry
+ }
+ }
+ fifos.SinkOther <- fmt.Sprintf("%s\tauthorization required", req.URL.Host)
+ user, pass, err := authDialog(host, resp.Header.Get("WWW-Authenticate"))
+ if err != nil {
+ authCacheM.Unlock()
+ fifos.SinkErr <- fmt.Sprintf("%s\t%s", req.URL.Host, err.Error())
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ authCache[req.URL.Host] = [2]string{user, pass}
+ authCacheM.Unlock()
+ req.SetBasicAuth(user, pass)
+ goto Retry
+ }
+ if unauthorized {
+ reqFlags = append(reqFlags, "auth")
+ }
+ if resp.TLS != nil && resp.TLS.NegotiatedProtocol != "" {
+ reqFlags = append(reqFlags, resp.TLS.NegotiatedProtocol)
+ }
+
for k, vs := range resp.Header {
if _, ok := proxyHeaders[k]; ok {
continue
}
resp.Body.Close()
msg := fmt.Sprintf(
- "%s %s\t%s\t%s\t%s",
+ "%s %s\t%s\t%s\t%s\t%s",
req.Method,
req.URL.String(),
resp.Status,
resp.Header.Get("Content-Type"),
humanize.IBytes(uint64(n)),
+ strings.Join(reqFlags, ","),
)
if resp.StatusCode == http.StatusOK {
fifos.SinkOK <- msg