Skip to content
This repository was archived by the owner on Aug 4, 2026. It is now read-only.

Commit 15b1daf

Browse files
committed
Add gateway URL parser
Allows HTTP gateway urls as arguments
1 parent 796cbc7 commit 15b1daf

2 files changed

Lines changed: 41 additions & 10 deletions

File tree

main.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"fmt"
55
"os"
66
"os/signal"
7+
"regexp"
78
"strings"
89
"syscall"
910

10-
path "gx/ipfs/QmT3rzed1ppXefourpmoZ7tyVQfsGPQZ1pHDngLmCvXxd3/go-path"
11+
ipath "gx/ipfs/QmT3rzed1ppXefourpmoZ7tyVQfsGPQZ1pHDngLmCvXxd3/go-path"
1112
fallback "gx/ipfs/QmaWDhoQaV6cDyy6NSKFgPaUAGRtb4SMiLpaDYEsxP7X8P/fallback-ipfs-shell"
1213
cli "gx/ipfs/Qmc1AtgBdoUHP8oYSqU81NRYdzohmF45t5XNwVMvhCxsBA/cli"
1314
)
@@ -36,21 +37,19 @@ func main() {
3637
}
3738

3839
outfile := c.String("output")
39-
arg := c.Args().First()
40+
inPath, err := parsePath(c.Args().First())
41+
if err != nil {
42+
fmt.Fprintf(os.Stderr, "Argument parse failure: %s\n", err)
43+
os.Exit(1)
44+
}
4045

4146
// Use the final segment of the object's path if no path was given.
4247
if outfile == "" {
43-
ipfsPath, err := path.ParsePath(arg)
44-
if err != nil {
45-
fmt.Fprintf(os.Stderr, "ParsePath failure: %s\n", err)
46-
os.Exit(1)
47-
}
48-
segments := ipfsPath.Segments()
48+
segments := inPath.Segments()
4949
outfile = segments[len(segments)-1]
5050
}
5151

5252
var shell fallback.Shell
53-
var err error
5453

5554
if c.String("node") == "fallback" {
5655
shell, err = fallback.NewShell()
@@ -76,7 +75,7 @@ func main() {
7675
return nil
7776
}
7877

79-
if err := shell.Get(arg, outfile); err != nil {
78+
if err := shell.Get(inPath.String(), outfile); err != nil {
8079
os.Remove(outfile)
8180
fmt.Fprintf(os.Stderr, "ipget failed: %s\n", err)
8281
os.Exit(2)
@@ -132,3 +131,17 @@ func movePostfixOptions(args []string) []string {
132131
// append extracted arguments to the real args
133132
return append(args, the_args...)
134133
}
134+
135+
func parsePath(path string) (ipath.Path, error) {
136+
ipfsPath, err := ipath.ParsePath(path)
137+
if err == nil { // valid canonical path
138+
return ipfsPath, nil
139+
}
140+
// allow HTTP gateway URLs as input as well
141+
matches := regexp.MustCompile(`^https?://.*(/(?:ipfs|ipns|ipld)/.+)$`).FindStringSubmatch(path)
142+
if len(matches) < 1 {
143+
return "", fmt.Errorf("%q does not appear to be a valid IPFS path or HTTP gateway URL", path)
144+
}
145+
146+
return ipath.ParsePath(matches[1])
147+
}

sharness/t0030-arguments.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
test_description="test the ipget argument parser"
4+
5+
. ./lib/sharness/sharness.sh
6+
7+
test_expect_success "retrieve a known popular single file with a (HTTP) gateway URL" "
8+
ipget http://ipfs.io/ipfs/QmQ2r6iMNpky5f1m4cnm3Yqw8VSvjuKpTcK1X7dBR1LkJF/cat.gif &&
9+
echo 'c5ea0d6cacf1e54635685803ec4edbe0d4fe8465' > expected &&
10+
shasum cat.gif | cut -d ' ' -f 1 > actual &&
11+
diff expected actual
12+
"
13+
14+
test_expect_failure "don't allow non-(HTTP)gateway URLS" "
15+
ipget ftp://ipfs.io/ipfs/QmQ2r6iMNpky5f1m4cnm3Yqw8VSvjuKpTcK1X7dBR1LkJF/cat.gif
16+
"
17+
18+
test_done

0 commit comments

Comments
 (0)