summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-21 22:32:49 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-21 22:32:49 -0400
commit54a4f6f720979dc8c8d6224acd24f04d147ef761 (patch)
tree5c7d8c19d8ca0f145cee9234578e881366989b35
parent360f7e248a065a766890db9b4f65757dc6365eb2 (diff)
downloadgetwtxt-54a4f6f720979dc8c8d6224acd24f04d147ef761.tar.gz
fixed http 400 response and test
-rw-r--r--http.go3
-rw-r--r--http_test.go2
-rw-r--r--post.go18
3 files changed, 9 insertions, 14 deletions
diff --git a/http.go b/http.go
index 9b5dd81..b6c1cee 100644
--- a/http.go
+++ b/http.go
@@ -45,9 +45,10 @@ func log200(r *http.Request) {
 }
 
 // log output for 400s
-func log400(r *http.Request, err error) {
+func log400(w http.ResponseWriter, r *http.Request, err error) {
 	uip := getIPFromCtx(r.Context())
 	log.Printf("*** %v :: 400 :: %v %v :: %v\n", uip, r.Method, r.URL, err)
+	http.Error(w, "400 Bad Request: "+err.Error(), http.StatusBadRequest)
 }
 
 // log output for 404s
diff --git a/http_test.go b/http_test.go
index 57635e5..67ccc88 100644
--- a/http_test.go
+++ b/http_test.go
@@ -12,7 +12,7 @@ func Test_log400(t *testing.T) {
 	t.Run("log400", func(t *testing.T) {
 		w := httptest.NewRecorder()
 		req := httptest.NewRequest("POST", "/400", nil)
-		log400(req, errors.New("400 Test"))
+		log400(w, req, errors.New("400 Test"))
 		resp := w.Result()
 		if resp.StatusCode != http.StatusBadRequest {
 			t.Errorf("Didn't receive 400, received: %v\n", resp.StatusCode)
diff --git a/post.go b/post.go
index b9c830e..25d1902 100644
--- a/post.go
+++ b/post.go
@@ -14,15 +14,13 @@ import (
 // registry before adding each user to the local cache.
 func apiPostUser(w http.ResponseWriter, r *http.Request) {
 	if err := r.ParseForm(); err != nil {
-		_, _ = w.Write([]byte(fmt.Sprintf("400 Bad Request: %v\n", err)))
-		log400(r, err)
+		log400(w, r, err)
 		return
 	}
 	nick := r.FormValue("nickname")
 	urls := r.FormValue("url")
 	if nick == "" || urls == "" {
-		_, _ = w.Write([]byte("400 Bad Request: Nickname or URL Missing\n"))
-		log400(r, fmt.Errorf("nickname or URL missing"))
+		log400(w, r, fmt.Errorf("Nickname or URL missing"))
 		return
 	}
 
@@ -30,8 +28,7 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 
 	out, remoteRegistry, err := registry.GetTwtxt(urls)
 	if err != nil {
-		_, _ = w.Write([]byte(fmt.Sprintf("400 Bad Request: %v\n", err)))
-		log400(r, err)
+		log400(w, r, err)
 		return
 	}
 
@@ -42,8 +39,7 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 
 		err := twtxtCache.ScrapeRemoteRegistry(urls)
 		if err != nil {
-			_, _ = w.Write([]byte(fmt.Sprintf("400 Bad Request: %v\n", err)))
-			log400(r, err)
+			log400(w, r, err)
 			return
 		}
 		log200(r)
@@ -52,15 +48,13 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 
 	statuses, err := registry.ParseUserTwtxt(out, nick, urls)
 	if err != nil {
-		_, _ = w.Write([]byte(fmt.Sprintf("400 Bad Request: %v\n", err)))
-		log400(r, err)
+		log400(w, r, err)
 		return
 	}
 
 	err = twtxtCache.AddUser(nick, urls, uip, statuses)
 	if err != nil {
-		_, _ = w.Write([]byte(fmt.Sprintf("400 Bad Request: %v\n", err)))
-		log400(r, err)
+		log400(w, r, err)
 		return
 	}
 
nd_parser.html?h=v1.8.0&id=2ca2e862e616eba862ac80f5ee0003d1aa984a32'>^
c9383c72 ^

4c13e1f2 ^

c9383c72 ^

4c13e1f2 ^







c9383c72 ^







4c13e1f2 ^
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