Linux C/C++ reactor模式下实现简易的web服务器

简介: Linux C/C++ reactor模式下实现简易的web服务器

一、HTTP协议

1.客户端请求消息

2.服务器响应消息

HTTP 响应也由四个部分组成,分别是:状态行、消息报头、空行和响应正文。

3.http请求与响应实例

4.实现内容

比如在服务器目录下存在一个文件,index.html

(例子)需要在浏览器输入 192.168.192.128:8888/index.html

根据服务器的ip以及端口号,资源名称,进行http请求

响应后,可以在浏览器查看到index.html的内容。

二、reactor模式下如何实现web服务

Linux C/C++ reactor原理与实现的reactor基础上,引入业务逻辑

通过在recv_cb(接受回调函数)中 接受客户端http请求

通过在send_cb(发送回调函数)中 发送http响应

在accept_cb(接受客户端连接的回调函数)中可以做 1.ip限制 2.负载均衡 [暂不实现]

http的tcp连接的生命周期为 1.accept_cb 2.recv_cb 3.send_cb

三、完整代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#define BUFFER_LENGTH   4096
#define MAX_EPOLL_EVENTS  1024
#define SERVER_PORT     8888
#define PORT_COUNT      1
#define HTTP_WEBSERVER_HTML_ROOT  "html"
#define HTTP_METHOD_GET   0
#define HTTP_METHOD_POST  1
typedef int NCALLBACK(int ,int, void*);
struct ntyevent {
  int fd;
  int events;
  void *arg;
  int (*callback)(int fd, int events, void *arg);
  int status;
  char buffer[BUFFER_LENGTH];
  int length;
  long last_active;
  // http param
  int method; //GET or POST
  char resource[BUFFER_LENGTH]; //资源地址
  int ret_code;  //http 状态码
};
struct eventblock {
  struct eventblock *next;
  struct ntyevent *events;
};
struct ntyreactor {
  int epfd;
  int blkcnt;
  struct eventblock *evblk; //fd --> 100w
};
int recv_cb(int fd, int events, void *arg);
int send_cb(int fd, int events, void *arg);
struct ntyevent *ntyreactor_idx(struct ntyreactor *reactor, int sockfd);
void nty_event_set(struct ntyevent *ev, int fd, NCALLBACK callback, void *arg) {
  ev->fd = fd;
  ev->callback = callback;
  ev->events = 0;
  ev->arg = arg;
  ev->last_active = time(NULL);
  return ;
}
int nty_event_add(int epfd, int events, struct ntyevent *ev) {
  struct epoll_event ep_ev = {0, {0}};
  ep_ev.data.ptr = ev;
  ep_ev.events = ev->events = events;
  int op;
  if (ev->status == 1) {
    op = EPOLL_CTL_MOD;
  } else {
    op = EPOLL_CTL_ADD;
    ev->status = 1;
  }
  if (epoll_ctl(epfd, op, ev->fd, &ep_ev) < 0) {
    printf("event add failed [fd=%d], events[%d]\n", ev->fd, events);
    return -1;
  }
  return 0;
}
int nty_event_del(int epfd, struct ntyevent *ev) {
  struct epoll_event ep_ev = {0, {0}};
  if (ev->status != 1) {
    return -1;
  }
  ep_ev.data.ptr = ev;
  ev->status = 0;
  epoll_ctl(epfd, EPOLL_CTL_DEL, ev->fd, &ep_ev);
  return 0;
}
int readline(char *allbuf, int idx, char *linebuf) {
  int len = strlen(allbuf);
  for(;idx < len;idx ++) {
    if (allbuf[idx] == '\r' && allbuf[idx+1] == '\n') {
      return idx+2;
    } else {
      *(linebuf++) = allbuf[idx];
    }
  }
  return -1;
}
int http_request(struct ntyevent *ev) {
  // GET, POST
  char linebuf[1024] = {0};
  int idx = readline(ev->buffer, 0, linebuf);
  if (strstr(linebuf, "GET")) {
    ev->method = HTTP_METHOD_GET;
    //uri
    int i = 0;
    while (linebuf[sizeof("GET ") + i] != ' ') i++;
    linebuf[sizeof("GET ")+i] = '\0';
    sprintf(ev->resource, "./%s/%s", HTTP_WEBSERVER_HTML_ROOT, linebuf+sizeof("GET "));
  } else if (strstr(linebuf, "POST")) {
  }
}
int recv_cb(int fd, int events, void *arg) {
  struct ntyreactor *reactor = (struct ntyreactor*)arg;
  struct ntyevent *ev = ntyreactor_idx(reactor, fd);
  int len = recv(fd, ev->buffer, BUFFER_LENGTH, 0); // 
  if (len > 0) {
    ev->length = len;
    ev->buffer[len] = '\0';
    printf("C[%d]:%s\n", fd, ev->buffer); //http
    http_request(ev);
    //send();
    nty_event_del(reactor->epfd, ev);
    nty_event_set(ev, fd, send_cb, reactor);
    nty_event_add(reactor->epfd, EPOLLOUT, ev);
  } else if (len == 0) {
    nty_event_del(reactor->epfd, ev);
    close(ev->fd);
    //printf("[fd=%d] pos[%ld], closed\n", fd, ev-reactor->events);
  } else {
    nty_event_del(reactor->epfd, ev);
    close(ev->fd);
    printf("recv[fd=%d] error[%d]:%s\n", fd, errno, strerror(errno));
  }
  return len;
}
int http_response(struct ntyevent *ev) {
  if (ev == NULL) return -1;
  memset(ev->buffer, 0, BUFFER_LENGTH);
#if 0
  const char *html = "<html><head><title>hello http</title></head><body><H1>King</H1></body></html>\r\n\r\n";
  ev->length = sprintf(ev->buffer, 
    "HTTP/1.1 200 OK\r\n\
     Date: Thu, 11 Nov 2021 12:28:52 GMT\r\n\
     Content-Type: text/html;charset=ISO-8859-1\r\n\
     Content-Length: 83\r\n\r\n%s", 
     html);
#else
  printf("resource: %s\n", ev->resource);
  int filefd = open(ev->resource, O_RDONLY);//打开html文件资源
  if (filefd == -1) { // return 404
    ev->ret_code = 404;
    ev->length = sprintf(ev->buffer, 
      "HTTP/1.1 404 Not Found\r\n"
      "Date: Thu, 11 Nov 2021 12:28:52 GMT\r\n"
      "Content-Type: text/html;charset=ISO-8859-1\r\n"
      "Content-Length: 85\r\n\r\n"
      "<html><head><title>404 Not Found</title></head><body><H1>404</H1></body></html>\r\n\r\n" );
  } else {
    struct stat stat_buf;//用来获取文件的长度
    fstat(filefd, &stat_buf);
    close(filefd);
    if (S_ISDIR(stat_buf.st_mode)) {
      ev->ret_code = 404;
      ev->length = sprintf(ev->buffer, 
        "HTTP/1.1 404 Not Found\r\n"
        "Date: Thu, 11 Nov 2021 12:28:52 GMT\r\n"
        "Content-Type: text/html;charset=ISO-8859-1\r\n"
        "Content-Length: 85\r\n\r\n"
        "<html><head><title>404 Not Found</title></head><body><H1>404</H1></body></html>\r\n\r\n" );
    } else if (S_ISREG(stat_buf.st_mode)) {
      ev->ret_code = 200;
      ev->length = sprintf(ev->buffer, 
        "HTTP/1.1 200 OK\r\n"
        "Date: Thu, 11 Nov 2021 12:28:52 GMT\r\n"
        "Content-Type: text/html;charset=ISO-8859-1\r\n"
        "Content-Length: %ld\r\n\r\n", 
          stat_buf.st_size );
    }
  }
#endif
  return ev->length;
}
int send_cb(int fd, int events, void *arg) {
  struct ntyreactor *reactor = (struct ntyreactor*)arg;
  struct ntyevent *ev = ntyreactor_idx(reactor, fd);
  http_response(ev);
  //
  int len = send(fd, ev->buffer, ev->length, 0); //第一次send http_header
  if (len > 0) {
    printf("send[fd=%d], [%d]%s\n", fd, len, ev->buffer);
    if (ev->ret_code == 200) {
      int filefd = open(ev->resource, O_RDONLY);
      struct stat stat_buf;
      fstat(filefd, &stat_buf);
      sendfile(fd, filefd, NULL, stat_buf.st_size);//第二次 send file
      close(filefd);
    }
    nty_event_del(reactor->epfd, ev);
    nty_event_set(ev, fd, recv_cb, reactor);
    nty_event_add(reactor->epfd, EPOLLIN, ev);
  } else {
    close(ev->fd);
    nty_event_del(reactor->epfd, ev);
    printf("send[fd=%d] error %s\n", fd, strerror(errno));
  }
  return len;
}
int accept_cb(int fd, int events, void *arg) {
  struct ntyreactor *reactor = (struct ntyreactor*)arg;
  if (reactor == NULL) return -1;
  struct sockaddr_in client_addr;
  socklen_t len = sizeof(client_addr);
  int clientfd;
  if ((clientfd = accept(fd, (struct sockaddr*)&client_addr, &len)) == -1) {
    if (errno != EAGAIN && errno != EINTR) {
    }
    printf("accept: %s\n", strerror(errno));
    return -1;
  }
  int flag = 0;
  if ((flag = fcntl(clientfd, F_SETFL, O_NONBLOCK)) < 0) {
    printf("%s: fcntl nonblocking failed, %d\n", __func__, MAX_EPOLL_EVENTS);
    return -1;
  }
  struct ntyevent *event = ntyreactor_idx(reactor, clientfd);
  nty_event_set(event, clientfd, recv_cb, reactor);
  nty_event_add(reactor->epfd, EPOLLIN, event);
  printf("new connect [%s:%d], pos[%d]\n", 
    inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), clientfd);
  return 0;
}
int init_sock(short port) {
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  fcntl(fd, F_SETFL, O_NONBLOCK);
  struct sockaddr_in server_addr;
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(port);
  bind(fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
  if (listen(fd, 20) < 0) {
    printf("listen failed : %s\n", strerror(errno));
  }
  return fd;
}
int ntyreactor_alloc(struct ntyreactor *reactor) {
  if (reactor == NULL) return -1;
  if (reactor->evblk == NULL) return -1;
  struct eventblock *blk = reactor->evblk;
  while (blk->next != NULL) {
    blk = blk->next;
  }
  struct ntyevent *evs = (struct ntyevent*)malloc((MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
  if (evs == NULL) {
    printf("ntyreactor_alloc ntyevents failed\n");
    return -2;
  }
  memset(evs, 0, (MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
  struct eventblock *block = (struct eventblock *)malloc(sizeof(struct eventblock));
  if (block == NULL) {
    printf("ntyreactor_alloc eventblock failed\n");
    return -2;
  }
  memset(block, 0, sizeof(struct eventblock));
  block->events = evs;
  block->next = NULL;
  blk->next = block;
  reactor->blkcnt ++; //
  return 0;
}
struct ntyevent *ntyreactor_idx(struct ntyreactor *reactor, int sockfd) {
  int blkidx = sockfd / MAX_EPOLL_EVENTS;
  while (blkidx >= reactor->blkcnt) {
    ntyreactor_alloc(reactor);
  }
  int i = 0;
  struct eventblock *blk = reactor->evblk;
  while(i ++ < blkidx && blk != NULL) {
    blk = blk->next;
  }
  return &blk->events[sockfd % MAX_EPOLL_EVENTS];
}
int ntyreactor_init(struct ntyreactor *reactor) {
  if (reactor == NULL) return -1;
  memset(reactor, 0, sizeof(struct ntyreactor));
  reactor->epfd = epoll_create(1);
  if (reactor->epfd <= 0) {
    printf("create epfd in %s err %s\n", __func__, strerror(errno));
    return -2;
  }
  struct ntyevent *evs = (struct ntyevent*)malloc((MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
  if (evs == NULL) {
    printf("ntyreactor_alloc ntyevents failed\n");
    return -2;
  }
  memset(evs, 0, (MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
  struct eventblock *block = (struct eventblock *)malloc(sizeof(struct eventblock));
  if (block == NULL) {
    printf("ntyreactor_alloc eventblock failed\n");
    return -2;
  }
  memset(block, 0, sizeof(struct eventblock));
  block->events = evs;
  block->next = NULL;
  reactor->evblk = block;
  reactor->blkcnt = 1;
  return 0;
}
int ntyreactor_destory(struct ntyreactor *reactor) {
  close(reactor->epfd);
  //free(reactor->events);
  struct eventblock *blk = reactor->evblk;
  struct eventblock *blk_next = NULL;
  while (blk != NULL) {
    blk_next = blk->next;
    free(blk->events);
    free(blk);
    blk = blk_next;
  }
  return 0;
}
int ntyreactor_addlistener(struct ntyreactor *reactor, int sockfd, NCALLBACK *acceptor) {
  if (reactor == NULL) return -1;
  if (reactor->evblk == NULL) return -1;
  //reactor->evblk->events[sockfd];
  struct ntyevent *event = ntyreactor_idx(reactor, sockfd);
  nty_event_set(event, sockfd, acceptor, reactor);
  nty_event_add(reactor->epfd, EPOLLIN, event);
  return 0;
}
int ntyreactor_run(struct ntyreactor *reactor) {
  if (reactor == NULL) return -1;
  if (reactor->epfd < 0) return -1;
  if (reactor->evblk == NULL) return -1;
  struct epoll_event events[MAX_EPOLL_EVENTS+1];
  int checkpos = 0, i;
  while (1) {
/*
    long now = time(NULL);
    for (i = 0;i < 100;i ++, checkpos ++) {
      if (checkpos == MAX_EPOLL_EVENTS) {
        checkpos = 0;
      }
      if (reactor->events[checkpos].status != 1) {
        continue;
      }
      long duration = now - reactor->events[checkpos].last_active;
      if (duration >= 60) {
        close(reactor->events[checkpos].fd);
        printf("[fd=%d] timeout\n", reactor->events[checkpos].fd);
        nty_event_del(reactor->epfd, &reactor->events[checkpos]);
      }
    }
*/
    int nready = epoll_wait(reactor->epfd, events, MAX_EPOLL_EVENTS, 1000);
    if (nready < 0) {
      printf("epoll_wait error, exit\n");
      continue;
    }
    for (i = 0;i < nready;i ++) {
      struct ntyevent *ev = (struct ntyevent*)events[i].data.ptr;
      if ((events[i].events & EPOLLIN) && (ev->events & EPOLLIN)) {
        ev->callback(ev->fd, events[i].events, ev->arg);
      }
      if ((events[i].events & EPOLLOUT) && (ev->events & EPOLLOUT)) {
        ev->callback(ev->fd, events[i].events, ev->arg);
      }
    }
  }
}
// 3, 6w, 1, 100 == 
// <remoteip, remoteport, localip, localport>
int main(int argc, char *argv[]) {
  unsigned short port = SERVER_PORT; // listen 8888
  if (argc == 2) {
    port = atoi(argv[1]);
  }
  struct ntyreactor *reactor = (struct ntyreactor*)malloc(sizeof(struct ntyreactor));
  ntyreactor_init(reactor);
  int i = 0;
  int sockfds[PORT_COUNT] = {0};
  for (i = 0;i < PORT_COUNT;i ++) {
    sockfds[i] = init_sock(port+i);
    ntyreactor_addlistener(reactor, sockfds[i], accept_cb);
  }
  ntyreactor_run(reactor);
  ntyreactor_destory(reactor);
  for (i = 0;i < PORT_COUNT;i ++) {
    close(sockfds[i]);
  }
  free(reactor);
  return 0;
}

四、补充

1、规范写法

如果 recv接收到的请求,没有读完的话,就要再一次设置为EPOLLIN,让它继续读(ET模式),(如果是LT模式就没有关系)

因此不能直接send()数据给客户端,因为可能会没有读完。因此需要一个判断,下次是EPOLLIN还是EPOLLOUT,来作为响应,如果没读完,就继续EPOLLIN,否则就EPOLLOUT

2、查看http请求内容

Linux C/C++ reactor原理与实现的reactor直接利用浏览器去访问该服务器,如果直接对recv的数据,打印出来的话,可以看到,就是http请求的内容

3、粘包

TCP 协议可能会将应用层发送的数据分成多个包依次发送,服务端需要对收到的数据进行拆分,如果拆分不好,就可能出现粘包的现象。

例子:比如有3个客户端分别发送3个1k的数据给 服务器,但是服务器收到了2个1.5k的数据(粘包)

解决办法:

  • 分隔符\r\n\r\n
  • 定义包的长度

4、可以将包多次发送给客户端

服务器对客户端响应的时候,可以一次性send (header和body),也可以多次发送,只要保证数据完善就行了

5、利用分隔符\r\n\r\n分隔报头和正文

注意 消息报头和响应正文用两个\r\n分隔。响应正文末尾也用两个\r\n

6、资源文件

客户端请求资源的根目录

客户端就可以直接访问资源,也就是服务器对应资源根目录下的文件

假设目录下有index.html,那么根据 根目录,和文件名,就可以在浏览器中访问到

7、sendfile

如果服务器将磁盘中的文件件,拷贝到内存中,再给sockfd, 发送给客服端,会有拷贝的过程,影响速度。sendfile函数在两个文件描述符之间传递数据(完全在内核中操作),从而避免了内核缓冲区和用户缓冲区之间的数据拷贝,效率很高,被称为零拷贝。

高级I/O函数之sendfile函数

8、浏览器出现一直转圈的情况

如果 http头部的content-Length和 响应正文的长度 不匹配的话,浏览器内访问 的时候就会一直转圈

9、事件event中新加入3个参数

其中method对应GET或者POST

resource为资源地址

ret_code为HTTP 状态码(比如200为成功,404为未找到资源)


相关文章
|
25天前
|
存储 弹性计算 编解码
ecs实例规格工作负载模式
阿里云ECS实例有多种工作负载模式:计算密集型(适合高性能计算)、内存密集型(适用于内存数据库)、通用型(平衡资源,多场景适用)、大数据型(优化大数据分析)、共享型(低成本,轻负载)和企业级实例(高稳定性和隔离性)。用户依据业务需求选择实例规格,结合SLB和ESS服务可优化架构,应对动态负载。
22 4
|
26天前
|
设计模式 Java uml
C++设计模式之 依赖注入模式探索
C++设计模式之 依赖注入模式探索
36 0
|
25天前
|
编解码 Linux C语言
探索C++与Live555实现RTSP服务器的艺术(一)
探索C++与Live555实现RTSP服务器的艺术
67 1
|
28天前
|
网络协议 Shell 网络安全
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
161 0
|
21天前
|
前端开发 应用服务中间件 nginx
使用Docker快速搭建Web服务器Nginx
本文指导如何使用Docker快速搭建Nginx服务器。首先,通过`docker pull`命令获取Nginx镜像,然后以容器形式运行Nginx并映射端口。通过挂载目录实现本地文件与容器共享,便于自定义网页。使用`docker ps`检查运行状态,访问IP:8088确认部署成功。最后,介绍了停止、删除Nginx容器的命令,强调Docker简化了服务器部署和管理。
36 0
|
14天前
|
Ubuntu Linux 虚拟化
【Linux】ubuntu安装samba服务器
【Linux】ubuntu安装samba服务器
|
20天前
|
算法 编译器 程序员
深入理解C++编译模式:了解Debug和Release的区别
深入理解C++编译模式:了解Debug和Release的区别
59 2
|
21天前
|
设计模式 算法 编译器
【C/C++ PIMPL模式 】 深入探索C++中的PIMPL模式
【C/C++ PIMPL模式 】 深入探索C++中的PIMPL模式
50 0
|
24天前
|
Shell Linux Apache
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
154 1
|
25天前
|
编解码 C++ 流计算
探索C++与Live555实现RTSP服务器的艺术(三)
探索C++与Live555实现RTSP服务器的艺术
19 1