一、http服务器工作的原理
由于http协议是在tcp协议基础上封装了一层,所以收到数据后需要解析后再打包发给客户端。
具体过程:
在浏览器中输入目标IP+目标端口号+请求资源(图片),服务器收到http请求后 进行解析,获取请求的种类:get、post等和请求的资源。 send之前需要注意http响应也是有格式的,需要返回http头部(200,数据长度等等)和http 主体部分(返回的资源),所以send之前需要按照响应格式打包再发送出去。
二、使用sendfile
1.对send http body的优化
直接使用send发送http body,需要open打开资源文件拷贝到用户层的buff中,最后send又会从用户buff传递到sock buff,需要借用用户层,拷贝次数和上下文切换较多。这种直接将磁盘数据发送给网络的方式就是sendfile的应用场景!
sendfile(fd,fileid,null,size),直接将资源文件传到sock buff中,不经过用户层,传输效率更高。
2.参考代码
在原先的reacotr代码基础上做改动,代码如下:
server.c
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 nty_http_request(struct ntyevent *ev) {
char linebuffer[1024] = {0};
int idx = readline(ev->buffer, 0, linebuffer);
if (strstr(linebuffer, "GET")) {
ev->method = HTTP_METHOD_GET;
int i = 0;
while(linebuffer[sizeof("GET ") + i] != ' ') i ++;
linebuffer[sizeof("GET ") + i] = '\0';
sprintf(ev->resource, "%s/%s", HTTP_WEB_ROOT, linebuffer+sizeof("GET "));
} else if (strstr(linebuffer, "POST")) {
ev->method = HTTP_METHOD_POST;
}
}
int nty_http_response_get_method(struct ntyevent *ev) {
int len;
int filefd = open(ev->resource, O_RDONLY);
struct stat stat_buf;
fstat(filefd, &stat_buf);
close(filefd);
len = sprintf(ev->wbuffer,
"HTTP/1.1 200 OK\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Length: %ld\r\n"
"Content-Type: text/html\r\n", stat_buf.st_size);
ev->wlength = len;
}
#endif
return len;
}
int nty_http_response(struct ntyevent *ev) {
// ev->method, ev->resouces
if (ev->method == HTTP_METHOD_GET) {
return nty_http_response_get_method(ev);
} else if (ev->method == HTTP_METHOD_POST) {
}
}
int recv_cb(int fd, int events, void *arg) {
struct ntyreactor *reactor = (struct ntyreactor*)arg;
struct ntyevent *ev = ntyreactor_idx(reactor, fd);
if (ev == NULL) return -1;
//
int len = recv(fd, ev->buffer, BUFFER_LENGTH, 0);
nty_event_del(reactor->epfd, ev);
if (len > 0) {
ev->length = len;
ev->buffer[len] = '\0';
nty_http_request(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);
} else {
if (errno == EAGAIN && errno == EWOULDBLOCK) {
} else if (errno == ECONNRESET){
nty_event_del(reactor->epfd, ev);
close(ev->fd);
}
}
return len;
}
int send_cb(int fd, int events, void *arg) {
struct ntyreactor *reactor = (struct ntyreactor*)arg;
struct ntyevent *ev = ntyreactor_idx(reactor, fd);
if (ev == NULL) return -1;
nty_http_response(ev); //打包的过程
int len = send(fd, ev->wbuffer, ev->wlength, 0);
if (len > 0) {
int filefd = open(ev->resource, O_RDONLY);
struct stat stat_buf;
fstat(filefd, &stat_buf);
int flag = fcntl(fd, F_GETFL, 0);
flag &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, flag);
//使用sendfile前需要设置为fd阻塞模式才能使用
int ret = sendfile(fd, filefd, NULL, stat_buf.st_size);
if (ret == -1) {
printf("sendfile: errno: %d\n", errno);
}
//使用完之后将fd阻塞恢复原样
flag |= O_NONBLOCK;
fcntl(fd, F_SETFL, flag);
close(filefd);
send(fd, "\r\n", 2, 0);
nty_event_del(reactor->epfd, ev);
nty_event_set(ev, fd, recv_cb, reactor);
nty_event_add(reactor->epfd, EPOLLIN, ev);
} else {
nty_event_del(reactor->epfd, ev);
close(ev->fd);
}
return len;
}