/
utils.go
130 lines (113 loc) · 3.2 KB
/
utils.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
package util
import (
"crypto/rand"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"errors"
"io"
"log"
"net/http"
"os"
"regexp"
"github.com/1f604/supersimplewiki/cmd/server/globals"
)
func WriteHeaderNoCache(w http.ResponseWriter) {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.
w.Header().Set("Pragma", "no-cache") // HTTP 1.0.
w.Header().Set("Expires", "0") // Proxies.
}
func WriteBodyNoRefresh(w http.ResponseWriter, bytes []byte) {
const noSubmitOnRefreshJS = `<script src="/public_assets/norefresh.js"></script>
`
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(noSubmitOnRefreshJS))
w.Write(bytes)
}
func WriteHTTPResponse(w http.ResponseWriter, errorcode int, msg string) {
w.WriteHeader(errorcode)
w.Write([]byte(msg))
}
func WriteHTTPNoRefreshResponse(w http.ResponseWriter, errorcode int, msg string) {
w.WriteHeader(errorcode)
WriteBodyNoRefresh(w, []byte(msg))
}
// used for creating new session tokens
func GetRandomStringBASE64() string {
c := 24
b := make([]byte, c)
_, err := rand.Read(b)
if err != nil {
log.Fatal("error:", err)
}
// The slice should now contain random bytes instead of only zeroes.
str := base64.StdEncoding.EncodeToString(b)
return str
}
// used for checking if text was modified
func GetSHA1sum(bytes []byte) string {
h := sha1.New()
h.Write(bytes)
bs := h.Sum(nil)
return hex.EncodeToString(bs)
}
// used to check if file was modified while user was editing it
func GetSHA1sumOfFile(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
return "", err
}
defer f.Close()
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal("Failed to get checksum of file:", err)
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// used for creating new filenames
func getRandomStringHex(c int) string {
b := make([]byte, c)
_, err := rand.Read(b)
if err != nil {
log.Fatal("error:", err)
}
// The slice should now contain random bytes instead of only zeroes.
str := hex.EncodeToString(b)
return str
}
// Guaranteed to return a filename that does not currently exist
func generateRandomFilename() string {
for {
name := getRandomStringHex(5) // 255 ^ 5 possible file names.
if _, err := os.Stat("./pages/" + name); errors.Is(err, os.ErrNotExist) {
return name
}
//println("Oh no! Filename already exists!")
}
}
// This function assumes the user is already logged in.
func GetUsernameFromRequest(r *http.Request) string {
c, err := r.Cookie("session_token")
if err != nil {
log.Fatal("Failed to get session token")
}
sessionToken := c.Value
username, ok := globals.TokenMap[sessionToken]
if !ok {
log.Fatal("Failed to get username from session token")
}
return username
}
func Create_dir_if_not_exists(path_to_dir string) {
err := os.MkdirAll(path_to_dir, os.ModePerm)
if err != nil {
log.Fatal("failed to create dir:", path_to_dir, err)
}
}
func MatchRegex(s string, r *regexp.Regexp) ([]string, error) {
if !r.Match([]byte(s)) {
return nil, errors.New("Failed to match regex: " + r.String() + " to string: " + s)
} else {
return r.FindStringSubmatch(s), nil
}
}