-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: add application/json and application/cbor
- Loading branch information
Showing
3 changed files
with
69 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package corehttp | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"html" | ||
"net/http" | ||
"time" | ||
|
||
ipldlegacy "github.com/ipfs/go-ipld-legacy" | ||
ipath "github.com/ipfs/interface-go-ipfs-core/path" | ||
"github.com/ipfs/kubo/tracing" | ||
"github.com/ipld/go-ipld-prime" | ||
"github.com/ipld/go-ipld-prime/multicodec" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
var unixEpochTime = time.Unix(0, 0) | ||
|
||
func (i *gatewayHandler) serveCodec(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time, ctype string, codec uint64) { | ||
ctx, span := tracing.Span(ctx, "Gateway", "ServeCodec", trace.WithAttributes(attribute.String("path", resolvedPath.String()), attribute.String("ctype", ctype))) | ||
defer span.End() | ||
|
||
// Set Cache-Control and read optional Last-Modified time | ||
modtime := addCacheControlHeaders(w, r, contentPath, resolvedPath.Cid()) | ||
|
||
// Sets correct Last-Modified header. This code is borrowed from the standard | ||
// library (net/http/server.go) as we cannot use serveFile. | ||
if !(modtime.IsZero() || modtime.Equal(unixEpochTime)) { | ||
w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat)) | ||
} | ||
|
||
addContentDispositionHeader(w, r, contentPath) | ||
w.Header().Set("Content-Type", ctype) | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
|
||
obj, err := i.api.Dag().Get(ctx, resolvedPath.Cid()) | ||
if err != nil { | ||
webError(w, "ipfs dag get "+html.EscapeString(resolvedPath.String()), err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
universal, ok := obj.(ipldlegacy.UniversalNode) | ||
if !ok { | ||
webError(w, "todo", fmt.Errorf("%T is not a valid IPLD node", obj), http.StatusInternalServerError) | ||
return | ||
} | ||
finalNode := universal.(ipld.Node) | ||
|
||
encoder, err := multicodec.LookupEncoder(codec) | ||
if err != nil { | ||
webError(w, "todo", err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
_ = encoder(finalNode, w) | ||
} |
This file was deleted.
Oops, something went wrong.