7
7
"log"
8
8
"math/rand"
9
9
"net/http"
10
+ "net/url"
10
11
"os/exec"
11
12
"regexp"
12
13
"strconv"
@@ -81,7 +82,7 @@ func LoginHandler(w http.ResponseWriter, req *http.Request, p httprouter.Params)
81
82
if verifyUser (w , req , email , password ) {
82
83
http .Redirect (w , req , "/admin/" , http .StatusFound )
83
84
} else {
84
- fmt . Fprintf (w , " Invalid email/ password" )
85
+ http . Redirect (w , req , "/error/ Invalid email or password", http . StatusFound )
85
86
}
86
87
}
87
88
@@ -184,15 +185,21 @@ func AdminPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
184
185
"Blogs" : getBlogsForUser (db , username ),
185
186
})
186
187
} else {
187
- fmt . Fprintf (w , " You must be authenticated!") // TODO make this look better
188
+ http . Redirect (w , r , "/error/ You must be authenticated!", http . StatusFound )
188
189
}
189
190
}
190
191
191
192
func AdminHandler (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
192
193
blogname := r .FormValue ("blogname" )
193
- website := r .FormValue ("website" )
194
+ websiteOriginal := r .FormValue ("website" )
194
195
port := rand .Intn (63000 ) + 2000
195
196
197
+ website , err := checkUrl (websiteOriginal )
198
+ if err != nil {
199
+ http .Redirect (w , r , fmt .Sprintf ("/error/%s is not a valid url" , websiteOriginal ), http .StatusFound )
200
+ return
201
+ }
202
+
196
203
re := regexp .MustCompile ("[^A-Za-z]" )
197
204
blogname = re .ReplaceAllString (blogname , "" )
198
205
@@ -217,20 +224,23 @@ func AdminHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
217
224
fmt .Println (err )
218
225
} else {
219
226
fmt .Println ("80 -> " + strconv .Itoa (port ))
220
- fmt .Fprintf ( w , "%s" , create )
227
+ fmt .Println ( create )
221
228
db .Update (func (tx * bolt.Tx ) error {
222
229
b := tx .Bucket ([]byte ("BlogMappingBucket" ))
223
230
err := b .Put ([]byte (blogname ), []byte (website ))
224
231
return err
225
232
})
226
233
addBlogToUser (db , username , blogname , website )
227
234
http .Redirect (w , r , "/admin/" , http .StatusFound )
235
+ return
228
236
}
229
237
} else {
230
- fmt .Fprintf (w , "Failure creating blog! Please choose a different name!" )
238
+ http .Redirect (w , r , "/error/Failure creating blog! Please choose a different name!" , http .StatusFound )
239
+ return
231
240
}
232
241
} else {
233
- fmt .Fprintf (w , "You must be authenticated!" ) // TODO make this look better
242
+ http .Redirect (w , r , "/error/You must be authenticated!" , http .StatusFound )
243
+ return
234
244
}
235
245
}
236
246
@@ -383,6 +393,16 @@ func getUserFromCookie(value string) string {
383
393
return ""
384
394
}
385
395
396
+ func checkUrl (s string ) (string , error ) {
397
+ u , err := url .Parse (s )
398
+
399
+ if err != nil || u .Host == "" {
400
+ u , err = url .Parse ("http://" + s )
401
+ }
402
+
403
+ return u .Host , err
404
+ }
405
+
386
406
func main () {
387
407
fmt .Println ("Started server on port 1337" )
388
408
router := httprouter .New ()
0 commit comments