mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 02:30:39 +08:00
server/http: Set Content-Type header based on command output
This commit is contained in:

committed by
Juan Batiz-Benet

parent
12b0ebff7d
commit
0d830e5a24
@ -103,6 +103,16 @@ func (i *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
req := cmds.NewRequest(path, opts, nil, nil)
|
||||
res := commands.Root.Call(req)
|
||||
|
||||
// set the Content-Type based on res output
|
||||
if _, ok := res.Value().(io.Reader); ok {
|
||||
// TODO: set based on actual Content-Type of file
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
} else {
|
||||
// TODO: get proper MIME type for encoding from multicodec lib
|
||||
enc, _ := req.Option(cmds.EncShort)
|
||||
w.Header().Set("Content-Type", "application/"+enc.(string))
|
||||
}
|
||||
|
||||
// if response contains an error, write an HTTP error status code
|
||||
if e := res.Error(); e != nil {
|
||||
if e.Code == cmds.ErrClient {
|
||||
@ -115,6 +125,7 @@ func (i *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
_, err = io.Copy(w, res)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Write([]byte(err.Error()))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user