Mongoose只有一个.h一个.c,而且能够很好的跨平台
https://github.com/cesanta/mongoose/
可以用来开发小型的嵌入式服务器,也可以用来做为支持HTTPS请求的客户端的解决方案。
一般来说,要想实现HTTPS请求,要不使用WinAPI,要不使用curl。
WinAPI不能跨平台,但是体积小。
curl能跨平台,但是需要占用较大的程序空间。
mongoose由于设计给ARM使用的,程序相对会比较小。
测试一下能不能为VisualFreeBasic所用
下载https://codeload.github.com/cesanta/mongoose/zip/refs/tags/7.9
切换到examples/http-server文件夹
输入
make mingw
会报错
make[1]: Entering directory '/d/mongoose-7.9/examples/http-server'
cc ../../mongoose.c main.c -I../.. -I../.. -W -Wall -DMG_ENABLE_LINES=1 -DMG_ENABLE_IPV6=1 -DMG_ENABLE_SSI=1 -DMG_HTTP_DIRLIST_TIME=1 -o example
make[1]: cc: No such file or directory
make[1]: *** [Makefile:12: example] Error 127
make[1]: Leaving directory '/d/mongoose-7.9/examples/http-server'
make: *** [Makefile:45: all] Error 2
额,缺少cc指令。。搞不懂为什么不用gcc。。
切换到mssy2的mingw32/bin/目录,执行以下
ln -s gcc.exe cc.exe
增加一个快捷链接呗。。
再重新执行
make mingw
编译成功
测试了一下demo,效果确实不错
下面尝试一下把库编译好之后给VisualFreeBasic使用
不过要想支持SSL/TLS协议,我们得先编译好mbedtls
作者对OpenSSL的体积很不爽,所以考虑使用mbedtls
编译的时候,发现mbedtls的aes加密也支持的很完善,这个下面我们再抽个时间把这个也剥离来做一下测试
https://github.com/Mbed-TLS/mbedtls/releases/tag/v3.4.0
下载之后,直接命令
make
编译出来的libmbedcrypto.a(740k)、libmbedtls.a(256k)、libmbedx509.a(78k)拷贝到Compile\expand\lib\win32目录
PS:感觉体积很合理啊。。
下面再编译,要特别注意的是要编译的时候,要加-I 参数,后面是mbedtls源码目录下面的include文件夹,不然编译器会找不到mbedtls/debug.h这些头文件而失败
gcc -c mongoose.c -DMG_ENABLE_MBEDTLS=1 -I ../mbedtls/include
ar rcs libmongoose.a mongoose.o
编译得到libmongoose.a
把libmbedcrypto.a、libmbedtls.a、libmbedx509.a、libmongoose.a拷贝到Compile\expand\lib\win32文件夹
下一步使用
拷贝fbfrog.exe把defaut.h中下面三行删除
#define __INT64_C(c) c ## LL
#define __UINT64_C(c) c##ULL
#define CHAR_BIT 8
把
#if defined __FB_DOS__ || \
defined __FB_WIN32__ || \
defined __FB_CYGWIN__
#define __USER_LABEL_PREFIX__ _
#else
#define __USER_LABEL_PREFIX__
#endif
也删除了
fbfrog.exe *.h -removeinclude direct.h -removeinclude fcntl.h -removeinclude signal.h -removeinclude sys/stat.h -removeinclude stdbool.h -removeinclude ws2tcpip.h -removeinclude process.h -removeinclude winerror.h -removeinclude winsock2.h -define DMG_ENABLE_MBEDTLS 1 -define MG_ARCH MG_ARCH_WIN32 -define MG_ARCH MG_ARCH_WIN32 -define __cplusplus -o mongoose.bi
编译出来头文件mongoose.bi
把
#define sleep(x) Sleep(x)
#define mkdir(a, b) _mkdir(a)
#define mg_str(s) mg_str_s(s)
#define MG_LOG(level, args) scope : /' TODO: if (mg_log_prefix((level), __FILE__, __LINE__, __func__)) mg_log args; '/ : end scope
#define MG_ERROR(args) MG_LOG(MG_LL_ERROR, args)
#define MG_INFO(args) MG_LOG(MG_LL_INFO, args)
#define MG_DEBUG(args) MG_LOG(MG_LL_DEBUG, args)
#define MG_VERBOSE(args) MG_LOG(MG_LL_VERBOSE, args)
这几行删除
并将文件拷贝到Compile\expand\inc文件夹
到这里,我们c转freebasic的准备工作完成了
使用VisualFreeBasic创建一个标准exe项目,在起始模块加入库引用
#inclib "ws2_32"
#inclib "mbedtls"
#inclib "mbedcrypto"
#inclib "mbedx509"
#inclib "mongoose"
在Form1加上
Sub Form1_Shown(hWndForm As hWnd, UserData As Integer)
Threaddetach ThreadCreate(@Form1_WebServer, 0)
End Sub
Sub Form1_WebServer(ByVal userdata As Any Ptr)
Dim mgr As mg_mgr
mg_log_set(MG_LL_DEBUG)
mg_mgr_init(@mgr)
Dim c As mg_connection Ptr = mg_http_listen(@mgr, "http://0.0.0.0:80", @Form1_CallBack, NULL)
If c = NULL then
Debug.Print "监听失败"
Return
End If
Do
mg_mgr_poll(@mgr, 1000)
Loop Until False
Debug.Print "程序结束"
End Sub
Sub Form1_CallBack cdecl(ByVal c As mg_connection Ptr, ByVal ev As Long, ByVal ev_data As Any Ptr, ByVal fn_data As Any Ptr)
If ev = MG_EV_HTTP_MSG Then
mg_http_reply(c, 200, "", "OK")
End If
End Sub
要特别注意,回调函数得指定是cdecl的,不然首次调用结束,程序就会崩溃。
最后生成的程序体积为858K,也就是说集成一个支持SSL/TLS的Web服务器,一共只需要707K,这已经是一个极其迷你的体积了。
通过这个模块,可以实现自定义的API服务器,保证客户端和服务器之间的通信是加密的。