|
@@ -58,7 +58,7 @@ HV_INLINE Response request(http_method method, const char* url, const http_body&
|
|
|
return request(req);
|
|
return request(req);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-HV_INLINE Response uploadFile(http_method method, const char* url, const char* filepath, const http_headers& headers = DefaultHeaders) {
|
|
|
|
|
|
|
+HV_INLINE Response uploadFile(const char* url, const char* filepath, http_method method = HTTP_POST, const http_headers& headers = DefaultHeaders) {
|
|
|
Request req(new HttpRequest);
|
|
Request req(new HttpRequest);
|
|
|
req->method = method;
|
|
req->method = method;
|
|
|
req->url = url;
|
|
req->url = url;
|
|
@@ -70,11 +70,15 @@ HV_INLINE Response uploadFile(http_method method, const char* url, const char* f
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#ifndef WITHOUT_HTTP_CONTENT
|
|
#ifndef WITHOUT_HTTP_CONTENT
|
|
|
-HV_INLINE Response uploadFormFile(http_method method, const char* url, const char* name, const char* filepath, const http_headers& headers = DefaultHeaders) {
|
|
|
|
|
|
|
+HV_INLINE Response uploadFormFile(const char* url, const char* name, const char* filepath, std::map<std::string, std::string>& params = hv::empty_map, http_method method = HTTP_POST, const http_headers& headers = DefaultHeaders) {
|
|
|
Request req(new HttpRequest);
|
|
Request req(new HttpRequest);
|
|
|
req->method = method;
|
|
req->method = method;
|
|
|
req->url = url;
|
|
req->url = url;
|
|
|
- req->FormFile(name, filepath);
|
|
|
|
|
|
|
+ req->content_type = MULTIPART_FORM_DATA;
|
|
|
|
|
+ req->SetFormFile(name, filepath);
|
|
|
|
|
+ for (auto& param : params) {
|
|
|
|
|
+ req->SetFormData(param.first.c_str(), param.second);
|
|
|
|
|
+ }
|
|
|
if (&headers != &DefaultHeaders) {
|
|
if (&headers != &DefaultHeaders) {
|
|
|
req->headers = headers;
|
|
req->headers = headers;
|
|
|
}
|
|
}
|
|
@@ -82,6 +86,55 @@ HV_INLINE Response uploadFormFile(http_method method, const char* url, const cha
|
|
|
}
|
|
}
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
|
|
+// see examples/wget.cpp
|
|
|
|
|
+typedef std::function<void(size_t received_bytes, size_t total_bytes)> download_progress_cb;
|
|
|
|
|
+HV_INLINE size_t downloadFile(const char* url, const char* filepath, download_progress_cb progress_cb = NULL) {
|
|
|
|
|
+ // open file
|
|
|
|
|
+ std::string filepath_download(filepath);
|
|
|
|
|
+ filepath_download += ".download";
|
|
|
|
|
+ HFile file;
|
|
|
|
|
+ int ret = file.open(filepath_download.c_str(), "wb");
|
|
|
|
|
+ if (ret != 0) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ // download
|
|
|
|
|
+ Request req(new HttpRequest);
|
|
|
|
|
+ req->method = HTTP_GET;
|
|
|
|
|
+ req->url = url;
|
|
|
|
|
+ size_t content_length = 0;
|
|
|
|
|
+ size_t received_bytes = 0;
|
|
|
|
|
+ req->http_cb = [&file, &content_length, &received_bytes, &progress_cb]
|
|
|
|
|
+ (HttpMessage* resp, http_parser_state state, const char* data, size_t size) {
|
|
|
|
|
+ if (state == HP_HEADERS_COMPLETE) {
|
|
|
|
|
+ content_length = hv::from_string<size_t>(resp->GetHeader("Content-Length"));
|
|
|
|
|
+ } else if (state == HP_BODY) {
|
|
|
|
|
+ if (data && size) {
|
|
|
|
|
+ // write file
|
|
|
|
|
+ file.write(data, size);
|
|
|
|
|
+ received_bytes += size;
|
|
|
|
|
+ if (progress_cb) {
|
|
|
|
|
+ progress_cb(received_bytes, content_length);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ auto resp = request(req);
|
|
|
|
|
+ file.close();
|
|
|
|
|
+ if (resp == NULL || resp->status_code != 200) {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ // check filesize
|
|
|
|
|
+ if (content_length != 0) {
|
|
|
|
|
+ if (hv_filesize(filepath_download.c_str()) == content_length) {
|
|
|
|
|
+ rename(filepath_download.c_str(), filepath);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ remove(filepath_download.c_str());
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return hv_filesize(filepath);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
HV_INLINE Response head(const char* url, const http_headers& headers = DefaultHeaders) {
|
|
HV_INLINE Response head(const char* url, const http_headers& headers = DefaultHeaders) {
|
|
|
return request(HTTP_HEAD, url, NoBody, headers);
|
|
return request(HTTP_HEAD, url, NoBody, headers);
|
|
|
}
|
|
}
|