Real Software Forums
http://forums.realsoftware.com/

TCPSocket.Write() mime types
http://forums.realsoftware.com/viewtopic.php?f=2&t=46394
Page 1 of 1

Author:  pperroud [ Mon Dec 31, 2012 5:40 am ]
Post subject:  TCPSocket.Write() mime types

Hello,

When I write html data to RB's TCPSocket, the data is received as text/plain mime type and 8859-1 encoding by Firefox and this is an issue to my web server ServiceApplication that handles requests from web browsers on a private intranet.

This service core procedure is based on a sample code from RB 2012.r2 @ /Example Projects/Communication/Internet/Web Server.

This sample code is not working with Firefox as Firefox is getting the socket reply as plain text truly: html tags are just ignored and the entire data chunk is wrapped by <pre> tags for plain text display in Firefox.

As a result, Firefox is showing html data as plain text when Chrome or Safari are smart enough to handle it as text/html even using the UTF8 encoding as defined in the html header.

To my understanding this is not really a bug in Firefox that could hardly be blamed for handling the data my server is sending just like it was written to the TCPSocket.

A correction would be to have the html data sent as text/html and UTF8 thru a TCPSocket Write() instead of default text/plain and 8859-1 encoding...

Therefore my question is: how could I configure a TCPSocket to make sure the client browser at the other end will get it loud and clear that this is not text/plain but text/html data - to be handled as HTML and not plain text by Firefox?

Author:  charonn0 [ Mon Dec 31, 2012 9:31 pm ]
Post subject:  Re: TCPSocket.Write() mime types

The TCPSocket is just a pipe that you write bytes to; it doesn't handle the encoding at all. Additionally, the HTTPServer example isn't an HTTP-compliant server. All it does is accept requests (over HTTP) and then writes the requested file to the socket. It doesn't send the required HTTP response headers, etc.

A quick fix would be something like this:

Protected Sub ProcessHeaders(headers as string)
dim temp,cmd,param as string
dim bs as binaryStream
dim f as folderItem

// parse out the header data
temp = nthField(headers,chr(13),1)
cmd = nthField(temp," ",1)
param = nthField(temp," ",2)

if cmd = "GET" then// we got a get command
f = getHTTPFileFromPath(param)
if f = NIL OR f.exists = false then// could not find the file (404)
me.write "Error 404."
return
end
bs = BinaryStream.Open(f, False)

'FIX STARTS HERE
'This next line puts some basic HTTP headers into the 'responseheaders' string
Dim responseheaders As String = "HTTP/1.1 200 OK" + EndOfLine.Windows + "Content-Type: text/html; charset=UTF-8"
'This line writes the headers, two end-of-line markers, and then the actual content
me.write responseheaders + EndOfLine.Windows + EndOfLine.Windows + bs.read(bs.length)

bs.close
end
End Sub


Note the Content-Type header. This header specifies the datatype and encoding of the data being written to the socket.


As I said, however, the sample HTTP server is not a compliant HTTP server; it's merely for demonstration purposes. A much more robust HTTP implementation in RealBasic can be found here: https://github.com/bskrtich/RBHTTPServer

Author:  pperroud [ Tue Jan 01, 2013 6:58 am ]
Post subject:  Re: TCPSocket.Write() mime types

Hello,

Thank you for this fix that's proven working: I had tried something similar before but didn't get the EndOfLine correctly like your do - therefore it had not solved the issue before...

I know examples are not providing compliant code but they usely provide starting pointers...

Anyhow - Firefox is now rendering my HTML sample test almost properly but writing such an HTML header is raising another issue: the CSS link in my <head> section that was working fine with Chrome and Safari before the HTML header fix is not working anymore - it's not working with Firefox neither actually.

<link type="text/css" rel="stylesheet" href="test.css">

Here I suspect a problem with the "text/css" link type that could break the web browsers at client's side since the last header they received was "text/html"...

This is interesting since I have a javascript, anchor links or images in the body section that are all working fine - I didn't try a POST from a form action yet - but links with different mime types like css or script in the html <head> section are now broken...

I will certainly look at the more robust implementation you are recommending but still be intested by any shared thoughts or comments about the new issue(s) with the fixed sample code.

Author:  charonn0 [ Tue Jan 01, 2013 4:08 pm ]
Post subject:  Re: TCPSocket.Write() mime types

Each time the browser requests a page from your server, the server must send the proper headers, including the content-type. If all your sever does is blindly send the text/html type (which is what my fix does) then other types may break. You would need to detect what sort of file is being requested and send the proper content-type when that file is requested.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/