Using HTTP
The sock. object can function as an HTTP server. This means that when certain conditions are met, individual sockets will switch into the HTTP mode and output the data in a style consistent with HTTP server functionality.
Certain Tibbo BASIC/C information about the HTTP server has already been provided in Working With HTML.
When the socket is in the HTTP mode, your program has no control over the received data (HTTP requests) and only sometimes has control over the transmitted data (HTTP reply).
In the simplest case, the file returned to the web browser is static — a "fixed" HTML page, a graphic, or some other file. Processing of such a static file requires no intervention from your program whatsoever. Just set up the socket(s) to be able to accept HTTP requests and the sock. object will take care of the rest.
More often than not, however, you have to create a dynamic HTML page. Dynamic pages include fragments of Tibbo BASIC/C code. When the sock. object encounters such a fragment in the file being sent to the browser, it executes the code. This code, in turn, performs some action; for example, it generates and sends some dynamic data to the browser or jumps to another place in the HTML file.
The HTTP server built into the sock. object understands two request types — GET and POST. Both can carry "HTTP variables" that the server will extract and pass to the Tibbo BASIC code.
Supported Content (File) Types
At the moment, the following file extensions (types) are explicitly supported:
Extension |
Support |
Returned Content-Type |
.html |
All platforms |
text/html |
.txt |
All platforms |
text/plain |
.gif |
All platforms |
image/gif |
.jpg |
All platforms |
image/jpg |
.png |
All platforms |
image/png |
.ico |
All platforms |
image/ico |
.bmp |
All platforms |
image/bmp |
.svg |
32-bit platforms |
image/svg+xml |
.js |
32-bit platforms |
text/javascript |
.json |
32-bit platforms |
application/json |
.css |
32-bit platforms |
text/css |
.woff |
32-bit platforms |
font/woff |
HTML files may include executable Tibbo BASIC/C code. This allows for the creation of dynamic HTML output.
All other file types are always static and are sent to the browser "as is." There is, however, a method of programmatic generation of such files — see URL Substitution.
Some file types are only supported on 32-bit platforms. Platform type is listed on each platform's page.
Currently, the sock. object can only access the first 65,534 bytes of each file, even if the actual file is larger! Make sure that all HTML files (and other files that will be returned by the built-in server of your device) are not larger than 65,534 bytes. This is not to be confused with the size of HTTP output generated by an HTTP file. A very large output can be generated by a small HTML file (due to dynamic data generation) — and that is OK.
The 65,534-byte limit is actually the size limitation for the compiled HTML file. When compiling your project, TIDE will separate the static portion of the file from the Tibbo BASIC/C code fragments. Only the compiled file size matters.
File Compression (32-Bit Platforms Only)
On 32-bit platforms, files of all types except HTML can be attached to the project in .gz (compressed) form.
For example, rather then attaching picture.jpg, you can compress this file and attach picture.jpg.gz. The TiOS webserver will handle this correctly:
- Whenever a browser requests picture.jpg, the server will first look for this file and, if not found, will also look for picture.jpg.gz. If the latter is found, the browser will output this file with "Content-Encoding: gzip" in the reply header.
- Compressed HTML files are not supported. This is because HTML files can potentially include executable Tibbo BASIC/C code. Therefore, TiOS must store such files in their original, uncompressed, form.
Additional Resources:
In this section:
- HTTP-Related Buffers
- Setting the Socket for HTTP
- Socket Behavior in the HTTP Mode
- Including Tibbo BASIC/C Code in HTTP Files
- Generating Dynamic HTML Pages
- URL Substitution
- Working With HTTP Variables