From 386c74a10bd88c49d8f15a370d2e7b7dcc0f566f Mon Sep 17 00:00:00 2001 From: Mikhail Klementyev Date: Sun, 20 Nov 2016 16:17:46 +0300 Subject: [PATCH] Implements cookie serialization/deserialization --- storage/storage.go | 88 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/storage/storage.go b/storage/storage.go index 83536be..1ebd2d0 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -9,14 +9,61 @@ package storage import ( + "bytes" "database/sql" + "encoding/base64" + "encoding/gob" "errors" + "log" + "net/http" "reflect" "strings" _ "github.com/mattn/go-sqlite3" ) +func toGOB64(m http.Cookie) string { + b := bytes.Buffer{} + e := gob.NewEncoder(&b) + err := e.Encode(m) + if err != nil { + panic(err) + } + return base64.StdEncoding.EncodeToString(b.Bytes()) +} + +func fromGOB64(str string) http.Cookie { + m := http.Cookie{} + by, err := base64.StdEncoding.DecodeString(str) + if err != nil { + panic(err) + } + b := bytes.Buffer{} + b.Write(by) + d := gob.NewDecoder(&b) + err = d.Decode(&m) + if err != nil { + panic(err) + } + return m +} + +func serializeCookies(cookies []*http.Cookie) (s string) { + for _, c := range cookies { + s += toGOB64(*c) + " " + } + return +} + +func deserializeCookies(s string) (cookies []*http.Cookie) { + gob64Objects := strings.Split(s, " ") + for _, g := range gob64Objects { + c := fromGOB64(g) + cookies = append(cookies, &c) + } + return +} + func OpenDB(path string) (db *sql.DB, err error) { db, err = sql.Open("sqlite3", path) if err != nil { @@ -40,7 +87,7 @@ func OpenDB(path string) (db *sql.DB, err error) { " `form_id` INTEGER, " + " `hidden` BOOLEAN, " + " `value` TEXT, " + - " `name` TEXT );") + " `name` TEXT );") if err != nil { return } @@ -49,6 +96,45 @@ func OpenDB(path string) (db *sql.DB, err error) { "( `id` INTEGER PRIMARY KEY AUTOINCREMENT, " + " `post` BOOLEAN, " + " `url` TEXT );") + if err != nil { + return + } + + _, err = db.Exec("CREATE TABLE IF NOT EXISTS `cookies` " + + "( `id` INTEGER PRIMARY KEY AUTOINCREMENT, " + + " `url` BOOLEAN, " + + " `cookies` TEXT );") + + return +} + +func AddCookies(db *sql.DB, url string, cookies []*http.Cookie) (err error) { + log.Println("Add cookies", url, cookies) + stmt, err := db.Prepare("INSERT INTO `cookies` " + + "(`url`, `cookies`) VALUES ($1, $2);") + if err != nil { + return + } + defer stmt.Close() + + _, err = stmt.Exec(url, serializeCookies(cookies)) + return +} + +func GetCookies(db *sql.DB, url string) (cookies []*http.Cookie, err error) { + stmt, err := db.Prepare("SELECT `cookies` FROM `cookies` WHERE url=$1;") + if err != nil { + return + } + defer stmt.Close() + + var rawCookies string + err = stmt.QueryRow(url).Scan(&rawCookies) + if err != nil { + return + } + + cookies = deserializeCookies(rawCookies) return }