Rewrite is used to wrap the entire Iris Router. Rewrite is a bit faster than Handler because it's executed even before any route matched and it stops on redirect pattern match. Use it to wrap the entire Iris Application, otherwise look `Handler` instead. Usage: app.WrapRouter(engine.Rewrite).
(w http.ResponseWriter, r *http.Request, routeHandler http.HandlerFunc)
| 171 | // Usage: |
| 172 | // app.WrapRouter(engine.Rewrite). |
| 173 | func (e *Engine) Rewrite(w http.ResponseWriter, r *http.Request, routeHandler http.HandlerFunc) { |
| 174 | if primarySubdomain := e.options.PrimarySubdomain; primarySubdomain != "" { |
| 175 | hostport := context.GetHost(r) |
| 176 | root := context.GetDomain(hostport) |
| 177 | |
| 178 | e.initDebugf("Begin request: full host: %s and root domain: %s\n", hostport, root) |
| 179 | // Note: |
| 180 | // localhost and 127.0.0.1 are not supported for subdomain rewrite, by purpose, |
| 181 | // use a virtual host instead. |
| 182 | // GetDomain will return will return localhost or www.localhost |
| 183 | // on expected loopbacks. |
| 184 | if e.domainValidator(root) { |
| 185 | root += getPort(hostport) |
| 186 | subdomain := strings.TrimSuffix(hostport, root) |
| 187 | |
| 188 | e.debugf("Domain is not a loopback, requested subdomain: %s\n", subdomain) |
| 189 | |
| 190 | if subdomain == "" { |
| 191 | // we are in root domain, full redirect to its primary subdomain. |
| 192 | newHost := primarySubdomain + root |
| 193 | e.debugf("Redirecting from root domain to: %s\n", newHost) |
| 194 | r.Host = newHost |
| 195 | r.URL.Host = newHost |
| 196 | http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently) |
| 197 | return |
| 198 | } |
| 199 | |
| 200 | if subdomain == primarySubdomain { |
| 201 | // keep root domain as the Host field inside the next handlers, |
| 202 | // for consistently use and |
| 203 | // to bypass the subdomain router (`routeHandler`) |
| 204 | // do not return, redirects should be respected. |
| 205 | rootHost := strings.TrimPrefix(hostport, subdomain) |
| 206 | e.debugf("Request host field was modified to: %s. Proceed without redirection\n", rootHost) |
| 207 | // modify those for the next redirects or the route handler. |
| 208 | r.Host = rootHost |
| 209 | r.URL.Host = rootHost |
| 210 | } |
| 211 | |
| 212 | // maybe other subdomain or not at all, let's continue. |
| 213 | } else { |
| 214 | e.debugf("Primary subdomain is: %s but redirect response was not sent. Domain is a loopback?\n", primarySubdomain) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | for _, rd := range e.redirects { |
| 219 | src := r.URL.Path |
| 220 | if !rd.isRelativePattern { |
| 221 | // don't change the request, use a full redirect. |
| 222 | src = context.GetScheme(r) + context.GetHost(r) + r.URL.RequestURI() |
| 223 | } |
| 224 | |
| 225 | if target, ok := rd.matchAndReplace(src); ok { |
| 226 | if target == src { |
| 227 | e.debugf("WARNING: source and target URLs match: %s\n", src) |
| 228 | routeHandler(w, r) |
| 229 | return |
| 230 | } |
no test coverage detected