api_classic.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2015 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. //go:build appengine
  5. // +build appengine
  6. package internal
  7. import (
  8. "context"
  9. "errors"
  10. "fmt"
  11. "net/http"
  12. "time"
  13. "appengine"
  14. "appengine_internal"
  15. basepb "appengine_internal/base"
  16. "github.com/golang/protobuf/proto"
  17. )
  18. var contextKey = "holds an appengine.Context"
  19. // fromContext returns the App Engine context or nil if ctx is not
  20. // derived from an App Engine context.
  21. func fromContext(ctx context.Context) appengine.Context {
  22. c, _ := ctx.Value(&contextKey).(appengine.Context)
  23. return c
  24. }
  25. // This is only for classic App Engine adapters.
  26. func ClassicContextFromContext(ctx context.Context) (appengine.Context, error) {
  27. c := fromContext(ctx)
  28. if c == nil {
  29. return nil, errNotAppEngineContext
  30. }
  31. return c, nil
  32. }
  33. func withContext(parent context.Context, c appengine.Context) context.Context {
  34. ctx := context.WithValue(parent, &contextKey, c)
  35. s := &basepb.StringProto{}
  36. c.Call("__go__", "GetNamespace", &basepb.VoidProto{}, s, nil)
  37. if ns := s.GetValue(); ns != "" {
  38. ctx = NamespacedContext(ctx, ns)
  39. }
  40. return ctx
  41. }
  42. func IncomingHeaders(ctx context.Context) http.Header {
  43. if c := fromContext(ctx); c != nil {
  44. if req, ok := c.Request().(*http.Request); ok {
  45. return req.Header
  46. }
  47. }
  48. return nil
  49. }
  50. func ReqContext(req *http.Request) context.Context {
  51. return WithContext(context.Background(), req)
  52. }
  53. func WithContext(parent context.Context, req *http.Request) context.Context {
  54. c := appengine.NewContext(req)
  55. return withContext(parent, c)
  56. }
  57. type testingContext struct {
  58. appengine.Context
  59. req *http.Request
  60. }
  61. func (t *testingContext) FullyQualifiedAppID() string { return "dev~testcontext" }
  62. func (t *testingContext) Call(service, method string, _, _ appengine_internal.ProtoMessage, _ *appengine_internal.CallOptions) error {
  63. if service == "__go__" && method == "GetNamespace" {
  64. return nil
  65. }
  66. return fmt.Errorf("testingContext: unsupported Call")
  67. }
  68. func (t *testingContext) Request() interface{} { return t.req }
  69. func ContextForTesting(req *http.Request) context.Context {
  70. return withContext(context.Background(), &testingContext{req: req})
  71. }
  72. func Call(ctx context.Context, service, method string, in, out proto.Message) error {
  73. if ns := NamespaceFromContext(ctx); ns != "" {
  74. if fn, ok := NamespaceMods[service]; ok {
  75. fn(in, ns)
  76. }
  77. }
  78. if f, ctx, ok := callOverrideFromContext(ctx); ok {
  79. return f(ctx, service, method, in, out)
  80. }
  81. // Handle already-done contexts quickly.
  82. select {
  83. case <-ctx.Done():
  84. return ctx.Err()
  85. default:
  86. }
  87. c := fromContext(ctx)
  88. if c == nil {
  89. // Give a good error message rather than a panic lower down.
  90. return errNotAppEngineContext
  91. }
  92. // Apply transaction modifications if we're in a transaction.
  93. if t := transactionFromContext(ctx); t != nil {
  94. if t.finished {
  95. return errors.New("transaction context has expired")
  96. }
  97. applyTransaction(in, &t.transaction)
  98. }
  99. var opts *appengine_internal.CallOptions
  100. if d, ok := ctx.Deadline(); ok {
  101. opts = &appengine_internal.CallOptions{
  102. Timeout: d.Sub(time.Now()),
  103. }
  104. }
  105. err := c.Call(service, method, in, out, opts)
  106. switch v := err.(type) {
  107. case *appengine_internal.APIError:
  108. return &APIError{
  109. Service: v.Service,
  110. Detail: v.Detail,
  111. Code: v.Code,
  112. }
  113. case *appengine_internal.CallError:
  114. return &CallError{
  115. Detail: v.Detail,
  116. Code: v.Code,
  117. Timeout: v.Timeout,
  118. }
  119. }
  120. return err
  121. }
  122. func Middleware(next http.Handler) http.Handler {
  123. panic("Middleware called; this should be impossible")
  124. }
  125. func logf(c appengine.Context, level int64, format string, args ...interface{}) {
  126. var fn func(format string, args ...interface{})
  127. switch level {
  128. case 0:
  129. fn = c.Debugf
  130. case 1:
  131. fn = c.Infof
  132. case 2:
  133. fn = c.Warningf
  134. case 3:
  135. fn = c.Errorf
  136. case 4:
  137. fn = c.Criticalf
  138. default:
  139. // This shouldn't happen.
  140. fn = c.Criticalf
  141. }
  142. fn(format, args...)
  143. }