-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathHttpResponseStreamPrelude.cs
More file actions
95 lines (83 loc) · 3.78 KB
/
HttpResponseStreamPrelude.cs
File metadata and controls
95 lines (83 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#if NET8_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.Versioning;
using System.Text.Json;
namespace Amazon.Lambda.Core.ResponseStreaming
{
/// <summary>
/// The HTTP response prelude to be sent as the first chunk of a streaming response when using <see cref="LambdaResponseStreamFactory.CreateHttpStream"/>.
/// </summary>
[RequiresPreviewFeatures(LambdaResponseStreamFactory.PreviewMessage)]
public class HttpResponseStreamPrelude
{
/// <summary>
/// The Http status code to include in the response prelude.
/// </summary>
public HttpStatusCode? StatusCode { get; set; }
/// <summary>
/// The response headers to include in the response prelude. This collection supports setting single value for the same headers.
/// </summary>
public IDictionary<string, string> Headers { get; set; } = new Dictionary<string, string>();
/// <summary>
/// The response headers to include in the response prelude. This collection supports setting multiple values for the same headers.
/// </summary>
public IDictionary<string, IList<string>> MultiValueHeaders { get; set; } = new Dictionary<string, IList<string>>();
/// <summary>
/// The list of cookies to include in the response prelude. This is used for Lambda Function URL responses, which support a separate "cookies" field in the response JSON for setting cookies, rather than requiring cookies to be set via the "Set-Cookie" header.
/// </summary>
public IList<string> Cookies { get; set; } = new List<string>();
internal byte[] ToByteArray()
{
var bufferWriter = new System.Buffers.ArrayBufferWriter<byte>();
using (var writer = new Utf8JsonWriter(bufferWriter))
{
writer.WriteStartObject();
if (StatusCode.HasValue)
writer.WriteNumber("statusCode", (int)StatusCode);
if (Headers?.Count > 0)
{
writer.WriteStartObject("headers");
foreach (var header in Headers)
{
writer.WriteString(header.Key, header.Value);
}
writer.WriteEndObject();
}
if (MultiValueHeaders?.Count > 0)
{
writer.WriteStartObject("multiValueHeaders");
foreach (var header in MultiValueHeaders)
{
writer.WriteStartArray(header.Key);
foreach (var value in header.Value)
{
writer.WriteStringValue(value);
}
writer.WriteEndArray();
}
writer.WriteEndObject();
}
if (Cookies?.Count > 0)
{
writer.WriteStartArray("cookies");
foreach (var cookie in Cookies)
{
writer.WriteStringValue(cookie);
}
writer.WriteEndArray();
}
writer.WriteEndObject();
}
if (string.Equals(Environment.GetEnvironmentVariable("LAMBDA_NET_SERIALIZER_DEBUG"), "true", StringComparison.OrdinalIgnoreCase))
{
LambdaLogger.Log(LogLevel.Information, "HTTP Response Stream Prelude JSON: {Prelude}", System.Text.Encoding.UTF8.GetString(bufferWriter.WrittenSpan));
}
return bufferWriter.WrittenSpan.ToArray();
}
}
}
#endif