http.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2023 The frp Authors
  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. package http
  15. import (
  16. "encoding/base64"
  17. "net"
  18. "net/http"
  19. "strings"
  20. )
  21. func OkResponse() *http.Response {
  22. header := make(http.Header)
  23. res := &http.Response{
  24. Status: "OK",
  25. StatusCode: 200,
  26. Proto: "HTTP/1.1",
  27. ProtoMajor: 1,
  28. ProtoMinor: 1,
  29. Header: header,
  30. }
  31. return res
  32. }
  33. func ProxyUnauthorizedResponse() *http.Response {
  34. header := make(http.Header)
  35. header.Set("Proxy-Authenticate", `Basic realm="Restricted"`)
  36. res := &http.Response{
  37. Status: "Proxy Authentication Required",
  38. StatusCode: 407,
  39. Proto: "HTTP/1.1",
  40. ProtoMajor: 1,
  41. ProtoMinor: 1,
  42. Header: header,
  43. }
  44. return res
  45. }
  46. // canonicalHost strips port from host if present and returns the canonicalized
  47. // host name.
  48. func CanonicalHost(host string) (string, error) {
  49. var err error
  50. host = strings.ToLower(host)
  51. if hasPort(host) {
  52. host, _, err = net.SplitHostPort(host)
  53. if err != nil {
  54. return "", err
  55. }
  56. }
  57. // Strip trailing dot from fully qualified domain names.
  58. host = strings.TrimSuffix(host, ".")
  59. return host, nil
  60. }
  61. // hasPort reports whether host contains a port number. host may be a host
  62. // name, an IPv4 or an IPv6 address.
  63. func hasPort(host string) bool {
  64. colons := strings.Count(host, ":")
  65. if colons == 0 {
  66. return false
  67. }
  68. if colons == 1 {
  69. return true
  70. }
  71. return host[0] == '[' && strings.Contains(host, "]:")
  72. }
  73. func ParseBasicAuth(auth string) (username, password string, ok bool) {
  74. const prefix = "Basic "
  75. // Case insensitive prefix match. See Issue 22736.
  76. if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
  77. return
  78. }
  79. c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
  80. if err != nil {
  81. return
  82. }
  83. cs := string(c)
  84. s := strings.IndexByte(cs, ':')
  85. if s < 0 {
  86. return
  87. }
  88. return cs[:s], cs[s+1:], true
  89. }
  90. func BasicAuth(username, passwd string) string {
  91. auth := username + ":" + passwd
  92. return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
  93. }