安装ulfius启用http服务

https://github.com/babelouest/ulfius/releases/latest

现在地址,安装后打开INSTALL.md 文件,查看需要安装的依赖库文件

apt install -y libmicrohttpd-dev libjansson-dev libcurl4-gnutls-dev libgnutls28-dev libgcrypt20-dev zlib1g-dev

安装zip压缩包,继续手动安装依赖库:

Last Orcania release: [https://github.com/babelouest/orcania/releases/latest/](https://github.com/babelouest/orcania/releases/latest/)
Last Yder release: [https://github.com/babelouest/yder/releases/latest/](https://github.com/babelouest/orcania/releases/latest/)

注:这两个使用apt命令安装的版本太久,所以需要安装最新版本从源代码安装。

在configure.ac 编写:




   AC_CHECK_LIB([ulfius], [ulfius_init_instance], [], [AC_MSG_ERROR([ulfius library not found])])
   if test "$ulfius_found" = "yes"; then
      AC_DEFINE(HAVE_CRYPTO, 1, [Define when ulfius library is found])
      LIBS = "$LIBS -lulfius"




编写代码启用https服务:

do_something() //http方法触发的回调函数

// 读取ssl文件函数
static char * read_file(const char * filename) {
    char * buffer = NULL;
    long length;
    FILE * f;
    if (filename != NULL) {
        f = fopen (filename, "rb");
        if (f) {
            fseek (f, 0, SEEK_END);
            length = ftell (f);
            fseek (f, 0, SEEK_SET);
            buffer = malloc ((size_t)(length + 1));
            if (buffer != NULL) {
                fread (buffer, 1, (size_t)length, f);
                buffer[length] = '\0';
            }
            fclose (f);
        }
        return buffer;
    } else {
        return NULL;
    }
}
int https_init()  
{
    struct _u_instance g_instance;


	if (ulfius_init_instance(&g_instance, REST_PORT, NULL, NULL) != U_OK) {
		return -1;
	}// 初始化



    
    ulfius_add_endpoint_by_val(&g_instance, "GET", "/start", NULL, 0, &do_something, NULL); 

    // 读取ssl密钥
    char * key_pem = read_file("./ca.key"), * cert_pem = read_file("./ca.crt");
    
    // 启动https服务
    ret = ulfius_start_secure_framework(&g_instance, key_pem, cert_pem);

    free(key_pem);
    free(cert_pem);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值