Skip to content

Commit 74446a3

Browse files
committed
Fix remote document conformance cases.
1 parent 2f3ca8e commit 74446a3

File tree

5 files changed

+75
-14
lines changed

5 files changed

+75
-14
lines changed

src/JsonLD/Core/Context.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,20 @@ public virtual JsonLD.Core.Context Parse(JToken localContext, List<string> remot
180180
}
181181
remoteContexts.Add(uri);
182182
// 3.2.3: Dereference context
183-
RemoteDocument rd = this.options.documentLoader.LoadDocument(uri);
183+
RemoteDocument rd;
184+
try
185+
{
186+
rd = this.options.documentLoader.LoadDocument(uri);
187+
}
188+
catch (JsonLdError err)
189+
{
190+
if (err.Message.StartsWith(JsonLdError.Error.LoadingDocumentFailed.ToString()))
191+
{
192+
throw new JsonLdError(JsonLdError.Error.LoadingRemoteContextFailed);
193+
}
194+
else
195+
throw;
196+
}
184197
JToken remoteContext = rd.document;
185198
if (!(remoteContext is JObject) || !((JObject)remoteContext
186199
).ContainsKey("@context"))

src/JsonLD/Core/DocumentLoader.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
22
using System.Collections;
33
using System.IO;
4+
using System.Linq;
45
using JsonLD.Core;
56
using JsonLD.Util;
7+
using System.Net;
8+
using System.Collections.Generic;
69

710
namespace JsonLD.Core
811
{
@@ -14,18 +17,52 @@ public virtual RemoteDocument LoadDocument(string url)
1417
RemoteDocument doc = new RemoteDocument(url, null);
1518
try
1619
{
17-
doc.Document = JSONUtils.FromURL(new Uri(url));
20+
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
21+
req.Accept = AcceptHeader;
22+
WebResponse resp = req.GetResponse();
23+
bool isJsonld = resp.Headers[HttpResponseHeader.ContentType] == "application/ld+json";
24+
if (!resp.Headers[HttpResponseHeader.ContentType].Contains("json"))
25+
{
26+
throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url);
27+
}
28+
29+
string[] linkHeaders = resp.Headers.GetValues("Link");
30+
if (!isJsonld && linkHeaders != null)
31+
{
32+
linkHeaders = linkHeaders.SelectMany((h) => h.Split(",".ToCharArray()))
33+
.Select(h => h.Trim()).ToArray();
34+
IEnumerable<string> linkedContexts = linkHeaders.Where(v => v.EndsWith("rel=\"http://www.w3.org/ns/json-ld#context\""));
35+
if (linkedContexts.Count() > 1)
36+
{
37+
throw new JsonLdError(JsonLdError.Error.MultipleContextLinkHeaders);
38+
}
39+
string header = linkedContexts.First();
40+
string linkedUrl = header.Substring(1, header.IndexOf(">") - 1);
41+
string resolvedUrl = URL.Resolve(url, linkedUrl);
42+
var remoteContext = this.LoadDocument(resolvedUrl);
43+
doc.contextUrl = remoteContext.documentUrl;
44+
doc.context = remoteContext.document;
45+
}
46+
47+
Stream stream = resp.GetResponseStream();
48+
49+
doc.DocumentUrl = req.Address.ToString();
50+
doc.Document = JSONUtils.FromInputStream(stream);
51+
}
52+
catch (JsonLdError)
53+
{
54+
throw;
1855
}
1956
catch (Exception)
2057
{
21-
throw new JsonLdError(JsonLdError.Error.LoadingRemoteContextFailed, url);
58+
throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url);
2259
}
2360
return doc;
2461
}
2562

26-
// /// <summary>An HTTP Accept header that prefers JSONLD.</summary>
27-
// /// <remarks>An HTTP Accept header that prefers JSONLD.</remarks>
28-
// public const string AcceptHeader = "application/ld+json, application/json;q=0.9, application/javascript;q=0.5, text/javascript;q=0.5, text/plain;q=0.2, */*;q=0.1";
63+
/// <summary>An HTTP Accept header that prefers JSONLD.</summary>
64+
/// <remarks>An HTTP Accept header that prefers JSONLD.</remarks>
65+
public const string AcceptHeader = "application/ld+json, application/json;q=0.9, application/javascript;q=0.5, text/javascript;q=0.5, text/plain;q=0.2, */*;q=0.1";
2966

3067
// private static volatile IHttpClient httpClient;
3168

src/JsonLD/Core/JsonLdProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static JArray Expand(JToken input, JsonLdOptions opts)
7171
{
7272
try
7373
{
74-
RemoteDocument tmp = null; // opts.documentLoader.LoadDocument((string)input);
74+
RemoteDocument tmp = opts.documentLoader.LoadDocument((string)input);
7575
input = tmp.document;
7676
}
7777
catch (Exception e)

src/JsonLD/Core/JsonLdUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ private static void Resolve(JToken input, JObject cycles)
924924
_cycles[url_1] = true;
925925
try
926926
{
927-
JObject ctx = null; //(IDictionary<string, object>)DocumentLoader.FromURL(new URL(url_1));
927+
JObject ctx = (JObject)new DocumentLoader().LoadDocument(url_1).Document;
928928
if (!ctx.ContainsKey("@context"))
929929
{
930930
ctx = new JObject();

src/JsonLD/Core/RemoteDocument.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ public virtual string DocumentUrl
1212
}
1313
set
1414
{
15-
string documentUrl = value;
16-
this.documentUrl = documentUrl;
15+
this.documentUrl = value;
1716
}
1817
}
1918

@@ -25,8 +24,7 @@ public virtual JToken Document
2524
}
2625
set
2726
{
28-
JToken document = value;
29-
this.document = document;
27+
this.document = value;
3028
}
3129
}
3230

@@ -38,17 +36,30 @@ public virtual string ContextUrl
3836
}
3937
set
4038
{
41-
string contextUrl = value;
42-
this.contextUrl = contextUrl;
39+
this.contextUrl = value;
4340
}
4441
}
4542

43+
public virtual JToken Context
44+
{
45+
get
46+
{
47+
return context;
48+
}
49+
set
50+
{
51+
this.context = value;
52+
}
53+
}
54+
4655
internal string documentUrl;
4756

4857
internal JToken document;
4958

5059
internal string contextUrl;
5160

61+
internal JToken context;
62+
5263
public RemoteDocument(string url, JToken document)
5364
: this(url, document, null)
5465
{

0 commit comments

Comments
 (0)