httptoo.go 979 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package httptoo
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "github.com/bradfitz/iter"
  7. "github.com/anacrolix/missinggo"
  8. )
  9. func OriginatingProtocol(r *http.Request) string {
  10. if fp := r.Header.Get("X-Forwarded-Proto"); fp != "" {
  11. return fp
  12. } else if r.TLS != nil {
  13. return "https"
  14. } else {
  15. return "http"
  16. }
  17. }
  18. // Clears the named cookie for every domain that leads to the current one.
  19. func NukeCookie(w http.ResponseWriter, r *http.Request, name, path string) {
  20. parts := strings.Split(missinggo.SplitHostMaybePort(r.Host).Host, ".")
  21. for i := range iter.N(len(parts) + 1) { // Include the empty domain.
  22. http.SetCookie(w, &http.Cookie{
  23. Name: name,
  24. MaxAge: -1,
  25. Path: path,
  26. Domain: strings.Join(parts[i:], "."),
  27. })
  28. }
  29. }
  30. // Performs quoted-string from http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html
  31. func EncodeQuotedString(s string) string {
  32. return strconv.Quote(s)
  33. }
  34. // https://httpstatuses.com/499
  35. const StatusClientCancelledRequest = 499