-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include optional Ipni-Cid-Schema-Type HTTP header (#221)
* Include optional Ipni-Cid-Schema-Type HTTP header This optional header, when present, serves as an indication to advertisement publishers what type of data is being requested and is identified by the CID. This may help some publishers more quickly lookup the data. The publisher, who receives the Ipni-Cid-Schema-Type HTTP header, does not validate the value, because newer values may need to be received by consumer that is using an older version of library. Implements fix for ipni/storetheindex#2662
- Loading branch information
Showing
9 changed files
with
280 additions
and
11 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,71 @@ | ||
package ipnisync | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
const ( | ||
// CidSchemaHeader is the HTTP header used as an optional hint about the | ||
// type of data requested by a CID. | ||
CidSchemaHeader = "Ipni-Cid-Schema-Type" | ||
// CidSchemaAdvertisement is a value for the CidSchemaHeader specifying | ||
// advertiesement data is being requested. Referrs to Advertisement in | ||
// https://github.com/ipni/go-libipni/blob/main/ingest/schema/schema.ipldsch | ||
CidSchemaAdvertisement = "Advertisement" | ||
// CidSchemaEntries is a value for the CidSchemaHeader specifying | ||
// advertisement entries (multihash chunks) data is being requested. | ||
// Referrs to Entry chunk in | ||
// https://github.com/ipni/go-libipni/blob/main/ingest/schema/schema.ipldsch | ||
CidSchemaEntryChunk = "EntryChunk" | ||
) | ||
|
||
var ErrUnknownCidSchema = errors.New("unknown cid schema type value") | ||
|
||
// cidSchemaTypeKey is the type used for the key of CidSchemaHeader when set as | ||
// a context value. | ||
type cidSchemaTypeCtxKey string | ||
|
||
// cidSchemaCtxKey is used to get the key used to store or extract the cid | ||
// schema value in a context. | ||
const cidSchemaCtxKey cidSchemaTypeCtxKey = CidSchemaHeader | ||
|
||
// CidSchemaFromCtx extracts the CID schema name from the context. If the | ||
// scheam value is not set, then returns "". If the schema value is set, but is | ||
// not recognized, then ErrUnknownCidSchema is returned along with the value. | ||
// | ||
// Returning unrecognized values with an error allows consumers to retrieved | ||
// newer values that are not recognized by an older version of this library. | ||
func CidSchemaFromCtx(ctx context.Context) (string, error) { | ||
cidSchemaType, ok := ctx.Value(cidSchemaCtxKey).(string) | ||
if !ok { | ||
return "", nil | ||
} | ||
|
||
var err error | ||
switch cidSchemaType { | ||
case CidSchemaAdvertisement, CidSchemaEntryChunk: | ||
default: | ||
err = ErrUnknownCidSchema | ||
} | ||
return cidSchemaType, err | ||
} | ||
|
||
// CtxWithCidSchema creates a derived context that has the specified value for | ||
// the CID schema type. | ||
// | ||
// Setting an unrecognized value, even when an error is retruned, allows | ||
// producers to set context values that are not recognized by an older version | ||
// of this library. | ||
func CtxWithCidSchema(ctx context.Context, cidSchemaType string) (context.Context, error) { | ||
if cidSchemaType == "" { | ||
return ctx, nil | ||
} | ||
var err error | ||
switch cidSchemaType { | ||
case CidSchemaAdvertisement, CidSchemaEntryChunk: | ||
default: | ||
err = ErrUnknownCidSchema | ||
} | ||
return context.WithValue(ctx, cidSchemaCtxKey, cidSchemaType), err | ||
} |
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,46 @@ | ||
package ipnisync_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ipni/go-libipni/dagsync/ipnisync" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCtxWithCidSchema(t *testing.T) { | ||
ctxOrig := context.Background() | ||
ctx, err := ipnisync.CtxWithCidSchema(ctxOrig, "") | ||
require.NoError(t, err) | ||
require.Equal(t, ctxOrig, ctx) | ||
|
||
ctx, err = ipnisync.CtxWithCidSchema(ctxOrig, ipnisync.CidSchemaAdvertisement) | ||
require.NoError(t, err) | ||
require.NotEqual(t, ctxOrig, ctx) | ||
|
||
value, err := ipnisync.CidSchemaFromCtx(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, ipnisync.CidSchemaAdvertisement, value) | ||
|
||
ctx, err = ipnisync.CtxWithCidSchema(ctx, ipnisync.CidSchemaEntryChunk) | ||
require.NoError(t, err) | ||
value, err = ipnisync.CidSchemaFromCtx(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, ipnisync.CidSchemaEntryChunk, value) | ||
|
||
value, err = ipnisync.CidSchemaFromCtx(ctxOrig) | ||
require.NoError(t, err) | ||
require.Empty(t, value) | ||
|
||
const unknownVal = "unknown" | ||
|
||
// Setting unknown value returns error as well as context with value. | ||
ctx, err = ipnisync.CtxWithCidSchema(ctxOrig, unknownVal) | ||
require.ErrorIs(t, err, ipnisync.ErrUnknownCidSchema) | ||
require.NotNil(t, ctxOrig, ctx) | ||
|
||
// Getting unknown value returns error as well as value. | ||
value, err = ipnisync.CidSchemaFromCtx(ctx) | ||
require.ErrorIs(t, err, ipnisync.ErrUnknownCidSchema) | ||
require.Equal(t, unknownVal, value) | ||
} |
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
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
Oops, something went wrong.