|
| 1 | +using Sentry.Extensibility; |
| 2 | +using Sentry.Internal; |
| 3 | +using Sentry.Internal.Extensions; |
| 4 | + |
| 5 | +namespace Sentry.Protocol; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Sentry Response context interface. |
| 9 | +/// </summary> |
| 10 | +/// <example> |
| 11 | +///{ |
| 12 | +/// "contexts": { |
| 13 | +/// "response": { |
| 14 | +/// "cookies": "PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;", |
| 15 | +/// "headers": { |
| 16 | +/// "content-type": "text/html" |
| 17 | +/// /// ... |
| 18 | +/// }, |
| 19 | +/// "status_code": 500, |
| 20 | +/// "body_size": 1000, // in bytes |
| 21 | +/// } |
| 22 | +/// } |
| 23 | +///} |
| 24 | +/// </example> |
| 25 | +/// <see href="https://develop.sentry.dev/sdk/event-payloads/types/#responsecontext"/> |
| 26 | +public sealed class Response : IJsonSerializable, ICloneable<Response>, IUpdatable<Response> |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Tells Sentry which type of context this is. |
| 30 | + /// </summary> |
| 31 | + public const string Type = "response"; |
| 32 | + |
| 33 | + internal Dictionary<string, string>? InternalHeaders { get; private set; } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets or sets the HTTP response body size. |
| 37 | + /// </summary> |
| 38 | + public long? BodySize { get; set; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Gets or sets (optional) cookie values |
| 42 | + /// </summary> |
| 43 | + public string? Cookies { get; set; } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Gets or sets the headers. |
| 47 | + /// </summary> |
| 48 | + /// <remarks> |
| 49 | + /// If a header appears multiple times it needs to be merged according to the HTTP standard for header merging. |
| 50 | + /// </remarks> |
| 51 | + public IDictionary<string, string> Headers => InternalHeaders ??= new Dictionary<string, string>(); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets or sets the HTTP Status response code |
| 55 | + /// </summary> |
| 56 | + /// <value>The HTTP method.</value> |
| 57 | + public short? StatusCode { get; set; } |
| 58 | + |
| 59 | + internal void AddHeaders(IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers) |
| 60 | + { |
| 61 | + foreach (var header in headers) |
| 62 | + { |
| 63 | + Headers.Add( |
| 64 | + header.Key, |
| 65 | + string.Join("; ", header.Value) |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Clones this instance. |
| 72 | + /// </summary> |
| 73 | + public Response Clone() |
| 74 | + { |
| 75 | + var response = new Response(); |
| 76 | + |
| 77 | + response.UpdateFrom(this); |
| 78 | + |
| 79 | + return response; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Updates this instance with data from the properties in the <paramref name="source"/>, |
| 84 | + /// unless there is already a value in the existing property. |
| 85 | + /// </summary> |
| 86 | + public void UpdateFrom(Response source) |
| 87 | + { |
| 88 | + BodySize ??= source.BodySize; |
| 89 | + Cookies ??= source.Cookies; |
| 90 | + StatusCode ??= source.StatusCode; |
| 91 | + source.InternalHeaders?.TryCopyTo(Headers); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Updates this instance with data from the properties in the <paramref name="source"/>, |
| 96 | + /// unless there is already a value in the existing property. |
| 97 | + /// </summary> |
| 98 | + public void UpdateFrom(object source) |
| 99 | + { |
| 100 | + if (source is Response response) |
| 101 | + { |
| 102 | + UpdateFrom(response); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <inheritdoc /> |
| 107 | + public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger) |
| 108 | + { |
| 109 | + writer.WriteStartObject(); |
| 110 | + |
| 111 | + writer.WriteString("type", Type); |
| 112 | + writer.WriteNumberIfNotNull("body_size", BodySize); |
| 113 | + writer.WriteStringIfNotWhiteSpace("cookies", Cookies); |
| 114 | + writer.WriteStringDictionaryIfNotEmpty("headers", InternalHeaders!); |
| 115 | + writer.WriteNumberIfNotNull("status_code", StatusCode); |
| 116 | + |
| 117 | + writer.WriteEndObject(); |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Parses from JSON. |
| 122 | + /// </summary> |
| 123 | + public static Response FromJson(JsonElement json) |
| 124 | + { |
| 125 | + var bodySize = json.GetPropertyOrNull("body_size")?.GetInt64(); |
| 126 | + var cookies = json.GetPropertyOrNull("cookies")?.GetString(); |
| 127 | + var headers = json.GetPropertyOrNull("headers")?.GetStringDictionaryOrNull(); |
| 128 | + var statusCode = json.GetPropertyOrNull("status_code")?.GetInt16(); |
| 129 | + |
| 130 | + return new Response |
| 131 | + { |
| 132 | + BodySize = bodySize, |
| 133 | + Cookies = cookies, |
| 134 | + InternalHeaders = headers?.WhereNotNullValue().ToDictionary(), |
| 135 | + StatusCode = statusCode |
| 136 | + }; |
| 137 | + } |
| 138 | +} |
0 commit comments