The G-WAN API
Reducing complexity and size must be the goal in every step.
Smaller is Better
When languages count several thousands of native library calls their footprints are in the hundreds of megabytes. Then, they are trashing CPU caches – inflicting overwhelming learning curves to developers – for the sake of "productivity", we are told.
G-WAN scripts do not lack features (/usr/lib lists thousands of libraries). Libraries let you decide what your application needs. This is why G-WAN aims to offer only core services. Exceptions (in-memory GIF I/O, 2D frame buffer, charts, 'wait-free' KV store) exist only because no library was found doing the job fast enough.
API Categories
The information below is also available in the /gwan/include/gwan.h header:
- The server reply buffer
- Dynamic xbuffers
- The error.log file
- Environment Variables
- HTTP Response Headers
- Litteral HTTP code messages
- URL parameters
- Key-Value store
- Garbage collector
- Handler States
- Cache
- Server report
- JSON (de-)serialization
- HTML escaping
- Formatting
- In-memory GIF I/O
- Frame buffer
- Charts & sparklines
- Time
- Random numbers
- Checksums
- Hashing
- Encryption
- Compression
The server reply buffer
Get a pointer on the server reply dynamic buffer (used in almost all G-WAN C scripts), or use a pre-allocated buffer for the reply (instead of building a new reply):
Dynamic xbuffers
Dynamic buffers grow dynamically to let you build ASCII or binary contents by using the standard printf() syntax – without having to care about memory allocation.
xbuffers are used in almost all G-WAN C scripts, notably to build the server reply:
The error.log file
Output text in the current virtual host 'error.log' file:
Environment Variables
These enums allow you to get and modify G-WAN internal values like the traditional CGI environment variables, but also performance counters and even internal structures:
HTTP Response Headers
To modify G-WAN's generated HTTP Headers, or create ones from scratch:
HTTP Code Messages
Given an HTTP status code, return the status message ("200 OK", "404 Not found", etc.) or the long litteral description aimed at humans (like "The requested URL was not found on this server"):
URL parameters
Let C scripts fetch the value of a specified URL parameter:
Server report
Dumps an ASCII or HTML server report with information like system and server uptimes, levels of disk and RAM, traffic statistics, etc.
Key-Value store
The G-WAN Key-Value store has been used under the highest concurrency issues since it is used by G-WAN for many internal lists.
This KV is much faster than Tokyo Cabinet FIXED (an array that can only process fixed-size keys and values) – and, unlike Tokyo Cabinet, G-WAN's KV store scales linearly under multi-threaded read and write tests:
Garbage collector
These are malloc()/free() replacements (the gc_free() call does nothing) using G-WAN's internal memory allocator (which is faster than the system):
Handler States
Define which handler states we want to be notified in the handler's main():
Cache
Lets you add, get or delete entries in the G-WAN cache. Note that since G-WAN v2.8+ the KV store can store and serve directly any buffer (without copy, see the set_reply() call), making it even more efficient to build your own caches:
JSON (de-)serialization
Making JSON easier (and much faster) to use:
HTML escaping
Basic HTML and URL processing routines:
Formatting
The standard snprintf() and vsnprintf() calls, just with more features:
In-memory GIF I/O
An ultra-fast GIF generator and parser; to save a GIF image on disk, just save the buffer made by gif_build():
Frame buffer
2D routines to draw in 8-bit memory frame buffers (that can then be encoded as GIF images):
Charts & sparklines
Build real-time Area, Bar, Dot, Line, Pie, Ring charts with various styles:
Sending email from your C scripts:
Time
Initially made to replace the Windows WinInet's atrociously slow routines and then declined in various flavors for portable high-resolution timing and HTTP strings processing:
Random numbers
Whether you need raw speed or true (hardware) random numbers, this is probably your best options:
Checksums
Since they are already implemented in G-WAN, there is no real need to write your own:
Hashing
Like for Checksums, these calls aren't likely to be implemented better in your code, so G-WAN just makes them available for your applications:
Encryption
AES is the U.S. NIST FIPS PUB 197 standard (2001) developed by Belgians Joan Daemen & Vincent Rijmen and approved by the NSA. Useful to comply:
Compression
Gzip and Deflate compression at hand (decompression is left as an exercise for the reader because those two standards are dangerous by-design with untrusted input):
// if(gzip != 0) then we use the 'gzip' format, else we use the 'zlib' format // (the new 'zlib' format is both slower and larger than the old 'gzip' format) // if you already have the crc32, pass it into 'crc', else 'crc' MUST be NULL // return the dstlen, 0 on error u32 zlib_cmp(char*src, u32 *crc, u32 srclen, char *dst, u32 dstlen, int gzip); |