Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/common/protocolstate/context.go
2072 views
1
package protocolstate
2
3
import (
4
"context"
5
6
"github.com/rs/xid"
7
)
8
9
// contextKey is a type for context keys
10
type ContextKey string
11
12
type ExecutionContext struct {
13
ExecutionID string
14
}
15
16
// executionIDKey is the key used to store execution ID in context
17
const executionIDKey ContextKey = "execution_id"
18
19
// WithExecutionID adds an execution ID to the context
20
func WithExecutionID(ctx context.Context, executionContext *ExecutionContext) context.Context {
21
return context.WithValue(ctx, executionIDKey, executionContext)
22
}
23
24
// HasExecutionID checks if the context has an execution ID
25
func HasExecutionContext(ctx context.Context) bool {
26
_, ok := ctx.Value(executionIDKey).(*ExecutionContext)
27
return ok
28
}
29
30
// GetExecutionID retrieves the execution ID from the context
31
// Returns empty string if no execution ID is set
32
func GetExecutionContext(ctx context.Context) *ExecutionContext {
33
if id, ok := ctx.Value(executionIDKey).(*ExecutionContext); ok {
34
return id
35
}
36
return nil
37
}
38
39
// WithAutoExecutionContext creates a new context with an automatically generated execution ID
40
// If the input context already has an execution ID, it will be preserved
41
func WithAutoExecutionContext(ctx context.Context) context.Context {
42
if HasExecutionContext(ctx) {
43
return ctx
44
}
45
return WithExecutionID(ctx, &ExecutionContext{ExecutionID: xid.New().String()})
46
}
47
48