<commit_before>package cheshire

import (
	"log"
	"net/http"
)

type Session struct {
	//The cache provider
	cache Cache

	//Max age for the session
	sessionAgeSeconds int
}

func NewSession(cache Cache, sessionMaxSeconds int) *Session {
	return &Session{
		cache:             cache,
		sessionAgeSeconds: sessionMaxSeconds,
	}
}

//This is called before the Controller is called. 
//returning false will stop the execution
func (this *Session) Before(txn *Txn) bool {
	if txn.Type() != "html" {
		//skip
		return true
	}
	// log.Println("SESSION!")
	httpWriter, err := ToHttpWriter(txn)
	if err != nil {
		log.Printf("ERROR in session.before %s", err)
		return true //should we continue with the request?
	}

	cookie, err := httpWriter.HttpRequest.Cookie("session_id")

	if err != nil {
		//create new session id
		txn.Session.Put("session_id", SessionId())
		// log.Println("Created session!")
	} else {
		//load the session. 
		sessionId := cookie.Value
		// log.Printf("Found session cookie! %s", sessionId)
		bytes, ok := this.cache.Get(sessionId)
		if !ok {
			//create a new session, since the old one is gone
			sessionId = SessionId()
			// log.Printf("Old session expired, setting new one (%s)", sessionId)
		} else {
			err = txn.Session.UnmarshalJSON(bytes)
			if err != nil {
				log.Printf("Error unmarshaling json (%s) -> (%s)", bytes, err)
			}
		}
		txn.Session.Put("session_id", sessionId)
	}
	return true
}

func (this *Session) BeforeHtmlWrite(txn *Txn, writer http.ResponseWriter) bool {

	sessionId, ok := txn.Session.GetString("session_id")
	if !ok {
		log.Println("Error! No Sessionid in txn.  wtf?")
		return true
	}
	if txn.Session.MustBool("delete_session", false) {
		log.Println("Deleting session")
		cookie := &http.Cookie{
			Name:   "session_id",
			Value:  sessionId,
			MaxAge: 0,
		}
		http.SetCookie(writer, cookie)
		this.cache.Delete(sessionId)
		return true
	}

	//session will always have session_id param
	if len(txn.Session.Map) > 1 {
		//only write the session if there is something in it
		cookie := &http.Cookie{
			Name:   "session_id",
			Value:  sessionId,
			MaxAge: this.sessionAgeSeconds,
		}
		http.SetCookie(writer, cookie)
		bytes, err := txn.Session.MarshalJSON()
		if err != nil {<commit_msg>Fix in sessions, so once we store sessions, we keep storing them<commit_after>package cheshire

import (
	"log"
	"net/http"
)

type Session struct {
	//The cache provider
	cache Cache

	//Max age for the session
	sessionAgeSeconds int
}

func NewSession(cache Cache, sessionMaxSeconds int) *Session {
	return &Session{
		cache:             cache,
		sessionAgeSeconds: sessionMaxSeconds,
	}
}

//This is called before the Controller is called. 
//returning false will stop the execution
func (this *Session) Before(txn *Txn) bool {
	if txn.Type() != "html" {
		//skip
		return true
	}
	// log.Println("SESSION!")
	httpWriter, err := ToHttpWriter(txn)
	if err != nil {
		log.Printf("ERROR in session.before %s", err)
		return true //should we continue with the request?
	}

	cookie, err := httpWriter.HttpRequest.Cookie("session_id")

	if err != nil {
		//create new session id
		txn.Session.Put("session_id", SessionId())
		// log.Println("Created session!")
	} else {
		//load the session. 
		sessionId := cookie.Value
		// log.Printf("Found session cookie! %s", sessionId)
		bytes, ok := this.cache.Get(sessionId)
		if !ok {
			//create a new session, since the old one is gone
			sessionId = SessionId()
			// log.Printf("Old session expired, setting new one (%s)", sessionId)
		} else {
			err = txn.Session.UnmarshalJSON(bytes)
			if err != nil {
				log.Printf("Error unmarshaling json (%s) -> (%s)", bytes, err)
			}
		}
		txn.Session.Put("session_id", sessionId)
	}
	return true
}

func (this *Session) BeforeHtmlWrite(txn *Txn, writer http.ResponseWriter) bool {

	sessionId, ok := txn.Session.GetString("session_id")
	if !ok {
		log.Println("Error! No Sessionid in txn.  wtf?")
		return true
	}
	if txn.Session.MustBool("delete_session", false) {
		log.Println("Deleting session")
		cookie := &http.Cookie{
			Name:   "session_id",
			Value:  sessionId,
			MaxAge: 0,
		}
		http.SetCookie(writer, cookie)
		this.cache.Delete(sessionId)
		return true
	}

	//session will always have session_id param
	if len(txn.Session.Map) > 1 {
        //We set an internal flag in the session, so 
        //if keys are removed now we always save the session. 
        txn.Session.Put("_persisted", true)

		//only write the session if there is something in it
		cookie := &http.Cookie{
			Name:   "session_id",
			Value:  sessionId,
			MaxAge: this.sessionAgeSeconds,
		}
		http.SetCookie(writer, cookie)
		bytes, err := txn.Session.MarshalJSON()
		if err != nil {
{'fraction_non_alphanumeric': 0.1289568727427236, 'fraction_numerical': 0.0008497981729339282, 'mean_word_length': 3.395891690009337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4076323', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1581, 'n_words': 470}