mongoose 大于3.2 小于6.5版本demo 8080和443端口 chrome测试ok

#include <stdio.h>
#include <string.h>
#include "mongoose.h"




int SendResponse(const char *response, void *addInfo)
{
        struct mg_connection *conn = (struct mg_connection *) addInfo;
        if (mg_printf(conn, "HTTP/1.1 200 OK\r\n"
                "Content-Type: application/json\r\n"
                "Content-Length: %d\r\n"
                "\r\n"
                "%s", strlen(response), response) > 0) {
                return 1;
        } else {
                return 0;
        }
}






int callback(struct mg_connection *conn)
{
        const struct mg_request_info *request_info = mg_get_request_info(conn);
        char *readBuffer = NULL;
        int postSize = 0;






        if (strcmp(request_info->request_method, "GET") == 0) {


                        SendResponse("hello world", conn);
                        printf("get \n");
                        return 1;


        } else if (strcmp(request_info->request_method, "POST") == 0) {
                //get size of postData
                sscanf(mg_get_header(conn, "Content-Length"), "%d", &postSize);
                printf("post %d\n", postSize);


                //Mark the request as processed by our handler.
                return 1;
        } else {
                return 0;
        }
}


int main(void) {
  struct mg_context *ctx;
  const char *options[] = {"listening_ports", "8080,443s","ssl_certificate","ssl_cert.pem",NULL};
struct mg_callbacks callbacks;
                memset(&callbacks, 0, sizeof(callbacks));
callbacks.begin_request = callback;
  ctx = mg_start(&callbacks, NULL, options);
        printf(" started on port(s) %s with web root [%s]\n",
               "a", mg_get_option(ctx, "listening_ports"));


getchar();  // Wait until user hits "enter"
printf("char \n");
mg_stop(ctx);
printf("bye\n");
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值