Skip to content

Commit

Permalink
Set correct content-type for ipfs get
Browse files Browse the repository at this point in the history
This is a version of #2004, with CR from @jbenet integrated in, and should close #1824

License: MIT
Signed-off-by: Richard Littauer <richard.littauer@gmail.com>
  • Loading branch information
RichardLitt authored and Kubuxu committed May 31, 2016
1 parent 88da12e commit ddb8585
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
24 changes: 23 additions & 1 deletion commands/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,32 @@ const (
)

var mimeTypes = map[string]string{
cmds.Gzip: "application/gzip",
cmds.Protobuf: "application/protobuf",
cmds.JSON: "application/json",
cmds.Tar: "application/x-tar",
cmds.XML: "application/xml",
cmds.Text: "text/plain",
}

var xssSafeMimeTypes = []string{
mimeTypes[cmds.Gzip],
mimeTypes[cmds.JSON],
mimeTypes[cmds.Tar],
mimeTypes[cmds.Text],
mimeTypes[cmds.XML],
mimeTypes[cmds.Protobuf],
}

func xssSafeMimeType(mime string) bool {
for _, t := range xssSafeMimeTypes {
if t == mime {
return true
}
}
return false
}

type ServerConfig struct {
// Headers is an optional map of headers that is written out.
Headers map[string][]string
Expand Down Expand Up @@ -251,7 +271,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 !xssSafeMimeType(mime) {
mime = mimeTypes[cmds.Text]
}
h.Set(streamHeader, "1")
}

Expand Down
5 changes: 3 additions & 2 deletions commands/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ type EncodingType string

// Supported EncodingType constants.
const (
Gzip = "gzip"
JSON = "json"
XML = "xml"
Protobuf = "protobuf"
Tar = "tar"
Text = "text"
// TODO: support more encoding types
XML = "xml"
)

func marshalJson(value interface{}) (io.Reader, error) {
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
4 changes: 4 additions & 0 deletions test/sharness/t0090-get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ test_get_fail() {
'
}

test_expect_success "ipfs get response has the correct content-type" '
curl -I "http://localhost:$PORT_API/api/v0/get?arg=$HASH" | grep "^Content-Type: application/x-tar"
'

# should work offline
test_get_cmd

Expand Down

0 comments on commit ddb8585

Please sign in to comment.