api_classic.go 3.0 KB

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