Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
f6c6d5de
提交
f6c6d5de
authored
5月 07, 2015
作者:
David Braun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add CORS middleware handler to the API.
上级
cd37b674
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
108 行增加
和
13 行删除
+108
-13
handler.go
commands/http/handler.go
+37
-13
handler_test.go
commands/http/handler_test.go
+71
-0
没有找到文件。
commands/http/handler.go
浏览文件 @
f6c6d5de
...
...
@@ -8,6 +8,8 @@ import (
"strconv"
"strings"
"github.com/rs/cors"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
cmds
"github.com/ipfs/go-ipfs/commands"
...
...
@@ -16,10 +18,17 @@ import (
var
log
=
u
.
Logger
(
"commands/http"
)
// the internal handler for the API
type
internalHandler
struct
{
ctx
cmds
.
Context
root
*
cmds
.
Command
}
// The Handler struct is funny because we want to wrap our internal handler
// with CORS while keeping our fields.
type
Handler
struct
{
ctx
cmds
.
Context
root
*
cmds
.
Command
origin
string
internalHandler
corsHandler
http
.
Handler
}
var
ErrNotFound
=
errors
.
New
(
"404 page not found"
)
...
...
@@ -39,16 +48,31 @@ var mimeTypes = map[string]string{
cmds
.
Text
:
"text/plain"
,
}
func
NewHandler
(
ctx
cmds
.
Context
,
root
*
cmds
.
Command
,
o
rigin
string
)
*
Handler
{
func
NewHandler
(
ctx
cmds
.
Context
,
root
*
cmds
.
Command
,
allowedO
rigin
string
)
*
Handler
{
// allow whitelisted origins (so we can make API requests from the browser)
if
len
(
o
rigin
)
>
0
{
log
.
Info
(
"Allowing API requests from origin: "
+
o
rigin
)
if
len
(
allowedO
rigin
)
>
0
{
log
.
Info
(
"Allowing API requests from origin: "
+
allowedO
rigin
)
}
return
&
Handler
{
ctx
,
root
,
origin
}
// Create a handler for the API.
internal
:=
internalHandler
{
ctx
,
root
}
// Create a CORS object for wrapping the internal handler.
c
:=
cors
.
New
(
cors
.
Options
{
AllowedMethods
:
[]
string
{
"GET"
,
"POST"
,
"PUT"
},
// use AllowOriginFunc instead of AllowedOrigins because we want to be
// restrictive by default.
AllowOriginFunc
:
func
(
origin
string
)
bool
{
return
(
allowedOrigin
==
"*"
)
||
(
origin
==
allowedOrigin
)
},
})
// Wrap the internal handler with CORS handling-middleware.
return
&
Handler
{
internal
,
c
.
Handler
(
internal
)}
}
func
(
i
Handler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
i
internal
Handler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
log
.
Debug
(
"Incoming API request: "
,
r
.
URL
)
// error on external referers (to prevent CSRF attacks)
...
...
@@ -65,11 +89,6 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if
len
(
i
.
origin
)
>
0
{
w
.
Header
()
.
Set
(
"Access-Control-Allow-Origin"
,
i
.
origin
)
}
w
.
Header
()
.
Set
(
"Access-Control-Allow-Headers"
,
"Content-Type"
)
req
,
err
:=
Parse
(
r
,
i
.
root
)
if
err
!=
nil
{
if
err
==
ErrNotFound
{
...
...
@@ -168,6 +187,11 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
flushCopy
(
w
,
out
)
}
func
(
i
Handler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// Call the CORS handler which wraps the internal handler.
i
.
corsHandler
.
ServeHTTP
(
w
,
r
)
}
// flushCopy Copies from an io.Reader to a http.ResponseWriter.
// Flushes chunks over HTTP stream as they are read (if supported by transport).
func
flushCopy
(
w
http
.
ResponseWriter
,
out
io
.
Reader
)
error
{
...
...
commands/http/handler_test.go
0 → 100644
浏览文件 @
f6c6d5de
package
http
import
(
"net/http"
"net/http/httptest"
"testing"
"github.com/ipfs/go-ipfs/commands"
)
func
assertHeaders
(
t
*
testing
.
T
,
resHeaders
http
.
Header
,
reqHeaders
map
[
string
]
string
)
{
for
name
,
value
:=
range
reqHeaders
{
if
resHeaders
.
Get
(
name
)
!=
value
{
t
.
Errorf
(
"Invalid header `%s', wanted `%s', got `%s'"
,
name
,
value
,
resHeaders
.
Get
(
name
))
}
}
}
func
TestDisallowedOrigin
(
t
*
testing
.
T
)
{
res
:=
httptest
.
NewRecorder
()
req
,
_
:=
http
.
NewRequest
(
"GET"
,
"http://example.com/foo"
,
nil
)
req
.
Header
.
Add
(
"Origin"
,
"http://barbaz.com"
)
handler
:=
NewHandler
(
commands
.
Context
{},
nil
,
""
)
handler
.
ServeHTTP
(
res
,
req
)
assertHeaders
(
t
,
res
.
Header
(),
map
[
string
]
string
{
"Access-Control-Allow-Origin"
:
""
,
"Access-Control-Allow-Methods"
:
""
,
"Access-Control-Allow-Credentials"
:
""
,
"Access-Control-Max-Age"
:
""
,
"Access-Control-Expose-Headers"
:
""
,
})
}
func
TestWildcardOrigin
(
t
*
testing
.
T
)
{
res
:=
httptest
.
NewRecorder
()
req
,
_
:=
http
.
NewRequest
(
"GET"
,
"http://example.com/foo"
,
nil
)
req
.
Header
.
Add
(
"Origin"
,
"http://foobar.com"
)
handler
:=
NewHandler
(
commands
.
Context
{},
nil
,
"*"
)
handler
.
ServeHTTP
(
res
,
req
)
assertHeaders
(
t
,
res
.
Header
(),
map
[
string
]
string
{
"Access-Control-Allow-Origin"
:
"http://foobar.com"
,
"Access-Control-Allow-Methods"
:
""
,
"Access-Control-Allow-Headers"
:
""
,
"Access-Control-Allow-Credentials"
:
""
,
"Access-Control-Max-Age"
:
""
,
"Access-Control-Expose-Headers"
:
""
,
})
}
func
TestAllowedMethod
(
t
*
testing
.
T
)
{
res
:=
httptest
.
NewRecorder
()
req
,
_
:=
http
.
NewRequest
(
"OPTIONS"
,
"http://example.com/foo"
,
nil
)
req
.
Header
.
Add
(
"Origin"
,
"http://www.foobar.com"
)
req
.
Header
.
Add
(
"Access-Control-Request-Method"
,
"PUT"
)
handler
:=
NewHandler
(
commands
.
Context
{},
nil
,
"http://www.foobar.com"
)
handler
.
ServeHTTP
(
res
,
req
)
assertHeaders
(
t
,
res
.
Header
(),
map
[
string
]
string
{
"Access-Control-Allow-Origin"
:
"http://www.foobar.com"
,
"Access-Control-Allow-Methods"
:
"PUT"
,
"Access-Control-Allow-Headers"
:
""
,
"Access-Control-Allow-Credentials"
:
""
,
"Access-Control-Max-Age"
:
""
,
"Access-Control-Expose-Headers"
:
""
,
})
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论