danieleteti / delphimvcframework

DMVCFramework (for short) is a popular and powerful framework for WEB API in Delphi. Supports RESTful and JSON-RPC WEB APIs development.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Response Content-Type does not allow for optional parameters

HealthOneNZ opened this issue · comments

The http specification allows for optional parameters to be added to the Content-Type response header
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types

The form of the header is
type/subtype;parameter=value

I wish to add a header to a response of "application/fhir+xml; fhirVersion=4.0"

The procedure SplitContentMediaTypeAndCharset which is called by the Render method discards all parameters except for "charset"

A fix would be to split the passed in value by ";", find the charset if any and recombine all other parameters

Suggested code fix which correctly extracts the charset and them recombines all of the other parameters
procedure SplitContentMediaTypeAndCharset(const aContentType: string; var aContentMediaType: string;
var aContentCharSet: string);
var
lContentTypeValues: TArray<string>;
begin
if not aContentType.IsEmpty then
begin
lContentTypeValues := aContentType.Split([';']);
aContentCharSet := '';
for var I := Low(lContentTypeValues) to High(lContentTypeValues) do
begin
if lContentTypeValues[I].Trim.StartsWith('charset', True) then
begin
aContentCharSet := lContentTypeValues[I].Trim.Split(['='])[1].Trim;
for var J := I + 1 to High(lContentTypeValues) do
lContentTypeValues[J-1] := lContentTypeValues[J];
SetLength(lContentTypeValues, Length(lContentTypeValues) - 1);
Break;
end;
end;
aContentMediaType := string.Join(';', lContentTypeValues);
end
else
begin
aContentMediaType := '';
aContentCharSet := '';
end;
end;

Thank you for your contribute.
Is this the behavior you need?

having this controller

procedure TTestServerController.TestIssue526;
begin
  ContentType := 'application/fhir+xml; fhirVersion=4.0';
  ResponseStream.Append('OK');
  RenderResponseStream;
end;

you get this response

image

Thanks Danielle, all looks good apart from dropping the casing in the header value.

BTW, I posted the issue with my work github account. Apologies if this causes any confusion.

Good, the fix will be merged ASAP.
Thank you for your feedback