Note that there are some explanatory texts on larger screens.

plurals
  1. POC or C++ HTTP daemon in a thread?
    text
    copied!<p>I'm starting up a new embedded system design using FreeRTOS. My last one used eCos, which has a built-in HTTP server that's really lightweight, especially since I didn't have a filesystem. The way it worked, in short, was that every page was a CGI-like C function that got called when needed by the HTTP daemon. Specifically, you would write a function of the form:</p> <pre><code>int MyWebPage(FILE* resp, const char* page, const char* params, void* uData); </code></pre> <p>where <code>page</code> was the page part of the url, <code>params</code> were any form parameters (only GET was supported, not POST, which prevented file uploads and thus made burning the flash a pain), <code>uData</code> is a token passed in that was set when you registered the function, so you could have the same function serve multiple URLs or ranges with different data, and <code>resp</code> is a file handle that you write the HTTP response (headers and all) out to.</p> <p>Then you registered the function with:</p> <pre><code>CYG_HTTPD_TABLE_ENTRY(www_myPage, "/", MyWebPage, 0); </code></pre> <p>where <code>CYG_HTTPD_TABLE_ENTRY</code> is a macro where the first parameter was a variable name, the second was a page URL (the <code>*</code> wildcard is allowed; hence <code>page</code> getting passed to <code>MyWebPage()</code>), third is the function pointer, and last is the <code>uData</code> value.</p> <p>So a simple example:</p> <pre><code>int HelloWorldPage(FILE* resp, const char*, const char* params, void*) { fprintf("Content-Type: text/html;\n\n"); fprintf("&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello World!&lt;/title&gt;&lt;/head&gt;\n"); fprintf("&lt;body&gt;\n"); fprintf("&lt;h1&gt;Hello, World!&lt;/h1&gt;\n"); fprintf("&lt;p&gt;You passed in: %s&lt;/p&gt;\n", params); fprintf("&lt;/body&gt;&lt;/html&gt;\n"); } CYG_HTTPD_TABLE_ENTRY(www_hello, "/", HelloWorldPage, 0); </code></pre> <p>(Actually, <code>params</code> would be passed through a function to escape the HTML magic characters, and I'd use another couple functions to split the params and make a <code>&lt;ul&gt;</code> out of it, but I left that out for clarity.)</p> <p>The server itself just ran as a task (i.e. thread) and didn't get in the way as long as it had a lower priority than the critical tasks.</p> <p>Needless to say, having this proved invaluable for testing and debugging. (One problem with embedded work is that you generally can't toss up an XTerm to use as a log.) So when Supreme Programmer reflexively blamed me for something not working (path of least resistance, I guess), I could pull up the web page and show that he had sent me bad parameters. Saved a lot of debug time in integration.</p> <p><em>So anyway...</em> I'm wondering, <strong>is there something like this available as an independent library?</strong> Something that I can link in, register my callbacks, spawn a thread, and let it do the magic? Or do I need to crank out my own? I'd prefer C++, but can probably use a C library as well.</p> <p>EDIT: Since I'm putting a bounty on it, I need to clarify that the library would need to be under an open-source license.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload