| 81 | } |
| 82 | |
| 83 | func (p *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 84 | if p.Lookup == nil { |
| 85 | panic("no lookup function") |
| 86 | } |
| 87 | |
| 88 | if p.Config.RequestID != "" { |
| 89 | id := p.UUID |
| 90 | if id == nil { |
| 91 | id = uuid.NewUUID |
| 92 | } |
| 93 | r.Header.Set(p.Config.RequestID, id()) |
| 94 | } |
| 95 | |
| 96 | t := p.Lookup(r) |
| 97 | |
| 98 | if t == nil { |
| 99 | status := p.Config.NoRouteStatus |
| 100 | if status < 100 || status > 999 { |
| 101 | status = http.StatusNotFound |
| 102 | } |
| 103 | w.WriteHeader(status) |
| 104 | html := noroute.GetHTML() |
| 105 | if html != "" { |
| 106 | io.WriteString(w, html) |
| 107 | } |
| 108 | return |
| 109 | } |
| 110 | |
| 111 | if t.AccessDeniedHTTP(r) { |
| 112 | http.Error(w, "access denied", http.StatusForbidden) |
| 113 | return |
| 114 | } |
| 115 | |
| 116 | if !t.Authorized(r, w, p.AuthSchemes) { |
| 117 | http.Error(w, "authorization failed", http.StatusUnauthorized) |
| 118 | return |
| 119 | } |
| 120 | |
| 121 | // build the request url since r.URL will get modified |
| 122 | // by the reverse proxy and contains only the RequestURI anyway |
| 123 | requestURL := &url.URL{ |
| 124 | Scheme: scheme(r), |
| 125 | Host: r.Host, |
| 126 | Path: r.URL.Path, |
| 127 | RawQuery: r.URL.RawQuery, |
| 128 | } |
| 129 | |
| 130 | if t.RedirectCode != 0 && t.RedirectURL != nil { |
| 131 | http.Redirect(w, r, t.RedirectURL.String(), t.RedirectCode) |
| 132 | if p.Stats.RedirectCounter != nil { |
| 133 | p.Stats.RedirectCounter.With("code", strconv.Itoa(t.RedirectCode)).Add(1) |
| 134 | } |
| 135 | return |
| 136 | } |
| 137 | |
| 138 | // build the real target url that is passed to the proxy |
| 139 | targetURL := &url.URL{ |
| 140 | Scheme: t.URL.Scheme, |