<commit_before>package grack_http

import (
	"encoding/json"
	"fmt"
	. "github.com/metakeule/grack"
	"html"
	"net/http"
	"strings"
)

// returns the h.Header from the original http.ResponseWriter
func Header(c HTTPContexter) http.Header {
	h := c.HTTPContext_()
	return h.ResponseWriter.Header()
}

// this func writes the response back to the server
func Flush(c HTTPContexter) {
	h := c.HTTPContext_()
	if h.Status != 0 {
		h.ResponseWriter.WriteHeader(h.Status)
	}
	_, err := h.ResponseWriter.Write([]byte(h.Out))
	if err != nil {
		fmt.Printf("Error: %#v", err)
		//panic(err)
	}
}

func Request(c HTTPContexter) *http.Request {
	h := c.HTTPContext_()
	return h.Request
}

func Path(c HTTPContexter) string {
	return Request(c).URL.Path
}

func SetOut(c HTTPContexter, s string) {
	h := c.HTTPContext_()
	h.Out = s
}

func Out(c HTTPContexter) (s string) {
	h := c.HTTPContext_()
	return h.Out
}

func Add(c HTTPContexter, s string) {
	h := c.HTTPContext_()
	h.Out = h.Out + s
}

// sets the status
func SetStatus(c HTTPContexter, status int) {
	h := c.HTTPContext_()
	h.Status = status
}

// returns the Status
func Status(c HTTPContexter) int {
	h := c.HTTPContext_()
	return h.Status
}

// sets the status
func SetContentType(c HTTPContexter, ctype string) {
	h := Header(c)
	h["Content-Type"] = []string{ctype}
}

// TODO make a type switch and let it accept templates
func HTML(c HTTPContexter, html ...string) {
	SetContentType(c, "text/html")
	if len(html) > 0 {
		SetOut(c, html[0])
	}
}

// TODO make a type switch and let it accept templates
func JSON(c HTTPContexter, i ...interface{}) {
	SetContentType(c, "application/json")
	if len(i) > 0 {
		jsoned, _ := json.Marshal(i[0])
		s := string(jsoned)
		SetOut(c, s)
	}
}

func DebugRequest(r *http.Request) (debug map[string]string) {
	debug = map[string]string{}
	debug["Method"] = r.Method
	debug["Proto"] = r.Proto
	debug["Host"] = r.Host
	debug["RemoteAddr"] = r.RemoteAddr
	debug["RequestURI"] = r.RequestURI
	headers := r.Header
	for k, v := range headers {
		debug["Header "+k] = strings.Join(v, "<br>")
	}
	//debug["URL"] = fmt.Sprintf("%#v", r.URL)
	// debug["Form"] = fmt.Sprintf("%#v", r.Form)
	// debug["MultipartForm"] = fmt.Sprintf("%#v", r.MultipartForm)
	// debug["Body"] = fmt.Sprintf("%#v", r.Body)
	// debug["ContentLength"] = fmt.Sprintf("%d", r.ContentLength)
	// debug["TransferEncoding"] = fmt.Sprintf("%#v", r.TransferEncoding)
	return<commit_msg>last commit before rework<commit_after>package grack_http

import (
	"encoding/json"
	"fmt"
	. "github.com/metakeule/grack"
	"html"
	"net/http"
	"strings"
)

// returns the h.Header from the original http.ResponseWriter
func Header(c HTTPContexter) http.Header {
	h := c.HTTPContext_()
	return h.ResponseWriter.Header()
}

// this func writes the response back to the server
func Flush(c HTTPContexter) {
	h := c.HTTPContext_()
	if h.Status != 0 {
		h.ResponseWriter.WriteHeader(h.Status)
	}
	_, err := h.ResponseWriter.Write([]byte(h.Out))
	if err != nil {
		fmt.Printf("Error: %#v", err)
		//panic(err)
	}
}

func Request(c HTTPContexter) *http.Request {
	h := c.HTTPContext_()
	return h.Request
}

func Path(c HTTPContexter) string {
	return Request(c).URL.Path
}

func SetOut(c HTTPContexter, s string) {
	h := c.HTTPContext_()
	h.Out = s
}

func Out(c HTTPContexter) (s string) {
	h := c.HTTPContext_()
	return h.Out
}

func Add(c HTTPContexter, s string) {
	h := c.HTTPContext_()
	h.Out = h.Out + s
}

// sets the status
func SetStatus(c HTTPContexter, status int) {
	h := c.HTTPContext_()
	h.Status = status
}

// returns the Status
func Status(c HTTPContexter) int {
	h := c.HTTPContext_()
	return h.Status
}

// sets the status
func SetContentType(c HTTPContexter, ctype string) {
	h := Header(c)
	h["Content-Type"] = []string{ctype}
}

// TODO make a type switch and let it accept templates
func HTML(c HTTPContexter, html ...string) {
	SetContentType(c, "text/html")
	if len(html) > 0 {
		SetOut(c, html[0])
	}
}

// TODO make a type switch and let it accept templates
func JSON(c HTTPContexter, i ...interface{}) {
	SetContentType(c, "application/json")
	if len(i) > 0 {
		jsoned, _ := json.Marshal(i[0])
		s := string(jsoned)
		SetOut(c, s)
	}
}

// TODO make a type switch and let it accept templates
func CSS(c HTTPContexter, s string) {
	SetContentType(c, "text/css")
	SetOut(c, s)
}

func DebugRequest(r *http.Request) (debug map[string]string) {
	debug = map[string]string{}
	debug["Method"] = r.Method
	debug["Proto"] = r.Proto
	debug["Host"] = r.Host
	debug["RemoteAddr"] = r.RemoteAddr
	debug["RequestURI"] = r.RequestURI
	headers := r.Header
	for k, v := range headers {
		debug["Header "+k] = strings.Join(v, "<br>")
	}
	//debug["URL"] = fmt.Sprintf("%#v", r.URL)
	// debug["Form"] = fmt.Sprintf("%#v", r.Form)
	// debug["MultipartForm"] = fmt.Sprintf("%#v", r.MultipartForm)
	// debug["Body"] = fmt.Sprintf("%#v", r.Body)
	// debug["ContentLength"] = fmt.Sprintf("%d", r.ContentLength)
	// debug["TransferEncoding"] = fmt.Sprintf("%#v", r.TransferEncoding)
	return
{'fraction_non_alphanumeric': 0.17081425442962372, 'fraction_numerical': 0.001990842126219391, 'mean_word_length': 4.00398406374502, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '2314486', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1893, 'n_words': 467}