Jump to content

Recommended Posts

Posted

Following the update from 1.95.0.1610 to 1.99.0.1617, all Apache module projects report:
AH02429: Response header name 'Content-Type: text/plain; charset' contains invalid characters, aborting request

Delphi 13.2
Windows 10 German Locale
Apache 2.4.41

A completely new Apache module was created as a test.
The error occurred when the value ContentType = “text/...” was set.
The setting `ContentType = “application/...” works without any problems.

The character set is set to “utf-8” by default. Here is an example:

procedure TUniServerModule.UniGUIServerModuleHTTPCommand(
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo;
  var Handled: Boolean);
begin
  AResponseInfo.ContentType := 'text/plain';
  AResponseInfo.ContentText := '<html><body>OK</body></html>';
  AResponseInfo.ResponseNo := 200;
  Handled := True;
end;

That works without any problems:

procedure TUniServerModule.UniGUIServerModuleHTTPCommand(
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo;
  var Handled: Boolean);
begin
  AResponseInfo.ContentType := 'application/json'; 
  AResponseInfo.ContentText := '{"status":"success"}';
  AResponseInfo.ResponseNo := 200;
  Handled := True;
end;

Does anyone know a solution?

Many Thanks

Posted

Yes, it is the licensed Professional Edition.
The result is identical with Delphi 12.3.
The problem does not occur when running as a standalone server. 

 

Posted

Confirmed — it's a bug in the Apache module only (standalone/ISAPI emit headers differently). In uniGUIApache.pas (~line 439):

  WResponse.CustomHeaders.Text := ResultHeaders;

re-adds Content-Type (with "; charset=utf-8") into CustomHeaders, which is a TStrings that splits name/value on "=". So the header name becomes "Content-Type: text/plain; charset" and Apache rejects it (AH02429). application/* has no charset, so it works.

You have the Professional source, so replace that line with a ':'-based parse that skips the already-set headers:

  HL := TStringList.Create;

  try

    HL.Text := ResultHeaders;

    for i := 0 to HL.Count - 1 do

    begin

      p := Pos(':', HL[i]); if p = 0 then Continue;

      hn := Trim(Copy(HL[i], 1, p - 1));

      hv := Trim(Copy(HL[i], p + 1, MaxInt));

      if SameText(hn,'Content-Type') or SameText(hn,'Content-Length') then Continue;    WResponse.CustomHeaders.Values[hn] := hv;

    end;

  finally HL.Free; end;

Tested on Apache 2.4.52: before -> 500 / AH02429, after -> 200 with the charset kept and custom headers preserved.

×
×
  • Create New...