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-unixfs
Commits
ddd75401
Commit
ddd75401
authored
Jan 10, 2015
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cmds: flush output on standard readers
cc @mappum can we do this for the copyChunks case?
parent
0e2a554c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
5 deletions
+49
-5
commands/http/handler.go
commands/http/handler.go
+41
-4
commands/request.go
commands/request.go
+8
-1
No files found.
commands/http/handler.go
View file @
ddd75401
...
...
@@ -6,6 +6,8 @@ import (
"io"
"net/http"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
cmds
"github.com/jbenet/go-ipfs/commands"
u
"github.com/jbenet/go-ipfs/util"
)
...
...
@@ -26,6 +28,7 @@ const (
contentTypeHeader
=
"Content-Type"
contentLengthHeader
=
"Content-Length"
transferEncodingHeader
=
"Transfer-Encoding"
applicationJson
=
"application/json"
)
var
mimeTypes
=
map
[
string
]
string
{
...
...
@@ -44,6 +47,11 @@ func NewHandler(ctx cmds.Context, root *cmds.Command, origin string) *Handler {
}
func
(
i
Handler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// create a context.Context to pass into the commands.
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
TODO
())
defer
cancel
()
i
.
ctx
.
Context
=
ctx
log
.
Debug
(
"Incoming API request: "
,
r
.
URL
)
if
len
(
i
.
origin
)
>
0
{
...
...
@@ -106,19 +114,30 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
_
,
isChan
:=
res
.
Output
()
.
(
chan
interface
{})
streamChans
,
_
,
_
:=
req
.
Option
(
"stream-channels"
)
.
Bool
()
if
isChan
&&
streamChans
{
err
=
copyChunks
(
w
,
out
)
err
=
copyChunks
(
applicationJson
,
w
,
out
)
if
err
!=
nil
{
log
.
Error
(
err
)
}
return
}
io
.
Copy
(
w
,
out
)
flushCopy
(
w
,
out
)
}
// flushCopy Copies from an io.Reader to a http.ResponseWriter.
// Flushes chunks over HTTP stream as they are read (if supported by transport).
func
flushCopy
(
w
http
.
ResponseWriter
,
out
io
.
Reader
)
error
{
if
_
,
ok
:=
w
.
(
http
.
Flusher
);
!
ok
{
return
copyChunks
(
""
,
w
,
out
)
}
io
.
Copy
(
&
flushResponse
{
w
},
out
)
return
nil
}
// Copies from an io.Reader to a http.ResponseWriter.
// Flushes chunks over HTTP stream as they are read (if supported by transport).
func
copyChunks
(
w
http
.
ResponseWriter
,
out
io
.
Reader
)
error
{
func
copyChunks
(
contentType
string
,
w
http
.
ResponseWriter
,
out
io
.
Reader
)
error
{
hijacker
,
ok
:=
w
.
(
http
.
Hijacker
)
if
!
ok
{
return
errors
.
New
(
"Could not create hijacker"
)
...
...
@@ -130,7 +149,9 @@ func copyChunks(w http.ResponseWriter, out io.Reader) error {
defer
conn
.
Close
()
writer
.
WriteString
(
"HTTP/1.1 200 OK
\r\n
"
)
writer
.
WriteString
(
contentTypeHeader
+
": application/json
\r\n
"
)
if
contentType
!=
""
{
writer
.
WriteString
(
contentTypeHeader
+
": "
+
contentType
+
"
\r\n
"
)
}
writer
.
WriteString
(
transferEncodingHeader
+
": chunked
\r\n
"
)
writer
.
WriteString
(
channelHeader
+
": 1
\r\n\r\n
"
)
...
...
@@ -165,3 +186,19 @@ func copyChunks(w http.ResponseWriter, out io.Reader) error {
return
nil
}
type
flushResponse
struct
{
W
http
.
ResponseWriter
}
func
(
fr
*
flushResponse
)
Write
(
buf
[]
byte
)
(
int
,
error
)
{
n
,
err
:=
fr
.
W
.
Write
(
buf
)
if
err
!=
nil
{
return
n
,
err
}
if
flusher
,
ok
:=
fr
.
W
.
(
http
.
Flusher
);
ok
{
flusher
.
Flush
()
}
return
n
,
err
}
commands/request.go
View file @
ddd75401
...
...
@@ -6,6 +6,8 @@ import (
"reflect"
"strconv"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
"github.com/jbenet/go-ipfs/config"
"github.com/jbenet/go-ipfs/core"
u
"github.com/jbenet/go-ipfs/util"
...
...
@@ -14,6 +16,10 @@ import (
type
optMap
map
[
string
]
interface
{}
type
Context
struct
{
// this Context is temporary. Will be replaced soon, as we get
// rid of this variable entirely.
Context
context
.
Context
Online
bool
ConfigRoot
string
...
...
@@ -267,7 +273,8 @@ func NewRequest(path []string, opts optMap, args []string, file File, cmd *Comma
optDefs
=
make
(
map
[
string
]
Option
)
}
req
:=
&
request
{
path
,
opts
,
args
,
file
,
cmd
,
Context
{},
optDefs
}
ctx
:=
Context
{
Context
:
context
.
TODO
()}
req
:=
&
request
{
path
,
opts
,
args
,
file
,
cmd
,
ctx
,
optDefs
}
err
:=
req
.
ConvertOptions
()
if
err
!=
nil
{
return
nil
,
err
...
...
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