TL;DR: when I have a HTTPSocket in Listen mode, how do I make it return a “200” status to the caller?
I have this little project where I have a standalone app running on my server. It listens on a TCP port using ServerSocket and TCPSocket. That works fine.
I now need to also serve very basic http pages to a client (fastspring.com - they will notify me of purchases that way). So, I do not need to serve fancy html pages, I just need to implement the http protocol in order to receive some POST data and reply to it.
So, I tried this:
I use ServerSocket again, listening on the specific http port, and have it create new sockets that are a subclass of HTTPSocket.
And that works to the point that I can receive the POST via the socket’s PageReceived handler. But now I need to answer, e.g. by sending the http 200 OK status back. How do I do that from a HTTPSocket?
You can call the
Write method on the superclass, though in my experience this causes the HTTPSocket to send a malformed HTTP request immediately following whatever you wrote.
If all you need to do is grab the POST contents and return 200 OK then using a TCPSocket directly will probably be easier, e.g. in the DataAvailable event:
Dim headers As String = Me.Read(InStrB(Me.Lookahead, EndOfLine.Windows + EndOfLine.Windows) + 3)
Dim content As String = Me.ReadAll
Me.Write("HTTP/1.1 200 OK" + EndOfLine.Windows)
Me.Write("Connection: Close" + EndOfLine.Windows)
Me.Write("Some-Extra-Header: Value" + EndOfLine.Windows)
Me.Write("Last-Header: Value" + EndOfLine.Windows)
Me.Write(EndOfLine.Windows) ' end of headers
I don’t think the HTTPSocket was designed to handle server duties, only client. Having said that, there are examples of http server classes that have been posted by various people over time. If you search the forum/google for web server in realbasic or xojo I’m sure you’ll find something.
Actually I problem I worked on with SSL sockets and chrome might help you. Check feedback case 31800. There should be a basic functioning webserver in the attachments.
I figured that as well, eventually, and am doing something similar what Andrew suggests.
The issue with HTTPSocket possibly sending more stuff doesn’t seem to be a problem since I close the connection anyway.
Using HTTPSocker instead of TCPSocket helps a little because it parses the headers of the incoming request and therefore tells me in the ReceiveProgress event how much to expect, in case it doesn’t arrive all at once (which Andrew’s proposal does not cover, I think). I need to use the ReceiveProgress event because I won’t get the PageReceived event on time (the connection stays open until I reply, and therefore this event doesn’t get called).
Keep in mind that HTTPSocket is only an HTTP/1.0 socket and doesnt handle chunked transfers or persistent/pipelined connections.
Thanks, Greg. Fortunately, I don’t need that feature, either. FastSpring is just happy with the simplest protocol as long as I return “200 OK”
Paypal’s IPN (Instant Payment Notifications) require a more robust protocol, including TLS 1.2, but I got that already covered as well.
What bothers me most about this: I just purchased a full Pro license because I thought I’d need the latest and greatest Xojo version for all of this. Turns out that I could have handled it all this low level network stuff with RS 2012r2 just as well (which I currently do). I’ll still need x64 support for my desktop apps, eventually, but that didn’t require a Pro license. Well, I hope it all goes into paying someone to write a better usable IDE again