log_api.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include "RestfulFunc.h"
  2. #include "JsonConvertHelper.hpp"
  3. #include <sstream>
  4. #include <string>
  5. #include <locale>
  6. #include "log_util.h"
  7. #include "log_api.h"
  8. #include <string.h>
  9. #include "sds.h"
  10. #include "inner_log.h"
  11. //#define GBK_COMPACT
  12. static int is_utf8(const char* str)
  13. {
  14. int i;
  15. const unsigned char* bytes = (const unsigned char*)str;
  16. int num;
  17. if (str == NULL)
  18. return 1;
  19. #if 0
  20. while (*bytes != 0x00) {
  21. if ((*bytes & 0x80) == 0x00) {
  22. // U+0000 to U+007F
  23. num = 1;
  24. } else if ((*bytes & 0xE0) == 0xC0) {
  25. // U+0080 to U+07FF
  26. num = 2;
  27. } else if ((*bytes & 0xF0) == 0xE0) {
  28. // U+0800 to U+FFFF
  29. num = 3;
  30. } else if ((*bytes & 0xF8) == 0xF0) {
  31. // U+10000 to U+10FFFF
  32. num = 4;
  33. } else {
  34. return 0;
  35. }
  36. bytes += 1;
  37. for (i = 1; i < num; ++i) {
  38. if ((*bytes & 0xC0) != 0x80)
  39. return 0;
  40. bytes += 1;
  41. }
  42. }
  43. return 1;
  44. #else
  45. while (*bytes) {
  46. if ((// ASCII
  47. // use bytes[0] <= 0x7F to allow ASCII control characters
  48. bytes[0] == 0x09 ||
  49. bytes[0] == 0x0A ||
  50. bytes[0] == 0x0D ||
  51. (0x20 <= bytes[0] && bytes[0] <= 0x7E)
  52. )
  53. ) {
  54. bytes += 1;
  55. continue;
  56. }
  57. if ((// non-overlong 2-byte
  58. (0xC2 <= bytes[0] && bytes[0] <= 0xDF) &&
  59. (0x80 <= bytes[1] && bytes[1] <= 0xBF)
  60. )
  61. ) {
  62. bytes += 2;
  63. continue;
  64. }
  65. if ((// excluding overlongs
  66. bytes[0] == 0xE0 &&
  67. (0xA0 <= bytes[1] && bytes[1] <= 0xBF) &&
  68. (0x80 <= bytes[2] && bytes[2] <= 0xBF)
  69. ) ||
  70. (// straight 3-byte
  71. ((0xE1 <= bytes[0] && bytes[0] <= 0xEC) ||
  72. bytes[0] == 0xEE ||
  73. bytes[0] == 0xEF) &&
  74. (0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
  75. (0x80 <= bytes[2] && bytes[2] <= 0xBF)
  76. ) ||
  77. (// excluding surrogates
  78. bytes[0] == 0xED &&
  79. (0x80 <= bytes[1] && bytes[1] <= 0x9F) &&
  80. (0x80 <= bytes[2] && bytes[2] <= 0xBF)
  81. )
  82. ) {
  83. bytes += 3;
  84. continue;
  85. }
  86. if ((// planes 1-3
  87. bytes[0] == 0xF0 &&
  88. (0x90 <= bytes[1] && bytes[1] <= 0xBF) &&
  89. (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
  90. (0x80 <= bytes[3] && bytes[3] <= 0xBF)
  91. ) ||
  92. (// planes 4-15
  93. (0xF1 <= bytes[0] && bytes[0] <= 0xF3) &&
  94. (0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
  95. (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
  96. (0x80 <= bytes[3] && bytes[3] <= 0xBF)
  97. ) ||
  98. (// plane 16
  99. bytes[0] == 0xF4 &&
  100. (0x80 <= bytes[1] && bytes[1] <= 0x8F) &&
  101. (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
  102. (0x80 <= bytes[3] && bytes[3] <= 0xBF)
  103. )
  104. ) {
  105. bytes += 4;
  106. continue;
  107. }
  108. return 0;
  109. }
  110. return 1;
  111. #endif
  112. }
  113. std::wstring s2w(const std::string str)
  114. {
  115. if (str.empty()) {
  116. return L"";
  117. }
  118. std::wstring w_str(L"");
  119. const char* origin = setlocale(LC_CTYPE, NULL);
  120. unsigned len = str.size() + 1;
  121. setlocale(LC_CTYPE, "zh_CN.gbk");
  122. wchar_t* p = new wchar_t[len];
  123. if (-1 != mbstowcs(p, str.c_str(), len)) {
  124. w_str = p;
  125. } else {
  126. aos_error_log((LB, "s2w::mbstowcs failed"));
  127. }
  128. delete[] p;
  129. setlocale(LC_CTYPE, origin);
  130. return w_str;
  131. }
  132. std::string w2utf8(const std::wstring wstr)
  133. {
  134. if (wstr.empty()) {
  135. return "";
  136. }
  137. std::string str("");
  138. char* origin = setlocale(LC_CTYPE, NULL);
  139. unsigned len = wstr.size() + 1;
  140. setlocale(LC_CTYPE, "zh_CN.utf8");
  141. char* p = new char[len];
  142. if (-1 != wcstombs(p, wstr.c_str(), len)) {
  143. str = p;
  144. } else {
  145. aos_error_log((LB, "w2utf8::wcstombs failed"));
  146. }
  147. setlocale(LC_CTYPE, origin);
  148. return str;
  149. }
  150. std::string gbk2utf8(const std::string str)
  151. {
  152. std::wstring wstr = s2w(str);
  153. return w2utf8(wstr);
  154. }
  155. /*
  156. if (result->statusCode / 100 == 2)
  157. {
  158. return LOG_SEND_OK;
  159. }
  160. if (result->statusCode <= 0)
  161. {
  162. return LOG_SEND_NETWORK_ERROR;
  163. }
  164. if (result->statusCode >= 500 || result->requestID == NULL)
  165. {
  166. return LOG_SEND_SERVER_ERROR;
  167. }
  168. if (result->statusCode == 403)
  169. {
  170. return LOG_SEND_QUOTA_EXCEED;
  171. }
  172. if (result->statusCode == 401 || result->statusCode == 404)
  173. {
  174. return LOG_SEND_UNAUTHORIZED;
  175. }
  176. if (result->errorMessage != NULL && strstr(result->errorMessage, LOGE_TIME_EXPIRED) != NULL)
  177. {
  178. return LOG_SEND_TIME_ERROR;
  179. }
  180. return LOG_SEND_DISCARD_ERROR;
  181. */
  182. int LOG_OS_HttpPost(const char* url,
  183. char** header_array,
  184. int header_count,
  185. const void* data,
  186. int data_len, const char* channelId, const char* token)
  187. {
  188. int retCode = 480;
  189. HttpClientResponseResult result;
  190. HttpClientRequestConfig config(HttpRequestMethod::POST, url);
  191. config.SetTimeout(60);
  192. std::string str((const char*)data);
  193. #ifdef GBK_COMPACT
  194. if (!str.empty() && !is_utf8(str.c_str())) {
  195. std::string t = gbk2utf8(str);
  196. str = t;
  197. }
  198. #endif //GBK_COMPACT
  199. config.SetJsonBody(str.c_str());
  200. RestfulClient client = RestfulClient::getInstance();
  201. config.PreDo();
  202. client.Do(&config, &result);
  203. if (result.ResponseOK()) {
  204. retCode = 481;
  205. struct CommResponse
  206. {
  207. bool success;
  208. std::string errorCode;
  209. std::string returnCode;
  210. std::string errorMsg;
  211. std::string message;
  212. JSONCONVERT2OBJECT_MEMEBER_REGISTER(success, errorCode, returnCode, errorMsg, message)
  213. JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("success", "code", "return_code", "error_msg", "message")
  214. } responseIns;
  215. Json::Value rawRoot;
  216. if (GetJsonRootObject(rawRoot, result.content) && Json2Object(responseIns, rawRoot)) {
  217. if (responseIns.success) {
  218. retCode = 200;
  219. } else {
  220. if (responseIns.errorCode.find("RTI1002") != -1)
  221. retCode = 300;
  222. else if (!is_utf8((const char*)str.c_str())) {
  223. aos_warn_log((LB, "detect that content is not utf8-type ,skip it: %s", str.c_str()));
  224. retCode = 200;
  225. }
  226. }
  227. } else {
  228. aos_error_log((LB, "http %s extract failed: %s", url, result.content.c_str()));
  229. aos_info_log((LB, "content len: %d", data_len));
  230. }
  231. } else {
  232. retCode = result.statusCode;
  233. }
  234. return retCode;
  235. }
  236. extern long LOG_GET_TIME();
  237. log_status_t sls_log_init(int32_t log_global_flag)
  238. {
  239. return 0;
  240. }
  241. void sls_log_destroy()
  242. {
  243. }
  244. static const char sls_month_snames[12][4] =
  245. {
  246. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  247. };
  248. static const char sls_day_snames[7][4] =
  249. {
  250. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  251. };
  252. void sls_rfc822_date(char *date_str, struct tm * xt)
  253. {
  254. const char *s = NULL;
  255. int real_year = 2000;
  256. /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
  257. /* 12345678901234567890123456789 */
  258. s = &sls_day_snames[xt->tm_wday][0];
  259. *date_str++ = *s++;
  260. *date_str++ = *s++;
  261. *date_str++ = *s++;
  262. *date_str++ = ',';
  263. *date_str++ = ' ';
  264. *date_str++ = xt->tm_mday / 10 + '0';
  265. *date_str++ = xt->tm_mday % 10 + '0';
  266. *date_str++ = ' ';
  267. s = &sls_month_snames[xt->tm_mon][0];
  268. *date_str++ = *s++;
  269. *date_str++ = *s++;
  270. *date_str++ = *s++;
  271. *date_str++ = ' ';
  272. real_year = 1900 + xt->tm_year;
  273. /* This routine isn't y10k ready. */
  274. *date_str++ = real_year / 1000 + '0';
  275. *date_str++ = real_year % 1000 / 100 + '0';
  276. *date_str++ = real_year % 100 / 10 + '0';
  277. *date_str++ = real_year % 10 + '0';
  278. *date_str++ = ' ';
  279. *date_str++ = xt->tm_hour / 10 + '0';
  280. *date_str++ = xt->tm_hour % 10 + '0';
  281. *date_str++ = ':';
  282. *date_str++ = xt->tm_min / 10 + '0';
  283. *date_str++ = xt->tm_min % 10 + '0';
  284. *date_str++ = ':';
  285. *date_str++ = xt->tm_sec / 10 + '0';
  286. *date_str++ = xt->tm_sec % 10 + '0';
  287. *date_str++ = ' ';
  288. *date_str++ = 'G';
  289. *date_str++ = 'M';
  290. *date_str++ = 'T';
  291. *date_str++ = 0;
  292. return;
  293. }
  294. void get_now_time_str(char * buffer, int bufLen, int timeOffset)
  295. {
  296. time_t rawtime = LOG_GET_TIME();
  297. struct tm * timeinfo;
  298. if (timeOffset != 0)
  299. {
  300. rawtime += timeOffset;
  301. }
  302. timeinfo = gmtime(&rawtime);
  303. sls_rfc822_date(buffer, timeinfo);
  304. }
  305. void post_log_result_destroy(post_log_result * result)
  306. {
  307. if (result != NULL)
  308. {
  309. if (result->errorMessage != NULL)
  310. {
  311. sdsfree(result->errorMessage);
  312. }
  313. if (result->requestID != NULL)
  314. {
  315. sdsfree(result->requestID);
  316. }
  317. free(result);
  318. }
  319. }
  320. struct cur_slist {
  321. char *data;
  322. struct cur_slist *next;
  323. };
  324. struct cur_slist * cur_slist_append(struct cur_slist *lst, const char *s)
  325. {
  326. struct cur_slist* orig;
  327. struct cur_slist *t = (struct cur_slist *)malloc(sizeof(struct cur_slist));
  328. #ifdef WIN32
  329. t->data = _strdup(s);
  330. #else
  331. t->data = strdup(s);
  332. #endif
  333. t->next = NULL;
  334. if(lst == NULL)
  335. return t;
  336. orig = lst;
  337. while(lst->next) {
  338. lst = lst->next;
  339. }
  340. lst->next = t;
  341. return orig;
  342. }
  343. void cur_slist_free_all(struct cur_slist *lst)
  344. {
  345. while(lst != NULL) {
  346. struct cur_slist *n = lst->next;
  347. free(lst->data);
  348. free(lst);
  349. lst = n;
  350. }
  351. }
  352. post_log_result* post_logs(const char* endpoint, const char* accesskeyId, const char* accessKey, const char* stsToken, lz4_log_buf* buffer, log_post_option* option,
  353. const char* channelId, const char* token)
  354. {
  355. post_log_result * result = (post_log_result *)malloc(sizeof(post_log_result));
  356. memset(result, 0, sizeof(post_log_result));
  357. {
  358. //headers
  359. struct cur_slist* headers = NULL;
  360. int res;
  361. char* header_array[50];
  362. int header_count = 0;
  363. // url
  364. sds url = NULL;
  365. if (option->using_https) {
  366. url = sdsnew("https://");
  367. } else {
  368. url = sdsnew("http://");
  369. }
  370. url = sdscat(url, endpoint);
  371. if(buffer->data != NULL && buffer->length != 0)
  372. res = LOG_OS_HttpPost(url, header_array, header_count, (const void *) buffer->data, buffer->length, channelId, token);
  373. result->statusCode = res;
  374. cur_slist_free_all(headers); /* free the list again */
  375. sdsfree(url);
  376. }
  377. return result;
  378. }