mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-06 19:44:01 +08:00
139 lines
3.0 KiB
Go
139 lines
3.0 KiB
Go
package http
|
|
|
|
import (
|
|
"errors"
|
|
"mime"
|
|
"net/http"
|
|
"strings"
|
|
|
|
cmds "github.com/jbenet/go-ipfs/commands"
|
|
)
|
|
|
|
// Parse parses the data in a http.Request and returns a command Request object
|
|
func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
|
|
if !strings.HasPrefix(r.URL.Path, ApiPath) {
|
|
return nil, errors.New("Unexpected path prefix")
|
|
}
|
|
path := strings.Split(strings.TrimPrefix(r.URL.Path, ApiPath+"/"), "/")
|
|
|
|
stringArgs := make([]string, 0)
|
|
|
|
cmd, err := root.Get(path[:len(path)-1])
|
|
if err != nil {
|
|
// 404 if there is no command at that path
|
|
return nil, ErrNotFound
|
|
|
|
} else if sub := cmd.Subcommand(path[len(path)-1]); sub == nil {
|
|
if len(path) <= 1 {
|
|
return nil, ErrNotFound
|
|
}
|
|
|
|
// 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)
|
|
stringArgs = append(stringArgs, path[len(path)-1])
|
|
path = path[:len(path)-1]
|
|
|
|
} else {
|
|
cmd = sub
|
|
}
|
|
|
|
opts, stringArgs2 := parseOptions(r)
|
|
stringArgs = append(stringArgs, stringArgs2...)
|
|
|
|
// count required argument definitions
|
|
numRequired := 0
|
|
for _, argDef := range cmd.Arguments {
|
|
if argDef.Required {
|
|
numRequired++
|
|
}
|
|
}
|
|
|
|
// count the number of provided argument values
|
|
valCount := len(stringArgs)
|
|
|
|
args := make([]interface{}, valCount)
|
|
|
|
valIndex := 0
|
|
for _, argDef := range cmd.Arguments {
|
|
// skip optional argument definitions if there aren't sufficient remaining values
|
|
if valCount-valIndex <= numRequired && !argDef.Required {
|
|
continue
|
|
} else if argDef.Required {
|
|
numRequired--
|
|
}
|
|
|
|
if argDef.Type == cmds.ArgString {
|
|
if argDef.Variadic {
|
|
for _, s := range stringArgs {
|
|
args[valIndex] = s
|
|
valIndex++
|
|
}
|
|
valCount -= len(stringArgs)
|
|
|
|
} else if len(stringArgs) > 0 {
|
|
args[valIndex] = stringArgs[0]
|
|
stringArgs = stringArgs[1:]
|
|
valIndex++
|
|
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
optDefs, err := root.GetOptions(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// create cmds.File from multipart/form-data contents
|
|
contentType := r.Header.Get(contentTypeHeader)
|
|
mediatype, _, err := mime.ParseMediaType(contentType)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var f *cmds.MultipartFile
|
|
if mediatype == "multipart/form-data" {
|
|
f = &cmds.MultipartFile{Mediatype: mediatype}
|
|
f.Reader, err = r.MultipartReader()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
req, err := cmds.NewRequest(path, opts, args, f, cmd, optDefs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = cmd.CheckArguments(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func parseOptions(r *http.Request) (map[string]interface{}, []string) {
|
|
opts := make(map[string]interface{})
|
|
var args []string
|
|
|
|
query := r.URL.Query()
|
|
for k, v := range query {
|
|
if k == "arg" {
|
|
args = v
|
|
} else {
|
|
opts[k] = v[0]
|
|
}
|
|
}
|
|
|
|
// default to setting encoding to JSON
|
|
_, short := opts[cmds.EncShort]
|
|
_, long := opts[cmds.EncLong]
|
|
if !short && !long {
|
|
opts[cmds.EncShort] = cmds.JSON
|
|
}
|
|
|
|
return opts, args
|
|
}
|