1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 19:24:14 +08:00

commands/http: Moved http request parsing into a Parse function

This commit is contained in:
Matt Bell
2014-10-28 17:00:41 -07:00
committed by Juan Batiz-Benet
parent 29b96b64a5
commit 52bc8bd422
2 changed files with 55 additions and 34 deletions

View File

@ -1,9 +1,9 @@
package http
import (
"errors"
"io"
"net/http"
"strings"
cmds "github.com/jbenet/go-ipfs/commands"
"github.com/jbenet/go-ipfs/core/commands"
@ -13,22 +13,19 @@ type Handler struct {
Ctx cmds.Context
}
var ErrNotFound = errors.New("404 page not found")
func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := strings.Split(r.URL.Path, "/")[3:]
opts := getOptions(r)
// TODO: get args
// ensure the requested command exists, otherwise 404
_, err := commands.Root.Get(path)
req, err := Parse(r)
if err != nil {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 page not found"))
if err == ErrNotFound {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusBadRequest)
}
w.Write([]byte(err.Error()))
return
}
// build the Request
req := cmds.NewRequest(path, opts, nil, nil)
req.SetContext(i.Ctx)
// call the command
@ -60,24 +57,3 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(err.Error()))
}
}
// getOptions returns the command options in the given HTTP request
// (from the querystring and request body)
func getOptions(r *http.Request) map[string]interface{} {
opts := make(map[string]interface{})
query := r.URL.Query()
for k, v := range query {
opts[k] = v[0]
}
// TODO: get more options from request body (formdata, json, etc)
_, short := opts[cmds.EncShort]
_, long := opts[cmds.EncLong]
if !short && !long {
opts[cmds.EncShort] = cmds.JSON
}
return opts
}

45
commands/http/parse.go Normal file
View File

@ -0,0 +1,45 @@
package http
import (
"net/http"
"strings"
cmds "github.com/jbenet/go-ipfs/commands"
"github.com/jbenet/go-ipfs/core/commands"
)
// Parse parses the data in a http.Request and returns a command Request object
func Parse(r *http.Request) (cmds.Request, error) {
path := strings.Split(r.URL.Path, "/")[3:]
// 404 if there is no command at that path
if _, err := commands.Root.Get(path); err != nil {
return nil, ErrNotFound
}
opts, args := parseOptions(r)
// TODO: input stream (from request body)
return cmds.NewRequest(path, opts, args, nil), nil
}
func parseOptions(r *http.Request) (map[string]interface{}, []string) {
opts := make(map[string]interface{})
query := r.URL.Query()
for k, v := range query {
opts[k] = v[0]
}
// TODO: get more options from request body (formdata, json, etc)
// default to setting encoding to JSON
_, short := opts[cmds.EncShort]
_, long := opts[cmds.EncLong]
if !short && !long {
opts[cmds.EncShort] = cmds.JSON
}
return opts, nil
}