Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipfs get: set correct content-type on resp #2268

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions commands/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
cors "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/rs/cors"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/ipfs/go-ipfs/repo/config"
"github.com/ipfs/go-ipfs/util"

cmds "github.com/ipfs/go-ipfs/commands"
logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
Expand Down Expand Up @@ -61,12 +62,6 @@ const (
ACACredentials = "Access-Control-Allow-Credentials"
)

var mimeTypes = map[string]string{
cmds.JSON: "application/json",
cmds.XML: "application/xml",
cmds.Text: "text/plain",
}

type ServerConfig struct {
// Headers is an optional map of headers that is written out.
Headers map[string][]string
Expand Down Expand Up @@ -191,18 +186,18 @@ func guessMimeType(res cmds.Response) (string, error) {
return "", errors.New("no encoding option set")
}

if m, ok := mimeTypes[enc]; ok {
if m, ok := util.MimeTypes[enc]; ok {
return m, nil
}

return mimeTypes[cmds.JSON], nil
return util.MimeTypes[cmds.JSON], nil
}

func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req cmds.Request) {
h := w.Header()
// Expose our agent to allow identification
h.Set("Server", "go-ipfs/" + config.CurrentVersionNumber)
h.Set("Server", "go-ipfs/"+config.CurrentVersionNumber)

mime, err := guessMimeType(res)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -236,7 +231,9 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
if _, ok := res.Output().(io.Reader); ok {
// set streams output type to text to avoid issues with browsers rendering
// html pages on priveleged api ports
mime = "text/plain"
if !util.XssSafeMimeType(mime) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this function coming from? I'm not seeing it in master

mime = util.MimeTypes[cmds.Text]
}
h.Set(streamHeader, "1")
}

Expand Down
2 changes: 2 additions & 0 deletions commands/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type EncodingType string
const (
JSON = "json"
XML = "xml"
Tar = "tar"
Gzip = "gzip"
Text = "text"
// TODO: support more encoding types
)
Expand Down
6 changes: 6 additions & 0 deletions core/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ may also specify the level of compression by specifying '-l=<1-9>'.
return
}

// set the correct mime type for tar stream
req.SetOption(cmds.EncShort, cmds.Tar)
if cmplvl != gzip.NoCompression {
req.SetOption(cmds.EncShort, cmds.Gzip)
}

archive, _, _ := req.Option("archive").Bool()
reader, err := uarchive.DagArchive(ctx, dn, p.String(), node.DAG, archive, cmplvl)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions test/sharness/t0090-get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ test_get_cmd
# should work online
test_launch_ipfs_daemon
test_get_cmd
test_expect_success "ipfs get response has the correct content-type" '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably test the compressed one too

curl -I "http://localhost:$PORT_API/api/v0/get?arg=$HASH" | grep "^Content-Type: application/x-tar"
'
test_kill_ipfs_daemon

test_done