注意1:本文基于libhv-v1.2.5测试,其他版本不一定适用!
注意2:HTTP服务器可以用HFS工具模拟!
最近在测试hv的http上传图片功能,发现向HTTP服务器发送图片时,服务端总是收不到数据或者收不全,在官方QQ群反馈也没有响应。
经过对libhv代码调试跟踪找到了如下办法可以解决问题:
1. http/http_content.cpp的107行,增加一段代码
1 str += "\r\n";
2. http/client/http_client.cpp,将++fail_cnt == 1的判断修改为 ++fail_cnt == 0,即忽略此判断,避免出现重复提交图片的情况
实际测试程序的代码如下
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include "http_client.h" 4 5 int main(int argc, char* argv[]) 6 { 7 HttpResponse resp; 8 HttpRequest req; 9 req.method = HTTP_POST; 10 req.url = "http://192.168.1.200/upload"; 11 req.headers["Connection"] = "keep-alive"; 12 //req.headers["Content-Type"] = "application/octet-stream"; 13 req.timeout = 20; 14 FormData form; 15 form.filename = std::string("/tmp/up.jpg"); 16 req.form["file"] = form; 17 //int rc = http_client_send(http, &req, &resp); 18 //http_client_t* http = http_client_new(); 19 hv::HttpClient hc; 20 int r = hc.send(&req, &resp); 21 printf("http result = %d\n\n", r); 22 return 0; 23 }