西西软件园多重安全检测下载网站、值得信赖的软件下载站!
软件
软件
文章
搜索

首页西西教程操作系统 → libevent调用evbuffer_add_file卡死问题解决

libevent调用evbuffer_add_file卡死问题解决

相关软件相关文章发表评论 来源:stsword时间:2012/1/31 11:07:13字体大小:A-A+

作者:stsword点击:65次评论:0次标签: libevent

  • 类型:角色扮演大小:18.1M语言:中文 评分:.0
  • 标签:
立即下载

问题描述:

今天想做一个简单的HTTP服务器,发现libevent使用很方便,就用他的example里的httpserver代码试了下,发现一个问题,就是在打开部分文件时候,服务器会卡住。没有任何返回,浏览器一直处于等待状态。
后来调试后发现,卡死在evbuffer_add_file函数。

源代码如下:

01 /* Otherwise it's a file; add it to the buffer to get

02 * sent via sendfile */

03 const char *type = guess_content_type(decoded_path);

04 if ((fd = open(whole_path, O_RDONLY)) < 0) {

05 perror("open");

06 goto err;

07 }

08 if (fstat(fd, &st)<0) {

09 /* Make sure the length still matches, now that we

10 * opened the file :/ */

11 perror("fstat");

12 goto err;

13 }

14 evhttp_add_header(evhttp_request_get_output_headers(req),

15 "Content-Type", type);

16 evbuffer_add_file(evb, fd, 0, st.st_size);

后经过调试发现,是由于代码第四行中打开文件的方式有问题。缺少O_BINARY标志,导致对于某些包含不可显示或者中文的文件会卡死。
改成如下代码即可:

1 /* Otherwise it's a file; add it to the buffer to get

2 * sent via sendfile */

3 const char *type = guess_content_type(decoded_path);

4 if ((fd = open(whole_path, O_RDONLY|O_BINARY)) < 0) {

5 perror("open");

6 goto err;

7 }

    相关评论

    阅读本文后您有什么感想? 已有人给出评价!

    • 8 喜欢喜欢
    • 3 顶
    • 1 难过难过
    • 5 囧
    • 3 围观围观
    • 2 无聊无聊

    热门评论

    最新评论

    发表评论 查看所有评论(0)

    昵称:
    表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
    字数: 0/500 (您的评论需要经过审核才能显示)