@@ -24,17 +24,17 @@ import (
2424 "sync/atomic"
2525 "time"
2626
27+ netcontext "context"
28+
2729 "github.com/golang/protobuf/proto"
28- netcontext "golang.org/x/net/context"
2930
3031 basepb "google.golang.org/appengine/internal/base"
3132 logpb "google.golang.org/appengine/internal/log"
3233 remotepb "google.golang.org/appengine/internal/remote_api"
3334)
3435
3536const (
36- apiPath = "/rpc_http"
37- defaultTicketSuffix = "/default.20150612t184001.0"
37+ apiPath = "/rpc_http"
3838)
3939
4040var (
@@ -66,21 +66,22 @@ var (
6666 IdleConnTimeout : 90 * time .Second ,
6767 },
6868 }
69-
70- defaultTicketOnce sync.Once
71- defaultTicket string
72- backgroundContextOnce sync.Once
73- backgroundContext netcontext.Context
7469)
7570
76- func apiURL () * url.URL {
71+ func apiURL (ctx netcontext. Context ) * url.URL {
7772 host , port := "appengine.googleapis.internal" , "10001"
7873 if h := os .Getenv ("API_HOST" ); h != "" {
7974 host = h
8075 }
76+ if hostOverride := ctx .Value (apiHostOverrideKey ); hostOverride != nil {
77+ host = hostOverride .(string )
78+ }
8179 if p := os .Getenv ("API_PORT" ); p != "" {
8280 port = p
8381 }
82+ if portOverride := ctx .Value (apiPortOverrideKey ); portOverride != nil {
83+ port = portOverride .(string )
84+ }
8485 return & url.URL {
8586 Scheme : "http" ,
8687 Host : host + ":" + port ,
@@ -98,7 +99,6 @@ func handleHTTPMiddleware(next http.Handler) http.Handler {
9899 c := & context {
99100 req : r ,
100101 outHeader : w .Header (),
101- apiURL : apiURL (),
102102 }
103103 r = r .WithContext (withContext (r .Context (), c ))
104104 c .req = r
@@ -235,8 +235,6 @@ type context struct {
235235 lines []* logpb.UserAppLogLine
236236 flushes int
237237 }
238-
239- apiURL * url.URL
240238}
241239
242240var contextKey = "holds a *context"
@@ -304,59 +302,19 @@ func WithContext(parent netcontext.Context, req *http.Request) netcontext.Contex
304302 }
305303}
306304
307- // DefaultTicket returns a ticket used for background context or dev_appserver.
308- func DefaultTicket () string {
309- defaultTicketOnce .Do (func () {
310- if IsDevAppServer () {
311- defaultTicket = "testapp" + defaultTicketSuffix
312- return
313- }
314- appID := partitionlessAppID ()
315- escAppID := strings .Replace (strings .Replace (appID , ":" , "_" , - 1 ), "." , "_" , - 1 )
316- majVersion := VersionID (nil )
317- if i := strings .Index (majVersion , "." ); i > 0 {
318- majVersion = majVersion [:i ]
319- }
320- defaultTicket = fmt .Sprintf ("%s/%s.%s.%s" , escAppID , ModuleName (nil ), majVersion , InstanceID ())
321- })
322- return defaultTicket
323- }
324-
325- func BackgroundContext () netcontext.Context {
326- backgroundContextOnce .Do (func () {
327- // Compute background security ticket.
328- ticket := DefaultTicket ()
329-
330- c := & context {
331- req : & http.Request {
332- Header : http.Header {
333- ticketHeader : []string {ticket },
334- },
335- },
336- apiURL : apiURL (),
337- }
338- backgroundContext = toContext (c )
339-
340- // TODO(dsymonds): Wire up the shutdown handler to do a final flush.
341- go c .logFlusher (make (chan int ))
342- })
343-
344- return backgroundContext
345- }
346-
347305// RegisterTestRequest registers the HTTP request req for testing, such that
348- // any API calls are sent to the provided URL. It returns a closure to delete
349- // the registration.
306+ // any API calls are sent to the provided URL.
350307// It should only be used by aetest package.
351- func RegisterTestRequest (req * http.Request , apiURL * url.URL , decorate func (netcontext.Context ) netcontext.Context ) (* http.Request , func ()) {
352- c := & context {
353- req : req ,
354- apiURL : apiURL ,
355- }
356- ctx := withContext (decorate (req .Context ()), c )
357- req = req .WithContext (ctx )
358- c .req = req
359- return req , func () {}
308+ func RegisterTestRequest (req * http.Request , apiURL * url.URL , appID string ) * http.Request {
309+ ctx := req .Context ()
310+ ctx = withAPIHostOverride (ctx , apiURL .Hostname ())
311+ ctx = withAPIPortOverride (ctx , apiURL .Port ())
312+ ctx = WithAppIDOverride (ctx , appID )
313+
314+ // use the unregistered request as a placeholder so that withContext can read the headers
315+ c := & context {req : req }
316+ c .req = req .WithContext (withContext (ctx , c ))
317+ return c .req
360318}
361319
362320var errTimeout = & CallError {
@@ -401,10 +359,11 @@ func (c *context) WriteHeader(code int) {
401359 c .outCode = code
402360}
403361
404- func (c * context ) post (body []byte , timeout time.Duration ) (b []byte , err error ) {
362+ func post (ctx netcontext.Context , body []byte , timeout time.Duration ) (b []byte , err error ) {
363+ apiURL := apiURL (ctx )
405364 hreq := & http.Request {
406365 Method : "POST" ,
407- URL : c . apiURL ,
366+ URL : apiURL ,
408367 Header : http.Header {
409368 apiEndpointHeader : apiEndpointHeaderValue ,
410369 apiMethodHeader : apiMethodHeaderValue ,
@@ -413,13 +372,16 @@ func (c *context) post(body []byte, timeout time.Duration) (b []byte, err error)
413372 },
414373 Body : ioutil .NopCloser (bytes .NewReader (body )),
415374 ContentLength : int64 (len (body )),
416- Host : c .apiURL .Host ,
417- }
418- if info := c .req .Header .Get (dapperHeader ); info != "" {
419- hreq .Header .Set (dapperHeader , info )
375+ Host : apiURL .Host ,
420376 }
421- if info := c .req .Header .Get (traceHeader ); info != "" {
422- hreq .Header .Set (traceHeader , info )
377+ c := fromContext (ctx )
378+ if c != nil {
379+ if info := c .req .Header .Get (dapperHeader ); info != "" {
380+ hreq .Header .Set (dapperHeader , info )
381+ }
382+ if info := c .req .Header .Get (traceHeader ); info != "" {
383+ hreq .Header .Set (traceHeader , info )
384+ }
423385 }
424386
425387 tr := apiHTTPClient .Transport .(* http.Transport )
@@ -480,10 +442,6 @@ func Call(ctx netcontext.Context, service, method string, in, out proto.Message)
480442 }
481443
482444 c := fromContext (ctx )
483- if c == nil {
484- // Give a good error message rather than a panic lower down.
485- return errNotAppEngineContext
486- }
487445
488446 // Apply transaction modifications if we're in a transaction.
489447 if t := transactionFromContext (ctx ); t != nil {
@@ -504,20 +462,13 @@ func Call(ctx netcontext.Context, service, method string, in, out proto.Message)
504462 return err
505463 }
506464
507- ticket := c . req . Header . Get ( ticketHeader )
508- // Use a test ticket under test environment.
509- if ticket == "" {
510- if appid := ctx . Value ( & appIDOverrideKey ); appid != nil {
511- ticket = appid .( string ) + defaultTicketSuffix
465+ ticket := ""
466+ if c != nil {
467+ ticket = c . req . Header . Get ( ticketHeader )
468+ if dri := c . req . Header . Get ( devRequestIdHeader ); IsDevAppServer () && dri != "" {
469+ ticket = dri
512470 }
513471 }
514- // Fall back to use background ticket when the request ticket is not available in Flex or dev_appserver.
515- if ticket == "" {
516- ticket = DefaultTicket ()
517- }
518- if dri := c .req .Header .Get (devRequestIdHeader ); IsDevAppServer () && dri != "" {
519- ticket = dri
520- }
521472 req := & remotepb.Request {
522473 ServiceName : & service ,
523474 Method : & method ,
@@ -529,7 +480,7 @@ func Call(ctx netcontext.Context, service, method string, in, out proto.Message)
529480 return err
530481 }
531482
532- hrespBody , err := c . post (hreqBody , timeout )
483+ hrespBody , err := post (ctx , hreqBody , timeout )
533484 if err != nil {
534485 return err
535486 }
0 commit comments