| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package lhttp | 5 package lhttp |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "errors" | 8 "errors" |
| 9 "net" |
| 9 "net/url" | 10 "net/url" |
| 10 "strings" | 11 "strings" |
| 11 ) | 12 ) |
| 12 | 13 |
| 13 func hostRequiresSSL(host string) bool { | 14 func hostRequiresSSL(host string) bool { |
| 14 host = strings.ToLower(host) | 15 host = strings.ToLower(host) |
| 15 return strings.HasSuffix(host, ".appspot.com") | 16 return strings.HasSuffix(host, ".appspot.com") |
| 16 } | 17 } |
| 17 | 18 |
| 18 // CheckURL ensures that the URL has a valid scheme, and that, if it is an | 19 // CheckURL ensures that the URL has a valid scheme, and that, if it is an |
| (...skipping 13 matching lines...) Expand all Loading... |
| 32 return "", errors.New("Only http:// or https:// scheme is accept
ed.") | 33 return "", errors.New("Only http:// or https:// scheme is accept
ed.") |
| 33 } | 34 } |
| 34 if u.Scheme != "https" && hostRequiresSSL(u.Host) { | 35 if u.Scheme != "https" && hostRequiresSSL(u.Host) { |
| 35 return "", errors.New("only https:// scheme is accepted for apps
pot hosts, it can be omitted") | 36 return "", errors.New("only https:// scheme is accepted for apps
pot hosts, it can be omitted") |
| 36 } | 37 } |
| 37 if _, err = url.Parse(s); err != nil { | 38 if _, err = url.Parse(s); err != nil { |
| 38 return "", err | 39 return "", err |
| 39 } | 40 } |
| 40 return s, nil | 41 return s, nil |
| 41 } | 42 } |
| 43 |
| 44 // IsLocalHost returns true if hostport is local. |
| 45 func IsLocalHost(hostport string) bool { |
| 46 host, _, err := net.SplitHostPort(hostport) |
| 47 if err != nil { |
| 48 return false |
| 49 } |
| 50 switch { |
| 51 case host == "localhost", host == "": |
| 52 case net.ParseIP(host).IsLoopback(): |
| 53 |
| 54 default: |
| 55 return false |
| 56 } |
| 57 return true |
| 58 } |
| OLD | NEW |