This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add contentPathToCarUrl and tests
this implements part of #160 (comment) that fixes the way we encode content paths, without changing the interface of fetcher in caboose.
- Loading branch information
Showing
4 changed files
with
108 additions
and
28 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,39 @@ | ||
package lib | ||
|
||
import ( | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/ipfs/boxo/gateway" | ||
) | ||
|
||
// contentPathToCarUrl returns an URL that allows retrieval of specified resource | ||
// from a trustless gateway that implements IPIP-402 | ||
func contentPathToCarUrl(path gateway.ImmutablePath, params gateway.CarParams) *url.URL { | ||
return &url.URL{ | ||
Path: path.String(), | ||
RawQuery: carParamsToString(params), | ||
} | ||
} | ||
|
||
// carParamsToString converts CarParams to URL parameters compatible with IPIP-402 | ||
func carParamsToString(params gateway.CarParams) string { | ||
paramsBuilder := strings.Builder{} | ||
paramsBuilder.WriteString("format=car") // always send explicit format in URL, this makes debugging easier, even when Accept header was set | ||
if params.Scope != "" { | ||
paramsBuilder.WriteString("&dag-scope=") | ||
paramsBuilder.WriteString(string(params.Scope)) | ||
} | ||
if params.Range != nil { | ||
paramsBuilder.WriteString("&entity-bytes=") | ||
paramsBuilder.WriteString(strconv.FormatInt(params.Range.From, 10)) | ||
paramsBuilder.WriteString(":") | ||
if params.Range.To != nil { | ||
paramsBuilder.WriteString(strconv.FormatInt(*params.Range.To, 10)) | ||
} else { | ||
paramsBuilder.WriteString("*") | ||
} | ||
} | ||
return paramsBuilder.String() | ||
} |
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,57 @@ | ||
package lib | ||
|
||
import ( | ||
"testing" | ||
|
||
ifacepath "github.com/ipfs/boxo/coreiface/path" | ||
"github.com/ipfs/boxo/gateway" | ||
) | ||
|
||
func TestContentPathToCarUrl(t *testing.T) { | ||
negativeOffset := int64(-42) | ||
testCases := []struct { | ||
contentPath string // to be turned into gateway.ImmutablePath | ||
carParams gateway.CarParams | ||
expectedUrl string // url.URL.String() | ||
}{ | ||
{ | ||
contentPath: "/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", | ||
carParams: gateway.CarParams{}, | ||
expectedUrl: "/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi?format=car", | ||
}, | ||
{ | ||
contentPath: "/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", | ||
carParams: gateway.CarParams{Scope: "entity", Range: &gateway.DagByteRange{From: 0, To: nil}}, | ||
expectedUrl: "/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi?format=car&dag-scope=entity&entity-bytes=0:*", | ||
}, | ||
{ | ||
contentPath: "/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", | ||
carParams: gateway.CarParams{Scope: "block"}, | ||
expectedUrl: "/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi?format=car&dag-scope=block", | ||
}, | ||
{ | ||
contentPath: "/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", | ||
carParams: gateway.CarParams{Scope: "entity", Range: &gateway.DagByteRange{From: 4, To: &negativeOffset}}, | ||
expectedUrl: "/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi?format=car&dag-scope=entity&entity-bytes=4:-42", | ||
}, | ||
{ | ||
// a regression test for case described in https://github.com/ipfs/gateway-conformance/issues/115 | ||
contentPath: "/ipfs/bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze/I/Auditorio_de_Tenerife%2C_Santa_Cruz_de_Tenerife%2C_España%2C_2012-12-15%2C_DD_02.jpg.webp", | ||
carParams: gateway.CarParams{Scope: "entity", Range: &gateway.DagByteRange{From: 0, To: nil}}, | ||
expectedUrl: "/ipfs/bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze/I/Auditorio_de_Tenerife%252C_Santa_Cruz_de_Tenerife%252C_Espa%C3%B1a%252C_2012-12-15%252C_DD_02.jpg.webp?format=car&dag-scope=entity&entity-bytes=0:*", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run("TestContentPathToCarUrl", func(t *testing.T) { | ||
contentPath, err := gateway.NewImmutablePath(ifacepath.New(tc.contentPath)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
result := contentPathToCarUrl(contentPath, tc.carParams).String() | ||
if result != tc.expectedUrl { | ||
t.Errorf("Expected %q, but got %q", tc.expectedUrl, result) | ||
} | ||
}) | ||
} | ||
} |
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