C或C ++ HTTP守护进程在线程? [英] C or C++ HTTP daemon in a thread?

查看:146
本文介绍了C或C ++ HTTP守护进程在线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FreeRTOS启动了一个新的嵌入式系统设计。我最后一个使用eCos,它有一个内置的HTTP服务器,真的很轻,尤其是因为我没有一个文件系统。简而言之,它的工作方式是,每个页面都是一个类似CGI的C函数,在HTTP守护进程需要时调用它。具体来说,你将写一个形式的函数:

  int MyWebPage(FILE * resp,const char * page,const char * params,void * uData); 

其中页面 url, params 是任何表单参数(只支持GET,不支持POST,这阻止了文件上传,从而使闪光灯烧坏), uData 是在注册函数时设置的令牌,因此您可以使用相同的函数为多个URL或范围提供不同的数据, resp

然后你注册了这个函数:

  CYG_HTTPD_TABLE_ENTRY(www_myPage,/,MyWebPage,0); 

其中 CYG_HTTPD_TABLE_ENTRY 是一个宏,参数是一个变量名,第二个是页面URL(允许 * 通配符;因此 MyWebPage()),第三个是函数指针,最后是 uData
$ b

这样一个简单的例子:

  int HelloWorldPage(FILE * resp,const char *,const char * params,void *)
{
fprintf(Content-Type:text / html; \\\
\\\
);
fprintf(< html>< head>< title> Hello World!< / title>< / head> \\\

fprintf(< body> \\\
);
fprintf(< h1> Hello,World!< / h1> \\\
);
fprintf(< p>您通过:%s< / p> \\\
,params);
fprintf(< / body>< / html> \\\
);
}
CYG_HTTPD_TABLE_ENTRY(www_hello,/,HelloWorldPage,0)

(实际上, params 一个函数来逃避HTML魔术字符,我会使用另外几个函数来分割参数,并使一个< ul> ,但我离开了为了清楚起见。)



服务器本身只是作为一个任务运行(即线程),并没有阻碍,只要它的优先级低于临界任务。



不用说,这对于测试和调试已证明是无价的。 (嵌入式工作的一个问题是,你通常不能折腾一个XTerm用作日志。)所以当最高程序员反讽我的东西不工作(阻力最小的路径,我猜),我可以拉起网页,并显示他发送了我不好的参数。在集成中保存了很多调试时间。



无论如何... 我想知道,作为一个独立的库?我可以链接到,注册我的回调,产生一个线程,让它做魔术吗?还是我需要自行开发?我更喜欢C ++,但也可以使用C库。



编辑:因为我要给它一个赏金,我需要澄清的图书馆

解决方案

我建议你看看libmicrohttpd,嵌入式Web服务器:





它体积小,速度快,有一个简单的C API ,支持多线程,适用于嵌入式系统,支持POST,可选支持SSL / TLS,可根据LGPL或eCos许可证(取决于)提供。我相信这满足你的所有要求。如果你喜欢的话,在C ++中打包是很容易的。


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:

int MyWebPage(FILE* resp, const char* page, const char* params, void* uData);

where page was the page part of the url, params were any form parameters (only GET was supported, not POST, which prevented file uploads and thus made burning the flash a pain), uData 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 resp is a file handle that you write the HTTP response (headers and all) out to.

Then you registered the function with:

CYG_HTTPD_TABLE_ENTRY(www_myPage, "/", MyWebPage, 0);

where CYG_HTTPD_TABLE_ENTRY is a macro where the first parameter was a variable name, the second was a page URL (the * wildcard is allowed; hence page getting passed to MyWebPage()), third is the function pointer, and last is the uData value.

So a simple example:

int HelloWorldPage(FILE* resp, const char*, const char* params, void*)
{
    fprintf("Content-Type: text/html;\n\n");
    fprintf("<html><head><title>Hello World!</title></head>\n");
    fprintf("<body>\n");
    fprintf("<h1>Hello, World!</h1>\n");
    fprintf("<p>You passed in: %s</p>\n", params);
    fprintf("</body></html>\n");
}
CYG_HTTPD_TABLE_ENTRY(www_hello, "/", HelloWorldPage, 0);

(Actually, params 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 <ul> out of it, but I left that out for clarity.)

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.

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.

So anyway... I'm wondering, is there something like this available as an independent library? 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.

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.

解决方案

I suggest you have a look at libmicrohttpd, the embedded web server:

It is small and fast, has a simple C API, supports multithreading, is suitable for embedded systems, supports POST, optionally supports SSL/TLS, and is available under either the LGPL or eCos license (depending). I believe this fulfils all your requirements. It would be trivial to wrapper in C++ if you preferred.

这篇关于C或C ++ HTTP守护进程在线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆