示例#1
0
func (h *healthcheck) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	proxyWriter, ok := rw.(utils.ProxyResponseWriter)
	var accessLogRecord *schema.AccessLogRecord
	if ok {
		alr := proxyWriter.Context().Value("AccessLogRecord")
		if alr == nil {
			h.logger.Error("AccessLogRecord not set on context", errors.New("failed-to-access-log-record"))
		}
		accessLogRecord = alr.(*schema.AccessLogRecord)
	}
	if h.userAgent == "" || r.Header.Get("User-Agent") == h.userAgent {
		rw.Header().Set("Cache-Control", "private, max-age=0")
		rw.Header().Set("Expires", "0")

		draining := atomic.LoadInt32(h.heartbeatOK) == 0
		if !draining {
			rw.WriteHeader(http.StatusOK)
			rw.Write([]byte("ok\n"))
			r.Close = true
			if ok {
				accessLogRecord.StatusCode = http.StatusOK
			}
		} else {
			rw.WriteHeader(http.StatusServiceUnavailable)
			r.Close = true
			if ok {
				accessLogRecord.StatusCode = http.StatusServiceUnavailable
			}
		}
		return
	}
	next(rw, r)
}
	"code.cloudfoundry.org/gorouter/access_log/schema"
	"code.cloudfoundry.org/routing-api/models"

	router_http "code.cloudfoundry.org/gorouter/common/http"
	"code.cloudfoundry.org/gorouter/route"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"net/http"
	"net/url"
	"time"
)

var _ = Describe("AccessLogRecord", func() {
	var (
		endpoint *route.Endpoint
		record   *schema.AccessLogRecord
	)
	BeforeEach(func() {
		endpoint = route.NewEndpoint("FakeApplicationId", "1.2.3.4", 1234, "", "3", nil, 0, "", models.ModificationTag{})
		record = &schema.AccessLogRecord{
			Request: &http.Request{
				Host:   "FakeRequestHost",
				Method: "FakeRequestMethod",
				Proto:  "FakeRequestProto",
				URL: &url.URL{
					Opaque: "http://example.com/request",
				},
				Header: http.Header{
					"Referer":                       []string{"FakeReferer"},
					"User-Agent":                    []string{"FakeUserAgent"},
					"X-Forwarded-For":               []string{"FakeProxy1, FakeProxy2"},