-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Profile field support to HTTP ContentType (resolves #127) #174
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,10 @@ public ActivityPubClient(ActivityPubOptions apOptions, IJsonLdSerializer jsonLdS | |
// https://stackoverflow.com/questions/47176104/c-sharp-add-accept-header-to-httpclient | ||
foreach (var mimeType in apOptions.RequestContentTypes) | ||
{ | ||
var mediaType = new MediaTypeWithQualityHeaderValue(mimeType); | ||
var mediaType = new MediaTypeWithQualityHeaderValue(mimeType.MediaType); | ||
if (mimeType.Profile != "") { | ||
mediaType.Parameters.Add(new NameValueHeaderValue("profile", mimeType.Profile)); | ||
} | ||
_httpClient.DefaultRequestHeaders.Accept.Add(mediaType); | ||
} | ||
} | ||
|
@@ -83,9 +86,9 @@ private async Task<ASType> Get(Uri uri, Type targetType, int? maxRecursion, Canc | |
if (!resp.IsSuccessStatusCode) | ||
throw new ApplicationException($"Request failed: got status {resp.StatusCode}"); | ||
|
||
var mediaType = resp.Content.Headers.ContentType?.MediaType; | ||
if (mediaType == null || !_apOptions.ResponseContentTypes.Contains(mediaType)) | ||
throw new ApplicationException($"Request failed: unsupported content type {mediaType}"); | ||
var contentType = resp.Content.Headers.ContentType; | ||
if (contentType == null || !_apOptions.ResponseContentTypes.Any(expectedContentType => IsContentTypeMatch(contentType, expectedContentType))) | ||
throw new ApplicationException($"Request failed: unsupported content type {contentType?.MediaType}"); | ||
|
||
var json = await resp.Content.ReadAsStringAsync(cancellationToken); | ||
var jsonObj = _jsonLdSerializer.Deserialize(json, targetType); | ||
|
@@ -100,6 +103,17 @@ private async Task<ASType> Get(Uri uri, Type targetType, int? maxRecursion, Canc | |
return obj; | ||
} | ||
|
||
private static bool IsContentTypeMatch(MediaTypeHeaderValue actual, ActivityPubOptions.ContentType expected) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we move this logic to the equality method in |
||
if (!expected.MediaType.Equals(actual.MediaType)) { | ||
return false; | ||
} | ||
|
||
var actualProfile = actual.Parameters.FirstOrDefault(p => p.Name.Equals("profile"))?.Value ?? ""; | ||
|
||
return string.Equals(actualProfile, expected.Profile, | ||
StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
|
||
#region Dispose | ||
|
||
|
@@ -130,4 +144,4 @@ protected virtual void Dispose(bool disposing) | |
private bool _disposed; | ||
|
||
#endregion | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,22 +13,31 @@ public class ActivityPubOptions | |
/// This maps to the Content-Type header. | ||
/// </summary> | ||
/// <seealso href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type" /> | ||
public HashSet<string> ResponseContentTypes { get; set; } = | ||
public HashSet<ContentType> ResponseContentTypes { get; set; } = | ||
[ | ||
"application/activity+json", | ||
"application/ld+json", | ||
"application/json" | ||
new ContentType("application/ld+json", "https://www.w3.org/ns/activitystreams"), | ||
new ContentType("application/activity+json", ""), | ||
new ContentType("application/ld+json", ""), | ||
new ContentType("application/json", "") | ||
]; | ||
|
||
/// <summary> | ||
/// Content types to request from remote servers, in priority order. | ||
/// This maps to the Accept header. | ||
/// </summary> | ||
/// <seealso href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept" /> | ||
public List<string> RequestContentTypes { get; set; } = | ||
public List<ContentType> RequestContentTypes { get; set; } = | ||
[ | ||
"application/activity+json", | ||
"application/ld+json", | ||
"application/json" | ||
new ContentType("application/ld+json", "https://www.w3.org/ns/activitystreams"), | ||
new ContentType("application/activity+json", ""), | ||
new ContentType("application/ld+json", ""), | ||
new ContentType("application/json", "") | ||
]; | ||
} | ||
|
||
/// <summary> | ||
/// A Record that contains the MediaType and Profile for the HTTP Content-Type and Accept Headers | ||
/// </summary> | ||
/// <param name="MediaType">A MIME type for the header</param> | ||
/// <param name="Profile">A profile to append to the MIME type, leave empty for none</param> | ||
public record struct ContentType(string MediaType, string Profile); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we make Profile optional, with a default value of |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we move this logic to an implicit cast in
ContentType
?