@@ -37,46 +37,45 @@ public BrowserInjectingStreamWrapper(IAgent agent, Stream baseStream, HttpContex
37
37
38
38
public override Task FlushAsync ( CancellationToken cancellationToken )
39
39
{
40
- if ( ! Disabled && ! _isContentLengthSet && IsHtmlResponse ( ) )
40
+ if ( _context is { Response : not null } && ! Disabled && ! _isContentLengthSet && IsHtmlResponse ( ) )
41
41
{
42
42
if ( ! _context . Response . HasStarted ) // can't set headers if response has already started
43
43
_context . Response . ContentLength = null ;
44
44
_isContentLengthSet = true ;
45
45
}
46
46
47
- return _baseStream . FlushAsync ( cancellationToken ) ;
47
+ return _baseStream ? . FlushAsync ( cancellationToken ) ?? Task . CompletedTask ;
48
48
}
49
49
50
- public override void Flush ( ) => _baseStream . Flush ( ) ;
50
+ public override void Flush ( ) => _baseStream ? . Flush ( ) ;
51
51
52
- public override int Read ( byte [ ] buffer , int offset , int count ) => _baseStream . Read ( buffer , offset , count ) ;
52
+ public override int Read ( byte [ ] buffer , int offset , int count ) => _baseStream ? . Read ( buffer , offset , count ) ?? 0 ;
53
53
54
- public override long Seek ( long offset , SeekOrigin origin ) => _baseStream . Seek ( offset , origin ) ;
54
+ public override long Seek ( long offset , SeekOrigin origin ) => _baseStream ? . Seek ( offset , origin ) ?? 0 ;
55
55
56
56
public override void SetLength ( long value )
57
57
{
58
- _baseStream . SetLength ( value ) ;
58
+ _baseStream ? . SetLength ( value ) ;
59
59
60
60
if ( ! Disabled )
61
61
IsHtmlResponse ( forceReCheck : true ) ;
62
62
}
63
63
64
- public override void Write ( ReadOnlySpan < byte > buffer ) => _baseStream . Write ( buffer ) ;
65
-
66
- public override void WriteByte ( byte value ) => _baseStream . WriteByte ( value ) ;
64
+ public override void Write ( ReadOnlySpan < byte > buffer ) => _baseStream ? . Write ( buffer ) ;
67
65
66
+ public override void WriteByte ( byte value ) => _baseStream ? . WriteByte ( value ) ;
68
67
69
68
public override void Write ( byte [ ] buffer , int offset , int count )
70
69
{
71
70
// pass through without modification if we're already in the middle of injecting
72
71
// don't inject if the response isn't an HTML response
73
- if ( ! Disabled && ! CurrentlyInjecting ( ) && IsHtmlResponse ( ) )
72
+ if ( _context != null && ! Disabled && ! CurrentlyInjecting ( ) && IsHtmlResponse ( ) )
74
73
{
75
74
try
76
75
{
77
76
// Set a flag on the context to indicate we're in the middle of injecting - prevents multiple recursions when response compression is in use
78
77
StartInjecting ( ) ;
79
- _agent . TryInjectBrowserScriptAsync ( _context . Response . ContentType , _context . Request . Path . Value , buffer , _baseStream )
78
+ _agent . TryInjectBrowserScriptAsync ( _context . Response ? . ContentType , _context . Request ? . Path , buffer , _baseStream )
80
79
. GetAwaiter ( ) . GetResult ( ) ;
81
80
}
82
81
finally
@@ -96,13 +95,13 @@ public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, Cancella
96
95
{
97
96
// pass through without modification if we're already in the middle of injecting
98
97
// don't inject if the response isn't an HTML response
99
- if ( ! Disabled && ! CurrentlyInjecting ( ) && IsHtmlResponse ( ) )
98
+ if ( _context != null & ! Disabled && ! CurrentlyInjecting ( ) && IsHtmlResponse ( ) )
100
99
{
101
100
try
102
101
{
103
102
// Set a flag on the context to indicate we're in the middle of injecting - prevents multiple recursions when response compression is in use
104
103
StartInjecting ( ) ;
105
- await _agent . TryInjectBrowserScriptAsync ( _context . Response . ContentType , _context . Request . Path . Value , buffer . ToArray ( ) , _baseStream ) ;
104
+ await _agent . TryInjectBrowserScriptAsync ( _context . Response ? . ContentType , _context . Request ? . Path , buffer . ToArray ( ) , _baseStream ) ;
106
105
}
107
106
finally
108
107
{
@@ -126,8 +125,11 @@ public override async ValueTask DisposeAsync()
126
125
{
127
126
_context = null ;
128
127
129
- await _baseStream . DisposeAsync ( ) ;
130
- _baseStream = null ;
128
+ if ( _baseStream != null )
129
+ {
130
+ await _baseStream . DisposeAsync ( ) ;
131
+ _baseStream = null ;
132
+ }
131
133
}
132
134
133
135
public override bool CanRead { get ; }
@@ -154,15 +156,16 @@ private bool IsHtmlResponse(bool forceReCheck = false)
154
156
// Requirements for script injection:
155
157
// * text/html response
156
158
// * UTF-8 formatted (either explicitly or no charset defined)
159
+ var responseContentType = _context . Response . ContentType ;
157
160
_isHtmlResponse =
158
- _context . Response . ContentType != null &&
159
- _context . Response . ContentType . Contains ( "text/html" , StringComparison . OrdinalIgnoreCase ) &&
160
- ( _context . Response . ContentType . Contains ( "utf-8" , StringComparison . OrdinalIgnoreCase ) ||
161
- ! _context . Response . ContentType . Contains ( "charset=" , StringComparison . OrdinalIgnoreCase ) ) ;
161
+ ! string . IsNullOrEmpty ( responseContentType ) &&
162
+ responseContentType . Contains ( "text/html" , StringComparison . OrdinalIgnoreCase ) &&
163
+ ( responseContentType . Contains ( "utf-8" , StringComparison . OrdinalIgnoreCase ) ||
164
+ ! responseContentType . Contains ( "charset=" , StringComparison . OrdinalIgnoreCase ) ) ;
162
165
163
166
if ( ! _isHtmlResponse . Value )
164
167
{
165
- _agent . CurrentTransaction ? . LogFinest ( $ "Skipping RUM injection: Not an HTML response. ContentType is { _context . Response . ContentType } ") ;
168
+ _agent . CurrentTransaction ? . LogFinest ( $ "Skipping RUM injection: Not an HTML response. ContentType is { responseContentType } ") ;
166
169
return false ;
167
170
}
168
171
@@ -186,8 +189,7 @@ private bool IsHtmlResponse(bool forceReCheck = false)
186
189
187
190
private void LogExceptionAndDisable ( Exception e )
188
191
{
189
- _agent . Logger . Log ( Level . Error ,
190
- $ "Unexpected exception. Browser injection will be disabled. Exception: { e . Message } : { e . StackTrace } ") ;
192
+ _agent . Logger . Log ( Level . Error , e , "Unexpected exception. Browser injection will be disabled." ) ;
191
193
192
194
Disabled = true ;
193
195
}
0 commit comments