I cannot setup a HTTP server in Xojo

Guys, it seems it’s impossible to setup a HTTP server in Xojo that is just idle and waits (listens) for POST requests to it.

I’ve went through the shamelessly sparse (two pages) documentation in the User Guide - Framework, checked the online documentation (which has all the events, methods etc for SENDING GET and POST to websites) but couldn’t find anything on the reverse.

I’ve built up such a HTTP server in node.js with 5 lines of code and yet it seems impossible for Xojo. What I want to do is send a POST to this HTTP server on Xojo and then read the content of the POST, respond with a 200 code upon successful parsing of the content, which I will display in a specific TextArea depending on the parsing of the content.

Sounds like a trivia problem but then again I might stretch beyond what Xojo is supposed to do.

Thanks in advance,

Andreas

PS: I know about TCPSocket and I can write successfully to a TCP server in Xojo but I have decided for performance reasons (as TCP is always delivering and hence waiting on successful delivery or will otherwise deliver again) that I can’t use it in my project as it would slow down my node.js server (HTTP is just a fire-and-forget and won’t slow it down)

HTTP is a protocol built upon TCP. You’ll need to use a ServerSocket backed by a TCPSocket.

Or you could do it the easy way.

Ok, @Thom McGrath I recognize that you built such and gave it to another guy who put it up on GitHub (https://github.com/bskrtich/XojoHTTPServer). Now I’m a bit smarter (guided by your Google search) but unfortunately not closer to understanding how it works nor being able to do anything with the GitHub files.

I do understand though the differences between HTTP and TCP, so your first sentence doesn’t add any value to my question.

Your second sentence: [quote]You’ll need to use a ServerSocket backed by a TCPSocket.[/quote] is unfortunately unclear to me what you mean by that. Of course I used the super class HTTPSocket and placed it on my main window. Like I wrote I have a second connection by placing a TCPSocket which works fine when I communicate with it, it’s just that it ends up appending all strings in one big text area.

So, not being able to read a .rbo or .xml file and not being able to understand your “backed by a TCPSocket” I just hope that someone else enlightens me as to how it’s being able to implement a HTTP server in Xojo.

Andreas

I’ve tried to give you the tools to help yourself since you claim to already know what you’re doing. But that doesn’t mean I won’t help.

The HTTPSocket class is an HTTP client. It cannot serve requests. For that, you’ll need to implement the protocol yourself using the TCPSocket. Create a subclass, use the DataAvailable event to receive the buffer contents, and the Write method to send your reply.

Since TCPSocket can only handle a single connection at a time, you’ll need to use a ServerSocket as well. Create a ServerSocket subclass and return your custom TCPSocket from the AddSocket event.

HTTP is a relatively simple protocol, but far from quick or easy to implement, depending on the features you’d like to support. That’s why I recommend using the server on GitHub instead, it’ll get you going significantly faster that if you have to write your own.

Thanks for the explanation @Thom McGrath, I’ve tried my best to follow your steps though I have to admit that I never worked with subclasses or there creation. I think I did the right thing, pulled the TCPSocket from the library to the left navigation pane and it came up as CustomSomething. I renamed it to HTTPServerClass and added the Event Handler for the DataAvailable event (which in my case should just add to a listbox with the ReadAll method).

But I get an error message, claiming that for my new HTTPServerClass.DataAvailable Declaration I cannot implement a nonexistent event Sub DataAvailable(). Please see the following screenshot (hope it works to show here):

Please use the link if the picture doesn’t show (it doesn’t for me)

What am I doing wrong?

Besides that error, please allow me to ask a follow up question (should I ever get this to work error free). How do I read the specific parts of the URL, like the content part? In my example I want to POST to this new Xojo HTTP server the following:

http://localhost:8181/?id=123456&name=abc+def

If I only want to parse the ?id=123456&name=abc+def part, how can I do this?

TIA,

Andreas

I can’t tell why you’re getting that error, but it’s probably the least important problem right now.

I’m going to be blunt here, sorry. You’re in over your head. Your TCPSocket subclass, HTTPServerClass, will receive raw HTTP data over multiple DataAvailable events. Your DataAvailable may receive an entire HTTP request in one event, or it might receive the request in two or ten. Or it might even receive two requests in a single event. So your first order of business is parsing the content you do have, look for the Content-Length header, and keep reading until you have that many bytes after the header. You’ll need a Do…Loop construct so you can handle the situation where multiple request arrive in the same event. Basically, your DataAvailable should read your content from the buffer into your own MemoryBlock (not String, strings are immutable and thus slow) and keep looping over the content until you have a complete request.

Now that you have a request, you need to examine the first line to determine what to do. This will tell you the method, http version, and URI. In GET requests, the query string will be appended to the URL (forming the URI) which is what you’re looking for. In POST requests, that string will be in the HTTP entity (the content after the headers). You are responsible for parsing those on your own.

This document should explain the protocol in more detail: http://www.jmarshall.com/easy/http/

Sound a bit daunting so far? We’re just scratching the surface. I’ve already done all this, there is no need for you to reinvent the wheel.

It looks like you renamed the “Super class” not the class

The simplest thing to get an HTTP Server is

  1. go to https://github.com/bskrtich/XojoHTTPServer
  2. on the right hand side click “Download Zip”
  3. once the zip downloads unzip it
  4. open project & hit run

Then examine it & learn from the code provided

[quote=69484:@Norman Palardy]It looks like you renamed the “Super class” not the class

The simplest thing to get an HTTP Server is

  1. go to https://github.com/bskrtich/XojoHTTPServer
  2. on the right hand side click “Download Zip”
  3. once the zip downloads unzip it
  4. open project & hit run

Then examine it & learn from the code provided[/quote]
Thanks Norman, I’ve done it but I upon opening demo.rbp and starting the program I do get 6 errors in total:

  1. There is no class with this name - SocketTimer = NewMyHTTPServerTimer

all the other errors are around that SocketTimer, all this code: SocketTimer = New MyHTTPServerTimer SocketTimer.Mode = Timer.ModeMultiple SocketTimer.Period = 500 SocketTimer.ServerSocket = Me produces error messages

Again, maybe my level of understanding Xojo is misunderstood. I never worked with subclasses etc or used other peoples code from GitHub or other sources. I assume that when I download such that it works, probably I’m missing a step in here because there is another file named MyHTTPServerTimer.rbo with I don’t know what to do with but I think that’s why I get an error.

Sorry guys to be so noob here and testing your patience but if you could tell me what the missing step is I’m more than willing to learn from this piece of code

Andreas

[quote=69483:@Thom McGrath]I can’t tell why you’re getting that error, but it’s probably the least important problem right now.

I’m going to be blunt here, sorry. You’re in over your head. Your TCPSocket subclass, HTTPServerClass, will receive raw HTTP data over multiple DataAvailable events. Your DataAvailable may receive an entire HTTP request in one event, or it might receive the request in two or ten. Or it might even receive two requests in a single event. So your first order of business is parsing the content you do have, look for the Content-Length header, and keep reading until you have that many bytes after the header. You’ll need a Do…Loop construct so you can handle the situation where multiple request arrive in the same event. Basically, your DataAvailable should read your content from the buffer into your own MemoryBlock (not String, strings are immutable and thus slow) and keep looping over the content until you have a complete request.

Now that you have a request, you need to examine the first line to determine what to do. This will tell you the method, http version, and URI. In GET requests, the query string will be appended to the URL (forming the URI) which is what you’re looking for. In POST requests, that string will be in the HTTP entity (the content after the headers). You are responsible for parsing those on your own.

This document should explain the protocol in more detail: http://www.jmarshall.com/easy/http/

Sound a bit daunting so far? We’re just scratching the surface. I’ve already done all this, there is no need for you to reinvent the wheel.[/quote]
Thom, I’m aware of the problematic of TCP delivering it in octets but thought that using a HTTP server I would get around that problem of waiting to put it all together.

Please do note that I’m not trying to re-invent the wheel, the GitHub project which is based on your work is just something new to me and until @Norman Palardy pointed it out to download all as a zip file and then unzip I didn’t know how to get it to work. I’ve downloaded the .rbp file only before and couldn’t see anything in my Xojo, thought it’s my version that was causing it.

So it’s not a pissing contest of avoiding your code sample or re-inventing to make it better. I do have some knowledge on the general concept of HTTP and TCP but I have no domain specific knowledge of Xojo on these topics other than what I read in the sparsely documentation (like a said those 2 pages are a joke of a documentation) and in other posts here in the forum. When it comes to Xojo I’m still a Noob, I know it’s tough to have the patience when I Noob keeps asking so many questions on things that you and other Xojo experts consider as common knowledge (but it’s not for the Noob). Hope that clarifies it, so no offense.

Andreas

[quote=69484:@Norman Palardy]The simplest thing to get an HTTP Server is

go to https://github.com/bskrtich/XojoHTTPServer
on the right hand side click “Download Zip”
once the zip downloads unzip it
open project & hit run[/quote]

It does not run :frowning: Maybe needs updating ?

Same punishment with RS2012. One error only…

Looks like the project is not functional :frowning:

I’ll stick to html and php :wink:

It would seem that way

The only thing you should have to do is drag the MyHTTPServerTimer.rbo file that is next to the project into the project
Looks like it’s just not part of the project but it IS in the download

At that point if you have a little web site it can serve it up
You browse to the directory holding the index.html file & it will then serve it up

That worked :slight_smile:

I know :stuck_out_tongue:

Me too! :smiley:

[quote=69496:@Andreas West]Thom, I’m aware of the problematic of TCP delivering it in octets but thought that using a HTTP server I would get around that problem of waiting to put it all together.

Please do note that I’m not trying to re-invent the wheel, the GitHub project which is based on your work is just something new to me and until @Norman Palardy pointed it out to download all as a zip file and then unzip I didn’t know how to get it to work. I’ve downloaded the .rbp file only before and couldn’t see anything in my Xojo, thought it’s my version that was causing it.

So it’s not a pissing contest of avoiding your code sample or re-inventing to make it better. I do have some knowledge on the general concept of HTTP and TCP but I have no domain specific knowledge of Xojo on these topics other than what I read in the sparsely documentation (like a said those 2 pages are a joke of a documentation) and in other posts here in the forum. When it comes to Xojo I’m still a Noob, I know it’s tough to have the patience when I Noob keeps asking so many questions on things that you and other Xojo experts consider as common knowledge (but it’s not for the Noob). Hope that clarifies it, so no offense.

Andreas[/quote]
I’ll admit I was put off by the tone of your original message. So let us put it behind us.

It looks like Brandon has an error in his project, sorry about that. As Norman has pointed out, there is another file on disk you’ll need to import which should take care of you.

As for getting you up to speed with Xojo, have you read the “Intro to Programming” book from http://www.xojo.com/download/extras.php? Although you’re obviously familiar with programming, this book does a better job introducing you to the workflow of the Xojo IDE than the user’s guide will. I imagine there are passages that will make you want to shoot yourself (you already know what a variable is) but there is still a lot of value there.

I have fixed the demo project so this wont be a problem in the future.

Thanks @Brandon Skrtich but please be aware that it currently gives a warning message upon loading: “This project was saved with a never version of Xojo (version 2014.01).”

I assume it’s coming from the beta version which you used to save the project, 2014.01 is not out yet (just checked it) :wink:
Andreas

Any time you load a project that was saved in a newer version into an older IDE it will do this
Brandon could save in 2013r4.1 but anyone using an older version would see a similar warning

MOST times its harmless but there can be new features or classes in a newer IDE that will not load or work properly in an older IDE as things get fixed/ updated/ added to

It would be like opening a VB 6 project in VB 3 (if that worked at all)

Not sure about other users here but that’s not a solution for me. Let me explain why it’s not IMO:

  1. We’re replacing something that doesn’t exist within Xojo (at least for version 2013 Release 4.1) and where ONLY 2 pages are spend on “explaining” HTTPSocket (I’d rather say those 2 pages scratch the surface) with something that has NO documentation at all (README.MD is not a documentation in the usual sense of GitHub, it’s giving credit to people who helped built this).

  2. Upon starting this project, it asks for a folder to start the HTTP server. Why is that? Why in hell do I need to select a folder when all I want is a HTTP server silently listening to traffic coming in?

  3. After selecting a folder, it opens up a new browser window under the URL http://127.0.0.1:8080/ and showing a big graphic and the words “HTTP Requestor Demo”. Again, where in the source code can I change the PORT from 8080 to something else (as 8080 might be blocked by a firewall or taken by another app)? I went through 80% of the code and couldn’t find it, no documentation to point me to such a simple thing, so I gave up searching for it (and yes, I spent 10 minutes on this search).

I appreciate people like @Thom McGrath and @Brandon Skrtich putting up the effort of programming something that Xojo hasn’t put up yet, filling a gap left by Xojo. But without a proper documentation this is just a solution for geeks who spent hours reading through every line of code and “disassembling” it to understand what was done here, maybe even come up with an improvement and finding a bug. Kudos for people who do so, they are the heart of the open source community but for an end user who just wants a simple thing. Again, in node.js this is 5 lines of code to do so:

var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\ '); }).listen(3000, "127.0.0.1"); console.log('Server running at http://127.0.0.1:3000/');

  1. It takes 42ms on my MacBook Pro to get an answer from this solution. Usually in Node.js I get 0 or 1ms, again for a solution I have in mind I can’t waste 42ms of waiting time.

So though this might be a solution to some people, I doubt this is a solution for many people or users with less or around the same knowledge of Xojo like me.

Guess I’ll have to wait and see if Xojo decides to put a HTTP server socket into their product eventually in the future (with proper documentation).

Again, thanks to everyone who spent his time on this question to solve my problem.

Andreas

When I controlled the code, there was detailed documentation. It was a PDF. No idea if it is still there or not.

Xojo already has an HTTP server of sorts, it is used in the standalone server for web edition. It is built on my http server classes. Yet I don’t expect Xojo will ever make it public as it has been extremely customized for the needs of WE.