<commit_before>package api

import (
	"code.google.com/p/go.crypto/bcrypt"
	"fmt"
	"github.com/gorilla/sessions"
	"github.com/markberger/carton/db"
	"net/http"
)

func loginHandler(db db.DbManager, jar *sessions.CookieStore) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			session, _ := jar.Get(r, "carton-session")
			if _, ok := session.Values["user"]; ok {
				http.Error(w, "already signed in", 400)
				return
			}

			username := r.PostFormValue("user")
			password := r.PostFormValue("<PASSWORD>")

			if username == "" || password == "" {
				http.Error(w, "bad arguments", 400)
				return
			}

			dbHash := db.GetPwdHash(username)
			if dbHash == nil {
				http.Error(w, "user password combo doesn't exist", 400)
				return
			}

			err := bcrypt.CompareHashAndPassword(dbHash, []byte(password))
			if err != nil {
				http.Error(w, "user password combo doesn't exist", 400)
				return
			}
			session.Values["user"] = username
			session.Save(r, w)
			fmt.Fprintln(w, "login succeeded")
			w.WriteHeader(200)
		} else {
			return404(w)
		}
	})
}

func registerHandler(db db.DbManager, jar *sessions.CookieStore) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			session, _ := jar.Get(r, "carton-session")
			if _, ok := session.Values["user"]; ok {
				http.Error(w, "already signed in", 400)
				return
			}
			username := r.FormValue("user")
			pass1 := r.FormValue("<PASSWORD>1")
			pass2 := r.FormValue("pass2")

			if username == "" ||
				pass1 == "" ||
				pass2 == "" ||
				pass1 != pass2 {
				http.Error(w, "bad arguments", 400)
			}

			if db.IsUser(username) {
				http.Error(w, "user already exists", 400)
				return
			}

			bytePass := []byte(pass1)
			hash, err := bcrypt.GenerateFromPassword(bytePass, bcrypt.DefaultCost)
			if err != nil {
				http.Error(w, "error hashing password", 500)
				return
			}

			err = db.RegisterUser(username, hash)
			if err != nil {
				http.Error(w, "unable to add user", 500)
				return
			}
			session.Values["user"] = username
			session.Save(r, w)
			fmt.Fprintf(w, "Successfully registered %v", username)
			w.WriteHeader(201)
		} else {
			return404(w)
		}
	})
}

func logoutHandler(jar *sessions.CookieStore) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			session, _ := jar.Get(r, "carton-session")
			if _, ok := session.Values["user"]; !ok {
				http.Error(w, "no user to sign out", 400)
				return
			}
			delete(session.Values, "user")
			fmt.Fprintln(w, "Successfully logged out")
			w.WriteHeader(200)
		} else {
			return404(w)
		}
	})
}

func RegisterHandlers(db db.DbManager, jar *sessions.CookieStore) {
	http.Handle("/api/login", loginHandler(db, jar))
	http.Handle("/api/register", registerHandler(db, jar))
	http.Handle("/api/logout", logoutHandler(jar))
}<commit_msg>Replaces response codes with http constants<commit_after>package api

import (
	"code.google.com/p/go.crypto/bcrypt"
	"fmt"
	"github.com/gorilla/sessions"
	"github.com/markberger/carton/db"
	"net/http"
)

func loginHandler(db db.DbManager, jar *sessions.CookieStore) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			session, _ := jar.Get(r, "carton-session")
			if _, ok := session.Values["user"]; ok {
				http.Error(w, "already signed in", http.StatusBadRequest)
				return
			}

			username := r.PostFormValue("user")
			password := r.PostFormValue("<PASSWORD>")

			if username == "" || password == "" {
				http.Error(w, "bad arguments", http.StatusBadRequest)
				return
			}

			dbHash := db.GetPwdHash(username)
			if dbHash == nil {
				http.Error(
					w,
					"user password combo doesn't exist",
					http.StatusBadRequest,
				)
				return
			}

			err := bcrypt.CompareHashAndPassword(dbHash, []byte(password))
			if err != nil {
				http.Error(
					w,
					"user password combo doesn't exist",
					http.StatusBadRequest,
				)
				return
			}
			session.Values["user"] = username
			session.Save(r, w)
			fmt.Fprintln(w, "login succeeded")
			w.WriteHeader(http.StatusOK)
		} else {
			return404(w)
		}
	})
}

func registerHandler(db db.DbManager, jar *sessions.CookieStore) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			session, _ := jar.Get(r, "carton-session")
			if _, ok := session.Values["user"]; ok {
				http.Error(w, "already signed in", http.StatusBadRequest)
				return
			}
			username := r.FormValue("user")
			pass1 := r.FormValue("pass1")
			pass2 := r.FormValue("pass2")

			if username == "" ||
				pass1 == "" ||
				pass2 == "" ||
				pass1 != pass2 {
				http.Error(w, "bad arguments", http.StatusBadRequest)
			}

			if db.IsUser(username) {
				http.Error(w, "user already exists", http.StatusBadRequest)
				return
			}

			bytePass := []byte(pass1)
			hash, err := bcrypt.GenerateFromPassword(bytePass, bcrypt.DefaultCost)
			if err != nil {
				http.Error(
					w,
					"error hashing password",
					http.StatusInternalServerError,
				)
				return
			}

			err = db.RegisterUser(username, hash)
			if err != nil {
				http.Error(
					w,
					"unable to add user",
					http.StatusInternalServerError,
				)
				return
			}
			session.Values["user"] = username
			session.Save(r, w)
			fmt.Fprintf(w, "Successfully registered %v", username)
			w.WriteHeader(http.StatusCreated)
		} else {
			return404(w)
		}
	})
}

func logoutHandler(jar *sessions.CookieStore) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			session, _ := jar.Get(r, "carton-session")
			if _, ok := session.Values["user"]; !ok {
				http.Error(w, "no user to sign out", http.StatusBadRequest)
				return
			}
			delete(session.Values, "user")
			fmt.Fprintln(w, "Successfully logged out")
			w.WriteHeader(http.StatusOK)
		} else {
			return404(w)
		}
	})
}

func RegisterHandlers(db db.DbManager, jar *sessions.CookieStore) {
	http.Handle("/api/login", loginHandler(db, jar))
	http.Handle("/api/register", registerHandler(db, jar))
	http.Handle("/api/logout", logoutHandler(jar))
}
{'fraction_non_alphanumeric': 0.15555555555555556, 'fraction_numerical': 0.011990407673860911, 'mean_word_length': 3.293754289636239, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '1813846', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2170, 'n_words': 457}