warning.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Copyright 2015 Paul Querna
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package cacheobject
  18. import (
  19. "fmt"
  20. "net/http"
  21. "time"
  22. )
  23. // Repersents an HTTP Warning: http://tools.ietf.org/html/rfc7234#section-5.5
  24. type Warning int
  25. const (
  26. // Response is Stale
  27. // A cache SHOULD generate this whenever the sent response is stale.
  28. WarningResponseIsStale Warning = 110
  29. // Revalidation Failed
  30. // A cache SHOULD generate this when sending a stale
  31. // response because an attempt to validate the response failed, due to an
  32. // inability to reach the server.
  33. WarningRevalidationFailed Warning = 111
  34. // Disconnected Operation
  35. // A cache SHOULD generate this if it is intentionally disconnected from
  36. // the rest of the network for a period of time.
  37. WarningDisconnectedOperation Warning = 112
  38. // Heuristic Expiration
  39. //
  40. // A cache SHOULD generate this if it heuristically chose a freshness
  41. // lifetime greater than 24 hours and the response's age is greater than
  42. // 24 hours.
  43. WarningHeuristicExpiration Warning = 113
  44. // Miscellaneous Warning
  45. //
  46. // The warning text can include arbitrary information to be presented to
  47. // a human user or logged. A system receiving this warning MUST NOT
  48. // take any automated action, besides presenting the warning to the
  49. // user.
  50. WarningMiscellaneousWarning Warning = 199
  51. // Transformation Applied
  52. //
  53. // This Warning code MUST be added by a proxy if it applies any
  54. // transformation to the representation, such as changing the
  55. // content-coding, media-type, or modifying the representation data,
  56. // unless this Warning code already appears in the response.
  57. WarningTransformationApplied Warning = 214
  58. // Miscellaneous Persistent Warning
  59. //
  60. // The warning text can include arbitrary information to be presented to
  61. // a human user or logged. A system receiving this warning MUST NOT
  62. // take any automated action.
  63. WarningMiscellaneousPersistentWarning Warning = 299
  64. )
  65. func (w Warning) HeaderString(agent string, date time.Time) string {
  66. if agent == "" {
  67. agent = "-"
  68. } else {
  69. // TODO(pquerna): this doesn't escape agent if it contains bad things.
  70. agent = `"` + agent + `"`
  71. }
  72. return fmt.Sprintf(`%d %s "%s" %s`, w, agent, w.String(), date.Format(http.TimeFormat))
  73. }
  74. func (w Warning) String() string {
  75. switch w {
  76. case WarningResponseIsStale:
  77. return "Response is Stale"
  78. case WarningRevalidationFailed:
  79. return "Revalidation Failed"
  80. case WarningDisconnectedOperation:
  81. return "Disconnected Operation"
  82. case WarningHeuristicExpiration:
  83. return "Heuristic Expiration"
  84. case WarningMiscellaneousWarning:
  85. // TODO(pquerna): ideally had a better way to override this one code.
  86. return "Miscellaneous Warning"
  87. case WarningTransformationApplied:
  88. return "Transformation Applied"
  89. case WarningMiscellaneousPersistentWarning:
  90. // TODO(pquerna): same as WarningMiscellaneousWarning
  91. return "Miscellaneous Persistent Warning"
  92. }
  93. panic(w)
  94. }