Libevent::evhttp服务器下载

Posted osbreak

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Libevent::evhttp服务器下载相关的知识,希望对你有一定的参考价值。

 

void http_handler_Get_Download(struct evhttp_request *req, void *arg)
{
    if (req == NULL)
    {
        return;
    }
    const char *uri = evhttp_request_get_uri(req);
    string strUrl(uri);

    string strFilePath = DPC::get_Url_path(strUrl, "path=");
    printf("FilePath = %s 
", strFilePath.c_str());

    string strFileName = DPC::get_FileName_path(strUrl, "/");
    strFileName = "attachment; filename=" + strFileName;

    //查看文件是否存在
    if (access(strFilePath.c_str(), 0) == -1)
    {
        evhttp_send_error(req, HTTP_BADREQUEST, 0);
        return;
    }
    //=========
    std::ifstream t;
    t.open(strFilePath.c_str(), ios::in | ios::binary);
    t.seekg(0, std::ios::end);
    long length = t.tellg();
    t.seekg(0, std::ios::beg);

    char *buffer = new char[length];
    t.read(buffer, length);
    t.close();

    char FileSize[10] = {  };
    snprintf(FileSize, sizeof(FileSize), "%d", length);

    time_t timep;
    time(&timep);
    char s[50];
    sprintf(s, ctime(&timep));
    std::string strDate = std::string(s, s + (strlen(s) - 1));
    evhttp_add_header(evhttp_request_get_output_headers(req), "Server", "Unix");
    evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "application/octet-stream; charset=utf-8");
    evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Disposition", strFileName.c_str());
    evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Length", FileSize);
    evhttp_add_header(evhttp_request_get_output_headers(req), "Date", strDate.c_str());
    evhttp_add_header(evhttp_request_get_output_headers(req), "Connection", "close");
    //=========

    //回响应
    struct evbuffer *retbuff = NULL;
    retbuff = evbuffer_new();
    if (retbuff == NULL)
    {
        return;
    }
    evbuffer_add(retbuff, buffer, length);
    //evbuffer_add_printf(retbuff, "123");
    evhttp_send_reply(req, HTTP_OK, "Client", retbuff);
    evbuffer_free(retbuff);
}

 

以上是关于Libevent::evhttp服务器下载的主要内容,如果未能解决你的问题,请参考以下文章

libevent evhttp多线程

libevent evhttp多线程

libevent evhttp多线程

libevent evhttp多线程

Apollo Codegen 没有找到生成代码的操作或片段

根据图片的url地址下载图片到本地保存代码片段