1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- package redirection
- import (
- "log"
- "net/http"
- "strings"
- )
- /*
- handler.go
- This script store the handlers use for handling
- redirection request
- */
- // Check if a request URL is a redirectable URI
- func (t *RuleTable) IsRedirectable(r *http.Request) bool {
- requestPath := r.Host + r.URL.Path
- rr := t.MatchRedirectRule(requestPath)
- return rr != nil
- }
- // Handle the redirect request, return after calling this function to prevent
- // multiple write to the response writer
- // Return the status code of the redirection handling
- func (t *RuleTable) HandleRedirect(w http.ResponseWriter, r *http.Request) int {
- requestPath := r.Host + r.URL.Path
- rr := t.MatchRedirectRule(requestPath)
- if rr != nil {
- redirectTarget := rr.TargetURL
- //Always pad a / at the back of the target URL
- if redirectTarget[len(redirectTarget)-1:] != "/" {
- redirectTarget += "/"
- }
- if rr.ForwardChildpath {
- //Remove the first / in the path
- redirectTarget += strings.TrimPrefix(r.URL.Path, "/")
- if r.URL.RawQuery != "" {
- redirectTarget += "?" + r.URL.RawQuery
- }
- }
- if !strings.HasPrefix(redirectTarget, "http://") && !strings.HasPrefix(redirectTarget, "https://") {
- redirectTarget = "http://" + redirectTarget
- }
- http.Redirect(w, r, redirectTarget, rr.StatusCode)
- return rr.StatusCode
- } else {
- //Invalid usage
- w.WriteHeader(http.StatusInternalServerError)
- w.Write([]byte("500 - Internal Server Error"))
- log.Println("Target request URL do not have matching redirect rule. Check with IsRedirectable before calling HandleRedirect!")
- return 500
- }
- }
|