我遇到了一个名为mongoose的嵌入式Web服务器和http://code.google.com/p/mongoose/,我读了很棒的wiki,我搜索了一些示例hello world程序,但我找不到它..我找到了一些例子,但是这是用c ++ for windows编写的,任何人都可以提供一个示例c程序来运行这个webserver.
我正在使用libcurl向本地服务发送API命令(即在127.0.0.1上).
该程序旨在替换shell脚本(使用该curl程序.)
一切都在工作,除了在某个地方有1秒的延迟,即从我打电话curl_easy_perform()到第一次调用我的读回调函数时经过1秒.
C程序正在使用这些选项(省略了错误检查和回调代码):
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:12345/x");
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(curl, CURLOPT_INFILESIZE, (long)getLengthOfCommandObject());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &myReadFunction);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &myWriteFunction);
Run Code Online (Sandbox Code Playgroud)
但是,如果我curl像这样从shell 运行:
$ curl --data-binary '<command>' http://127.0.0.1:12345/x
Run Code Online (Sandbox Code Playgroud)
它会立即发送请求,而不会受到1秒延迟的影响.
可能导致延迟的原因是什么,我可以设置一个选项来阻止它吗?
编辑服务器基于mongoose
我想在我的应用程序上公开REST API,使用Mongoose Web服务器并为不同的查询提供处理程序.
查询的一个例子就是这样(我现在只使用GET,其余的HTTP动词将在以后出现):
GET /items -> returns a list of all items in JSON
GET /item/by/handle/123456789 -> returns item that has handle 123456789
GET /item/by/name/My%20Item -> returns item(s) that have the name "My Item"
Run Code Online (Sandbox Code Playgroud)
我很好奇的是我应该如何实现这些查询的解析.我可以很容易地解析第一个,因为它只是一个问题if( query.getURI() == "/items") return ....
但是对于接下来的两个查询,我必须以std::完全不同的方式操纵字符串,使用一些std::string::find()魔法和偏移来获得参数.
作为示例,这是我对第二个查询的实现:
size_t position = std::string::npos;
std::string path = "/item/by/handle/";
if( (position = query.getURI().find(path) ) != std::string::npos )
{
std::string argument = query.getURI().substr( position + path.size() );
// now parse the argument …Run Code Online (Sandbox Code Playgroud) 我尝试在 PHP 和 JS 中对两个字符串进行异或,但得到了不同的结果:
PHP函数
function xh($a, $b) {
$res = ""; $i = strlen($a); $j = strlen($b);
while($i-->0 && $j-->0) {
$res.= $a[$i] ^ $b[$j];
}
return base64_encode($res);
}
Run Code Online (Sandbox Code Playgroud)
JS函数
function xh(a, b) {
var res = "", i = a.length, j = b.length;
while (i-->0 && j-->0) {
res+= String.fromCharCode(a.charCodeAt(i) ^ b.charCodeAt(j));
}
return btoa(res);
}
Run Code Online (Sandbox Code Playgroud)
我检查了字节,发现 PHP 函数中的第六个字节始终为零,因此我将 JS 函数更新为
相当于PHP的JS函数
function xh2(a, b) {
var res = "", i = a.length, j = b.length;
while …Run Code Online (Sandbox Code Playgroud) 我尝试使用keep-alive连接mongoose,但似乎mongoose首先关闭连接.
我更改了embed.c以发回连接:keep-alive.响应后,连接仍然关闭.
border@ubuntu:~$ nc 127.0.0.1 9999
GET /test_get_request_info HTTP/1.1
Connection: keep-alive
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: keep-alive
Method: [GET]
URI: [/test_get_request_info]
HTTP version: [1/1]
HTTP header [Connection]: [keep-alive]
Query string: []
POST data: []
Remote IP: [2130706433]
Remote port: [56719]
Remote user: [] <-----------------connection closed, nc returns
border@ubuntu:~$
Run Code Online (Sandbox Code Playgroud) 我最近尝试使用代码:: blocks编译在Mongoose项目网站上链接的示例并获得某些错误.由于我不知道哪里出错了,我会列出我在这个过程中所做的一切.
正在使用的操作系统:Microsoft Windows 8(64位)
正在使用的Code :: Blocks版本:带有MinGW的codeblocks-12.11
Code :: Blocks:GNU GCC编译器使用的编译器
以下是Code :: Blocks生成的构建消息:
D:\pdthrow\cppprojects\server\mongoose.c|176|warning: "INT64_MAX" redefined [enabled by default]| e:\8\code blocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\stdint.h|95|note: this is the location of the previous definition|
D:\pdthrow\cppprojects\server\mongoose.c||In function 'set_ports_option':|
D:\pdthrow\cppprojects\server\mongoose.c|4626|warning: format '%d' expects argument of type 'int', but argument 6 has type 'DWORD' [-Wformat]|
D:\pdthrow\cppprojects\server\mongoose.c||In function 'getreq':|
D:\pdthrow\cppprojects\server\mongoose.c|5027|warning: implicit declaration of function '_strtoi64' [-Wimplicit-function-declaration]| …Run Code Online (Sandbox Code Playgroud) 简介: - (GCC版本4.6.3,OS-Ubuntu 12.04,解决mongoose Web服务器程序,所以当我运行"make"命令来编译和安装mongoose时,它完成了任务很好).
[问题的第1部分] 这个问题是关于stackowerflow的这篇文章.
Valenok通过提供hello示例程序的链接回复了这篇文章.
基本上,我正在尝试编译此链接上给出的示例hello程序代码: -
http://code.google.com/p/mongoose/source/browse/examples/hello.c
并将此代码放在已编译的mongoose目录中.(目录有mongoose.h文件)
以下是我编译hello程序的命令行输出.
akshay@akshay-Inspiron-N5010:~$ gcc mongoose/hello.c -o mongoose/hello
/tmp/ccroC5Z6.o: In function `callback':
hello.c:(.text+0x32): undefined reference to `mg_get_request_info'
hello.c:(.text+0x96): undefined reference to `mg_printf'
/tmp/ccroC5Z6.o: In function `main':
hello.c:(.text+0xee): undefined reference to `mg_start'
hello.c:(.text+0x103): undefined reference to `mg_stop'
collect2: ld returned 1 exit status
akshay@akshay-Inspiron-N5010:~$
Run Code Online (Sandbox Code Playgroud)
[问题的第2部分]
现在,我在mongoose.c文件中找到mg_stop,mg_start,mg_printf和mg_get_request_info的实现,因此我使用-c选项编译mongoose.c文件:gcc -c -o mongoose.o mongoose.c
我想我的问题类似于: -
但是当我在gcc上链接libmongoose.so和-L选项时,我得到以下错误: - (libmongoose.so存在于同一目录中,我的cwd)
akshay@akshay-Inspiron-N5010:~/mongoose$ gcc -L libmongoose.so -o hello hello.o mongoose.o
mongoose.o: In function `mg_start_thread':
mongoose.c:(.text+0x1369): …Run Code Online (Sandbox Code Playgroud) 我试图运行mongoose c服务器示例,但是当我尝试编译示例时.我收到以下错误.如果我将它包含在标题中,这些引用怎么会丢失?我在windows下用mingw编译.
gcc echo_server.c -out echo_server
echo_server.c:(.text+0x35): undefined reference to `mg_send'
echo_server.c:(.text+0x4a): undefined reference to `mbuf_remove'
echo_server.c:(.text+0x7f): undefined reference to `mg_mgr_init'
echo_server.c:(.text+0x9b): undefined reference to `mg_bind'
echo_server.c:(.text+0xb7): undefined reference to `mg_bind'
echo_server.c:(.text+0xe7): undefined reference to `mg_mgr_poll'
Run Code Online (Sandbox Code Playgroud)
这是echo_server.c
#include "mongoose.h"
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
struct mbuf *io = &nc->recv_mbuf;
(void) p;
switch (ev) {
case MG_EV_RECV:
mg_send(nc, io->buf, io->len); // Echo message back
mbuf_remove(io, io->len); // Discard message from recv buffer
break;
default:
break;
} …Run Code Online (Sandbox Code Playgroud) c windows compiler-errors undefined-reference mongoose-web-server
c ×3
c++ ×3
curl ×1
http ×1
javascript ×1
keep-alive ×1
libcurl ×1
linux ×1
parsing ×1
php ×1
rest ×1
web-services ×1
windows ×1
xor ×1