File: httptoo.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 872 kB
  • sloc: makefile: 4
file content (42 lines) | stat: -rw-r--r-- 979 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package httptoo

import (
	"net/http"
	"strconv"
	"strings"

	"github.com/bradfitz/iter"

	"github.com/anacrolix/missinggo"
)

func OriginatingProtocol(r *http.Request) string {
	if fp := r.Header.Get("X-Forwarded-Proto"); fp != "" {
		return fp
	} else if r.TLS != nil {
		return "https"
	} else {
		return "http"
	}
}

// Clears the named cookie for every domain that leads to the current one.
func NukeCookie(w http.ResponseWriter, r *http.Request, name, path string) {
	parts := strings.Split(missinggo.SplitHostMaybePort(r.Host).Host, ".")
	for i := range iter.N(len(parts) + 1) { // Include the empty domain.
		http.SetCookie(w, &http.Cookie{
			Name:   name,
			MaxAge: -1,
			Path:   path,
			Domain: strings.Join(parts[i:], "."),
		})
	}
}

// Performs quoted-string from http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html
func EncodeQuotedString(s string) string {
	return strconv.Quote(s)
}

// https://httpstatuses.com/499
const StatusClientCancelledRequest = 499