You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

160 lines
4.6 KiB

  1. // Copyright 2015 Google Inc. All rights reserved.
  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 port
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "net"
  19. "net/http"
  20. "strconv"
  21. "strings"
  22. "github.com/google/martian/parse"
  23. )
  24. func init() {
  25. parse.Register("port.Modifier", modifierFromJSON)
  26. }
  27. // Modifier alters the request URL and Host header to use the provided port.
  28. // Only one of port, defaultForScheme, or remove may be specified. Whichever is set last is the one that will take effect.
  29. // If remove is true, remove the port from the host string ('example.com').
  30. // If defaultForScheme is true, explicitly specify 80 for HTTP or 443 for HTTPS ('http://example.com:80'). Do nothing for a scheme that is not 'http' or 'https'.
  31. // If port is specified, explicitly add it to the host string ('example.com:1234').
  32. // If port is zero and the other fields are false, the request will not be modified.
  33. type Modifier struct {
  34. port int
  35. defaultForScheme bool
  36. remove bool
  37. }
  38. type modifierJSON struct {
  39. Port int `json:"port"`
  40. DefaultForScheme bool `json:"defaultForScheme"`
  41. Remove bool `json:"remove"`
  42. Scope []parse.ModifierType `json:"scope"`
  43. }
  44. // NewModifier returns a RequestModifier that can be configured to alter the request URL and Host header's port.
  45. // One of DefaultPortForScheme, UsePort, or RemovePort should be called to configure this modifier.
  46. func NewModifier() *Modifier {
  47. return &Modifier{}
  48. }
  49. // DefaultPortForScheme configures the modifier to explicitly specify 80 for HTTP or 443 for HTTPS ('http://example.com:80').
  50. // The modifier will not modify requests with a scheme that is not 'http' or 'https'.
  51. // This overrides any previous configuration for this modifier.
  52. func (m *Modifier) DefaultPortForScheme() {
  53. m.defaultForScheme = true
  54. m.remove = false
  55. }
  56. // UsePort configures the modifier to add the specified port to the host string ('example.com:1234').
  57. // This overrides any previous configuration for this modifier.
  58. func (m *Modifier) UsePort(port int) {
  59. m.port = port
  60. m.remove = false
  61. m.defaultForScheme = false
  62. }
  63. // RemovePort configures the modifier to remove the port from the host string ('example.com').
  64. // This overrides any previous configuration for this modifier.
  65. func (m *Modifier) RemovePort() {
  66. m.remove = true
  67. m.defaultForScheme = false
  68. }
  69. // ModifyRequest alters the request URL and Host header to modify the port as specified.
  70. // See docs for Modifier for details.
  71. func (m *Modifier) ModifyRequest(req *http.Request) error {
  72. if m.port == 0 && !m.defaultForScheme && !m.remove {
  73. return nil
  74. }
  75. host := req.URL.Host
  76. if strings.Contains(host, ":") {
  77. h, _, err := net.SplitHostPort(host)
  78. if err != nil {
  79. return err
  80. }
  81. host = h
  82. }
  83. if m.remove {
  84. req.URL.Host = host
  85. req.Header.Set("Host", host)
  86. return nil
  87. }
  88. if m.defaultForScheme {
  89. switch req.URL.Scheme {
  90. case "http":
  91. hp := net.JoinHostPort(host, "80")
  92. req.URL.Host = hp
  93. req.Header.Set("Host", hp)
  94. return nil
  95. case "https":
  96. hp := net.JoinHostPort(host, "443")
  97. req.URL.Host = hp
  98. req.Header.Set("Host", hp)
  99. return nil
  100. default:
  101. // Unknown scheme, do nothing.
  102. return nil
  103. }
  104. }
  105. // Not removing or using default for the scheme, so use the provided port number.
  106. hp := net.JoinHostPort(host, strconv.Itoa(m.port))
  107. req.URL.Host = hp
  108. req.Header.Set("Host", hp)
  109. return nil
  110. }
  111. func modifierFromJSON(b []byte) (*parse.Result, error) {
  112. msg := &modifierJSON{}
  113. if err := json.Unmarshal(b, msg); err != nil {
  114. return nil, err
  115. }
  116. errMsg := fmt.Errorf("Must specify only one of port, defaultForScheme or remove")
  117. mod := NewModifier()
  118. // Check that exactly one field of port, defaultForScheme, and remove is set.
  119. switch {
  120. case msg.Port != 0:
  121. if msg.DefaultForScheme || msg.Remove {
  122. return nil, errMsg
  123. }
  124. mod.UsePort(msg.Port)
  125. case msg.DefaultForScheme:
  126. if msg.Port != 0 || msg.Remove {
  127. return nil, errMsg
  128. }
  129. mod.DefaultPortForScheme()
  130. case msg.Remove:
  131. if msg.Port != 0 || msg.DefaultForScheme {
  132. return nil, errMsg
  133. }
  134. mod.RemovePort()
  135. default:
  136. return nil, errMsg
  137. }
  138. return parse.NewResult(mod, msg.Scope)
  139. }