Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
43c61a4e
Commit
43c61a4e
authored
Oct 24, 2014
by
Matt Bell
Committed by
Juan Batiz-Benet
Nov 04, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands/http: Moved HTTP RPC handler into commands/http
parent
8b61daa1
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
80 additions
and
0 deletions
+80
-0
commands/http/handler.go
commands/http/handler.go
+80
-0
No files found.
commands/http/handler.go
0 → 100644
View file @
43c61a4e
package
http
import
(
"io"
"net/http"
"strings"
cmds
"github.com/jbenet/go-ipfs/commands"
"github.com/jbenet/go-ipfs/core/commands"
)
type
Handler
struct
{}
// TODO: store ipfsnode context
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
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusNotFound
)
w
.
Write
([]
byte
(
"404 page not found"
))
return
}
// build the Request and call the command
req
:=
cmds
.
NewRequest
(
path
,
opts
,
nil
,
nil
)
res
:=
commands
.
Root
.
Call
(
req
)
// set the Content-Type based on res output
if
_
,
ok
:=
res
.
Value
()
.
(
io
.
Reader
);
ok
{
// TODO: set based on actual Content-Type of file
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/octet-stream"
)
}
else
{
// TODO: get proper MIME type for encoding from multicodec lib
enc
,
_
:=
req
.
Option
(
cmds
.
EncShort
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/"
+
enc
.
(
string
))
}
// if response contains an error, write an HTTP error status code
if
e
:=
res
.
Error
();
e
!=
nil
{
if
e
.
Code
==
cmds
.
ErrClient
{
w
.
WriteHeader
(
http
.
StatusBadRequest
)
}
else
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
}
}
_
,
err
=
io
.
Copy
(
w
,
res
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/plain"
)
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment