IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> libhv教程18--动手写一个tinyhttpd -> 正文阅读

[Java知识库]libhv教程18--动手写一个tinyhttpd

libhv作为一个高性能异步网络库,想要快速入门,莫过于动手实现一个tinyhttpd(即微型HTTP服务)。
libhv examples目录下的 tinyhttpd.c 源代码包括注释在内不到400行,麻雀虽小,五脏俱全,代码绝对是工业级水准,可直接用于生产环境,不是和著名 Tinyhttpd 一样的玩具。
通过这个示例你将掌握事件循环TCP服务定时器拆包HTTP协议 等核心功能。

废话不多说,先上代码:

/*
 * tinyhttpd
 *
 * @build    make examples
 *
 * @server   bin/tinyhttpd 8000
 *
 * @client   bin/curl -v http://127.0.0.1:8000/
 *           bin/curl -v http://127.0.0.1:8000/ping
 *           bin/curl -v http://127.0.0.1:8000/echo -d "hello,world!"
 *
 * @webbench bin/wrk  http://127.0.0.1:8000/ping
 *
 */

#include "hv.h"
#include "hloop.h"

/*
 * workflow:
 * hloop_new -> hloop_create_tcp_server -> hloop_run ->
 * on_accept -> HV_ALLOC(http_conn_t) -> hio_readline ->
 * on_recv -> parse_http_request_line -> hio_readline ->
 * on_recv -> parse_http_head -> ...  -> hio_readbytes(content_length) ->
 * on_recv -> on_request -> http_reply-> hio_write -> hio_close ->
 * on_close -> HV_FREE(http_conn_t)
 *
 */

#define HTTP_KEEPALIVE_TIMEOUT  60000 // ms
#define HTTP_HEAD_MAX_LENGTH    1024

#define HTML_TAG_BEGIN  "<html><body><center><h1>"
#define HTML_TAG_END    "</h1></center></body></html>"

// status_message
#define HTTP_OK         "OK"
#define NOT_FOUND       "Not Found"
#define NOT_IMPLEMENTED "Not Implemented"

// Content-Type
#define TEXT_PLAIN      "text/plain"
#define TEXT_HTML       "text/html"

typedef enum {
    s_begin,
    s_first_line,
    s_request_line = s_first_line,
    s_status_line = s_first_line,
    s_head,
    s_head_end,
    s_body,
    s_end
} http_state_e;

typedef struct {
    // first line
    int             major_version;
    int             minor_version;
    union {
        // request line
        struct {
            char method[32];
            char path[256];
        };
        // status line
        struct {
            int  status_code;
            char status_message[64];
        };
    };
    // headers
    int         content_length;
    char        content_type[64];
    unsigned    keepalive:  1;
    // body
    const char* body; // body_len = content_length
} http_msg_t;

typedef struct {
    hio_t*          io;
    http_state_e    state;
    http_msg_t      request;
    http_msg_t      response;
} http_conn_t;

static char s_date[32] = {0};
static void update_date(htimer_t* timer) {
    uint64_t now = hloop_now(hevent_loop(timer));
    gmtime_fmt(now, s_date);
}

static int http_response_dump(http_msg_t* msg, char* buf, int len) {
    int offset = 0;
    // status line
    offset += snprintf(buf + offset, len - offset, "HTTP/%d.%d %d %s\r\n", msg->major_version, msg->minor_version, msg->status_code, msg->status_message);
    // headers
    offset += snprintf(buf + offset, len - offset, "Server: libhv/%s\r\n", hv_version());
    if (msg->content_length > 0) {
        offset += snprintf(buf + offset, len - offset, "Content-Length: %d\r\n", msg->content_length);
    }
    if (*msg->content_type) {
        offset += snprintf(buf + offset, len - offset, "Content-Type: %s\r\n", msg->content_type);
    }
    if (*s_date) {
        offset += snprintf(buf + offset, len - offset, "Date: %s\r\n", s_date);
    }
    // TODO: Add your headers
    offset += snprintf(buf + offset, len - offset, "\r\n");
    // body
    if (msg->body && msg->content_length > 0) {
        memcpy(buf + offset, msg->body, msg->content_length);
        offset += msg->content_length;
    }
    return offset;
}

static int http_reply(http_conn_t* conn,
            int status_code, const char* status_message,
            const char* content_type,
            const char* body, int body_len) {
    char stackbuf[HTTP_HEAD_MAX_LENGTH + 1024] = {0};
    char* buf = stackbuf;
    int buflen = sizeof(stackbuf);
    http_msg_t* req  = &conn->request;
    http_msg_t* resp = &conn->response;
    resp->major_version = req->major_version;
    resp->minor_version = req->minor_version;
    resp->status_code = status_code;
    if (status_message) strcpy(resp->status_message, status_message);
    if (content_type)   strcpy(resp->content_type, content_type);
    if (body) {
        if (body_len <= 0) body_len = strlen(body);
        resp->content_length = body_len;
        resp->body = body;
    }
    if (resp->content_length > buflen - HTTP_HEAD_MAX_LENGTH) {
        HV_ALLOC(buf, HTTP_HEAD_MAX_LENGTH + resp->content_length);
    }
    int msglen = http_response_dump(resp, buf, buflen);
    int nwrite = hio_write(conn->io, buf, msglen);
    if (buf != stackbuf) HV_FREE(buf);
    return nwrite < 0 ? nwrite : msglen;
}

static int http_serve_file(http_conn_t* conn) {
    http_msg_t* req = &conn->request;
    http_msg_t* resp = &conn->response;
    // GET / HTTP/1.1\r\n
    const char* filepath = req->path + 1;
    // homepage
    if (*filepath == '\0') {
        filepath = "index.html";
    }
    FILE* fp = fopen(filepath, "rb");
    if (!fp) {
        http_reply(conn, 404, NOT_FOUND, TEXT_HTML, HTML_TAG_BEGIN NOT_FOUND HTML_TAG_END, 0);
        return 404;
    }
    char buf[4096] = {0};
    // send head
    size_t filesize = hv_filesize(filepath);
    resp->content_length = filesize;
    const char* suffix = hv_suffixname(filepath);
    const char* content_type = NULL;
    if (strcmp(suffix, "html") == 0) {
        content_type = TEXT_HTML;
    } else {
        // TODO: set content_type by suffix
    }
    int nwrite = http_reply(conn, 200, "OK", content_type, NULL, 0);
    if (nwrite < 0) return nwrite; // disconnected
    // send file
    int nread = 0;
    while (1) {
        nread = fread(buf, 1, sizeof(buf), fp);
        if (nread <= 0) break;
        nwrite = hio_write(conn->io, buf, nread);
        if (nwrite < 0) return nwrite; // disconnected
        if (nwrite == 0) {
            // send too fast or peer recv too slow
            // WARN: too large file should control sending rate, just delay a while in the demo!
            hv_delay(10);
        }
    }
    fclose(fp);
    return 200;
}

static bool parse_http_request_line(http_conn_t* conn, char* buf, int len) {
    // GET / HTTP/1.1
    http_msg_t* req = &conn->request;
    sscanf(buf, "%s %s HTTP/%d.%d", req->method, req->path, &req->major_version, &req->minor_version);
    if (req->major_version != 1) return false;
    // printf("%s %s HTTP/%d.%d\r\n", req->method, req->path, req->major_version, req->minor_version);
    return true;
}

static bool parse_http_head(http_conn_t* conn, char* buf, int len) {
    http_msg_t* req = &conn->request;
    // Content-Type: text/html
    const char* key = buf;
    const char* val = buf;
    char* delim = strchr(buf, ':');
    if (!delim) return false;
    *delim = '\0';
    val = delim + 1;
    // trim space
    while (*val == ' ') ++val;
    // printf("%s: %s\r\n", key, val);
    if (stricmp(key, "Content-Length") == 0) {
        req->content_length = atoi(val);
    } else if (stricmp(key, "Content-Type") == 0) {
        strcpy(req->content_type, val);
    } else if (stricmp(key, "Connection") == 0) {
        if (stricmp(val, "keep-alive") == 0) {
            req->keepalive = 1;
        } else {
            req->keepalive = 0;
        }
    } else {
        // TODO: save head
    }
    return true;
}

static int on_request(http_conn_t* conn) {
    http_msg_t* req = &conn->request;
    // TODO: router
    if (strcmp(req->method, "GET") == 0) {
        // GET /ping HTTP/1.1\r\n
        if (strcmp(req->path, "/ping") == 0) {
            http_reply(conn, 200, "OK", TEXT_PLAIN, "pong", 4);
            return 200;
        } else {
            // TODO: Add handler for your path
        }
        return http_serve_file(conn);
    } else if (strcmp(req->method, "POST") == 0) {
        // POST /echo HTTP/1.1\r\n
        if (strcmp(req->path, "/echo") == 0) {
            http_reply(conn, 200, "OK", req->content_type, req->body, req->content_length);
            return 200;
        } else {
            // TODO: Add handler for your path
        }
    } else {
        // TODO: handle other method
    }
    http_reply(conn, 501, NOT_IMPLEMENTED, TEXT_HTML, HTML_TAG_BEGIN NOT_IMPLEMENTED HTML_TAG_END, 0);
    return 501;
}

static void on_close(hio_t* io) {
    // printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
    http_conn_t* conn = (http_conn_t*)hevent_userdata(io);
    if (conn) {
        HV_FREE(conn);
        hevent_set_userdata(io, NULL);
    }
}

static void on_recv(hio_t* io, void* buf, int readbytes) {
    char* str = (char*)buf;
    // printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
    // printf("%.*s", readbytes, str);
    http_conn_t* conn = (http_conn_t*)hevent_userdata(io);
    http_msg_t* req = &conn->request;
    switch (conn->state) {
    case s_begin:
        // printf("s_begin");
        conn->state = s_first_line;
    case s_first_line:
        // printf("s_first_line\n");
        if (readbytes < 2) {
            fprintf(stderr, "Not match \r\n!");
            hio_close(io);
            return;
        }
        str[readbytes - 2] = '\0';
        if (parse_http_request_line(conn, str, readbytes - 2) == false) {
            fprintf(stderr, "Failed to parse http request line:\n%s\n", str);
            hio_close(io);
            return;
        }
        // start read head
        conn->state = s_head;
        hio_readline(io);
        break;
    case s_head:
        // printf("s_head\n");
        if (readbytes < 2) {
            fprintf(stderr, "Not match \r\n!");
            hio_close(io);
            return;
        }
        if (readbytes == 2 && str[0] == '\r' && str[1] == '\n') {
            conn->state = s_head_end;
        } else {
            str[readbytes - 2] = '\0';
            if (parse_http_head(conn, str, readbytes - 2) == false) {
                fprintf(stderr, "Failed to parse http head:\n%s\n", str);
                hio_close(io);
                return;
            }
            hio_readline(io);
            break;
        }
    case s_head_end:
        // printf("s_head_end\n");
        if (req->content_length == 0) {
            conn->state = s_end;
            goto s_end;
        } else {
            // start read body
            conn->state = s_body;
            // WARN: too large content_length should read multiple times!
            hio_readbytes(io, req->content_length);
            break;
        }
    case s_body:
        // printf("s_body\n");
        req->body = str;
        if (readbytes == req->content_length) {
            conn->state = s_end;
        } else {
            // WARN: too large content_length should be handled by streaming!
            break;
        }
    case s_end:
s_end:
        // printf("s_end\n");
        // received complete request
        on_request(conn);
        if (hio_is_closed(io)) return;
        if (req->keepalive) {
            // Connection: keep-alive\r\n
            // reset and receive next request
            memset(&conn->request,  0, sizeof(http_msg_t));
            memset(&conn->response, 0, sizeof(http_msg_t));
            conn->state = s_first_line;
            hio_readline(io);
        } else {
            // Connection: close\r\n
            hio_close(io);
        }
        break;
    default: break;
    }
}

static void on_accept(hio_t* io) {
    // printf("on_accept connfd=%d\n", hio_fd(io));
    /*
    char localaddrstr[SOCKADDR_STRLEN] = {0};
    char peeraddrstr[SOCKADDR_STRLEN] = {0};
    printf("accept connfd=%d [%s] <= [%s]\n", hio_fd(io),
            SOCKADDR_STR(hio_localaddr(io), localaddrstr),
            SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
    */

    hio_setcb_close(io, on_close);
    hio_setcb_read(io, on_recv);
    hio_set_keepalive_timeout(io, HTTP_KEEPALIVE_TIMEOUT);

    http_conn_t* conn = NULL;
    HV_ALLOC_SIZEOF(conn);
    conn->io = io;
    hevent_set_userdata(io, conn);
    // start read first line
    conn->state = s_first_line;
    hio_readline(io);
}

int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Usage: %s port\n", argv[0]);
        return -10;
    }
    const char* host = "0.0.0.0";
    int port = atoi(argv[1]);

    hloop_t* loop = hloop_new(0);
    hio_t* listenio = hloop_create_tcp_server(loop, host, port, on_accept);
    if (listenio == NULL) {
        return -20;
    }
    printf("listenfd=%d\n", hio_fd(listenio));
    // NOTE: add timer to update date every 1s
    htimer_add(loop, update_date, 1000, INFINITE);
    hloop_run(loop);
    hloop_free(&loop);
    return 0;
}

编译:

git clone https://gitee.com/libhv/libhv.git
./configure
make

windows平台编译可查看libhv教程02–编译与安装

运行:

HTTP服务端

bin/tinyhttpd 8000

HTTP客户端

bin/curl -v http://127.0.0.1:8000/
bin/curl -v http://127.0.0.1:8000/ping
bin/curl -v http://127.0.0.1:8000/echo -d "hello,world!"

HTTP压测

bin/wrk http://127.0.0.1:8000/ping

Github Actions 上你可以找到测试结果,单线程也可轻松达到7w QPS
webbench

代码流程图

TCP服务

Created with Rapha?l 2.3.0 main hloop_new 创建事件循环 hloop_create_tcp_server 创建TCP服务 hloop_run 运行事件循环 hloop_free 释放事件循环 结束

HTTP请求响应

Created with Rapha?l 2.3.0 on_accept 接收到连接 hio_readline 读取一行 on_recv 接收到数据 parse_http_request_line 解析HTTP请求行 hio_readline 读取一行 on_recv 接收到数据 parse_http_head 解析HTTP头 ...... hio_readbytes 读取body on_recv 接收到数据 on_request 接收到完整的HTTP请求 http_reply HTTP响应 hio_write 发送数据 keepalive? hio_close 关闭连接 on_close 连接断开 yes no

代码解读

上面的流程图里基本展示了所有用到的核心函数,下面主要说下hio_readlinehio_readbytes两个用于流式读取的方法。

看过 libhv教程14–200行实现一个纯C版jsonrpc框架 这篇教程的应该知道,libhv里提供了一个设置拆包规则的函数hio_set_unpack,支持固定包长、分隔符、头部长度字段三种常见的拆包方式,内部会根据拆包规则处理粘包与分包,保证回调上来的是完整的一包数据。但是这个函数却并不适用于HTTP协议的拆包,HTTP协议是文本协议,看似比较简单,实际拆包却并不简单。

一个基本的HTTP POST请求如下所示:

POST /echo HTTP/1.1
Accept: */*
Content-Length: 5
Content-Type: text/plain
Host: 127.0.0.1:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

hello

HTTP头部是根据\r\n分割,body长度则由头部的Content-Length进行说明,以上三种拆包规则不能满足这种复合方式。所以libhv提供了hio_read_until_delimhio_read_until_length来完美cover这种情况。

  • hio_read_until_delim: 读取数据直到遇到指定的分隔符
  • hio_read_until_length:读取一定长度的数据

hio_readlinehio_readbytes其实是宏别名,见 hloop.h 头文件里的定义:

HV_EXPORT int hio_read_until_length(hio_t* io, unsigned int len);
HV_EXPORT int hio_read_until_delim (hio_t* io, unsigned char delim);
#define hio_readline(io)        hio_read_until_delim(io, '\n')
#define hio_readstring(io)      hio_read_until_delim(io, '\0')
#define hio_readbytes(io, len)  hio_read_until_length(io, len)

我们使用hio_readline读取HTTP头hio_readbytes读取HTTP body,就得到了完整的一份HTTP请求了。

当然,代码里还有很多小技巧等待你去挖掘,如用htimer_add添加了一个定时器去每秒更新GMT时间字符串,用于HTTP Date头,避免每次响应都要重复生成,追求极致性能。再如,hio_set_keepalive_timeout一行就实现了一段时间内无请求过来就自动断开连接以节省资源。

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-12-04 13:15:32  更:2021-12-04 13:18:47 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 3:32:45-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码