github.com/greenpau/go-authcrunch@v1.1.4/pkg/util/redirect.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package util 16 17 import ( 18 "net/http" 19 urlpkg "net/url" 20 "strings" 21 ) 22 23 // GetBaseURL returns base path based on some match. 24 func GetBaseURL(r *http.Request, s string) (string, string) { 25 for _, p := range strings.Split(s, ",") { 26 i := strings.Index(r.URL.Path, p) 27 if i >= 0 { 28 return GetCurrentBaseURL(r), r.URL.Path[:i] 29 } 30 } 31 return GetCurrentBaseURL(r), r.URL.Path 32 } 33 34 // GetRelativeURL returns relative path to current URL. 35 func GetRelativeURL(r *http.Request, orig, repl string) string { 36 i := strings.Index(r.URL.Path, orig) 37 if i < 0 { 38 return GetCurrentBaseURL(r) + repl 39 } 40 return GetCurrentBaseURL(r) + r.URL.Path[:i] + repl 41 } 42 43 // GetCurrentURL returns current URL. 44 func GetCurrentURL(r *http.Request) string { 45 return GetCurrentBaseURL(r) + r.URL.Path 46 } 47 48 // GetIssuerURL returns issuer URL. 49 func GetIssuerURL(r *http.Request) string { 50 s := GetCurrentURL(r) 51 if !strings.HasSuffix(s, "callback") { 52 return s 53 } 54 s = strings.TrimRightFunc(s, func(r rune) bool { 55 if r == '/' { 56 return false 57 } 58 return true 59 }) 60 61 // i := strings.LastIndexByte(s, '/') 62 // if i > 0 { 63 // 64 // } 65 return s 66 } 67 68 // GetCurrentBaseURL returns current base URL. 69 func GetCurrentBaseURL(r *http.Request) string { 70 redirHost := r.Header.Get("X-Forwarded-Host") 71 if redirHost == "" { 72 redirHost = r.Host 73 } 74 redirProto := r.Header.Get("X-Forwarded-Proto") 75 if redirProto == "" { 76 if r.TLS == nil { 77 redirProto = "http" 78 } else { 79 redirProto = "https" 80 } 81 } 82 redirPort := r.Header.Get("X-Forwarded-Port") 83 redirectBaseURL := redirProto + "://" + redirHost 84 85 if redirPort != "" { 86 switch redirPort { 87 case "443": 88 if redirProto != "https" { 89 redirectBaseURL += ":" + redirPort 90 } 91 case "80": 92 if redirProto != "http" { 93 redirectBaseURL += ":" + redirPort 94 } 95 default: 96 redirectBaseURL += ":" + redirPort 97 } 98 } 99 redirPrefix := r.Header.Get("X-Forwarded-Prefix") 100 redirectBaseURL += redirPrefix 101 102 return redirectBaseURL 103 } 104 105 // StripQueryParam removes a specific query parameter from a URL. 106 func StripQueryParam(url string, param string) string { 107 u, err := urlpkg.Parse(url) 108 if err != nil { 109 return url 110 } 111 q := u.Query() 112 q.Del(param) 113 u.RawQuery = q.Encode() 114 return u.String() 115 }