Rework to include form values

This commit is contained in:
Eliot Whalan 2016-06-23 11:00:33 +10:00
parent 0af4b7ad6b
commit 387d5f313b
1 changed files with 10 additions and 12 deletions

22
main.go
View File

@ -9,7 +9,6 @@ import (
"fmt" "fmt"
"html" "html"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
@ -69,20 +68,19 @@ func generateName() string {
return s return s
} }
func hash(paste []byte) string { func hash(paste string) string {
hasher := sha1.New() hasher := sha1.New()
hasher.Write(paste)
hasher.Write([]byte(paste))
sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
return sha return sha
} }
func save(raw []byte) []string { func save(raw string) []string {
p := raw[86 : len(raw)-46]
db, err := sql.Open("mysql", DATABASE) db, err := sql.Open("mysql", DATABASE)
check(err) check(err)
sha := hash(p) sha := hash(raw)
query, err := db.Query("select id, hash, data, delkey from pastebin") query, err := db.Query("select id, hash, data, delkey from pastebin")
for query.Next() { for query.Next() {
var id, hash, paste, delkey string var id, hash, paste, delkey string
@ -96,7 +94,7 @@ func save(raw []byte) []string {
id := generateName() id := generateName()
url := ADDRESS + "/p/" + id url := ADDRESS + "/p/" + id
delKey := uniuri.NewLen(40) delKey := uniuri.NewLen(40)
paste := html.EscapeString(string(p)) paste := html.EscapeString(raw)
stmt, err := db.Prepare("INSERT INTO pastebin(id, hash, data, delkey) values(?,?,?,?)") stmt, err := db.Prepare("INSERT INTO pastebin(id, hash, data, delkey) values(?,?,?,?)")
check(err) check(err)
@ -134,12 +132,12 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
output := vars["output"] output := vars["output"]
switch r.Method { switch r.Method {
case "POST": case "POST":
buf, err := ioutil.ReadAll(r.Body) paste := r.FormValue("p")
if err != nil { if paste == "" {
http.Error(w, err.Error(), 500) http.Error(w, "Empty paste", 500)
} }
values := save(buf) values := save(paste)
b := &Response{ b := &Response{
ID: values[0], ID: values[0],
HASH: values[1], HASH: values[1],