@cryptotaxi247 / kubo / commits / 753162529

commands/http: Parse command args

Matt Bell committed Oct 28, 2014 at 17:21 UTC 753162529a34c65143e53cbe2df3c808eaa2ec37
1 file changed +18 -7
commands/http/parse.go
+18 -7
@@ -11,13 +11,21 @@ import (
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 + args := make([]string, 0)
15
15 - // 404 if there is no command at that path
16 - if _, err := commands.Root.Get(path); err != nil {
16 + if cmd, err := commands.Root.Get(path[:len(path)-1]); err != nil {
17 + // 404 if there is no command at that path
18 return nil, ErrNotFound
19 +
20 + } else if cmd.Subcommand(path[len(path)-1]) == nil {
21 + // if the last string in the path isn't a subcommand, use it as an argument
22 + // e.g. /objects/Qabc12345 (we are passing "Qabc12345" to the "objects" command)
23 + args = append(args, path[len(path)-1])
24 + path = path[:len(path)-1]
25 }
26
20 - opts, args := parseOptions(r)
27 + opts, args2 := parseOptions(r)
28 + args = append(args, args2...)
29
30 // TODO: input stream (from request body)
31
@@ -26,14 +34,17 @@ func Parse(r *http.Request) (cmds.Request, error) {
34
35 func parseOptions(r *http.Request) (map[string]interface{}, []string) {
36 opts := make(map[string]interface{})
37 + var args []string
38
39 query := r.URL.Query()
40 for k, v := range query {
32 - opts[k] = v[0]
41 + if k == "arg" {
42 + args = v
43 + } else {
44 + opts[k] = v[0]
45 + }
46 }
47
35 - // TODO: get more options from request body (formdata, json, etc)
36 -
48 // default to setting encoding to JSON
49 _, short := opts[cmds.EncShort]
50 _, long := opts[cmds.EncLong]
@@ -41,5 +52,5 @@ func parseOptions(r *http.Request) (map[string]interface{}, []string) {
52 opts[cmds.EncShort] = cmds.JSON
53 }
54
44 - return opts, nil
55 + return opts, args
56 }