/
gus.go
151 lines (133 loc) · 3.95 KB
/
gus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// author: Mark Silvis
package main
import (
"fmt"
"log"
"strings"
"math"
"net/http"
"html/template"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
const port = ":4000"
var base62 [62]byte
var base10 map[byte]int = make(map[byte]int)
func embiggen(short string, db *sql.DB) (url string, err error) {
id := getId(short)
query := "select url from url_mappings where id=?;"
err = db.QueryRow(query, id).Scan(&url)
return
}
func getId(path string) int {
var id float64
split := strings.Split(path, "")
for i, v := range split {
rune := []byte(v)
w := base10[rune[0]]
id += float64(w)*math.Pow(62, float64(len(split)-(i+1)))
}
return int(id)
}
func shrinkUrl(id int, c chan int) {
for id != 0 {
rem := id % 62
id = id / 62
c <- rem
}
close(c)
}
func newUrlMapping(url string, db *sql.DB) string {
var lastId int
query := "select id from url_mappings order by id desc limit 1;"
err := db.QueryRow(query).Scan(&lastId)
if err != nil {
lastId = 0
}
lastId++
c := make(chan int, 6)
var shrunk []byte
shrinkUrl(lastId, c)
for i := range c {
shrunk = append(shrunk, base62[i])
}
short := string(shrunk[:])
query = "INSERT INTO url_mappings (url, short) Values(?, ?);"
_, err = db.Exec(query, url, short)
if err != nil {
log.Fatal(err)
}
return short
}
func handlePath(w http.ResponseWriter, r *http.Request, db *sql.DB, path string) {
// this will send a redirect to the full website
redirect, err := embiggen(path, db)
if err != nil {
fmt.Fprintln(w, "404: page not found")
} else {
http.Redirect(w, r, "https://"+redirect, http.StatusFound)
}
}
func handleRoot(w http.ResponseWriter, r *http.Request, db *sql.DB) {
if r.Method == "GET" {
http.ServeFile(w, r, "./resources/index.html")
} else if r.Method == "POST" {
url := r.FormValue("url")
escaped := template.JSEscapeString(url)
if escaped != url {
fmt.Fprintln(w, "Sorry, no XSS attacks")
return
} else if len(escaped) < 2 {
fmt.Fprintln(w, "Error: invalid url")
return
}
escaped = strings.TrimPrefix(escaped, "http://")
escaped = strings.TrimPrefix(escaped, "https://")
escaped = strings.TrimPrefix(escaped, "www.")
var short string
query := "SELECT short FROM url_mappings WHERE url=?;"
err := db.QueryRow(query, escaped).Scan(&short)
if err != nil {
short = newUrlMapping(escaped, db)
}
fmt.Fprintf(w, "<a href=\"localhost%s/%s\">localhost%s/%s</a>", port, short, port, short)
} else {
fmt.Fprintf(w, "405: method not allowed")
}
}
func handler(db *sql.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[1:]
if len(path) == 0 {
handleRoot(w, r, db)
} else {
handlePath(w, r, db, path)
}
})
}
func main() {
db, err := sql.Open("sqlite3", "./db/gus.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// build base62 array for mapping urls
for i := 0; i < 62; i++ {
if i < 26 {
base62[i] = byte(i+65)
} else if i < 52 {
base62[i] = byte(i+71)
} else {
base62[i] = byte(i-52)
}
}
// build base10 array for recovering urls
for i, v := range base62 {
base10[v] = i
}
http.Handle("/stylesheets/", http.StripPrefix("/stylesheets/", http.FileServer(http.Dir("./resources/stylesheets/"))))
http.Handle("/typeface/", http.StripPrefix("/typeface/", http.FileServer(http.Dir("./resources/typeface/"))))
http.Handle("/", handler(db))
// http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./css/"))))
log.Fatal(http.ListenAndServe(port, nil))
}