Skip to content
Permalink
Browse files Browse the repository at this point in the history
refactor: Add constants for HTTP Header field names
Added constants for HTTP Header field names

Set Content-Disposition header to attachment for Binary requests
  • Loading branch information
kennethmyhra committed May 10, 2021
1 parent a381902 commit 9c79320
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
15 changes: 15 additions & 0 deletions src/Spark.Engine/Core/HttpHeaderName.cs
@@ -0,0 +1,15 @@
namespace Spark.Engine.Core
{
internal static class HttpHeaderName
{
public const string ACCEPT = "Accept";
public const string CONTENT_DISPOSITION = "Content-Disposition";
public const string CONTENT_LOCATION = "Content-Location";
public const string CONTENT_TYPE = "Content-Type";
public const string ETAG = "ETag";
public const string LOCATION = "Location";
public const string LAST_MODIFIED = "Last-Modified";

public const string X_CONTENT_TYPE = "X-Content-Type";
}
}
12 changes: 8 additions & 4 deletions src/Spark.Engine/Extensions/HttpRequestFhirExtensions.cs
Expand Up @@ -158,17 +158,17 @@ internal static void AcquireHeaders(this HttpResponse response, FhirResponse fhi
{
if (fhirResponse.Key != null)
{
response.Headers.Add("ETag", ETag.Create(fhirResponse.Key.VersionId)?.ToString());
response.Headers.Add(HttpHeaderName.ETAG, ETag.Create(fhirResponse.Key.VersionId)?.ToString());

Uri location = fhirResponse.Key.ToUri();
response.Headers.Add("Location", location.OriginalString);
response.Headers.Add(HttpHeaderName.LOCATION, location.OriginalString);

if (response.Body != null)
{
response.Headers.Add("Content-Location", location.OriginalString);
response.Headers.Add(HttpHeaderName.CONTENT_LOCATION, location.OriginalString);
if (fhirResponse.Resource != null && fhirResponse.Resource.Meta != null)
{
response.Headers.Add("Last-Modified", fhirResponse.Resource.Meta.LastUpdated.Value.ToString("R"));
response.Headers.Add(HttpHeaderName.LAST_MODIFIED, fhirResponse.Resource.Meta.LastUpdated.Value.ToString("R"));
}
}
}
Expand All @@ -192,6 +192,10 @@ internal static void AcquireHeaders(this HttpResponseMessage response, FhirRespo
{
response.Content.Headers.LastModified = fhirResponse.Resource.Meta.LastUpdated;
}
if(fhirResponse.Resource is Binary)
{
response.Content.Headers.Add(HttpHeaderName.CONTENT_DISPOSITION, "attachment");
}
}
}
}
Expand Down
Expand Up @@ -37,6 +37,7 @@ public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext co
}
if (binary == null) return;

context.HttpContext.Response.Headers.Add(HttpHeaderName.CONTENT_DISPOSITION, "attachment");
context.HttpContext.Response.ContentType = binary.ContentType;

Stream stream = new MemoryStream(binary.Data);
Expand Down
8 changes: 4 additions & 4 deletions src/Spark.Engine/Handlers/NetCore/FormatTypeHandler.cs
Expand Up @@ -25,11 +25,11 @@ public async Task InvokeAsync(HttpContext context)
ResourceFormat accepted = ContentType.GetResourceFormatFromFormatParam(format);
if (accepted != ResourceFormat.Unknown)
{
if (context.Request.Headers.ContainsKey("Accept")) context.Request.Headers.Remove("Accept");
if (context.Request.Headers.ContainsKey(HttpHeaderName.ACCEPT)) context.Request.Headers.Remove(HttpHeaderName.ACCEPT);
if (accepted == ResourceFormat.Json)
context.Request.Headers.Add("Accept", new StringValues(ContentType.JSON_CONTENT_HEADER));
context.Request.Headers.Add(HttpHeaderName.ACCEPT, new StringValues(ContentType.JSON_CONTENT_HEADER));
else
context.Request.Headers.Add("Accept", new StringValues(ContentType.XML_CONTENT_HEADER));
context.Request.Headers.Add(HttpHeaderName.ACCEPT, new StringValues(ContentType.XML_CONTENT_HEADER));
}
}

Expand All @@ -38,7 +38,7 @@ public async Task InvokeAsync(HttpContext context)
if (!HttpRequestExtensions.IsContentTypeHeaderFhirMediaType(context.Request.ContentType))
{
string contentType = context.Request.ContentType;
context.Request.Headers.Add("X-Content-Type", contentType);
context.Request.Headers.Add(HttpHeaderName.X_CONTENT_TYPE, contentType);
context.Request.ContentType = FhirMediaType.OctetStreamMimeType;
}
}
Expand Down

0 comments on commit 9c79320

Please sign in to comment.