Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
0fe423c6
提交
0fe423c6
authored
5月 20, 2018
作者:
Kevin Atkinson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
gateway: Turn Symbolic Links Into HTTP Redirects
License: MIT Signed-off-by:
Kevin Atkinson
<
k@kevina.org
>
上级
139d6240
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
95 行增加
和
6 行删除
+95
-6
errors.go
core/coreapi/interface/errors.go
+3
-2
unixfs.go
core/coreapi/interface/unixfs.go
+3
-0
unixfs.go
core/coreapi/unixfs.go
+34
-4
gateway_handler.go
core/corehttp/gateway_handler.go
+15
-0
t0110-gateway.sh
test/sharness/t0110-gateway.sh
+40
-0
没有找到文件。
core/coreapi/interface/errors.go
浏览文件 @
0fe423c6
...
...
@@ -3,6 +3,7 @@ package iface
import
"errors"
var
(
ErrIsDir
=
errors
.
New
(
"object is a directory"
)
ErrOffline
=
errors
.
New
(
"can't resolve, ipfs node is offline"
)
ErrIsDir
=
errors
.
New
(
"object is a directory"
)
ErrOffline
=
errors
.
New
(
"can't resolve, ipfs node is offline"
)
ErrIsSymLink
=
errors
.
New
(
"object is a symbolic link"
)
)
core/coreapi/interface/unixfs.go
浏览文件 @
0fe423c6
...
...
@@ -17,4 +17,7 @@ type UnixfsAPI interface {
// Ls returns the list of links in a directory
Ls
(
context
.
Context
,
Path
)
([]
*
ipld
.
Link
,
error
)
// ReadSymLink returns the contents of a symbolic link
ReadSymLink
(
context
.
Context
,
Path
)
(
string
,
error
)
}
core/coreapi/unixfs.go
浏览文件 @
0fe423c6
...
...
@@ -2,12 +2,16 @@ package coreapi
import
(
"context"
"errors"
"io"
coreiface
"github.com/ipfs/go-ipfs/core/coreapi/interface"
coreunix
"github.com/ipfs/go-ipfs/core/coreunix"
mdag
"github.com/ipfs/go-ipfs/merkledag"
uio
"github.com/ipfs/go-ipfs/unixfs/io"
ftpb
"github.com/ipfs/go-ipfs/unixfs/pb"
proto
"gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
cid
"gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
ipld
"gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)
...
...
@@ -38,12 +42,14 @@ func (api *UnixfsAPI) Cat(ctx context.Context, p coreiface.Path) (coreiface.Read
}
r
,
err
:=
uio
.
NewDagReader
(
ctx
,
dagnode
,
dget
)
if
err
==
uio
.
ErrIsDir
{
switch
err
{
case
uio
.
ErrIsDir
:
return
nil
,
coreiface
.
ErrIsDir
}
else
if
err
!=
nil
{
return
nil
,
err
case
uio
.
ErrCantReadSymlinks
:
return
nil
,
coreiface
.
ErrIsSymLink
default
:
}
return
r
,
nil
return
r
,
err
}
// Ls returns the contents of an IPFS or IPNS object(s) at path p, with the format:
...
...
@@ -76,6 +82,30 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path) ([]*ipld.Link, e
return
links
,
nil
}
var
NotASymLink
=
errors
.
New
(
"not a symbolic link"
)
func
(
api
*
UnixfsAPI
)
ReadSymLink
(
ctx
context
.
Context
,
p
coreiface
.
Path
)
(
string
,
error
)
{
dagnode
,
err
:=
api
.
core
()
.
ResolveNode
(
ctx
,
p
)
if
err
!=
nil
{
return
""
,
err
}
switch
n
:=
dagnode
.
(
type
)
{
case
*
mdag
.
ProtoNode
:
pb
:=
new
(
ftpb
.
Data
)
if
err
:=
proto
.
Unmarshal
(
n
.
Data
(),
pb
);
err
!=
nil
{
return
""
,
err
}
switch
pb
.
GetType
()
{
case
ftpb
.
Data_Symlink
:
return
string
(
pb
.
GetData
()),
nil
default
:
return
""
,
NotASymLink
}
default
:
return
""
,
NotASymLink
}
}
func
(
api
*
UnixfsAPI
)
core
()
coreiface
.
CoreAPI
{
return
(
*
CoreAPI
)(
api
)
}
core/corehttp/gateway_handler.go
浏览文件 @
0fe423c6
...
...
@@ -185,12 +185,15 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
dr
,
err
:=
i
.
api
.
Unixfs
()
.
Cat
(
ctx
,
resolvedPath
)
dir
:=
false
symLink
:=
false
switch
err
{
case
nil
:
// Cat() worked
defer
dr
.
Close
()
case
coreiface
.
ErrIsDir
:
dir
=
true
case
coreiface
.
ErrIsSymLink
:
symLink
=
true
default
:
webError
(
w
,
"ipfs cat "
+
escapedURLPath
,
err
,
http
.
StatusNotFound
)
return
...
...
@@ -266,6 +269,18 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
modtime
=
time
.
Unix
(
1
,
0
)
}
if
symLink
{
link
,
err
:=
i
.
api
.
Unixfs
()
.
ReadSymLink
(
ctx
,
resolvedPath
)
if
err
!=
nil
{
internalWebError
(
w
,
err
)
return
}
newPath
:=
gopath
.
Join
(
gopath
.
Dir
(
urlPath
),
link
)
http
.
Redirect
(
w
,
r
,
newPath
,
302
)
log
.
Debugf
(
"symlink: redirect to %s"
,
newPath
)
return
}
if
!
dir
{
name
:=
gopath
.
Base
(
urlPath
)
i
.
serveFile
(
w
,
r
,
name
,
modtime
,
dr
)
...
...
test/sharness/t0110-gateway.sh
浏览文件 @
0fe423c6
...
...
@@ -193,6 +193,46 @@ test_expect_success "GET compact blocks succeeds" '
test_cmp expected actual
'
#
#
#
test_expect_success
"create dir with symbolic links"
'
mkdir dirwlinks/ &&
( cd dirwlinks &&
echo "A Simple File" > afile &&
ln -s afile linktofile &&
mkdir adir &&
echo "Another File" > adir/bfile &&
ln -s adir linktodir
)
'
test_expect_success
"add dir with symbolic links"
'
DIRHASH=$(ipfs add -Q -r dirwlinks)
echo $DIRHASH
'
test_expect_success
"getting afile works"
'
curl -L -o actual "http://127.0.0.1:$port/ipfs/$DIRHASH/afile" &&
test_cmp dirwlinks/afile actual
'
test_expect_success
"getting linktofile works"
'
curl -L -o actual "http://127.0.0.1:$port/ipfs/$DIRHASH/linktofile" &&
test_cmp dirwlinks/afile actual
'
test_expect_success
"getting adir/bfile works"
'
curl -L -o actual "http://127.0.0.1:$port/ipfs/$DIRHASH/adir/bfile" &&
test_cmp dirwlinks/adir/bfile actual
'
test_expect_failure
"getting linktodir/bfile works"
'
curl -L -o actual "http://127.0.0.1:$port/ipfs/$DIRHASH/linktodir/bfile" &&
test_cmp dirwlinks/adir/bfile actual
'
test_kill_ipfs_daemon
test_done
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论