LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C-language embedded HTTP server; how to manage HTML for output (https://www.linuxquestions.org/questions/programming-9/c-language-embedded-http-server%3B-how-to-manage-html-for-output-4175414538/)

theNbomr 07-02-2012 07:53 PM

C-language embedded HTTP server; how to manage HTML for output
 
I am creating an application in C, that includes an embedded HTTP server (uses GNU libmicrohttpd). That part works fine so far. The issue is more a question of style or best practice.
The application uses the HTTP server as a way of providing quasi real-time output of data that it is monitoring. As such, it generates a lot of the HTML on the fly, from data structures maintained by the application at runtime. Presently, the HTML source code is embedded right in the C source code, but this is becoming cumbersome to manage. I would prefer to be able to develop HTML independently from the program logic, and using proper HTML coding tools. I am already moving as much as possible into CSS, in an attempt to decouple the view from the model & controller.

The question then becomes how to merge the HTML back into my application. I have thought of a few ways, and each has some strengths and weaknesses.
  1. I could create a simple tool that transforms the HTML into C-source literal strings, and #include it into the source code. This doesn't directly accommodate things that aren't known until runtime, such as how many rows a table might have and what content is in each cell of the HTML tables. Some sort of templating system should solve that, but then I have to create a parser for the template data.
  2. I could create a template that the application reads at runtime, and instantiate it with runtime data. That is basically deferring the above from compile time to run time.
  3. I could just continue to embed HTML code into the C source code. It seems that PHP programs have used this method for years. Just doesn't seems clean to me.
  4. I could use some kind of hybrid system, where the HTML that is static is in static files and the changing data is generated on the fly. Messy. No way.

Anyone else solved this issue already? Are there existing standard tools to help with any particular solution? Googling for a solution seems to turn up mostly articles relating to publishing source code as HTML-formatted data (i.e. the reverse of what I want).

--- rod.

elucches 07-03-2012 12:19 PM

Hi,
I used Handballer as server (http://code.google.com/p/handballer/) and then I serve static pages with content responding to messages transmitted through the server bus. Handballer modifies the Thttpd server for bus messaging.
Best regards,
Esteban
(Note: although I'm listed as one of its developers, the idea and the coding come from Sylvain Garden.)

theNbomr 07-03-2012 03:53 PM

HandBaller seems like a most intriguing tool, but doesn't seem to match my present requirements. I don't see at all how it addresses the matter of separation of HTML code from programming source code.
Thanks for your feedback.

My present muse is to modify libmicrohttpd. For those who aren't familiar with it, I will explain one of it's behaviors. It has a function which, given an open file descriptor, will send the file to the HTTP client, wrapped up in the associated HTTP machinery. I am considering adding a related function that would parse the specified file, and finding some special tag, perhaps using an existing HTML/XML SAX-style parser, would call a specified callback. The callback could then be used to expand the content of the tag with runtime data. This would allow one to craft pseudo-static HTML using traditional tools, but the runtime behavior would be to use it as a template.

Anyone have thoughts on this approach? Especially anyone who has already used libmicrohttpd?

--- rod.

lyle_s 07-03-2012 06:10 PM

Please have a look at clearsilver: http://www.clearsilver.net/

Lyle.

theNbomr 07-05-2012 03:31 PM

Okay, thanks guys, especially lyle_s. I've now located a few C-compatible 'HTML template engines' (that was the key; getting the right keywords to search for). I might go with clearsilver, but there are a few others that I'm looking at, and seem a bit simpler.
--- rod.


All times are GMT -5. The time now is 04:26 PM.