Esempio n. 1
0
func TestParsesHeaders(t *testing.T) {
	request := common.HTTPToSphinxRequest(constructMockRequestWithHeaders(map[string][]string{
		"Authorization":   []string{"Bearer 12345"},
		"X-Forwarded-For": []string{"IP1", "IP2"},
	}))
	if len(request["headers"].(http.Header)) != 2 {
		t.Fatalf("expected 2 headers, recevied %d", len(request["headers"].(http.Header)))
	}

	compareHeader(t, request["headers"].(http.Header), "Authorization", []string{"Bearer 12345"})
	compareHeader(t, request["headers"].(http.Header), "X-Forwarded-For", []string{"IP1", "IP2"})
	compareStrings(t, request["path"].(string), "/trolling/path")
}
Esempio n. 2
0
func (hrl httpRateLogger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	guid := uuid.New()
	request := common.HTTPToSphinxRequest(r)
	log.Printf("[%s] REQUEST: %s", guid, stringifyRequest(request).String())
	matches, err := hrl.rateLimiter.Add(request)
	if err != nil && err != leakybucket.ErrorFull {
		log.Printf("[%s] ERROR: %s", guid, err)
		hrl.proxy.ServeHTTP(w, r)
		return
	}
	log.Printf("[%s] RATE LIMIT HEADERS: %s", guid, stringifyHeaders(getRateLimitHeaders(matches)).String())
	if err == leakybucket.ErrorFull {
		log.Printf("[%s] BUCKET FULL", guid)
	}
	hrl.proxy.ServeHTTP(w, r)
}
Esempio n. 3
0
func (hrl httpRateLogger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	guid := uuid.New()
	request := common.HTTPToSphinxRequest(r)
	matches, err := hrl.rateLimiter.Add(request)
	if err != nil && err != leakybucket.ErrorFull {
		log.Printf("[%s] ERROR: %s", guid, err)
		hrl.proxy.ServeHTTP(w, r)
		return
	}

	rateLimitResponse := flattenRateLimitHeaders(getRateLimitHeaders(matches))
	if err == leakybucket.ErrorFull {
		rateLimitResponse["limit"] = true
	} else {
		rateLimitResponse["limit"] = false
	}
	rateLimitResponse["guid"] = guid

	common.Log.InfoD("http-logger", common.ConcatWithRequest(rateLimitResponse, request))
	hrl.proxy.ServeHTTP(w, r)
}
Esempio n. 4
0
func (hrl httpRateLimiter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	guid := r.Header.Get("X-Request-Id")
	request := common.HTTPToSphinxRequest(r)
	matches, err := hrl.rateLimiter.Add(request)
	if err == leakybucket.ErrorFull {
		addRateLimitHeaders(w, matches)
		common.Log.InfoD("ratelimited", common.ConcatWithRequest(common.M{"guid": guid}, request))
		w.WriteHeader(StatusTooManyRequests)
		return
	}
	if err != nil {
		common.Log.WarnD("error",
			common.ConcatWithRequest(common.M{"guid": guid, "err": err.Error()}, request))
		if !hrl.allowOnError {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		matches = []ratelimiter.Status{ratelimiter.NilStatus}
	}
	addRateLimitHeaders(w, matches)
	hrl.proxy.ServeHTTP(w, r)
}
Esempio n. 5
0
func (hrl httpRateLimiter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	guid := uuid.New()
	request := common.HTTPToSphinxRequest(r)
	log.Printf("[%s] REQUEST: %s", guid, stringifyRequest(request).String())
	matches, err := hrl.rateLimiter.Add(request)
	switch {
	case err == leakybucket.ErrorFull:
		addRateLimitHeaders(w, matches)
		w.WriteHeader(StatusTooManyRequests)
	case err != nil && hrl.allowOnError:
		log.Printf("[%s] ERROR: %s", guid, err)
		log.Printf("[%s] WARNING: bypassing rate limiter due to Error", guid)
		addRateLimitHeaders(w, []ratelimiter.Status{ratelimiter.NilStatus})
		hrl.proxy.ServeHTTP(w, r)
	case err != nil:
		log.Printf("[%s] ERROR: %s", guid, err)
		w.WriteHeader(http.StatusInternalServerError)

	default:
		addRateLimitHeaders(w, matches)
		hrl.proxy.ServeHTTP(w, r)
	}
}
Esempio n. 6
0
func getRequest(headers map[string][]string) common.Request {
	httprequest := common.ConstructMockRequestWithHeaders(headers)
	return common.HTTPToSphinxRequest(httprequest)
}