Skip to content

Commit d714906

Browse files
committed
Implemented some methods #14
1 parent 62d0284 commit d714906

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

AngleSharp.Scripting.JavaScript/Dom/XmlHttpRequest.cs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using AngleSharp.Dom;
55
using AngleSharp.Network;
66
using System;
7+
using System.Collections.Generic;
8+
using System.IO;
79

810
/// <summary>
911
/// Defines the XHR. For more information see:
@@ -13,17 +15,20 @@
1315
[DomExposed("Window")]
1416
[DomExposed("DedicatedWorker")]
1517
[DomExposed("SharedWorker")]
16-
public sealed class XmlHttpRequest : XmlHttpRequestEventTarget
18+
public sealed class XmlHttpRequest : XmlHttpRequestEventTarget, IRequest
1719
{
1820
#region Fields
1921

22+
readonly Dictionary<String, String> _headers;
2023
RequesterState _readyState;
2124
Int32 _timeout;
2225
Boolean _credentials;
2326
IResponse _response;
2427
HttpMethod _method;
2528
Url _url;
2629
Boolean _async;
30+
String _mime;
31+
Stream _body;
2732

2833
#endregion
2934

@@ -39,6 +44,7 @@ public XmlHttpRequest()
3944
_method = HttpMethod.Get;
4045
_url = null;
4146
_response = null;
47+
_mime = null;
4248
_readyState = RequesterState.Unsent;
4349
_credentials = false;
4450
_timeout = 45000;
@@ -211,7 +217,8 @@ public void Open(String method, String url, Boolean async, String username = nul
211217
[DomName("send")]
212218
public void Send(Object body = null)
213219
{
214-
//TODO
220+
_body = Stream.Null;
221+
215222
}
216223

217224
/// <summary>
@@ -222,7 +229,7 @@ public void Send(Object body = null)
222229
[DomName("setRequestHeader")]
223230
public void SetRequestHeader(String name, String value)
224231
{
225-
//TODO
232+
_headers[name] = value;
226233
}
227234

228235
/// <summary>
@@ -233,7 +240,11 @@ public void SetRequestHeader(String name, String value)
233240
[DomName("getResponseHeader")]
234241
public String GetResponseHeader(String name)
235242
{
236-
//TODO
243+
var value = default(String);
244+
245+
if (_response != null && _response.Headers.TryGetValue(name, out value))
246+
return value;
247+
237248
return String.Empty;
238249
}
239250

@@ -244,7 +255,18 @@ public String GetResponseHeader(String name)
244255
[DomName("getAllResponseHeaders")]
245256
public String GetAllResponseHeaders()
246257
{
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+
248270
return String.Empty;
249271
}
250272

@@ -255,7 +277,31 @@ public String GetAllResponseHeaders()
255277
[DomName("overrideMimeType")]
256278
public void OverrideMimeType(String mime)
257279
{
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; }
259305
}
260306

261307
#endregion

0 commit comments

Comments
 (0)