Exemplo n.º 1
0
func (h *staticHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	p := path.Clean(r.URL.Path)
	if p != r.URL.Path {
		http.Redirect(w, r, p, 301)
		return
	}

	etag, err := h.etag(p)
	if err != nil {
		h.error(w, r, http.StatusNotFound, err)
		return
	}

	maxAge := h.ss.MaxAge
	if maxAge == 0 {
		maxAge = 24 * time.Hour
	}
	if r.FormValue("v") != "" {
		maxAge = 365 * 24 * time.Hour
	}

	cacheControl := fmt.Sprintf("public, max-age=%d", maxAge/time.Second)

	for _, e := range header.ParseList(r.Header, "If-None-Match") {
		if e == etag {
			w.Header().Set("Cache-Control", cacheControl)
			w.Header().Set("Etag", etag)
			w.WriteHeader(http.StatusNotModified)
			return
		}
	}

	rc, cl, ct, err := h.open(p)
	if err != nil {
		h.error(w, r, http.StatusNotFound, err)
		return
	}
	defer rc.Close()

	w.Header().Set("Cache-Control", cacheControl)
	w.Header().Set("Etag", etag)
	if ct != "" {
		w.Header().Set("Content-Type", ct)
	}
	if cl != 0 {
		w.Header().Set("Content-Length", strconv.FormatInt(cl, 10))
	}
	w.WriteHeader(http.StatusOK)
	if r.Method != "HEAD" {
		io.Copy(w, rc)
	}
}
Exemplo n.º 2
0
// Parse the "X-Forwarded-For" header.
func ParseXForwardedFor(hdr http.Header) (legs []Leg, err error) {
	parts := header.ParseList(hdr, "X-Forwarded-For")

	for _, p := range parts {
		p = strings.TrimSpace(p)
		ip, port, err := denet.FuzzySplitHostPortIPI("", p)
		if err != nil {
			return nil, err
		}

		legs = append(legs, Leg{
			SourceIP:   ip,
			SourcePort: port,
			From:       FromXForwardedFor,
		})
	}

	return
}
Exemplo n.º 3
0
// Parse the RFC 7239 "Forwarded" header and returns the legs described in the
// header.
func ParseRFC7239(hdr http.Header) (legs []Leg, err error) {
	return parseForwarded(header.ParseList(hdr, "Forwarded"))
}