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.

140 linhas
3.5 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 verify
  15. import (
  16. "encoding/json"
  17. "net/http"
  18. "github.com/google/martian"
  19. "github.com/google/martian/log"
  20. )
  21. // Handler is an http.Handler that returns the request and response
  22. // verifications of reqv and resv as JSON.
  23. type Handler struct {
  24. reqv RequestVerifier
  25. resv ResponseVerifier
  26. }
  27. // ResetHandler is an http.Handler that resets the request and response
  28. // verifications of reqv and resv.
  29. type ResetHandler struct {
  30. reqv RequestVerifier
  31. resv ResponseVerifier
  32. }
  33. type verifyResponse struct {
  34. Errors []verifyError `json:"errors"`
  35. }
  36. type verifyError struct {
  37. Message string `json:"message"`
  38. }
  39. // NewHandler returns an http.Handler for requesting the verification
  40. // error status.
  41. func NewHandler() *Handler {
  42. return &Handler{}
  43. }
  44. // NewResetHandler returns an http.Handler for reseting the verification error
  45. // status.
  46. func NewResetHandler() *ResetHandler {
  47. return &ResetHandler{}
  48. }
  49. // SetRequestVerifier sets the RequestVerifier to verify.
  50. func (h *Handler) SetRequestVerifier(reqv RequestVerifier) {
  51. h.reqv = reqv
  52. }
  53. // SetResponseVerifier sets the ResponseVerifier to verify.
  54. func (h *Handler) SetResponseVerifier(resv ResponseVerifier) {
  55. h.resv = resv
  56. }
  57. // ServeHTTP writes out a JSON response containing a list of verification
  58. // errors that occurred during the requests and responses sent to the proxy.
  59. func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
  60. rw.Header().Set("Content-Type", "application/json")
  61. if req.Method != "GET" {
  62. rw.Header().Set("Allow", "GET")
  63. rw.WriteHeader(405)
  64. log.Errorf("verify: invalid request method: %s", req.Method)
  65. return
  66. }
  67. vres := &verifyResponse{
  68. Errors: make([]verifyError, 0),
  69. }
  70. if h.reqv != nil {
  71. if err := h.reqv.VerifyRequests(); err != nil {
  72. appendError(vres, err)
  73. }
  74. }
  75. if h.resv != nil {
  76. if err := h.resv.VerifyResponses(); err != nil {
  77. appendError(vres, err)
  78. }
  79. }
  80. json.NewEncoder(rw).Encode(vres)
  81. }
  82. func appendError(vres *verifyResponse, err error) {
  83. merr, ok := err.(*martian.MultiError)
  84. if !ok {
  85. vres.Errors = append(vres.Errors, verifyError{Message: err.Error()})
  86. return
  87. }
  88. for _, err := range merr.Errors() {
  89. vres.Errors = append(vres.Errors, verifyError{Message: err.Error()})
  90. }
  91. }
  92. // SetRequestVerifier sets the RequestVerifier to reset.
  93. func (h *ResetHandler) SetRequestVerifier(reqv RequestVerifier) {
  94. h.reqv = reqv
  95. }
  96. // SetResponseVerifier sets the ResponseVerifier to reset.
  97. func (h *ResetHandler) SetResponseVerifier(resv ResponseVerifier) {
  98. h.resv = resv
  99. }
  100. // ServeHTTP resets the verifier for the given ID so that it may
  101. // be run again.
  102. func (h *ResetHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
  103. if req.Method != "POST" {
  104. rw.Header().Set("Allow", "POST")
  105. rw.WriteHeader(405)
  106. log.Errorf("verify: invalid request method: %s", req.Method)
  107. return
  108. }
  109. if h.reqv != nil {
  110. h.reqv.ResetRequestVerifications()
  111. }
  112. if h.resv != nil {
  113. h.resv.ResetResponseVerifications()
  114. }
  115. rw.WriteHeader(204)
  116. }