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-cmds
Commits
0be53b00
Commit
0be53b00
authored
Dec 17, 2014
by
Matt Bell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands/http: Made Handler stream channel output
parent
d0813e44
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
3 deletions
+40
-3
http/handler.go
http/handler.go
+40
-3
No files found.
http/handler.go
View file @
0be53b00
...
...
@@ -20,8 +20,11 @@ type Handler struct {
var
ErrNotFound
=
errors
.
New
(
"404 page not found"
)
const
(
streamHeader
=
"X-Stream-Output"
contentTypeHeader
=
"Content-Type"
streamHeader
=
"X-Stream-Output"
channelHeader
=
"X-Chunked-Output"
contentTypeHeader
=
"Content-Type"
contentLengthHeader
=
"Content-Length"
transferEncodingHeader
=
"Transfer-Encoding"
)
var
mimeTypes
=
map
[
string
]
string
{
...
...
@@ -80,6 +83,11 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w
.
Header
()
.
Set
(
contentTypeHeader
,
mime
)
}
// if the res output is a channel, set a custom header for it
if
_
,
ok
:=
res
.
Output
()
.
(
chan
interface
{});
ok
{
w
.
Header
()
.
Set
(
channelHeader
,
"1"
)
}
// if response contains an error, write an HTTP error status code
if
e
:=
res
.
Error
();
e
!=
nil
{
if
e
.
Code
==
cmds
.
ErrClient
{
...
...
@@ -97,5 +105,34 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
io
.
Copy
(
w
,
out
)
err
=
copyChunks
(
w
,
out
)
if
err
!=
nil
{
log
.
Error
(
err
)
}
}
// 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
{
buf
:=
make
([]
byte
,
32
*
1024
)
for
{
n
,
err
:=
out
.
Read
(
buf
)
if
n
>
0
{
_
,
err
:=
w
.
Write
(
buf
[
0
:
n
])
if
err
!=
nil
{
return
err
}
if
f
,
ok
:=
w
.
(
http
.
Flusher
);
ok
{
f
.
Flush
()
}
}
if
err
!=
nil
{
return
err
}
}
return
nil
}
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