Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package middleware | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "net/http" | |
| 10 | |
| 11 "github.com/julienschmidt/httprouter" | |
| 12 "golang.org/x/net/context" | |
| 13 ) | |
| 14 | |
| 15 // RequireCron ensures that this handler was run from the appengine 'cron' | |
| 16 // service. Otherwise it aborts the request with a StatusForbidden. | |
| 17 func RequireCron(h Handler) Handler { | |
| 18 return func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { | |
| 19 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
| |
| 20 rw.WriteHeader(http.StatusForbidden) | |
| 21 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.
| |
| 22 return | |
| 23 } | |
| 24 h(c, rw, r, p) | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 // RequireTaskQueue ensures that this handler was run from the specified | |
| 29 // appengine 'taskqueue' queue. Otherwise it aborts the request with | |
| 30 // a StatusForbidden. | |
| 31 // | |
| 32 // if `queue` is the empty string, than this simply checks that this handler was | |
| 33 // run from ANY appengine taskqueue. | |
| 34 func RequireTaskQueue(queue string, h Handler) Handler { | |
| 35 return func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { | |
| 36 qName := r.Header.Get("X-AppEngine-QueueName") | |
| 37 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.
| |
| 38 rw.WriteHeader(http.StatusForbidden) | |
| 39 if queue == "" { | |
| 40 fmt.Fprintf(rw, "error: must be run from a taskq ueue") | |
| 41 } else { | |
| 42 fmt.Fprintf(rw, "error: must be run from the tas kqueue %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
| |
| 43 } | |
| 44 return | |
| 45 } | |
| 46 h(c, rw, r, p) | |
| 47 } | |
| 48 } | |
| OLD | NEW |