parse.go 2.78 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 43
	args := make([]interface{}, 0)

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	// count required argument definitions
	lenRequired := 0
	for _, argDef := range cmd.Arguments {
		if argDef.Required {
			lenRequired++
		}
	}

	// 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)
	if r.Body != nil {
		valCount += 1
	}

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

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

			} else if len(stringArgs) > 0 {
75
				args = append(args, stringArgs[0])
76
				stringArgs = stringArgs[1:]
Matt Bell's avatar
Matt Bell committed
77
				valCount--
78 79 80

			} else {
				break
81 82 83 84
			}

		} else {
			// TODO: create multipart streams for file args
85
			args = append(args, r.Body)
86 87
		}
	}
88

89 90 91 92
	if valCount-1 > 0 {
		args = append(args, make([]interface{}, valCount-1))
	}

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

	req := cmds.NewRequest(path, opts, args, cmd, optDefs)
99 100 101 102 103 104 105

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

	return req, nil
106 107
}

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

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

	// 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
128
	return opts, args
129
}