• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2011 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
5// +build !appengine
6
7package internal
8
9import (
10	"net/http"
11	"os"
12
13	netcontext "golang.org/x/net/context"
14)
15
16// These functions are implementations of the wrapper functions
17// in ../appengine/identity.go. See that file for commentary.
18
19const (
20	hDefaultVersionHostname = "X-AppEngine-Default-Version-Hostname"
21	hRequestLogId           = "X-AppEngine-Request-Log-Id"
22	hDatacenter             = "X-AppEngine-Datacenter"
23)
24
25func ctxHeaders(ctx netcontext.Context) http.Header {
26	c := fromContext(ctx)
27	if c == nil {
28		return nil
29	}
30	return c.Request().Header
31}
32
33func DefaultVersionHostname(ctx netcontext.Context) string {
34	return ctxHeaders(ctx).Get(hDefaultVersionHostname)
35}
36
37func RequestID(ctx netcontext.Context) string {
38	return ctxHeaders(ctx).Get(hRequestLogId)
39}
40
41func Datacenter(ctx netcontext.Context) string {
42	return ctxHeaders(ctx).Get(hDatacenter)
43}
44
45func ServerSoftware() string {
46	// TODO(dsymonds): Remove fallback when we've verified this.
47	if s := os.Getenv("SERVER_SOFTWARE"); s != "" {
48		return s
49	}
50	return "Google App Engine/1.x.x"
51}
52
53// TODO(dsymonds): Remove the metadata fetches.
54
55func ModuleName(_ netcontext.Context) string {
56	if s := os.Getenv("GAE_MODULE_NAME"); s != "" {
57		return s
58	}
59	return string(mustGetMetadata("instance/attributes/gae_backend_name"))
60}
61
62func VersionID(_ netcontext.Context) string {
63	if s1, s2 := os.Getenv("GAE_MODULE_VERSION"), os.Getenv("GAE_MINOR_VERSION"); s1 != "" && s2 != "" {
64		return s1 + "." + s2
65	}
66	return string(mustGetMetadata("instance/attributes/gae_backend_version")) + "." + string(mustGetMetadata("instance/attributes/gae_backend_minor_version"))
67}
68
69func InstanceID() string {
70	if s := os.Getenv("GAE_MODULE_INSTANCE"); s != "" {
71		return s
72	}
73	return string(mustGetMetadata("instance/attributes/gae_backend_instance"))
74}
75
76func partitionlessAppID() string {
77	// gae_project has everything except the partition prefix.
78	appID := os.Getenv("GAE_LONG_APP_ID")
79	if appID == "" {
80		appID = string(mustGetMetadata("instance/attributes/gae_project"))
81	}
82	return appID
83}
84
85func fullyQualifiedAppID(_ netcontext.Context) string {
86	appID := partitionlessAppID()
87
88	part := os.Getenv("GAE_PARTITION")
89	if part == "" {
90		part = string(mustGetMetadata("instance/attributes/gae_partition"))
91	}
92
93	if part != "" {
94		appID = part + "~" + appID
95	}
96	return appID
97}
98
99func IsDevAppServer() bool {
100	return os.Getenv("RUN_WITH_DEVAPPSERVER") != ""
101}
102