<commit_before>package Golf

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

// Context is a wrapper of http.Request and http.ResponseWriter.
type Context struct {
	// http.Request
	Request *http.Request

	// http.ResponseWriter
	Response http.ResponseWriter

	// URL Parameter
	Params map[string]string

	// HTTP status code
	StatusCode int

	// HTTP header as a map
	Header map[string]string

	// HTTP response body as a byte string
	Body []byte

	// The application
	App *Application

	// Data used for sharing values between middlewares
	Data map[string]interface{}

	// Session instance for the current context.
	Session *Session

	// Indicating if the response is already sent.
	IsSent bool

	// Indicating loader of the template
	templateLoader string
}

// NewContext creates a Golf.Context instance.
func NewContext(req *http.Request, res http.ResponseWriter, app *Application) *Context {
	ctx := new(Context)
	ctx.Request = req
	ctx.Response = res
	ctx.App = app
	ctx.Header = make(map[string]string)
	ctx.StatusCode = 200
	ctx.Header["Content-Type"] = "text/html;charset=UTF-8"
	ctx.Request.ParseForm()
	ctx.IsSent = false
	ctx.Data = make(map[string]interface{})
	return ctx
}

// Query method retrieves the form data, return empty string if not found.
func (ctx *Context) Query(key string, index ...int) (string, error) {
	if val, ok := ctx.Request.Form[key]; ok {
		if len(index) == 1 {
			return val[index[0]], nil
		}
		return val[0], nil
	}
	return "", errors.New("Query key not found.")
}

// Param method retrieves the parameters from url
// If the url is /:id/, then id can be retrieved by calling `ctx.Param(id)`
func (ctx *Context) Param(key string) (string, error) {
	if val, ok := ctx.Params[key]; ok {
		return val, nil
	}
	return "", errors.New("Parameter not found.")
}

// Redirect method sets the response as a 301 redirection.
// If you need a 302 redirection, please do it by setting the Header manually.
func (ctx *Context) Redirect(url string) {
	ctx.Header["Location"] = url
	ctx.StatusCode = 301
}

// SetCookie set cookies for the request. If expire is 0, create a session cookie.
func (ctx *Context) SetCookie(key string, value string, expire int) {
	now := time.Now()
	cookie := &http.Cookie{
		Name:   key,
		Value:  value,
		Path:   "/",
		MaxAge: expire,
	}
	if expire != 0 {<commit_msg>[feat] Implemented a wrapper for `Request.Cookie`<commit_after>package Golf

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

// Context is a wrapper of http.Request and http.ResponseWriter.
type Context struct {
	// http.Request
	Request *http.Request

	// http.ResponseWriter
	Response http.ResponseWriter

	// URL Parameter
	Params map[string]string

	// HTTP status code
	StatusCode int

	// HTTP header as a map
	Header map[string]string

	// HTTP response body as a byte string
	Body []byte

	// The application
	App *Application

	// Data used for sharing values between middlewares
	Data map[string]interface{}

	// Session instance for the current context.
	Session *Session

	// Indicating if the response is already sent.
	IsSent bool

	// Indicating loader of the template
	templateLoader string
}

// NewContext creates a Golf.Context instance.
func NewContext(req *http.Request, res http.ResponseWriter, app *Application) *Context {
	ctx := new(Context)
	ctx.Request = req
	ctx.Response = res
	ctx.App = app
	ctx.Header = make(map[string]string)
	ctx.StatusCode = 200
	ctx.Header["Content-Type"] = "text/html;charset=UTF-8"
	ctx.Request.ParseForm()
	ctx.IsSent = false
	ctx.Data = make(map[string]interface{})
	return ctx
}

// Query method retrieves the form data, return empty string if not found.
func (ctx *Context) Query(key string, index ...int) (string, error) {
	if val, ok := ctx.Request.Form[key]; ok {
		if len(index) == 1 {
			return val[index[0]], nil
		}
		return val[0], nil
	}
	return "", errors.New("Query key not found.")
}

// Param method retrieves the parameters from url
// If the url is /:id/, then id can be retrieved by calling `ctx.Param(id)`
func (ctx *Context) Param(key string) (string, error) {
	if val, ok := ctx.Params[key]; ok {
		return val, nil
	}
	return "", errors.New("Parameter not found.")
}

// Redirect method sets the response as a 301 redirection.
// If you need a 302 redirection, please do it by setting the Header manually.
func (ctx *Context) Redirect(url string) {
	ctx.Header["Location"] = url
	ctx.StatusCode = 301
}

// Cookie returns the value of the cookie by indicating the key.
func (ctx *Context) Cookie(key string) (string, error) {
	c, err := ctx.Request.Cookie(key)
	if err != nil {
		return nil, err
	}
	return c.Value, nil
}

// SetCookie set cookies for the request. If expire is 0, create a session cookie.
func (ctx *Context) SetCookie(key string, value string, expire int) {
	now := time.Now()
	cookie := &http.Cookie{
		Name:   key,
		Value:  value,
		Path:   "/",
		MaxAge: expire,
	}
	if expire != 0 {
{'fraction_non_alphanumeric': 0.11838637745793634, 'fraction_numerical': 0.007297790391242651, 'mean_word_length': 3.6155285313376986, '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': '1392465', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1550, 'n_words': 571}