25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

106 satır
2.9 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 cookie allows for the modification of cookies on http requests and responses.
  15. package cookie
  16. import (
  17. "encoding/json"
  18. "net/http"
  19. "time"
  20. "github.com/google/martian"
  21. "github.com/google/martian/log"
  22. "github.com/google/martian/parse"
  23. )
  24. func init() {
  25. parse.Register("cookie.Modifier", modifierFromJSON)
  26. }
  27. type modifier struct {
  28. cookie *http.Cookie
  29. }
  30. type modifierJSON struct {
  31. Name string `json:"name"`
  32. Value string `json:"value"`
  33. Path string `json:"path"`
  34. Domain string `json:"domain"`
  35. Expires time.Time `json:"expires"`
  36. Secure bool `json:"secure"`
  37. HTTPOnly bool `json:"httpOnly"`
  38. MaxAge int `json:"maxAge"`
  39. Scope []parse.ModifierType `json:"scope"`
  40. }
  41. // ModifyRequest adds cookie to the request.
  42. func (m *modifier) ModifyRequest(req *http.Request) error {
  43. req.AddCookie(m.cookie)
  44. log.Debugf("cookie.ModifyRequest: %s: cookie: %s", req.URL, m.cookie)
  45. return nil
  46. }
  47. // ModifyResponse sets cookie on the response.
  48. func (m *modifier) ModifyResponse(res *http.Response) error {
  49. res.Header.Add("Set-Cookie", m.cookie.String())
  50. log.Debugf("cookie.ModifyResponse: %s: cookie: %s", res.Request.URL, m.cookie)
  51. return nil
  52. }
  53. // NewModifier returns a modifier that injects the provided cookie into the
  54. // request or response.
  55. func NewModifier(cookie *http.Cookie) martian.RequestResponseModifier {
  56. return &modifier{
  57. cookie: cookie,
  58. }
  59. }
  60. // modifierFromJSON takes a JSON message as a byte slice and returns a
  61. // CookieModifier and an error.
  62. //
  63. // Example JSON Configuration message:
  64. // {
  65. // "name": "Martian-Cookie",
  66. // "value": "some value",
  67. // "path": "/some/path",
  68. // "domain": "example.com",
  69. // "expires": "2025-04-12T23:20:50.52Z", // RFC 3339
  70. // "secure": true,
  71. // "httpOnly": false,
  72. // "maxAge": 86400,
  73. // "scope": ["request", "result"]
  74. // }
  75. func modifierFromJSON(b []byte) (*parse.Result, error) {
  76. msg := &modifierJSON{}
  77. if err := json.Unmarshal(b, msg); err != nil {
  78. return nil, err
  79. }
  80. c := &http.Cookie{
  81. Name: msg.Name,
  82. Value: msg.Value,
  83. Path: msg.Path,
  84. Domain: msg.Domain,
  85. Expires: msg.Expires,
  86. Secure: msg.Secure,
  87. HttpOnly: msg.HTTPOnly,
  88. MaxAge: msg.MaxAge,
  89. }
  90. return parse.NewResult(NewModifier(c), msg.Scope)
  91. }