<commit_before>package gorange

import (
	"fmt"
	"net/http"
	"net/url"
	"time"
)

// ProxyConfig specifies the configuration for a gorange proxy HTTP server.
type ProxyConfig struct {
	CheckVersionPeriodicity time.Duration
	Port                    uint
	Servers                 []string
}

// Proxy creates a proxy http server on the port that proxies range queries to the specified range
// servers.
func Proxy(config ProxyConfig) error {
	defaultQuerier, err := NewQuerier(&Configurator{
		CheckVersionPeriodicity: config.CheckVersionPeriodicity,
		RetryCount:              len(config.Servers),
		Servers:                 config.Servers,
	})
	if err != nil {
		return err
	}
	http.HandleFunc("/range/list", func(w http.ResponseWriter, r *http.Request) {
		query, err := url.QueryUnescape(r.URL.RawQuery)
		if err != nil {
			fmt.Fprintf(w, "Invalid query: %q\r\n", query)
			return
		}
		response, err := defaultQuerier.Query(query)
		if err != nil {
			fmt.Fprintf(w, "Cannot resolve query: %q; error: %s\r\n", query, err)
			return
		}
		for _, item := range response {
			fmt.Fprintf(w, "%s\r\n", item)
		}
	})
	return http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
}<commit_msg>Proxy responds with appropriate HTTP status codes when error * supports 'Accept-Encoding: gzip'<commit_after>package gorange

import (
	"compress/gzip"
	"errors"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"strings"
	"time"
)

// ProxyConfig specifies the configuration for a gorange proxy HTTP server.
type ProxyConfig struct {
	CheckVersionPeriodicity time.Duration
	Port                    uint
	Servers                 []string
}

// Proxy creates a proxy http server on the port that proxies range queries to the specified range
// servers.
func Proxy(config ProxyConfig) error {
	querier, err := NewQuerier(&Configurator{
		CheckVersionPeriodicity: config.CheckVersionPeriodicity,
		RetryCount:              len(config.Servers),
		Servers:                 config.Servers,
	})
	if err != nil {
		return err
	}
	http.HandleFunc("/range/list", makeGzipHandler(makeRangeHandler(querier)))
	return http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
}

func makeRangeHandler(querier Querier) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			logHttpError(&w, r, errors.New(r.Method), http.StatusMethodNotAllowed)
			return
		}
		query, err := url.QueryUnescape(r.URL.RawQuery)
		if err != nil {
			logHttpError(&w, r, fmt.Errorf("cannot decode query: %s", err), http.StatusBadRequest)
		}
		response, err := querier.Query(query)
		if err != nil {
			logHttpError(&w, r, fmt.Errorf("cannot resolve query: %s", err), http.StatusBadGateway)
			return
		}
		for _, item := range response {
			fmt.Fprintf(w, "%s\r\n", item)
		}
	}
}

func logHttpError(w *http.ResponseWriter, r *http.Request, err error, status int) {
	http.Error(*w, err.Error(), status)
}

type gzipResponseWriter struct {
	io.Writer
	http.ResponseWriter
}

func (w gzipResponseWriter) Write(b []byte) (int, error) {
	return w.Writer.Write(b)
}

func makeGzipHandler(fn http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
			fn(w, r)
			return
		}
		w.Header().Set("Content-Encoding", "gzip")
		gz := gzip.NewWriter(w)
		fn(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
		err := gz.Close()
		if err != nil {
			logHttpError(&w, r, fmt.Errorf("cannot compress response: %s", err), http.StatusInternalServerError)
		}
	}
}
{'fraction_non_alphanumeric': 0.1302885962454469, 'fraction_numerical': 0.0, 'mean_word_length': 3.630350194552529, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3445444', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1197, 'n_words': 307}