log_api.cpp 10 KB

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