<commit_before>package polaris

import (
	"encoding/json"
	"net/http"
)

type Env struct {
	Request *http.Request
	Status  int

	w        http.ResponseWriter
	finished bool
}

func newEnv(w http.ResponseWriter, r *http.Request) *Env {
	e := new(Env)

	e.Request = r
	e.w = w

	e.finished = false
	e.Status = http.StatusOK

	return e
}

func (e *Env) Header() http.Header {
	return e.w.Header()
}

func (e *Env) SetContentType(tp string) {
	e.w.Header().Set("Content-type", tp)
}

func (e *Env) SetContentJson() {
	e.w.Header().Set("Content-type", "application/json; charset=utf-8")
}

func (e *Env) SetStatus(status int) {
	e.Status = status
}

func (e *Env) Write(v interface{}) {
	if e.finished {
		return
	}

	buf, err := json.Marshal(v)
	if err != nil {
		e.WriteError(http.StatusInternalServerError, err)
	} else {
		e.SetContentJson()

		e.WriteBuffer(buf)
	}
}

func (e *Env) WriteString(data string) {
	if e.finished {
		return
	}

	e.WriteBuffer([]byte(data))
}

func (e *Env) WriteBuffer(data []byte) {
	if e.finished {
		return
	}

	e.finished = true

	e.w.WriteHeader(e.Status)
	e.w.Write(data)
}

func (e *Env) WriteError(status int, err error) {
	e.Status = status
	e.WriteString(err.Error())
}
<commit_msg>add default content-type if not set<commit_after>package polaris

import (
	"encoding/json"
	"net/http"
	"strconv"
)

type Env struct {
	Request *http.Request
	Status  int

	w        http.ResponseWriter
	finished bool
}

func newEnv(w http.ResponseWriter, r *http.Request) *Env {
	e := new(Env)

	e.Request = r
	e.w = w

	e.finished = false
	e.Status = http.StatusOK

	return e
}

func (e *Env) Header() http.Header {
	return e.w.Header()
}

func (e *Env) SetContentType(tp string) {
	e.w.Header().Set("Content-type", tp)
}

func (e *Env) SetContentJson() {
	e.w.Header().Set("Content-type", "application/json; charset=utf-8")
}

func (e *Env) SetStatus(status int) {
	e.Status = status
}

func (e *Env) Write(v interface{}) {
	if e.finished {
		return
	}

	buf, err := json.Marshal(v)
	if err != nil {
		e.WriteError(http.StatusInternalServerError, err)
	} else {
		e.SetContentJson()

		e.write(buf)
	}
}

func (e *Env) WriteString(data string) {
	if len(e.Header().Get("Content-type")) == 0 {
		e.SetContentType("text/plain")
	}

	e.write([]byte(data))
}

func (e *Env) WriteBuffer(data []byte) {
	if len(e.Header().Get("Content-type")) == 0 {
		e.SetContentType("application/octet-stream")
	}

	e.write(data)
}

func (e *Env) write(data []byte) {
	if e.finished {
		return
	}

	e.finished = true

	e.w.Header().Set("Content-Length", strconv.Itoa(len(data)))

	e.w.WriteHeader(e.Status)
	e.w.Write(data)
}

func (e *Env) WriteError(status int, err error) {
	e.Status = status
	e.WriteString(err.Error())
}

{'fraction_non_alphanumeric': 0.16666666666666666, 'fraction_numerical': 0.0014652014652014652, 'mean_word_length': 3.6209813874788495, 'pattern_counts': {'":': 0, '<': 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': '3757947', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1083, 'n_words': 243}