Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
021ef434
提交
021ef434
authored
11月 16, 2015
作者:
Lars Gierth
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
gateway: add path prefix for directory listings
License: MIT Signed-off-by:
Lars Gierth
<
larsg@systemli.org
>
上级
ca443b35
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
72 行增加
和
9 行删除
+72
-9
gateway_handler.go
core/corehttp/gateway_handler.go
+14
-6
gateway_test.go
core/corehttp/gateway_test.go
+57
-2
ipns_hostname.go
core/corehttp/ipns_hostname.go
+1
-1
没有找到文件。
core/corehttp/gateway_handler.go
浏览文件 @
021ef434
...
...
@@ -92,16 +92,24 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
urlPath
:=
r
.
URL
.
Path
// If the gateway is behind a reverse proxy and mounted at a sub-path,
// the prefix header can be set to signal this sub-path.
// It will be prepended to links in directory listings and the index.html redirect.
prefix
:=
""
if
prefixHdr
:=
r
.
Header
[
"X-Ipfs-Gateway-Prefix"
];
len
(
prefixHdr
)
>
0
{
log
.
Debugf
(
"X-Ipfs-Gateway-Prefix: %s"
,
prefixHdr
[
0
])
prefix
=
prefixHdr
[
0
]
}
// IPNSHostnameOption might have constructed an IPNS path using the Host header.
// In this case, we need the original path for constructing redirects
// and links that match the requested URL.
// For example, http://example.net would become /ipns/example.net, and
// the redirects and links would end up as http://example.net/ipns/example.net
originalUrlPath
:=
urlPath
originalUrlPath
:=
prefix
+
urlPath
ipnsHostname
:=
false
hdr
:=
r
.
Header
[
"X-IPNS-Original-Path"
]
if
len
(
hdr
)
>
0
{
originalUrlPath
=
hdr
[
0
]
if
hdr
:=
r
.
Header
[
"X-Ipns-Original-Path"
];
len
(
hdr
)
>
0
{
originalUrlPath
=
prefix
+
hdr
[
0
]
ipnsHostname
=
true
}
...
...
@@ -211,7 +219,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
if
r
.
Method
!=
"HEAD"
{
// construct the correct back link
// https://github.com/ipfs/go-ipfs/issues/1365
var
backLink
string
=
urlPath
var
backLink
string
=
prefix
+
urlPath
// don't go further up than /ipfs/$hash/
pathSplit
:=
strings
.
Split
(
backLink
,
"/"
)
...
...
@@ -233,7 +241,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
// strip /ipfs/$hash from backlink if IPNSHostnameOption touched the path.
if
ipnsHostname
{
backLink
=
"/"
backLink
=
prefix
+
"/"
if
len
(
pathSplit
)
>
5
{
// also strip the trailing segment, because it's a backlink
backLinkParts
:=
pathSplit
[
3
:
len
(
pathSplit
)
-
2
]
...
...
core/corehttp/gateway_test.go
浏览文件 @
021ef434
...
...
@@ -213,6 +213,30 @@ func TestIPNSHostnameRedirect(t *testing.T) {
}
else
if
hdr
[
0
]
!=
"/foo/"
{
t
.
Errorf
(
"location header is %v, expected /foo/"
,
hdr
[
0
])
}
// make request with prefix to directory containing index.html
req
,
err
=
http
.
NewRequest
(
"GET"
,
ts
.
URL
+
"/foo"
,
nil
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
req
.
Host
=
"example.net"
req
.
Header
.
Set
(
"X-Ipfs-Gateway-Prefix"
,
"/prefix"
)
res
,
err
=
doWithoutRedirect
(
req
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// expect 302 redirect to same path, but with prefix and trailing slash
if
res
.
StatusCode
!=
302
{
t
.
Errorf
(
"status is %d, expected 302"
,
res
.
StatusCode
)
}
hdr
=
res
.
Header
[
"Location"
]
if
len
(
hdr
)
<
1
{
t
.
Errorf
(
"location header not present"
)
}
else
if
hdr
[
0
]
!=
"/prefix/foo/"
{
t
.
Errorf
(
"location header is %v, expected /prefix/foo/"
,
hdr
[
0
])
}
}
func
TestIPNSHostnameBacklinks
(
t
*
testing
.
T
)
{
...
...
@@ -282,7 +306,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
t
.
Fatalf
(
"expected file in directory listing"
)
}
// make request to directory listing
// make request to directory listing
at root
req
,
err
=
http
.
NewRequest
(
"GET"
,
ts
.
URL
,
nil
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -294,7 +318,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
t
.
Fatal
(
err
)
}
// expect correct backlinks
// expect correct backlinks
at root
body
,
err
=
ioutil
.
ReadAll
(
res
.
Body
)
if
err
!=
nil
{
t
.
Fatalf
(
"error reading response: %s"
,
err
)
...
...
@@ -341,4 +365,35 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
if
!
strings
.
Contains
(
s
,
"<a href=
\"
/foo/bar/file.txt
\"
>"
)
{
t
.
Fatalf
(
"expected file in directory listing"
)
}
// make request to directory listing with prefix
req
,
err
=
http
.
NewRequest
(
"GET"
,
ts
.
URL
,
nil
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
req
.
Host
=
"example.net"
req
.
Header
.
Set
(
"X-Ipfs-Gateway-Prefix"
,
"/prefix"
)
res
,
err
=
doWithoutRedirect
(
req
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// expect correct backlinks with prefix
body
,
err
=
ioutil
.
ReadAll
(
res
.
Body
)
if
err
!=
nil
{
t
.
Fatalf
(
"error reading response: %s"
,
err
)
}
s
=
string
(
body
)
t
.
Logf
(
"body: %s
\n
"
,
string
(
body
))
if
!
strings
.
Contains
(
s
,
"Index of /prefix"
)
{
t
.
Fatalf
(
"expected a path in directory listing"
)
}
if
!
strings
.
Contains
(
s
,
"<a href=
\"
/prefix/
\"
>"
)
{
t
.
Fatalf
(
"expected backlink in directory listing"
)
}
if
!
strings
.
Contains
(
s
,
"<a href=
\"
/prefix/file.txt
\"
>"
)
{
t
.
Fatalf
(
"expected file in directory listing"
)
}
}
core/corehttp/ipns_hostname.go
浏览文件 @
021ef434
...
...
@@ -24,7 +24,7 @@ func IPNSHostnameOption() ServeOption {
if
len
(
host
)
>
0
&&
isd
.
IsDomain
(
host
)
{
name
:=
"/ipns/"
+
host
if
_
,
err
:=
n
.
Namesys
.
Resolve
(
ctx
,
name
);
err
==
nil
{
r
.
Header
[
"X-I
PNS
-Original-Path"
]
=
[]
string
{
r
.
URL
.
Path
}
r
.
Header
[
"X-I
pns
-Original-Path"
]
=
[]
string
{
r
.
URL
.
Path
}
r
.
URL
.
Path
=
name
+
r
.
URL
.
Path
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论