-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderers.go
161 lines (132 loc) · 3.74 KB
/
renderers.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
152
153
154
155
156
157
158
159
160
161
package main
import (
"bytes"
"encoding/json"
"html/template"
"log"
"net/http"
"strconv"
"strings"
dice "git.cmcode.dev/cmcode/go-dicewarelib"
)
func handleParams(r *http.Request) (int, string, int, int, bool) {
var n string
var s string
var maxLen string
var minLen string
var extra string
if r.Method == http.MethodPost {
err := r.ParseForm()
if err != nil {
log.Printf("handleParams failed to parse: %v", err.Error())
}
n = r.Form.Get("n")
s = r.Form.Get("s")
maxLen = r.Form.Get("u")
minLen = r.Form.Get("l")
extra = r.Form.Get("e")
} else {
n = r.URL.Query().Get("n")
s = r.URL.Query().Get("s")
maxLen = r.URL.Query().Get("u")
minLen = r.URL.Query().Get("l")
extra = r.URL.Query().Get("e")
}
if strings.ToLower(s) == "space" {
s = " "
}
// if no values are specified, set the default separator to a space
// character - this is because the default load page doesn't specify any
// params
if n == "" && s == "" && maxLen == "" && minLen == "" {
s = " "
}
// assign a default value of 3 words if the user didn't
// specify a desired number of words
nn := int64(3)
if n != "" {
var err error
nn, err = strconv.ParseInt(n, 10, 64)
if err != nil {
nn = 3
}
}
// assign a default value of 32 characters if the user didn't
// specify a desired max length
maxLenInt := int64(32)
if maxLen != "" {
maxLenInt, _ = strconv.ParseInt(maxLen, 10, 64)
}
// assign a default value of 20 characters if the user didn't
// specify a desired min length
minLenInt := int64(20)
if minLen != "" {
minLenInt, _ = strconv.ParseInt(minLen, 10, 64)
}
useExtra := false
// HTML form submits a checked box as "on"
if extra == "on" {
useExtra = true
} else if extra != "" {
useExtra, _ = strconv.ParseBool(extra)
}
return int(nn), s, int(maxLenInt), int(minLenInt), useExtra
}
// Generates a password via an API call.
func renderGenPassword(w http.ResponseWriter, r *http.Request, words *dice.Words) {
nn, s, maxLenInt, minLenInt, extendedWords := handleParams(r)
if !flagExtra {
extendedWords = false
}
result := make(map[string]string)
result["p"] = dice.GeneratePassword(words, nn, s, maxLenInt, minLenInt, extendedWords)
w.Header().Add(contentTypeHeader, "application/json")
b, err := json.Marshal(result)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("GenPassword failed to marshal result: %v", err.Error())
return
}
_, err = w.Write(b)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("GenPassword failed to write result: %v", err.Error())
return
}
}
// Generates a password and renders the index template.
func renderIndex(w http.ResponseWriter, r *http.Request, words *dice.Words, index *template.Template) {
nn, s, maxLenInt, minLenInt, extendedWords := handleParams(r)
result := dice.GeneratePassword(words, nn, s, maxLenInt, minLenInt, extendedWords)
buf := new(bytes.Buffer)
failed := false
if result == "" {
failed = true
}
data := map[string]any{
"pw": result,
"simpleWordCount": words.SimpleCount,
"extendedWordCount": words.ComplexCount,
"nn": nn,
"s": s,
"maxLenInt": maxLenInt,
"minLenInt": minLenInt,
"e": extendedWords,
"pwLength": len(result),
"failed": failed,
"flagExtra": flagExtra,
}
err := index.Execute(buf, data)
if err != nil {
log.Printf("failed to render index template: %v", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add(contentTypeHeader, "text/html")
_, err = w.Write(buf.Bytes())
if err != nil {
log.Printf("failed to write gzip result to w: %v", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
}