-
Notifications
You must be signed in to change notification settings - Fork 110
/
HttpRequest.cpp
executable file
·144 lines (117 loc) · 4.47 KB
/
HttpRequest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "HttpRequest.h"
#include "LibeventLog.h"
#include "Threadable.h"
#include "Configure.h"
static void http_request_callback(struct evhttp_request *req, void *args) {
HttpRequest *request = (HttpRequest *)args;
if (request) {
request->http_callback(req, args);
} else {
LibeventLog::userLog("http_request_get_callback args is NULL");
}
}
void HttpRequest::RePostHttpRequest() {
HttpRequest *request = new HttpRequest(_base);
request->setStartTime(_start_time);
int ret = request->postHttpRequest(_post_data, _post_len, userCallback, _user_arg);
if (ret != 0) {
LibeventLog::userLog("RePostHttpRequest->postHttpRequest --> failed.");
delete request;
}
}
void HttpRequest::http_callback(struct evhttp_request *req, void *args) {
HttpRequest *request = (HttpRequest *)args;
request->_end_time = time(NULL);
ConnectionManager *cmg = ConnectionManager::getInstance();
cmg->responseInc();
cmg->addRecycle(this);
struct evhttp_request *response = req;
if (response == NULL) {
E_RESPONSE_TYPE resType = REQUEST_MAX;
if (isTimeout() || reachTimeout(1)) {
cmg->timeoutInc();
resType = REQUEST_TIMEOUT;
LibeventLog::userLog("[response to user] request timeout.");
} else {
cmg->connExcaptionInc();
resType = CONNECTION_EXCEPTION;
if (errno != 0) {
LibeventLog::userLog("make request error : %s.", strerror(errno));
}
Threadable::milliSleep(10);
RePostHttpRequest();
}
if (request->userCallback != NULL) {
request->userCallback(resType, NULL, 0, _user_arg);
}
return;
}
int resCode = evhttp_request_get_response_code(response);
if (resCode != HTTP_OK) {
cmg->errorInc();
LibeventLog::userLog("[error] response code: %d ", resCode);
if (request->userCallback != NULL) {
request->userCallback(RESPONSE_ERROR, &resCode, sizeof(int), _user_arg);
}
} else {
cmg->okInc();
struct evbuffer* buf = evhttp_request_get_input_buffer(response);
size_t len = evbuffer_get_length(buf);
char *tmpbuf = (char *)malloc(len+1);
memcpy(tmpbuf, evbuffer_pullup(buf, -1), len);
tmpbuf[len] = '\0';
if (request->userCallback != NULL) {
request->userCallback(RESPONSE_OK, tmpbuf, len, _user_arg);
}
free(tmpbuf);
}
}
int HttpRequest::configureHttpHeader(struct evhttp_request *request) {
Configure *cfg = Configure::readConfigFile(CONFIG_FILE_PATH);
for (int i=0; i<cfg->http_header.size(); i+=2) {
string key = cfg->http_header[i];
string value = cfg->http_header[i+1];
if (key.empty() || evhttp_add_header(request->output_headers, key.c_str(), value.c_str()) != 0) {
continue;
}
}
return 0;
}
int HttpRequest::createHttpRequest(const char *http_url, user_callback cb, void *cb_arg) {
int ret = 0;
setUrl(http_url);
set_cb_arg(cb_arg);
_uri = evhttp_uri_parse(http_url);
_conn = evhttp_connection_base_new(_base, NULL, evhttp_uri_get_host(_uri), evhttp_uri_get_port(_uri));
_request = evhttp_request_new(http_request_callback, this);
evhttp_connection_set_timeout(_conn, _time_out);
setUserCallback(cb, cb_arg);
ret = configureHttpHeader(_request);
return ret;
}
int HttpRequest::makeHttpRequest(const char *http_url, evhttp_cmd_type type
, char *post_data, unsigned int data_len, user_callback cb, void *cb_arg) {
ConnectionManager *cmg = ConnectionManager::getInstance();
int ret = 0;
CHECK_CALL_RET(createHttpRequest(http_url, cb, cb_arg), ret);
CHECK_CALL_RET(setPostData(post_data, data_len), ret);
CHECK_CALL_RET(evhttp_make_request(_conn, _request, type, evhttp_uri_get_path(_uri)), ret);
cmg->connectionInc();
return ret;
}
//post
int HttpRequest::postHttpRequest(char *post_data, unsigned int data_len, user_callback cb, void *cb_arg) {
Configure *cfg = Configure::readConfigFile(CONFIG_FILE_PATH);
return postHttpRequestWithUrl(cfg->url.c_str(), post_data, data_len, cb, cb_arg);
}
int HttpRequest::postHttpRequestWithUrl(const char *http_url, char *post_data, unsigned int data_len, user_callback cb, void *cb_arg) {
return makeHttpRequest(http_url, EVHTTP_REQ_POST, post_data, data_len, cb, cb_arg);
}
//get
int HttpRequest::getHttpRequest(user_callback cb, void *cb_arg) {
Configure *cfg = Configure::readConfigFile(CONFIG_FILE_PATH);
return getHttpRequestWithUrl(cfg->url.c_str(), cb, cb_arg);
}
int HttpRequest::getHttpRequestWithUrl(const char *http_url, user_callback cb, void *cb_arg) {
return makeHttpRequest(http_url, EVHTTP_REQ_GET, NULL, 0, cb, cb_arg);
}