4
4
using AngleSharp . Dom ;
5
5
using AngleSharp . Network ;
6
6
using System ;
7
+ using System . Collections . Generic ;
8
+ using System . IO ;
7
9
8
10
/// <summary>
9
11
/// Defines the XHR. For more information see:
13
15
[ DomExposed ( "Window" ) ]
14
16
[ DomExposed ( "DedicatedWorker" ) ]
15
17
[ DomExposed ( "SharedWorker" ) ]
16
- public sealed class XmlHttpRequest : XmlHttpRequestEventTarget
18
+ public sealed class XmlHttpRequest : XmlHttpRequestEventTarget , IRequest
17
19
{
18
20
#region Fields
19
21
22
+ readonly Dictionary < String , String > _headers ;
20
23
RequesterState _readyState ;
21
24
Int32 _timeout ;
22
25
Boolean _credentials ;
23
26
IResponse _response ;
24
27
HttpMethod _method ;
25
28
Url _url ;
26
29
Boolean _async ;
30
+ String _mime ;
31
+ Stream _body ;
27
32
28
33
#endregion
29
34
@@ -39,6 +44,7 @@ public XmlHttpRequest()
39
44
_method = HttpMethod . Get ;
40
45
_url = null ;
41
46
_response = null ;
47
+ _mime = null ;
42
48
_readyState = RequesterState . Unsent ;
43
49
_credentials = false ;
44
50
_timeout = 45000 ;
@@ -211,7 +217,8 @@ public void Open(String method, String url, Boolean async, String username = nul
211
217
[ DomName ( "send" ) ]
212
218
public void Send ( Object body = null )
213
219
{
214
- //TODO
220
+ _body = Stream . Null ;
221
+
215
222
}
216
223
217
224
/// <summary>
@@ -222,7 +229,7 @@ public void Send(Object body = null)
222
229
[ DomName ( "setRequestHeader" ) ]
223
230
public void SetRequestHeader ( String name , String value )
224
231
{
225
- //TODO
232
+ _headers [ name ] = value ;
226
233
}
227
234
228
235
/// <summary>
@@ -233,7 +240,11 @@ public void SetRequestHeader(String name, String value)
233
240
[ DomName ( "getResponseHeader" ) ]
234
241
public String GetResponseHeader ( String name )
235
242
{
236
- //TODO
243
+ var value = default ( String ) ;
244
+
245
+ if ( _response != null && _response . Headers . TryGetValue ( name , out value ) )
246
+ return value ;
247
+
237
248
return String . Empty ;
238
249
}
239
250
@@ -244,7 +255,18 @@ public String GetResponseHeader(String name)
244
255
[ DomName ( "getAllResponseHeaders" ) ]
245
256
public String GetAllResponseHeaders ( )
246
257
{
247
- //TODO
258
+ if ( _response != null )
259
+ {
260
+ var headers = _response . Headers ;
261
+ var lines = new String [ headers . Count ] ;
262
+ var index = 0 ;
263
+
264
+ foreach ( var header in headers )
265
+ lines [ index ++ ] = String . Concat ( header . Key , ": " , header . Value ) ;
266
+
267
+ return String . Join ( Environment . NewLine , lines ) ;
268
+ }
269
+
248
270
return String . Empty ;
249
271
}
250
272
@@ -255,7 +277,31 @@ public String GetAllResponseHeaders()
255
277
[ DomName ( "overrideMimeType" ) ]
256
278
public void OverrideMimeType ( String mime )
257
279
{
258
- //TODO
280
+ _mime = mime ;
281
+ }
282
+
283
+ #endregion
284
+
285
+ #region Request
286
+
287
+ Url IRequest . Address
288
+ {
289
+ get { return _url ; }
290
+ }
291
+
292
+ Stream IRequest . Content
293
+ {
294
+ get { return _body ; }
295
+ }
296
+
297
+ Dictionary < String , String > IRequest . Headers
298
+ {
299
+ get { return _headers ; }
300
+ }
301
+
302
+ HttpMethod IRequest . Method
303
+ {
304
+ get { return _method ; }
259
305
}
260
306
261
307
#endregion
0 commit comments