Chromium Code Reviews| Index: appengine/middleware/appengine.go |
| diff --git a/appengine/middleware/appengine.go b/appengine/middleware/appengine.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..261fe8bb72a42a0f3860d37b87bdcd7120b9451d |
| --- /dev/null |
| +++ b/appengine/middleware/appengine.go |
| @@ -0,0 +1,48 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package middleware |
| + |
| +import ( |
| + "fmt" |
| + "net/http" |
| + |
| + "github.com/julienschmidt/httprouter" |
| + "golang.org/x/net/context" |
| +) |
| + |
| +// RequireCron ensures that this handler was run from the appengine 'cron' |
| +// service. Otherwise it aborts the request with a StatusForbidden. |
| +func RequireCron(h Handler) Handler { |
| + return func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { |
| + if r.Header.Get("X-Appengine-Cron") != "true" { |
|
Vadim Sh.
2015/10/13 01:29:14
consider adding "&& !IsDevAppServer()"
it simplif
iannucci
2015/10/13 01:37:17
WDYT about just adding this header in BaseTest?
Vadim Sh.
2015/10/13 01:40:26
What do you mean?
By "local manual testing" I mea
|
| + rw.WriteHeader(http.StatusForbidden) |
| + fmt.Fprint(rw, "error: must be run from cron") |
|
dnj
2015/10/13 01:44:49
Worth logging, too.
iannucci
2015/10/13 01:56:34
Ah, right, meant to do that. Done.
|
| + return |
| + } |
| + h(c, rw, r, p) |
| + } |
| +} |
| + |
| +// RequireTaskQueue ensures that this handler was run from the specified |
| +// appengine 'taskqueue' queue. Otherwise it aborts the request with |
| +// a StatusForbidden. |
| +// |
| +// if `queue` is the empty string, than this simply checks that this handler was |
| +// run from ANY appengine taskqueue. |
| +func RequireTaskQueue(queue string, h Handler) Handler { |
| + return func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { |
| + qName := r.Header.Get("X-AppEngine-QueueName") |
| + if qName == "" || (queue != "" && queue != qName) { |
|
dnj
2015/10/13 01:44:49
Why have "" be special? IMO should be simple:
if q
iannucci
2015/10/13 01:56:34
Docs explain it pretty well I think?
dnj
2015/10/13 04:39:10
Ah okay, makes sense. Sorry was on a bus.
|
| + rw.WriteHeader(http.StatusForbidden) |
| + if queue == "" { |
| + fmt.Fprintf(rw, "error: must be run from a taskqueue") |
| + } else { |
| + fmt.Fprintf(rw, "error: must be run from the taskqueue %q", queue) |
|
dnj
2015/10/13 01:44:49
This could leak internal task queue names. Maybe l
iannucci
2015/10/13 01:56:34
done
|
| + } |
| + return |
| + } |
| + h(c, rw, r, p) |
| + } |
| +} |