1
votes

I'm having the craziest issue with Firefox. I'm not sure if this issue is coming from Firefox its self, or from my custom web server (built in Delphi) or what it might be. This problem only happens in Firefox (and Opera) where it is both moving everything from the head down to the body, and also adding random characters at the beginning of the body. Strangely, it even does it with a completely 100% empty web page.

I'm testing with a page as simple as this:

<html>
<head>
    <title>Test</title>
</head>
<body>

</body>
</html>

As for the Web Server, I'm building a custom HTTP App in Delphi using IdHTTPWebBrokerBridge (Indy) and simply replacing the ContentStream (or Content) of the Request like so...

procedure TDashModule.DashConsoleHomeAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  Response.ContentType:= 'text/html';
  Response.ContentStream:= TFileStream.Create('C:\SomeDir\SomeFile.html', fmOpenRead or fmShareDenyNone);
end;

...where SomeFile.html is the empty page as posted above, and the procedure being an event handler for the default request handler.

In Chrome, IE, Safari, etc. everything shows exactly as the original code. However, Firefox (and Opera) are producing this:

<html>
<head>
</head>
<body>

<title>Test</title>
</body>
</html>

As you can see, the title tag has been moved down to the body, and some random characters  are appearing at the very beginning of the body. When viewing the raw page as its original file in Firefox, it shows correctly. But when using my web server in Delphi, Firefox is destroying this page (and all pages, for that matter).

What could be doing this and how to fix it?

Steps to reproduce

A) Create a new HTML Page (for me in Visual Studio 2010) with just this content:

<html>
<head>
    <title>Test</title>
</head>
<body>

</body>
</html>

B) In Delphi XE2, start a new project: File > New > Other... > Web Broker > Web Server Application

C) Choose Stand-alone VCL Application > Next > Finish

D) Create a default handler (Right-click WebModule1 and choose Action Editor...)

E) For DefaultHandler item, go to the Event Properties and go to the Event Handler for OnAction

F) Replace any code that's already there with this (replace filename with HTML filename):

Response.ContentStream:= TFileStream.Create('C:\SomeDir\SomeFile.html', fmOpenRead or fmShareDenyNone);

E) Run application and click "Show In Browser" - copy/paste the URL to Firefox if necessary.

UPDATE

Thanks to the help, I've come to realize the issue is with the BOM tag of the file, which did not belong there. It was due to how the TFileStream works, it loads every little piece of the file, which included this code. I've changed my method to using TStringList.LoadFromFile() instead, because this automatically detects it and I can also read TStringList.Text to assign to Response.Content.

2
It appears so, although some comments were out of criticism and sarcasm, others were in good detail and shouldn't have been deleted. - Jerry Dodge
criticism is not that bad :-) Anyway, it appears all this geheimestaatsmoderation grown little out of control. - OnTheFly
@user I must agree with you here. Comment deletion is not infrequently too draconian, in my view. - David Heffernan
To re-cap on my comments which were deleted, I didn't think the problem was in my code, others said the problem was with my code but couldn't say what the problem actually was, until David answered, I barely had a chance to understand what his answer meant when he deleted his answer, in the mean-time, other comments and answers explained the same, but in more detail, I found the source of the issue in my code, Added update to question, then the question got many more downvotes with critical explanations as to why. Comments deleted, now here. - Jerry Dodge

2 Answers

7
votes

Apparently those three characters constitute a Unicode UTF-8 Byte-Order Mark. They're supposed to be placed at the start of a file and not actually get rendered; they're metadata to let the text interpreter know the encoding that's being used. No idea why you're seeing one in the middle of your file, though! Try opening the original in a hex editor and seeing if it's got a BOM embedded in it.

(In case you don't have a hex editor, you can get a very good free one here. It's even written in Delphi!)

2
votes

Those 3 characters are the UTF-8 BOM. The BOM should not be there. I can't say where it comes from (perhaps it is from the file) but you need to remove it.

The fault is certainly at your end though! Don't be led astray by the fact that IE and the Webkit browsers display the page fine. That just means that those browsers are being lenient.