api_classic.go 3.9 KB

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