mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 10:49:24 +08:00
commands/http: handler: Fixed chunk copier to be able to write response before request is done
This commit is contained in:
@ -2,6 +2,7 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"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
|
// if the res output is a channel, set a custom header for it
|
||||||
|
isChan := false
|
||||||
if _, ok := res.Output().(chan interface{}); ok {
|
if _, ok := res.Output().(chan interface{}); ok {
|
||||||
w.Header().Set(channelHeader, "1")
|
w.Header().Set(channelHeader, "1")
|
||||||
|
isChan = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// if response contains an error, write an HTTP error status code
|
// 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = copyChunks(w, out)
|
if isChan {
|
||||||
if err != nil {
|
err = copyChunks(w, out)
|
||||||
log.Error(err)
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
io.Copy(w, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copies from an io.Reader to a http.ResponseWriter.
|
// Copies from an io.Reader to a http.ResponseWriter.
|
||||||
// Flushes chunks over HTTP stream as they are read (if supported by transport).
|
// Flushes chunks over HTTP stream as they are read (if supported by transport).
|
||||||
func copyChunks(w http.ResponseWriter, out io.Reader) error {
|
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)
|
buf := make([]byte, 32*1024)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, err := out.Read(buf)
|
n, err := out.Read(buf)
|
||||||
|
|
||||||
if n > 0 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if f, ok := w.(http.Flusher); ok {
|
writer.WriteString("\r\n")
|
||||||
f.Flush()
|
writer.Flush()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil && err != io.EOF {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
writer.WriteString("0\r\n\r\n")
|
||||||
|
writer.Flush()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user