Files
tasks/common/headers/headers.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

93 lines
3.0 KiB
Go

package headers
import (
"context"
"google.golang.org/grpc/metadata"
)
// Note the nexusoperations component references these headers and adds them to a list of disallowed headers for users to set.
// If any other headers are added for internal use, they should be added to the disallowed headers list.
const (
ClientNameHeaderName = "client-name"
ClientVersionHeaderName = "client-version"
SupportedServerVersionsHeaderName = "supported-server-versions"
SupportedFeaturesHeaderName = "supported-features"
SupportedFeaturesHeaderDelim = ","
CallerNameHeaderName = "caller-name"
CallerTypeHeaderName = "caller-type"
CallOriginHeaderName = "call-initiation"
)
var (
// propagateHeaders are the headers to propagate from the frontend to other services.
propagateHeaders = []string{
ClientNameHeaderName,
ClientVersionHeaderName,
SupportedServerVersionsHeaderName,
SupportedFeaturesHeaderName,
CallerNameHeaderName,
CallerTypeHeaderName,
CallOriginHeaderName,
}
)
// GetValues returns header values for passed header names.
// It always returns slice of the same size as number of passed header names.
func GetValues(ctx context.Context, headerNames ...string) []string {
headerValues := make([]string, len(headerNames))
for i, headerName := range headerNames {
if values := metadata.ValueFromIncomingContext(ctx, headerName); len(values) > 0 {
headerValues[i] = values[0]
}
}
return headerValues
}
// Propagate propagates version headers from incoming context to outgoing context.
// It copies all headers to outgoing context only if they are exist in incoming context
// and doesn't exist in outgoing context already.
func Propagate(ctx context.Context) context.Context {
headersToAppend := make([]string, 0, len(propagateHeaders)*2)
mdOutgoing, mdOutgoingExist := metadata.FromOutgoingContext(ctx)
for _, headerName := range propagateHeaders {
if incomingValue := metadata.ValueFromIncomingContext(ctx, headerName); len(incomingValue) > 0 && len(mdOutgoing.Get(headerName)) == 0 {
headersToAppend = append(headersToAppend, headerName, incomingValue[0])
}
}
if headersToAppend != nil {
if mdOutgoingExist {
ctx = metadata.AppendToOutgoingContext(ctx, headersToAppend...)
} else {
ctx = metadata.NewOutgoingContext(ctx, metadata.Pairs(headersToAppend...))
}
}
return ctx
}
// HeaderGetter is an interface for getting a single header value from a case insensitive key.
type HeaderGetter interface {
Get(string) string
}
// Wrapper for gRPC metadata that exposes a helper to extract a single metadata value.
type GRPCHeaderGetter struct {
ctx context.Context
}
func NewGRPCHeaderGetter(ctx context.Context) GRPCHeaderGetter {
return GRPCHeaderGetter{ctx: ctx}
}
// Get a single value from the underlying gRPC metadata.
// Returns an empty string if the metadata key is unset.
func (h GRPCHeaderGetter) Get(key string) string {
if values := metadata.ValueFromIncomingContext(h.ctx, key); len(values) > 0 {
return values[0]
}
return ""
}