parse.go 2.82 KB
Newer Older
1 2 3
package http

import (
4
	"errors"
5 6 7 8 9 10 11
	"net/http"
	"strings"

	cmds "github.com/jbenet/go-ipfs/commands"
)

// Parse parses the data in a http.Request and returns a command Request object
12
func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
13 14 15 16 17
	if !strings.HasPrefix(r.URL.Path, ApiPath) {
		return nil, errors.New("Unexpected path prefix")
	}
	path := strings.Split(strings.TrimPrefix(r.URL.Path, ApiPath+"/"), "/")

18
	stringArgs := make([]string, 0)
19

20
	cmd, err := root.Get(path[:len(path)-1])
21
	if err != nil {
Matt Bell's avatar
Matt Bell committed
22
		// 404 if there is no command at that path
23
		return nil, ErrNotFound
Matt Bell's avatar
Matt Bell committed
24

25
	} else if sub := cmd.Subcommand(path[len(path)-1]); sub == nil {
26 27 28 29
		if len(path) <= 1 {
			return nil, ErrNotFound
		}

Matt Bell's avatar
Matt Bell committed
30 31
		// if the last string in the path isn't a subcommand, use it as an argument
		// e.g. /objects/Qabc12345 (we are passing "Qabc12345" to the "objects" command)
32
		stringArgs = append(stringArgs, path[len(path)-1])
Matt Bell's avatar
Matt Bell committed
33
		path = path[:len(path)-1]
34 35 36

	} else {
		cmd = sub
37 38
	}

39 40 41
	opts, stringArgs2 := parseOptions(r)
	stringArgs = append(stringArgs, stringArgs2...)

42
	// count required argument definitions
43
	numRequired := 0
44 45
	for _, argDef := range cmd.Arguments {
		if argDef.Required {
46
			numRequired++
47 48 49 50 51 52
		}
	}

	// count the number of provided argument values
	valCount := len(stringArgs)
	// TODO: add total number of parts in request body (instead of just 1 if body is present)
53
	if r.Body != nil && r.ContentLength != 0 {
54 55 56
		valCount += 1
	}

57 58 59
	args := make([]interface{}, valCount)

	valIndex := 0
60 61
	for _, argDef := range cmd.Arguments {
		// skip optional argument definitions if there aren't sufficient remaining values
62
		if valCount-valIndex <= numRequired && !argDef.Required {
63
			continue
64
		} else if argDef.Required {
65
			numRequired--
66 67 68 69
		}

		if argDef.Type == cmds.ArgString {
			if argDef.Variadic {
70
				for _, s := range stringArgs {
71 72
					args[valIndex] = s
					valIndex++
73
				}
Matt Bell's avatar
Matt Bell committed
74
				valCount -= len(stringArgs)
75 76

			} else if len(stringArgs) > 0 {
77
				args[valIndex] = stringArgs[0]
78
				stringArgs = stringArgs[1:]
79
				valIndex++
80 81 82

			} else {
				break
83 84 85 86
			}

		} else {
			// TODO: create multipart streams for file args
87 88
			args[valIndex] = r.Body
			valIndex++
89 90
		}
	}
91

92 93 94 95 96
	optDefs, err := root.GetOptions(path)
	if err != nil {
		return nil, err
	}

97 98 99 100
	req, err := cmds.NewRequest(path, opts, args, cmd, optDefs)
	if err != nil {
		return nil, err
	}
101 102 103 104 105 106 107

	err = cmd.CheckArguments(req)
	if err != nil {
		return nil, err
	}

	return req, nil
108 109
}

110
func parseOptions(r *http.Request) (map[string]interface{}, []string) {
111
	opts := make(map[string]interface{})
112
	var args []string
113 114 115

	query := r.URL.Query()
	for k, v := range query {
Matt Bell's avatar
Matt Bell committed
116
		if k == "arg" {
117
			args = v
Matt Bell's avatar
Matt Bell committed
118 119 120
		} else {
			opts[k] = v[0]
		}
121 122 123 124 125 126 127 128 129
	}

	// default to setting encoding to JSON
	_, short := opts[cmds.EncShort]
	_, long := opts[cmds.EncLong]
	if !short && !long {
		opts[cmds.EncShort] = cmds.JSON
	}

Matt Bell's avatar
Matt Bell committed
130
	return opts, args
131
}