OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package memory | 5 package memory |
6 | 6 |
7 import ( | 7 import ( |
8 » "golang.org/x/net/context" | 8 » "fmt" |
| 9 » "regexp" |
9 | 10 |
10 "github.com/luci/gae/impl/dummy" | 11 "github.com/luci/gae/impl/dummy" |
11 "github.com/luci/gae/service/info" | 12 "github.com/luci/gae/service/info" |
| 13 "golang.org/x/net/context" |
12 ) | 14 ) |
13 | 15 |
14 type giContextKeyType int | 16 type giContextKeyType int |
15 | 17 |
16 var giContextKey giContextKeyType | 18 var giContextKey giContextKeyType |
17 | 19 |
| 20 // validNamespace matches valid namespace names. |
| 21 var validNamespace = regexp.MustCompile(`^[0-9A-Za-z._-]{0,100}$`) |
| 22 |
18 func curGID(c context.Context) *globalInfoData { | 23 func curGID(c context.Context) *globalInfoData { |
19 return c.Value(giContextKey).(*globalInfoData) | 24 return c.Value(giContextKey).(*globalInfoData) |
20 } | 25 } |
21 | 26 |
22 // useGI adds a gae.GlobalInfo context, accessible | 27 // useGI adds a gae.GlobalInfo context, accessible |
23 // by gae.GetGI(c) | 28 // by gae.GetGI(c) |
24 func useGI(c context.Context) context.Context { | 29 func useGI(c context.Context) context.Context { |
25 return info.SetFactory(c, func(ic context.Context) info.Interface { | 30 return info.SetFactory(c, func(ic context.Context) info.Interface { |
26 return &giImpl{dummy.Info(), curGID(ic), ic} | 31 return &giImpl{dummy.Info(), curGID(ic), ic} |
27 }) | 32 }) |
(...skipping 10 matching lines...) Expand all Loading... |
38 } | 43 } |
39 | 44 |
40 type giImpl struct { | 45 type giImpl struct { |
41 info.Interface | 46 info.Interface |
42 *globalInfoData | 47 *globalInfoData |
43 c context.Context | 48 c context.Context |
44 } | 49 } |
45 | 50 |
46 var _ = info.Interface((*giImpl)(nil)) | 51 var _ = info.Interface((*giImpl)(nil)) |
47 | 52 |
| 53 func (gi *giImpl) GetNamespace() string { |
| 54 return gi.namespace |
| 55 } |
| 56 |
48 func (gi *giImpl) Namespace(ns string) (ret context.Context, err error) { | 57 func (gi *giImpl) Namespace(ns string) (ret context.Context, err error) { |
| 58 if !validNamespace.MatchString(ns) { |
| 59 return nil, fmt.Errorf("appengine: namespace %q does not match /
%s/", ns, validNamespace) |
| 60 } |
49 return context.WithValue(gi.c, giContextKey, &globalInfoData{ns}), nil | 61 return context.WithValue(gi.c, giContextKey, &globalInfoData{ns}), nil |
50 } | 62 } |
51 | 63 |
52 func (gi *giImpl) AppID() string { | 64 func (gi *giImpl) AppID() string { |
53 return globalAppID | 65 return globalAppID |
54 } | 66 } |
OLD | NEW |