Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
1502f6bc
提交
1502f6bc
authored
5月 03, 2015
作者:
Henry
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
http gw: removed ResolvePath() in favour of core.Resolve()
上级
e633250c
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
39 行增加
和
63 行删除
+39
-63
gateway_handler.go
core/corehttp/gateway_handler.go
+39
-63
没有找到文件。
core/corehttp/gateway_handler.go
浏览文件 @
1502f6bc
...
...
@@ -69,39 +69,8 @@ func (i *gatewayHandler) loadTemplate() error {
return
nil
}
func
(
i
*
gatewayHandler
)
resolveNamePath
(
ctx
context
.
Context
,
p
string
)
(
string
,
error
)
{
p
=
gopath
.
Clean
(
p
)
if
strings
.
HasPrefix
(
p
,
IpnsPathPrefix
)
{
elements
:=
strings
.
Split
(
p
[
len
(
IpnsPathPrefix
)
:
],
"/"
)
hash
:=
elements
[
0
]
rp
,
err
:=
i
.
node
.
Namesys
.
Resolve
(
ctx
,
hash
)
if
err
!=
nil
{
return
""
,
err
}
elements
=
append
(
rp
.
Segments
(),
elements
[
1
:
]
...
)
p
=
gopath
.
Join
(
elements
...
)
}
if
!
strings
.
HasPrefix
(
p
,
IpfsPathPrefix
)
{
p
=
gopath
.
Join
(
IpfsPathPrefix
,
p
)
}
return
p
,
nil
}
func
(
i
*
gatewayHandler
)
ResolvePath
(
ctx
context
.
Context
,
p
string
)
(
*
dag
.
Node
,
string
,
error
)
{
p
,
err
:=
i
.
resolveNamePath
(
ctx
,
p
)
if
err
!=
nil
{
return
nil
,
""
,
err
}
node
,
err
:=
i
.
node
.
Resolver
.
ResolvePath
(
ctx
,
path
.
Path
(
p
))
if
err
!=
nil
{
return
nil
,
""
,
err
}
return
node
,
p
,
err
}
// TODO(cryptix): these four helper funcs shoudl also be available elsewhere, i think?
func
(
i
*
gatewayHandler
)
NewDagFromReader
(
r
io
.
Reader
)
(
*
dag
.
Node
,
error
)
{
return
importer
.
BuildDagFromReader
(
r
,
i
.
node
.
DAG
,
i
.
node
.
Pinning
.
GetManual
(),
chunk
.
DefaultSplitter
)
...
...
@@ -119,30 +88,23 @@ func (i *gatewayHandler) NewDagReader(nd *dag.Node) (uio.ReadSeekCloser, error)
return
uio
.
NewDagReader
(
i
.
node
.
Context
(),
nd
,
i
.
node
.
DAG
)
}
// TODO(btc): break this apart into separate handlers using a more expressive
// muxer
// TODO(btc): break this apart into separate handlers using a more expressive muxer
func
(
i
*
gatewayHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
i
.
config
.
Writable
&&
r
.
Method
==
"POST"
{
i
.
postHandler
(
w
,
r
)
return
}
if
i
.
config
.
Writable
&&
r
.
Method
==
"PUT"
{
i
.
putHandler
(
w
,
r
)
return
}
if
i
.
config
.
Writable
&&
r
.
Method
==
"DELETE"
{
i
.
deleteHandler
(
w
,
r
)
return
}
if
r
.
Method
==
"GET"
{
i
.
getOrHeadHandler
(
w
,
r
)
return
if
i
.
config
.
Writable
{
switch
r
.
Method
{
case
"POST"
:
i
.
postHandler
(
w
,
r
)
return
case
"PUT"
:
i
.
putHandler
(
w
,
r
)
return
case
"DELETE"
:
i
.
deleteHandler
(
w
,
r
)
return
}
}
if
r
.
Method
==
"HEAD"
{
if
r
.
Method
==
"
GET"
||
r
.
Method
==
"
HEAD"
{
i
.
getOrHeadHandler
(
w
,
r
)
return
}
...
...
@@ -156,7 +118,7 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
errmsg
=
errmsg
+
"bad request for "
+
r
.
URL
.
Path
}
w
.
Write
([]
byte
(
errmsg
))
log
.
Debug
(
errmsg
)
log
.
Error
(
errmsg
)
// TODO(cryptix): Why are we ignoring handler errors?
}
func
(
i
*
gatewayHandler
)
getOrHeadHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
@@ -171,19 +133,19 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
return
}
nd
,
p
,
err
:=
i
.
ResolvePath
(
ctx
,
urlPath
)
nd
,
err
:=
core
.
Resolve
(
ctx
,
i
.
node
,
path
.
Path
(
urlPath
)
)
if
err
!=
nil
{
webError
(
w
,
"Path Resolve error"
,
err
,
http
.
StatusBadRequest
)
return
}
etag
:=
gopath
.
Base
(
p
)
etag
:=
gopath
.
Base
(
urlPath
)
if
r
.
Header
.
Get
(
"If-None-Match"
)
==
etag
{
w
.
WriteHeader
(
http
.
StatusNotModified
)
return
}
w
.
Header
()
.
Set
(
"X-IPFS-Path"
,
p
)
w
.
Header
()
.
Set
(
"X-IPFS-Path"
,
urlPath
)
// Suborigin header, sandboxes apps from each other in the browser (even
// though they are served from the same gateway domain). NOTE: This is not
...
...
@@ -232,7 +194,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
log
.
Debug
(
"found index"
)
foundIndex
=
true
// return index page instead.
nd
,
_
,
err
:=
i
.
ResolvePath
(
ctx
,
urlPath
+
"/index.html"
)
nd
,
err
:=
core
.
Resolve
(
ctx
,
i
.
node
,
path
.
Path
(
urlPath
+
"/index.html"
)
)
if
err
!=
nil
{
internalWebError
(
w
,
err
)
return
...
...
@@ -328,14 +290,20 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
ctx
,
cancel
:=
context
.
WithCancel
(
i
.
node
.
Context
())
defer
cancel
()
ipfs
path
,
err
:=
i
.
resolveNamePath
(
ctx
,
urlPath
)
ipfs
Node
,
err
:=
core
.
Resolve
(
ctx
,
i
.
node
,
path
.
Path
(
urlPath
)
)
if
err
!=
nil
{
// FIXME HTTP error code
webError
(
w
,
"Could not resolve name"
,
err
,
http
.
StatusInternalServerError
)
return
}
h
,
components
,
err
:=
path
.
SplitAbsPath
(
path
.
Path
(
ipfspath
))
k
,
err
:=
ipfsNode
.
Key
()
if
err
!=
nil
{
webError
(
w
,
"Could not get key from resolved node"
,
err
,
http
.
StatusInternalServerError
)
return
}
h
,
components
,
err
:=
path
.
SplitAbsPath
(
path
.
FromKey
(
k
))
if
err
!=
nil
{
webError
(
w
,
"Could not split path"
,
err
,
http
.
StatusInternalServerError
)
return
...
...
@@ -351,6 +319,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
tctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Minute
)
defer
cancel
()
// TODO(cryptix): could this be core.Resolve() too?
rootnd
,
err
:=
i
.
node
.
Resolver
.
DAG
.
Get
(
tctx
,
u
.
Key
(
h
))
if
err
!=
nil
{
webError
(
w
,
"Could not resolve root object"
,
err
,
http
.
StatusBadRequest
)
...
...
@@ -400,14 +369,20 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
ctx
,
cancel
:=
context
.
WithCancel
(
i
.
node
.
Context
())
defer
cancel
()
ipfs
path
,
err
:=
i
.
resolveNamePath
(
ctx
,
urlPath
)
ipfs
Node
,
err
:=
core
.
Resolve
(
ctx
,
i
.
node
,
path
.
Path
(
urlPath
)
)
if
err
!=
nil
{
// FIXME HTTP error code
webError
(
w
,
"Could not resolve name"
,
err
,
http
.
StatusInternalServerError
)
return
}
h
,
components
,
err
:=
path
.
SplitAbsPath
(
path
.
Path
(
ipfspath
))
k
,
err
:=
ipfsNode
.
Key
()
if
err
!=
nil
{
webError
(
w
,
"Could not get key from resolved node"
,
err
,
http
.
StatusInternalServerError
)
return
}
h
,
components
,
err
:=
path
.
SplitAbsPath
(
path
.
FromKey
(
k
))
if
err
!=
nil
{
webError
(
w
,
"Could not split path"
,
err
,
http
.
StatusInternalServerError
)
return
...
...
@@ -427,6 +402,7 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
return
}
// TODO(cyrptix): assumes len(path_nodes) > 1 - not found is an error above?
err
=
path_nodes
[
len
(
path_nodes
)
-
1
]
.
RemoveNodeLink
(
components
[
len
(
components
)
-
1
])
if
err
!=
nil
{
webError
(
w
,
"Could not delete link"
,
err
,
http
.
StatusBadRequest
)
...
...
@@ -481,7 +457,7 @@ func webErrorWithCode(w http.ResponseWriter, message string, err error, code int
func
internalWebError
(
w
http
.
ResponseWriter
,
err
error
)
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
w
.
Write
([]
byte
(
err
.
Error
()))
log
.
Debug
(
"%s"
,
err
)
log
.
Error
(
"%s"
,
err
)
// TODO(cryptix): Why are we ignoring handler errors?
}
// Directory listing template
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论