@@ -121,69 +121,75 @@ internal unsafe IISHttpContext(
121
121
122
122
protected void InitializeContext ( )
123
123
{
124
- _thisHandle = GCHandle . Alloc ( this ) ;
124
+ // create a memory barrier between initialize and disconnect to prevent a possible
125
+ // NullRef with disconnect being called before these fields have been written
126
+ // disconnect aquires this lock as well
127
+ lock ( _abortLock )
128
+ {
129
+ _thisHandle = GCHandle . Alloc ( this ) ;
125
130
126
- Method = GetVerb ( ) ;
131
+ Method = GetVerb ( ) ;
127
132
128
- RawTarget = GetRawUrl ( ) ;
129
- // TODO version is slow.
130
- HttpVersion = GetVersion ( ) ;
131
- Scheme = SslStatus != SslStatus . Insecure ? Constants . HttpsScheme : Constants . HttpScheme ;
132
- KnownMethod = VerbId ;
133
- StatusCode = 200 ;
133
+ RawTarget = GetRawUrl ( ) ;
134
+ // TODO version is slow.
135
+ HttpVersion = GetVersion ( ) ;
136
+ Scheme = SslStatus != SslStatus . Insecure ? Constants . HttpsScheme : Constants . HttpScheme ;
137
+ KnownMethod = VerbId ;
138
+ StatusCode = 200 ;
134
139
135
- var originalPath = GetOriginalPath ( ) ;
140
+ var originalPath = GetOriginalPath ( ) ;
136
141
137
- if ( KnownMethod == HttpApiTypes . HTTP_VERB . HttpVerbOPTIONS && string . Equals ( RawTarget , "*" , StringComparison . Ordinal ) )
138
- {
139
- PathBase = string . Empty ;
140
- Path = string . Empty ;
141
- }
142
- else
143
- {
144
- // Path and pathbase are unescaped by RequestUriBuilder
145
- // The UsePathBase middleware will modify the pathbase and path correctly
146
- PathBase = string . Empty ;
147
- Path = originalPath ;
148
- }
142
+ if ( KnownMethod == HttpApiTypes . HTTP_VERB . HttpVerbOPTIONS && string . Equals ( RawTarget , "*" , StringComparison . Ordinal ) )
143
+ {
144
+ PathBase = string . Empty ;
145
+ Path = string . Empty ;
146
+ }
147
+ else
148
+ {
149
+ // Path and pathbase are unescaped by RequestUriBuilder
150
+ // The UsePathBase middleware will modify the pathbase and path correctly
151
+ PathBase = string . Empty ;
152
+ Path = originalPath ;
153
+ }
149
154
150
- var cookedUrl = GetCookedUrl ( ) ;
151
- QueryString = cookedUrl . GetQueryString ( ) ?? string . Empty ;
155
+ var cookedUrl = GetCookedUrl ( ) ;
156
+ QueryString = cookedUrl . GetQueryString ( ) ?? string . Empty ;
152
157
153
- RequestHeaders = new RequestHeaders ( this ) ;
154
- HttpResponseHeaders = new HeaderCollection ( ) ;
155
- ResponseHeaders = HttpResponseHeaders ;
158
+ RequestHeaders = new RequestHeaders ( this ) ;
159
+ HttpResponseHeaders = new HeaderCollection ( ) ;
160
+ ResponseHeaders = HttpResponseHeaders ;
156
161
157
- if ( _options . ForwardWindowsAuthentication )
158
- {
159
- WindowsUser = GetWindowsPrincipal ( ) ;
160
- if ( _options . AutomaticAuthentication )
162
+ if ( _options . ForwardWindowsAuthentication )
161
163
{
162
- User = WindowsUser ;
164
+ WindowsUser = GetWindowsPrincipal ( ) ;
165
+ if ( _options . AutomaticAuthentication )
166
+ {
167
+ User = WindowsUser ;
168
+ }
163
169
}
164
- }
165
170
166
- MaxRequestBodySize = _options . MaxRequestBodySize ;
171
+ MaxRequestBodySize = _options . MaxRequestBodySize ;
167
172
168
- ResetFeatureCollection ( ) ;
173
+ ResetFeatureCollection ( ) ;
169
174
170
- if ( ! _server . IsWebSocketAvailable ( _pInProcessHandler ) )
171
- {
172
- _currentIHttpUpgradeFeature = null ;
173
- }
175
+ if ( ! _server . IsWebSocketAvailable ( _pInProcessHandler ) )
176
+ {
177
+ _currentIHttpUpgradeFeature = null ;
178
+ }
174
179
175
- _streams = new Streams ( this ) ;
180
+ _streams = new Streams ( this ) ;
176
181
177
- ( RequestBody , ResponseBody ) = _streams . Start ( ) ;
182
+ ( RequestBody , ResponseBody ) = _streams . Start ( ) ;
178
183
179
- var pipe = new Pipe (
180
- new PipeOptions (
181
- _memoryPool ,
182
- readerScheduler : PipeScheduler . ThreadPool ,
183
- pauseWriterThreshold : PauseWriterThreshold ,
184
- resumeWriterThreshold : ResumeWriterTheshold ,
185
- minimumSegmentSize : MinAllocBufferSize ) ) ;
186
- _bodyOutput = new OutputProducer ( pipe ) ;
184
+ var pipe = new Pipe (
185
+ new PipeOptions (
186
+ _memoryPool ,
187
+ readerScheduler : PipeScheduler . ThreadPool ,
188
+ pauseWriterThreshold : PauseWriterThreshold ,
189
+ resumeWriterThreshold : ResumeWriterTheshold ,
190
+ minimumSegmentSize : MinAllocBufferSize ) ) ;
191
+ _bodyOutput = new OutputProducer ( pipe ) ;
192
+ }
187
193
188
194
NativeMethods . HttpSetManagedContext ( _pInProcessHandler , ( IntPtr ) _thisHandle ) ;
189
195
}
0 commit comments