api.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 cachecontrol
  18. import (
  19. "github.com/pquerna/cachecontrol/cacheobject"
  20. "net/http"
  21. "time"
  22. )
  23. type Options struct {
  24. // Set to True for a prviate cache, which is not shared amoung users (eg, in a browser)
  25. // Set to False for a "shared" cache, which is more common in a server context.
  26. PrivateCache bool
  27. }
  28. // Given an HTTP Request, the future Status Code, and an ResponseWriter,
  29. // determine the possible reasons a response SHOULD NOT be cached.
  30. func CachableResponseWriter(req *http.Request,
  31. statusCode int,
  32. resp http.ResponseWriter,
  33. opts Options) ([]cacheobject.Reason, time.Time, error) {
  34. return cacheobject.UsingRequestResponse(req, statusCode, resp.Header(), opts.PrivateCache)
  35. }
  36. // Given an HTTP Request and Response, determine the possible reasons a response SHOULD NOT
  37. // be cached.
  38. func CachableResponse(req *http.Request,
  39. resp *http.Response,
  40. opts Options) ([]cacheobject.Reason, time.Time, error) {
  41. return cacheobject.UsingRequestResponse(req, resp.StatusCode, resp.Header, opts.PrivateCache)
  42. }