@@ -54,7 +54,7 @@ internal class WebBackgroundWorker : IBackgroundWorker
5454 public WebBackgroundWorker ( SentryUnityOptions options , SentryMonoBehaviour behaviour )
5555 {
5656 _behaviour = behaviour ;
57- _transport = new UnityWebRequestTransport ( options , behaviour ) ;
57+ _transport = new UnityWebRequestTransport ( options ) ;
5858 }
5959
6060 public bool EnqueueEnvelope ( Envelope envelope )
@@ -72,7 +72,7 @@ internal class UnityWebRequestTransport : HttpTransportBase
7272 {
7373 private readonly SentryUnityOptions _options ;
7474
75- public UnityWebRequestTransport ( SentryUnityOptions options , SentryMonoBehaviour behaviour )
75+ public UnityWebRequestTransport ( SentryUnityOptions options )
7676 : base ( options )
7777 {
7878 _options = options ;
@@ -85,7 +85,8 @@ internal IEnumerator SendEnvelopeAsync(Envelope envelope)
8585 if ( processedEnvelope . Items . Count > 0 )
8686 {
8787 // Send envelope to ingress
88- var www = CreateWebRequest ( CreateRequest ( processedEnvelope ) ) ;
88+ var httpRequest = CreateRequest ( processedEnvelope ) ;
89+ var www = CreateWebRequest ( httpRequest , processedEnvelope ) ;
8990 yield return www . SendWebRequest ( ) ;
9091
9192 var response = GetResponse ( www ) ;
@@ -96,28 +97,32 @@ internal IEnumerator SendEnvelopeAsync(Envelope envelope)
9697 }
9798 }
9899
99- private UnityWebRequest CreateWebRequest ( HttpRequestMessage message )
100+ private UnityWebRequest CreateWebRequest ( HttpRequestMessage message , Envelope envelope )
100101 {
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+ envelope . Serialize ( stream , _options . DiagnosticLogger ) ;
106+ stream . Flush ( ) ;
107+
108+ var www = new UnityWebRequest
109+ {
110+ url = message . RequestUri . ToString ( ) ,
111+ method = message . Method . Method . ToUpperInvariant ( ) ,
112+ uploadHandler = new UploadHandlerRaw ( stream . ToArray ( ) ) ,
113+ downloadHandler = new DownloadHandlerBuffer ( )
114+ } ;
104115
105116 foreach ( var header in message . Headers )
106117 {
107118 www . SetRequestHeader ( header . Key , string . Join ( "," , header . Value ) ) ;
108119 }
109120
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 ( ) ;
115121 return www ;
116122 }
117123
118124 private HttpResponseMessage ? GetResponse ( UnityWebRequest www )
119125 {
120-
121126 // if (www.result == UnityWebRequest.Result.ConnectionError) // unity 2021+
122127 if ( www . isNetworkError ) // Unity 2019
123128 {
@@ -134,7 +139,7 @@ private UnityWebRequest CreateWebRequest(HttpRequestMessage message)
134139 response . Headers . Add ( header . Key , header . Value ) ;
135140 }
136141 }
137- response . Content = new ExposedStringContent ( www . downloadHandler . text ) ;
142+ response . Content = new StringContent ( www . downloadHandler . text ) ;
138143 return response ;
139144 }
140145 }
@@ -147,29 +152,4 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage _, Can
147152 throw new InvalidOperationException ( "UnityWebRequestMessageHandler must be unused" ) ;
148153 }
149154 }
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- }
175155}
0 commit comments