Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
45550858
提交
45550858
authored
10月 13, 2015
作者:
Artem Andreenko
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix races in http cors
License: MIT Signed-off-by:
Artem Andreenko
<
mio@volmy.com
>
上级
bea47c9b
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
76 行增加
和
33 行删除
+76
-33
handler.go
commands/http/handler.go
+56
-6
handler_test.go
commands/http/handler_test.go
+4
-8
commands.go
core/corehttp/commands.go
+16
-19
没有找到文件。
commands/http/handler.go
浏览文件 @
45550858
...
@@ -11,6 +11,7 @@ import (
...
@@ -11,6 +11,7 @@ import (
"runtime"
"runtime"
"strconv"
"strconv"
"strings"
"strings"
"sync"
cors
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/rs/cors"
cors
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/rs/cors"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
context
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
...
@@ -68,8 +69,11 @@ type ServerConfig struct {
...
@@ -68,8 +69,11 @@ type ServerConfig struct {
// Headers is an optional map of headers that is written out.
// Headers is an optional map of headers that is written out.
Headers
map
[
string
][]
string
Headers
map
[
string
][]
string
// CORSOpts is a set of options for CORS headers.
// cORSOpts is a set of options for CORS headers.
CORSOpts
*
cors
.
Options
cORSOpts
*
cors
.
Options
// cORSOptsRWMutex is a RWMutex for read/write CORSOpts
cORSOptsRWMutex
sync
.
RWMutex
}
}
func
skipAPIHeader
(
h
string
)
bool
{
func
skipAPIHeader
(
h
string
)
bool
{
...
@@ -93,7 +97,7 @@ func NewHandler(ctx cmds.Context, root *cmds.Command, cfg *ServerConfig) *Handle
...
@@ -93,7 +97,7 @@ func NewHandler(ctx cmds.Context, root *cmds.Command, cfg *ServerConfig) *Handle
// Wrap the internal handler with CORS handling-middleware.
// Wrap the internal handler with CORS handling-middleware.
// Create a handler for the API.
// Create a handler for the API.
internal
:=
internalHandler
{
ctx
,
root
,
cfg
}
internal
:=
internalHandler
{
ctx
,
root
,
cfg
}
c
:=
cors
.
New
(
*
cfg
.
C
ORSOpts
)
c
:=
cors
.
New
(
*
cfg
.
c
ORSOpts
)
return
&
Handler
{
internal
,
c
.
Handler
(
internal
)}
return
&
Handler
{
internal
,
c
.
Handler
(
internal
)}
}
}
...
@@ -322,6 +326,51 @@ func sanitizedErrStr(err error) string {
...
@@ -322,6 +326,51 @@ func sanitizedErrStr(err error) string {
return
s
return
s
}
}
func
NewServerConfig
()
*
ServerConfig
{
cfg
:=
new
(
ServerConfig
)
cfg
.
cORSOpts
=
new
(
cors
.
Options
)
return
cfg
}
func
(
cfg
ServerConfig
)
AllowedOrigins
()
[]
string
{
cfg
.
cORSOptsRWMutex
.
RLock
()
defer
cfg
.
cORSOptsRWMutex
.
RUnlock
()
return
cfg
.
cORSOpts
.
AllowedOrigins
}
func
(
cfg
*
ServerConfig
)
SetAllowedOrigins
(
origins
...
string
)
{
cfg
.
cORSOptsRWMutex
.
Lock
()
defer
cfg
.
cORSOptsRWMutex
.
Unlock
()
cfg
.
cORSOpts
.
AllowedOrigins
=
origins
}
func
(
cfg
*
ServerConfig
)
AppendAllowedOrigins
(
origins
...
string
)
{
cfg
.
cORSOptsRWMutex
.
Lock
()
defer
cfg
.
cORSOptsRWMutex
.
Unlock
()
cfg
.
cORSOpts
.
AllowedOrigins
=
append
(
cfg
.
cORSOpts
.
AllowedOrigins
,
origins
...
)
}
func
(
cfg
ServerConfig
)
AllowedMethods
()
[]
string
{
cfg
.
cORSOptsRWMutex
.
RLock
()
defer
cfg
.
cORSOptsRWMutex
.
RUnlock
()
return
[]
string
(
cfg
.
cORSOpts
.
AllowedMethods
)
}
func
(
cfg
*
ServerConfig
)
SetAllowedMethods
(
methods
...
string
)
{
cfg
.
cORSOptsRWMutex
.
Lock
()
defer
cfg
.
cORSOptsRWMutex
.
Unlock
()
if
cfg
.
cORSOpts
==
nil
{
cfg
.
cORSOpts
=
new
(
cors
.
Options
)
}
cfg
.
cORSOpts
.
AllowedMethods
=
methods
}
func
(
cfg
*
ServerConfig
)
SetAllowCredentials
(
flag
bool
)
{
cfg
.
cORSOptsRWMutex
.
Lock
()
defer
cfg
.
cORSOptsRWMutex
.
Unlock
()
cfg
.
cORSOpts
.
AllowCredentials
=
flag
}
// allowOrigin just stops the request if the origin is not allowed.
// allowOrigin just stops the request if the origin is not allowed.
// the CORS middleware apparently does not do this for us...
// the CORS middleware apparently does not do this for us...
func
allowOrigin
(
r
*
http
.
Request
,
cfg
*
ServerConfig
)
bool
{
func
allowOrigin
(
r
*
http
.
Request
,
cfg
*
ServerConfig
)
bool
{
...
@@ -333,8 +382,8 @@ func allowOrigin(r *http.Request, cfg *ServerConfig) bool {
...
@@ -333,8 +382,8 @@ func allowOrigin(r *http.Request, cfg *ServerConfig) bool {
if
origin
==
""
{
if
origin
==
""
{
return
true
return
true
}
}
origins
:=
cfg
.
AllowedOrigins
()
for
_
,
o
:=
range
cfg
.
CORSOpts
.
AllowedO
rigins
{
for
_
,
o
:=
range
o
rigins
{
if
o
==
"*"
{
// ok! you asked for it!
if
o
==
"*"
{
// ok! you asked for it!
return
true
return
true
}
}
...
@@ -375,7 +424,8 @@ func allowReferer(r *http.Request, cfg *ServerConfig) bool {
...
@@ -375,7 +424,8 @@ func allowReferer(r *http.Request, cfg *ServerConfig) bool {
// check CORS ACAOs and pretend Referer works like an origin.
// check CORS ACAOs and pretend Referer works like an origin.
// this is valid for many (most?) sane uses of the API in
// this is valid for many (most?) sane uses of the API in
// other applications, and will have the desired effect.
// other applications, and will have the desired effect.
for
_
,
o
:=
range
cfg
.
CORSOpts
.
AllowedOrigins
{
origins
:=
cfg
.
AllowedOrigins
()
for
_
,
o
:=
range
origins
{
if
o
==
"*"
{
// ok! you asked for it!
if
o
==
"*"
{
// ok! you asked for it!
return
true
return
true
}
}
...
...
commands/http/handler_test.go
浏览文件 @
45550858
...
@@ -6,8 +6,6 @@ import (
...
@@ -6,8 +6,6 @@ import (
"net/url"
"net/url"
"testing"
"testing"
cors
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/rs/cors"
cmds
"github.com/ipfs/go-ipfs/commands"
cmds
"github.com/ipfs/go-ipfs/commands"
ipfscmd
"github.com/ipfs/go-ipfs/core/commands"
ipfscmd
"github.com/ipfs/go-ipfs/core/commands"
coremock
"github.com/ipfs/go-ipfs/core/mock"
coremock
"github.com/ipfs/go-ipfs/core/mock"
...
@@ -28,12 +26,10 @@ func assertStatus(t *testing.T, actual, expected int) {
...
@@ -28,12 +26,10 @@ func assertStatus(t *testing.T, actual, expected int) {
}
}
func
originCfg
(
origins
[]
string
)
*
ServerConfig
{
func
originCfg
(
origins
[]
string
)
*
ServerConfig
{
return
&
ServerConfig
{
cfg
:=
NewServerConfig
()
CORSOpts
:
&
cors
.
Options
{
cfg
.
SetAllowedOrigins
(
origins
...
)
AllowedOrigins
:
origins
,
cfg
.
SetAllowedMethods
(
"GET"
,
"PUT"
,
"POST"
)
AllowedMethods
:
[]
string
{
"GET"
,
"PUT"
,
"POST"
},
return
cfg
},
}
}
}
type
testCase
struct
{
type
testCase
struct
{
...
...
core/corehttp/commands.go
浏览文件 @
45550858
...
@@ -7,8 +7,6 @@ import (
...
@@ -7,8 +7,6 @@ import (
"strconv"
"strconv"
"strings"
"strings"
cors
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/rs/cors"
commands
"github.com/ipfs/go-ipfs/commands"
commands
"github.com/ipfs/go-ipfs/commands"
cmdsHttp
"github.com/ipfs/go-ipfs/commands/http"
cmdsHttp
"github.com/ipfs/go-ipfs/commands/http"
core
"github.com/ipfs/go-ipfs/core"
core
"github.com/ipfs/go-ipfs/core"
...
@@ -41,10 +39,10 @@ func addCORSFromEnv(c *cmdsHttp.ServerConfig) {
...
@@ -41,10 +39,10 @@ func addCORSFromEnv(c *cmdsHttp.ServerConfig) {
origin
:=
os
.
Getenv
(
originEnvKey
)
origin
:=
os
.
Getenv
(
originEnvKey
)
if
origin
!=
""
{
if
origin
!=
""
{
log
.
Warning
(
originEnvKeyDeprecate
)
log
.
Warning
(
originEnvKeyDeprecate
)
if
c
.
CORSOpts
==
nil
{
if
len
(
c
.
AllowedOrigins
())
==
0
{
c
.
CORSOpts
.
AllowedOrigins
=
[]
string
{
origin
}
c
.
SetAllowedOrigins
([]
string
{
origin
}
...
)
}
}
c
.
CORSOpts
.
AllowedOrigins
=
append
(
c
.
CORSOpts
.
AllowedOrigins
,
origin
)
c
.
AppendAllowedOrigins
(
origin
)
}
}
}
}
...
@@ -52,14 +50,14 @@ func addHeadersFromConfig(c *cmdsHttp.ServerConfig, nc *config.Config) {
...
@@ -52,14 +50,14 @@ func addHeadersFromConfig(c *cmdsHttp.ServerConfig, nc *config.Config) {
log
.
Info
(
"Using API.HTTPHeaders:"
,
nc
.
API
.
HTTPHeaders
)
log
.
Info
(
"Using API.HTTPHeaders:"
,
nc
.
API
.
HTTPHeaders
)
if
acao
:=
nc
.
API
.
HTTPHeaders
[
cmdsHttp
.
ACAOrigin
];
acao
!=
nil
{
if
acao
:=
nc
.
API
.
HTTPHeaders
[
cmdsHttp
.
ACAOrigin
];
acao
!=
nil
{
c
.
CORSOpts
.
AllowedOrigins
=
acao
c
.
SetAllowedOrigins
(
acao
...
)
}
}
if
acam
:=
nc
.
API
.
HTTPHeaders
[
cmdsHttp
.
ACAMethods
];
acam
!=
nil
{
if
acam
:=
nc
.
API
.
HTTPHeaders
[
cmdsHttp
.
ACAMethods
];
acam
!=
nil
{
c
.
CORSOpts
.
AllowedMethods
=
acam
c
.
SetAllowedMethods
(
acam
...
)
}
}
if
acac
:=
nc
.
API
.
HTTPHeaders
[
cmdsHttp
.
ACACredentials
];
acac
!=
nil
{
if
acac
:=
nc
.
API
.
HTTPHeaders
[
cmdsHttp
.
ACACredentials
];
acac
!=
nil
{
for
_
,
v
:=
range
acac
{
for
_
,
v
:=
range
acac
{
c
.
CORSOpts
.
AllowCredentials
=
(
strings
.
ToLower
(
v
)
==
"true"
)
c
.
SetAllowCredentials
(
strings
.
ToLower
(
v
)
==
"true"
)
}
}
}
}
...
@@ -68,13 +66,13 @@ func addHeadersFromConfig(c *cmdsHttp.ServerConfig, nc *config.Config) {
...
@@ -68,13 +66,13 @@ func addHeadersFromConfig(c *cmdsHttp.ServerConfig, nc *config.Config) {
func
addCORSDefaults
(
c
*
cmdsHttp
.
ServerConfig
)
{
func
addCORSDefaults
(
c
*
cmdsHttp
.
ServerConfig
)
{
// by default use localhost origins
// by default use localhost origins
if
len
(
c
.
CORSOpts
.
AllowedOrigins
)
==
0
{
if
len
(
c
.
AllowedOrigins
()
)
==
0
{
c
.
CORSOpts
.
AllowedOrigins
=
defaultLocalhostOrigins
c
.
SetAllowedOrigins
(
defaultLocalhostOrigins
...
)
}
}
// by default, use GET, PUT, POST
// by default, use GET, PUT, POST
if
len
(
c
.
CORSOpts
.
AllowedMethods
)
==
0
{
if
len
(
c
.
AllowedMethods
()
)
==
0
{
c
.
CORSOpts
.
AllowedMethods
=
[]
string
{
"GET"
,
"POST"
,
"PUT"
}
c
.
SetAllowedMethods
(
"GET"
,
"POST"
,
"PUT"
)
}
}
}
}
...
@@ -90,23 +88,22 @@ func patchCORSVars(c *cmdsHttp.ServerConfig, addr net.Addr) {
...
@@ -90,23 +88,22 @@ func patchCORSVars(c *cmdsHttp.ServerConfig, addr net.Addr) {
}
}
// we're listening on tcp/udp with ports. ("udp!?" you say? yeah... it happens...)
// we're listening on tcp/udp with ports. ("udp!?" you say? yeah... it happens...)
for
i
,
o
:=
range
c
.
CORSOpts
.
AllowedOrigins
{
origins
:=
c
.
AllowedOrigins
()
for
i
,
o
:=
range
origins
{
// TODO: allow replacing <host>. tricky, ip4 and ip6 and hostnames...
// TODO: allow replacing <host>. tricky, ip4 and ip6 and hostnames...
if
port
!=
""
{
if
port
!=
""
{
o
=
strings
.
Replace
(
o
,
"<port>"
,
port
,
-
1
)
o
=
strings
.
Replace
(
o
,
"<port>"
,
port
,
-
1
)
}
}
c
.
CORSOpts
.
AllowedO
rigins
[
i
]
=
o
o
rigins
[
i
]
=
o
}
}
c
.
SetAllowedOrigins
(
origins
...
)
}
}
func
commandsOption
(
cctx
commands
.
Context
,
command
*
commands
.
Command
)
ServeOption
{
func
commandsOption
(
cctx
commands
.
Context
,
command
*
commands
.
Command
)
ServeOption
{
return
func
(
n
*
core
.
IpfsNode
,
l
net
.
Listener
,
mux
*
http
.
ServeMux
)
(
*
http
.
ServeMux
,
error
)
{
return
func
(
n
*
core
.
IpfsNode
,
l
net
.
Listener
,
mux
*
http
.
ServeMux
)
(
*
http
.
ServeMux
,
error
)
{
cfg
:=
&
cmdsHttp
.
ServerConfig
{
cfg
:=
cmdsHttp
.
NewServerConfig
()
CORSOpts
:
&
cors
.
Options
{
cfg
.
SetAllowedMethods
(
"GET"
,
"POST"
,
"PUT"
)
AllowedMethods
:
[]
string
{
"GET"
,
"POST"
,
"PUT"
},
},
}
rcfg
,
err
:=
n
.
Repo
.
Config
()
rcfg
,
err
:=
n
.
Repo
.
Config
()
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论