commands/http: Moved http request parsing into a Parse function
Matt Bell committed
Oct 28, 2014 at 17:00 UTC
52bc8bd42242f4d8f87fcbd966943fe6fa53698b
2 files changed
+55
-34
commands/http/handler.go
+10
-34
@@ -1,9 +1,9 @@
1
package http
2
3
import (
4
+ "errors"
5
"io"
6
"net/http"
6
- "strings"
7
8
cmds "github.com/jbenet/go-ipfs/commands"
9
"github.com/jbenet/go-ipfs/core/commands"
@@ -13,22 +13,19 @@ type Handler struct {
13
Ctx cmds.Context
14
}
15
16
-func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
17
- path := strings.Split(r.URL.Path, "/")[3:]
18
- opts := getOptions(r)
19
-
20
- // TODO: get args
16
+var ErrNotFound = errors.New("404 page not found")
17
22
- // ensure the requested command exists, otherwise 404
23
- _, err := commands.Root.Get(path)
18
+func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
19
+ req, err := Parse(r)
20
if err != nil {
25
- w.WriteHeader(http.StatusNotFound)
26
- w.Write([]byte("404 page not found"))
21
+ if err == ErrNotFound {
22
+ w.WriteHeader(http.StatusNotFound)
23
+ } else {
24
+ w.WriteHeader(http.StatusBadRequest)
25
+ }
26
+ w.Write([]byte(err.Error()))
27
return
28
}
29
-
30
- // build the Request
31
- req := cmds.NewRequest(path, opts, nil, nil)
29
req.SetContext(i.Ctx)
30
31
// call the command
@@ -60,24 +57,3 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
57
w.Write([]byte(err.Error()))
58
}
59
}
63
-
64
-// getOptions returns the command options in the given HTTP request
65
-// (from the querystring and request body)
66
-func getOptions(r *http.Request) map[string]interface{} {
67
- opts := make(map[string]interface{})
68
-
69
- query := r.URL.Query()
70
- for k, v := range query {
71
- opts[k] = v[0]
72
- }
73
-
74
- // TODO: get more options from request body (formdata, json, etc)
75
-
76
- _, short := opts[cmds.EncShort]
77
- _, long := opts[cmds.EncLong]
78
- if !short && !long {
79
- opts[cmds.EncShort] = cmds.JSON
80
- }
81
-
82
- return opts
83
-}
commands/http/parse.go
new
+45
@@ -0,0 +1,45 @@
1
+package http
2
+
3
+import (
4
+ "net/http"
5
+ "strings"
6
+
7
+ cmds "github.com/jbenet/go-ipfs/commands"
8
+ "github.com/jbenet/go-ipfs/core/commands"
9
+)
10
+
11
+// Parse parses the data in a http.Request and returns a command Request object
12
+func Parse(r *http.Request) (cmds.Request, error) {
13
+ path := strings.Split(r.URL.Path, "/")[3:]
14
+
15
+ // 404 if there is no command at that path
16
+ if _, err := commands.Root.Get(path); err != nil {
17
+ return nil, ErrNotFound
18
+ }
19
+
20
+ opts, args := parseOptions(r)
21
+
22
+ // TODO: input stream (from request body)
23
+
24
+ return cmds.NewRequest(path, opts, args, nil), nil
25
+}
26
+
27
+func parseOptions(r *http.Request) (map[string]interface{}, []string) {
28
+ opts := make(map[string]interface{})
29
+
30
+ query := r.URL.Query()
31
+ for k, v := range query {
32
+ opts[k] = v[0]
33
+ }
34
+
35
+ // TODO: get more options from request body (formdata, json, etc)
36
+
37
+ // default to setting encoding to JSON
38
+ _, short := opts[cmds.EncShort]
39
+ _, long := opts[cmds.EncLong]
40
+ if !short && !long {
41
+ opts[cmds.EncShort] = cmds.JSON
42
+ }
43
+
44
+ return opts, nil
45
+}