| 19 | CHttpClient::~CHttpClient() {} |
| 20 | |
| 21 | void CHttpClient::run(BOOL usePost, const char *filePath) |
| 22 | { |
| 23 | acl::http_url hu; |
| 24 | if (!hu.parse(m_url.GetString())) { |
| 25 | SetError("Invalid url=%s", m_url.GetString()); |
| 26 | SetEnd(); |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | const char* domain = hu.get_domain(); |
| 31 | unsigned short port = hu.get_port(); |
| 32 | CString addr; |
| 33 | addr.Format("%s|%d", domain, port); |
| 34 | acl::http_request request(addr.GetString()); |
| 35 | |
| 36 | CString url; |
| 37 | url.Format("%s", hu.get_url_path()); |
| 38 | const char* params = hu.get_url_params(); |
| 39 | if (*params != 0) { |
| 40 | url.AppendFormat("?%s", params); |
| 41 | } |
| 42 | |
| 43 | acl::http_header& header = request.request_header(); |
| 44 | header.set_url(url.GetString()).accept_gzip(true).set_host(domain); |
| 45 | |
| 46 | acl::string head; |
| 47 | acl::string body; |
| 48 | if (usePost) { |
| 49 | header.set_method(acl::HTTP_METHOD_POST); |
| 50 | |
| 51 | if (filePath != NULL) { |
| 52 | acl::ifstream::load(filePath, body); |
| 53 | } |
| 54 | |
| 55 | header.set_content_length(body.length()); |
| 56 | } |
| 57 | |
| 58 | header.build_request(head); |
| 59 | SetRequestHead(head.c_str()); |
| 60 | |
| 61 | if (!request.request(body.empty() ? NULL : body.c_str(), body.size())) { |
| 62 | SetError("Send request to %s error: %s", |
| 63 | addr.GetString(), acl::last_serror()); |
| 64 | SetEnd(); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | acl::http_client* client = request.get_client(); |
| 69 | head.clear(); |
| 70 | client->sprint_header(head); |
| 71 | SetResponseHead(head); |
| 72 | |
| 73 | long long length = request.body_length(); |
| 74 | SetBodyTotalLength(length); |
| 75 | |
| 76 | length = 0; |
| 77 | char buf[8192]; |
| 78 | while (true) { |
no test coverage detected