WebServer Example Not Working

Hi

Any one any idea why the WebServer Example mightn’t work?

[code] 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
’ Get the file we’d like to serve
f = GetHTTPFileFromPath( param )
If f = Nil Or f.Exists = False Then // could not find the file (404)
Me.write( “Error 404.” )
Return
End If

// Open the file up for reading only
bs = BinaryStream.Open(f, False)
// Write out the contents of the file to the remote side
Self.Write( bs.Read( bs.Length ) )
// Close the file
bs.close

End If[/code]

The server senses the request but when it comes to send the file back to the browser, the browser doesn’t receive it, so it looks like Self.Write( bs.Read( bs.Length ) ) isn’t writing to Self

Thanks for any light

OS?
Version of Xojo?
Did you make sure you have access to the file?

Thanks shao sean.

macOS 10.12.2
Xojo 2016 Release 3

And I did write the access file (I think it drops out just before that line if I hadn’t)

Thanks again for any help

You have to do something like that, so that the browser understands it:

[code]

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

’ Get the file we’d like to serve
f = GetHTTPFileFromPath( param )

If f = Nil Or f.Exists = False Then // could not find the file (404)

  Me.write( "Error 404." )
  Return

End If

// Open the file up for reading only
bs = BinaryStream.Open(f, False)
Self.write “HTTP/1.1 200 OK” + chr(13) + chr(10)
self.Write “Content-Type: text/html” + chr(13) + chr(10)
self.write "Content-Length: " + str(bs.Length) + chr(13) + chr(10)
self.Write “Connection: close” + chr(13) + chr(10) + chr(13) + chr(10)
// Write out the contents of the file to the remote side
Self.Write( bs.Read( bs.Length ) )
// Close the file
bs.close

End If[/code]

Thanks Marius Dieter Noetzel - That worked! So that would mean the Example has a bug in it and would appear never to have worked?

I would say it is no HTTP-Server. It is just anTCP-Socket that delivers HTML-Text.
Giving it the name WebServer is a bit over the top.
I assume, that older browserversions would have displayed that, ignoring that there are not HTTP-headers sent.

Perhaps someone should fix that, as

  • the example should deliver html, that is typically shown with browsers
  • examples should work out of the box

I filled feedback case for that (https://xojo.com/issue/46616)]https://xojo.com/issue/46616

[quote=309898:@Marius Dieter Noetzel]the example should deliver html, that is typically shown with browsers
examples should work out of the box[/quote]

Examples are largely the result of users efforts. If something does not quite work, it is IMHO the responsibility of anybody who has the competences to share a better project. Most of the frequent contributors here do it, and pass the result to Paul Lefebvre to update the examples. I have done so myself.

I see you know what to do. It would be nice if you think you can improve the present example.