@@ -54,7 +54,7 @@ internal class WebBackgroundWorker : IBackgroundWorker
54
54
public WebBackgroundWorker ( SentryUnityOptions options , SentryMonoBehaviour behaviour )
55
55
{
56
56
_behaviour = behaviour ;
57
- _transport = new UnityWebRequestTransport ( options , behaviour ) ;
57
+ _transport = new UnityWebRequestTransport ( options ) ;
58
58
}
59
59
60
60
public bool EnqueueEnvelope ( Envelope envelope )
@@ -72,7 +72,7 @@ internal class UnityWebRequestTransport : HttpTransportBase
72
72
{
73
73
private readonly SentryUnityOptions _options ;
74
74
75
- public UnityWebRequestTransport ( SentryUnityOptions options , SentryMonoBehaviour behaviour )
75
+ public UnityWebRequestTransport ( SentryUnityOptions options )
76
76
: base ( options )
77
77
{
78
78
_options = options ;
@@ -85,7 +85,8 @@ internal IEnumerator SendEnvelopeAsync(Envelope envelope)
85
85
if ( processedEnvelope . Items . Count > 0 )
86
86
{
87
87
// Send envelope to ingress
88
- var www = CreateWebRequest ( CreateRequest ( processedEnvelope ) ) ;
88
+ var httpRequest = CreateRequest ( processedEnvelope ) ;
89
+ var www = CreateWebRequest ( httpRequest , processedEnvelope ) ;
89
90
yield return www . SendWebRequest ( ) ;
90
91
91
92
var response = GetResponse ( www ) ;
@@ -96,28 +97,40 @@ internal IEnumerator SendEnvelopeAsync(Envelope envelope)
96
97
}
97
98
}
98
99
99
- private UnityWebRequest CreateWebRequest ( HttpRequestMessage message )
100
+ private UnityWebRequest CreateWebRequest ( HttpRequestMessage message , Envelope envelope )
100
101
{
101
- var www = new UnityWebRequest ( ) ;
102
- www . url = message . RequestUri . ToString ( ) ;
103
- www . method = message . Method . Method . ToUpperInvariant ( ) ;
102
+ // Note: In order to use the synchronous Envelope.Serialize() we ignore the `message.Content`
103
+ // which is an `EnvelopeHttpContent` instance and use the actual envelope it wraps.
104
+ var stream = new MemoryStream ( ) ;
105
+ try
106
+ {
107
+ envelope . Serialize ( stream , _options . DiagnosticLogger ) ;
108
+ stream . Flush ( ) ;
109
+ }
110
+ catch ( Exception e )
111
+ {
112
+ _options . DiagnosticLogger ? . LogError ( "Failed to serialize Envelope into the network stream" , e ) ;
113
+ throw ;
114
+ }
115
+
116
+ var www = new UnityWebRequest
117
+ {
118
+ url = message . RequestUri . ToString ( ) ,
119
+ method = message . Method . Method . ToUpperInvariant ( ) ,
120
+ uploadHandler = new UploadHandlerRaw ( stream . ToArray ( ) ) ,
121
+ downloadHandler = new DownloadHandlerBuffer ( )
122
+ } ;
104
123
105
124
foreach ( var header in message . Headers )
106
125
{
107
126
www . SetRequestHeader ( header . Key , string . Join ( "," , header . Value ) ) ;
108
127
}
109
128
110
- var stream = new MemoryStream ( ) ;
111
- _ = message . Content . CopyToAsync ( stream ) . Wait ( 2000 ) ;
112
- stream . Flush ( ) ;
113
- www . uploadHandler = new UploadHandlerRaw ( stream . ToArray ( ) ) ;
114
- www . downloadHandler = new DownloadHandlerBuffer ( ) ;
115
129
return www ;
116
130
}
117
131
118
132
private HttpResponseMessage ? GetResponse ( UnityWebRequest www )
119
133
{
120
-
121
134
// if (www.result == UnityWebRequest.Result.ConnectionError) // unity 2021+
122
135
if ( www . isNetworkError ) // Unity 2019
123
136
{
@@ -134,7 +147,7 @@ private UnityWebRequest CreateWebRequest(HttpRequestMessage message)
134
147
response . Headers . Add ( header . Key , header . Value ) ;
135
148
}
136
149
}
137
- response . Content = new ExposedStringContent ( www . downloadHandler . text ) ;
150
+ response . Content = new StringContent ( www . downloadHandler . text ) ;
138
151
return response ;
139
152
}
140
153
}
@@ -147,29 +160,4 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage _, Can
147
160
throw new InvalidOperationException ( "UnityWebRequestMessageHandler must be unused" ) ;
148
161
}
149
162
}
150
-
151
- internal class ExposedStringContent : StringContent
152
- {
153
- internal readonly String Content ;
154
- public ExposedStringContent ( String data ) : base ( data ) => Content = data ;
155
- }
156
-
157
- internal static class JsonExtensions
158
- {
159
- public static JsonElement ? GetPropertyOrNull ( this JsonElement json , string name )
160
- {
161
- if ( json . ValueKind != JsonValueKind . Object )
162
- {
163
- return null ;
164
- }
165
-
166
- if ( json . TryGetProperty ( name , out var result ) &&
167
- result . ValueKind is not JsonValueKind . Undefined and not JsonValueKind . Null )
168
- {
169
- return result ;
170
- }
171
-
172
- return null ;
173
- }
174
- }
175
163
}
0 commit comments