-
Notifications
You must be signed in to change notification settings - Fork 0
File Delivery
This example shows how to serve static files using a HttpNode.
Please read the Hello Browser example first, as this example builds upon the knowledge in it.
Like in Hello Browser, set up your project file. As little extra, add DEFINES += SOURCE_PATH="\\\"$$PWD\\\""
to it. This way we can find the path to the directory of the source at run-time later on, which saves us additional set-up later.
Next, create a directory called static and put some files into it. These files will be accesible through HTTP later on. For the sake of this example, I've added an {image of a flower](https://github.com/NuriaProject/FrameworkExamples/blob/master/Network/FileDelivery/static/flower.jpg) (Source).
Let's dive into the source file!
First, instantiate a server (reference 1). Next step (reference 2) is to tell the root node where static files can be found. We use the SOURCE_PATH
define from the project file here.
Third step (reference 3) is telling the node the static resouce mode it should use. There are three modes available:
- NoStaticResources - No static resources are served
- UseStaticResources - Serve static files, but only in the directory itself (excludes sub-directories) - This is the one we're using!
- UseNestedStaticResources - Serve static files with sub-directory support
And after this, it's just a matter of starting the server again like we did in Hello Browser (reference 4).
Now, compile and start the project. Afterwards, you can go to http://127.0.0.1:8080/flower.jpg to see it serving a static file to you.
When resolving a path, HttpNode will first look for slots and then for files if there's no slot nor sub-node with that name. Thus you can also mix slots (Through connectSlot()
like in Hello Browser) with static file delivery. To see for yourself, maybe toy around with it for a moment, mixing Hello Browser with this example.
When serving static files, HttpNode makes sure that no [directory traversal attacks}(http://en.wikipedia.org/wiki/Directory_traversal_attack) occur. The internal mechanism is pretty dumb in this regard: If there accessed path contains "." or ".." as single part in the URL (Roughly "it's between slashes"), then the request is denied.