mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-02 12:20:03 +08:00
commands/http: Moved http request parsing into a Parse function
This commit is contained in:

committed by
Juan Batiz-Benet

parent
29b96b64a5
commit
52bc8bd422
@ -1,9 +1,9 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
cmds "github.com/jbenet/go-ipfs/commands"
|
cmds "github.com/jbenet/go-ipfs/commands"
|
||||||
"github.com/jbenet/go-ipfs/core/commands"
|
"github.com/jbenet/go-ipfs/core/commands"
|
||||||
@ -13,22 +13,19 @@ type Handler struct {
|
|||||||
Ctx cmds.Context
|
Ctx cmds.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrNotFound = errors.New("404 page not found")
|
||||||
|
|
||||||
func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
path := strings.Split(r.URL.Path, "/")[3:]
|
req, err := Parse(r)
|
||||||
opts := getOptions(r)
|
|
||||||
|
|
||||||
// TODO: get args
|
|
||||||
|
|
||||||
// ensure the requested command exists, otherwise 404
|
|
||||||
_, err := commands.Root.Get(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
if err == ErrNotFound {
|
||||||
w.Write([]byte("404 page not found"))
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// build the Request
|
|
||||||
req := cmds.NewRequest(path, opts, nil, nil)
|
|
||||||
req.SetContext(i.Ctx)
|
req.SetContext(i.Ctx)
|
||||||
|
|
||||||
// call the command
|
// call the command
|
||||||
@ -60,24 +57,3 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte(err.Error()))
|
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
45
commands/http/parse.go
Normal 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
|
||||||
|
}
|
Reference in New Issue
Block a user