[SOLVED] C-language embedded HTTP server; how to manage HTML for output
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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).
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.)
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.