mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 19:24:14 +08:00
server/http: Use Response as Reader instead of manually getting output data
This commit is contained in:

committed by
Juan Batiz-Benet

parent
7564742305
commit
12b0ebff7d
@ -112,27 +112,10 @@ func (i *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
val := res.Value()
|
||||
|
||||
// if the output value is a io.Reader, stream its output in the request body
|
||||
if stream, ok := val.(io.Reader); ok {
|
||||
io.Copy(w, stream)
|
||||
return
|
||||
}
|
||||
|
||||
// otherwise, marshall and output the response value or error
|
||||
if val != nil || res.Error() != nil {
|
||||
output, err := res.Marshal()
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
if output != nil {
|
||||
w.Write(output)
|
||||
}
|
||||
_, err = io.Copy(w, res)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user