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
4239f192
Commit
4239f192
authored
Dec 17, 2014
by
Matt Bell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands/http: handler: Fixed chunk copier to be able to write response before request is done
parent
58029bb0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
8 deletions
+41
-8
http/handler.go
http/handler.go
+41
-8
No files found.
http/handler.go
View file @
4239f192
...
...
@@ -2,6 +2,7 @@ package http
import
(
"errors"
"fmt"
"io"
"net/http"
...
...
@@ -84,8 +85,10 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// if the res output is a channel, set a custom header for it
isChan
:=
false
if
_
,
ok
:=
res
.
Output
()
.
(
chan
interface
{});
ok
{
w
.
Header
()
.
Set
(
channelHeader
,
"1"
)
isChan
=
true
}
// if response contains an error, write an HTTP error status code
...
...
@@ -105,34 +108,64 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
err
=
copyChunks
(
w
,
out
)
if
err
!=
nil
{
log
.
Error
(
err
)
if
isChan
{
err
=
copyChunks
(
w
,
out
)
if
err
!=
nil
{
log
.
Error
(
err
)
fmt
.
Println
(
err
)
}
return
}
io
.
Copy
(
w
,
out
)
}
// 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
{
hijacker
,
ok
:=
w
.
(
http
.
Hijacker
)
if
!
ok
{
return
errors
.
New
(
"Could not create hijacker"
)
}
conn
,
writer
,
err
:=
hijacker
.
Hijack
()
if
err
!=
nil
{
return
err
}
defer
conn
.
Close
()
writer
.
WriteString
(
"HTTP/1.1 200 OK
\r\n
"
)
writer
.
WriteString
(
contentTypeHeader
+
": application/json
\r\n
"
)
writer
.
WriteString
(
transferEncodingHeader
+
": chunked
\r\n
"
)
writer
.
WriteString
(
channelHeader
+
": 1
\r\n\r\n
"
)
buf
:=
make
([]
byte
,
32
*
1024
)
for
{
n
,
err
:=
out
.
Read
(
buf
)
if
n
>
0
{
_
,
err
:=
w
.
Write
(
buf
[
0
:
n
])
length
:=
fmt
.
Sprintf
(
"%x
\r\n
"
,
n
)
writer
.
WriteString
(
length
)
_
,
err
:=
writer
.
Write
(
buf
[
0
:
n
])
if
err
!=
nil
{
return
err
}
if
f
,
ok
:=
w
.
(
http
.
Flusher
);
ok
{
f
.
Flush
()
}
writer
.
WriteString
(
"
\r\n
"
)
writer
.
Flush
()
}
if
err
!=
nil
{
if
err
!=
nil
&&
err
!=
io
.
EOF
{
return
err
}
if
err
==
io
.
EOF
{
break
}
}
writer
.
WriteString
(
"0
\r\n\r\n
"
)
writer
.
Flush
()
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