github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/domclean1/attribute-helper.go (about) 1 package domclean1 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/pbberlin/tools/net/http/routes" 8 9 "github.com/pbberlin/tools/appengine/util_appengine" 10 "golang.org/x/net/html" 11 ) 12 13 // type Attribute struct { 14 // Namespace, Key, Val string 15 // } 16 func rewriteAttributes(attributes []html.Attribute, proxyHostPort, remoteHost string) []html.Attribute { 17 18 rew := make([]html.Attribute, 0, len(attributes)) 19 20 for i := 0; i < len(attributes); i++ { 21 attr := attributes[i] 22 23 if attr.Key == "class" || attr.Key == "style" { 24 continue 25 } 26 27 if attr.Key == "href" || attr.Key == "src" || attr.Key == "action" { // make absolute 28 attr.Val = absolutize(attr.Val, remoteHost) 29 } 30 31 if attr.Key == "href" { 32 attr.Val = fmt.Sprintf("%v?url=%v", routes.ProxifyURI, attr.Val) 33 } 34 35 if attr.Key == "src" { 36 attr.Key = "href" 37 } 38 39 if attr.Key == "action" { 40 // attr.Val = fmt.Sprintf("/blob2/form-redirector?redirect-to=%v", attr.Val) // appended as form field, thus not needed here 41 if util_appengine.IsLocalEnviron() { 42 attr.Val = fmt.Sprintf("http://%v%v", proxyHostPort, routes.FormRedirector) 43 } else { 44 attr.Val = fmt.Sprintf("https://%v%v", proxyHostPort, routes.FormRedirector) 45 } 46 47 } 48 49 if attr.Key == "method" { 50 attr.Val = "post" 51 } 52 53 rew = append(rew, attr) 54 } 55 56 rew = append(rew, html.Attribute{Key: "was", Val: "rewritten"}) 57 rew = append(rew, html.Attribute{Key: "method", Val: "post"}) 58 59 return rew 60 } 61 62 func getAttrVal(attributes []html.Attribute, key string) string { 63 for i := 0; i < len(attributes); i++ { 64 attr := attributes[i] 65 if attr.Key == key { 66 return attr.Val 67 } 68 } 69 return "" 70 } 71 72 func absolutize(val, host string) string { 73 if strings.HasPrefix(val, "/") && !strings.HasPrefix(val, "//ssl.") { 74 val = fmt.Sprintf("https://%v%v", host, val) 75 } 76 return val 77 }