api_classic.go 3.7 KB

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