1

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.

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • 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 Mar 27 '12 at 15:56
  • 1
    criticism is not **that** bad :-) Anyway, it appears all this `geheimestaatsmoderation` grown little out of control. – OnTheFly Mar 27 '12 at 17:10
  • 1
    @user I must agree with you here. Comment deletion is not infrequently too draconian, in my view. – David Heffernan Mar 27 '12 at 23:08
  • 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 Mar 27 '12 at 23:31

2 Answers2

7

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!)

Community
  • 1
  • 1
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • 2
    The most likely reason for the BOM to appear in the `` is if the file starts with whitespace followed by a BOM.... – Boris Zbarsky Mar 26 '12 at 23:48
  • The issue of it being moved down to the body must be Firefox getting confused and thinking that this "text" belongs in the `body` tag, and while it's at it, somehow thinks everything in the `head` tag should go with it. – Jerry Dodge Mar 27 '12 at 00:08
  • @jerry: That could be. Have you verified the issue with a hex editor yet? – Mason Wheeler Mar 27 '12 at 00:17
  • Yup, that was very well the problem. Solved as mentioned above, the issue was deeper than just that. My solution doesn't even need to worry about whether this tag is there or not. – Jerry Dodge Mar 27 '12 at 00:22
  • @Jerry, if the issue was indeed the BOM, and Mason's answer helped you find it, you should accept it so people know the issue has been resolved. – Ken White Mar 27 '12 at 00:44
  • Sorry, just finally getting back to doing some code since it works fine now. – Jerry Dodge Mar 27 '12 at 00:49
2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490