Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

128 linhas
3.3 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 querystring
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "net/http"
  19. "strings"
  20. "github.com/google/martian"
  21. "github.com/google/martian/parse"
  22. "github.com/google/martian/verify"
  23. )
  24. func init() {
  25. parse.Register("querystring.Verifier", verifierFromJSON)
  26. }
  27. type verifier struct {
  28. key, value string
  29. err *martian.MultiError
  30. }
  31. type verifierJSON struct {
  32. Name string `json:"name"`
  33. Value string `json:"value"`
  34. Scope []parse.ModifierType `json:"scope"`
  35. }
  36. // NewVerifier returns a new param verifier.
  37. func NewVerifier(key, value string) (verify.RequestVerifier, error) {
  38. if key == "" {
  39. return nil, fmt.Errorf("no key provided to param verifier")
  40. }
  41. return &verifier{
  42. key: key,
  43. value: value,
  44. err: martian.NewMultiError(),
  45. }, nil
  46. }
  47. // ModifyRequest verifies that the request's URL params match the given params
  48. // in all modified requests. If no value is provided, the verifier will only
  49. // check if the given key is present. An error will be added to the contained
  50. // *MultiError if the param is unmatched.
  51. func (v *verifier) ModifyRequest(req *http.Request) error {
  52. // skip requests to API
  53. ctx := martian.NewContext(req)
  54. if ctx.IsAPIRequest() {
  55. return nil
  56. }
  57. if err := req.ParseForm(); err != nil {
  58. err := fmt.Errorf("request(%v) parsing failed; could not parse query parameters", req.URL)
  59. v.err.Add(err)
  60. return nil
  61. }
  62. vals, ok := req.Form[v.key]
  63. if !ok {
  64. err := fmt.Errorf("request(%v) param verification error: key %v not found", req.URL, v.key)
  65. v.err.Add(err)
  66. return nil
  67. }
  68. if v.value == "" {
  69. return nil
  70. }
  71. for _, val := range vals {
  72. if v.value == val {
  73. return nil
  74. }
  75. }
  76. err := fmt.Errorf("request(%v) param verification error: got %v for key %v, want %v", req.URL, strings.Join(vals, ", "), v.key, v.value)
  77. v.err.Add(err)
  78. return nil
  79. }
  80. // VerifyRequests returns an error if verification for any request failed.
  81. // If an error is returned it will be of type *martian.MultiError.
  82. func (v *verifier) VerifyRequests() error {
  83. if v.err.Empty() {
  84. return nil
  85. }
  86. return v.err
  87. }
  88. // ResetRequestVerifications clears all failed request verifications.
  89. func (v *verifier) ResetRequestVerifications() {
  90. v.err = martian.NewMultiError()
  91. }
  92. // verifierFromJSON builds a querystring.Verifier from JSON.
  93. //
  94. // Example JSON:
  95. // {
  96. // "querystring.Verifier": {
  97. // "scope": ["request", "response"],
  98. // "name": "Martian-Testing",
  99. // "value": "true"
  100. // }
  101. // }
  102. func verifierFromJSON(b []byte) (*parse.Result, error) {
  103. msg := &verifierJSON{}
  104. if err := json.Unmarshal(b, msg); err != nil {
  105. return nil, err
  106. }
  107. v, err := NewVerifier(msg.Name, msg.Value)
  108. if err != nil {
  109. return nil, err
  110. }
  111. return parse.NewResult(v, msg.Scope)
  112. }