EspLogDeserializer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. ##############################################################################
  3. # Copyright (C) 2011 HPCC Systems.
  4. #
  5. # All rights reserved. This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ##############################################################################
  18. */
  19. #pragma warning(disable:4786)
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string>
  23. #include <map>
  24. #include "jliball.hpp"
  25. #include "xsdparser.hpp"
  26. #include "http.hpp"
  27. // Utility class to read 1K at a time. Useful when there're a bunch of small reads.
  28. class BufferedReader : public CInterface, implements IInterface
  29. {
  30. private:
  31. int m_fd;
  32. char m_buf[1024];
  33. int m_remain;
  34. int m_curpos;
  35. public:
  36. IMPLEMENT_IINTERFACE;
  37. BufferedReader(int fd)
  38. {
  39. m_fd = fd;
  40. m_curpos = 0;
  41. m_remain = 0;
  42. }
  43. int read(char* buf, int buflen)
  44. {
  45. int totalread = 0;
  46. while(1)
  47. {
  48. if(m_remain <= 0)
  49. {
  50. int len = ::read(m_fd, m_buf, 1024);
  51. if(len <= 0)
  52. break;
  53. m_curpos = 0;
  54. m_remain = len;
  55. }
  56. if(m_remain >= buflen)
  57. {
  58. strncpy(buf+totalread, m_buf+m_curpos, buflen);
  59. totalread += buflen;
  60. m_remain -= buflen;
  61. m_curpos += buflen;
  62. break;
  63. }
  64. else
  65. {
  66. strncpy(buf+totalread, m_buf+m_curpos, m_remain);
  67. totalread += m_remain;
  68. m_remain = 0;
  69. m_curpos = 0;
  70. }
  71. }
  72. return totalread;
  73. }
  74. };
  75. // Utility class to read one line from a file.
  76. class BufferedLineReader : public CInterface, implements IInterface
  77. {
  78. private:
  79. Owned<BufferedReader> m_reader;
  80. public:
  81. IMPLEMENT_IINTERFACE;
  82. BufferedLineReader(int fd)
  83. {
  84. m_reader.setown(new BufferedReader(fd));
  85. }
  86. virtual ~BufferedLineReader()
  87. {
  88. }
  89. int readLine(StringBuffer& buf)
  90. {
  91. char onechar;
  92. int len = 0;
  93. char linebuf[8196];
  94. int curpos = 0;
  95. while((len = m_reader->read(&onechar, 1)) > 0)
  96. {
  97. if(curpos == 8196)
  98. {
  99. buf.append(8196, linebuf);
  100. curpos = 0;
  101. }
  102. linebuf[curpos++] = onechar;
  103. if (onechar == '\n')
  104. break;
  105. }
  106. if (curpos > 0)
  107. buf.append(curpos, linebuf);
  108. return buf.length();
  109. }
  110. };
  111. typedef std::map<std::string, std::string> StringStringMap;
  112. static StringStringMap s_methodMap;
  113. static StringStringMap s_requestNameMap;
  114. static void LoadMethodMappings()
  115. {
  116. FILE* fp = fopen("EspMethods.txt", "r");
  117. if (!fp)
  118. {
  119. puts("Failed to open EspMethods.txt file!");
  120. return;
  121. }
  122. char line[1024];
  123. int lineno = 0;
  124. StringBuffer service;
  125. StringArray strArray;
  126. while (fgets(line, sizeof(line)-1, fp))
  127. {
  128. lineno++;
  129. if (*line == '#')
  130. continue;
  131. char* p = line;
  132. while (isspace(*p))
  133. p++;
  134. char* lastChar = p + strlen(p) - 1;
  135. if (*lastChar == '\n')
  136. *lastChar = '\0';
  137. if (*p == '[')
  138. {
  139. const char* q = strchr(++p, ']');
  140. if (!q)
  141. q = p + strlen(p);
  142. service.clear().append(q-p, p).trim();
  143. }
  144. else if (*p)
  145. {
  146. strArray.kill();
  147. DelimToStringArray(p, strArray, "= \t()");
  148. const unsigned int ord = strArray.ordinality();
  149. if (ord == 0)
  150. printf( "Syntax error in EspMethods.txt at line %d: ", lineno++);
  151. else
  152. {
  153. StringBuffer method = strArray.item(0);
  154. method.trim();
  155. if (ord > 1)
  156. {
  157. //method=config(request)
  158. //method=*(request) when config=method
  159. StringBuffer config(strArray.item(1));
  160. StringBuffer url(service);
  161. if (0 != strcmp(config.trim().str(), "*"))
  162. url.append('/').append( method.str() );
  163. s_methodMap[config.str()] = url.str();
  164. if (strArray.ordinality() > 2)
  165. {
  166. StringBuffer request( strArray.item(2) );
  167. s_requestNameMap[ config.str() ] = request.trim().str();
  168. }
  169. }
  170. else
  171. s_methodMap[method.str()] = service.str();
  172. }
  173. }
  174. }
  175. int rc = ferror(fp);
  176. if (rc)
  177. ERRLOG("Loading EspMethods.txt failed (may be partially loaded), system error code: %d", rc);
  178. fclose(fp);
  179. }
  180. static bool lookupMethod(const char* config, StringBuffer& service, StringBuffer& method, StringBuffer& request)
  181. {
  182. bool rc = false;
  183. StringStringMap::const_iterator it = s_methodMap.find(config);
  184. if (it != s_methodMap.end())
  185. {
  186. StringBuffer s((*it).second.c_str());
  187. const char* p = strchr(s.str(), '/');
  188. if (!p)
  189. service.clear().append( s.str() );
  190. else
  191. {
  192. StringArray strArray;
  193. DelimToStringArray(s.str(), strArray, "/");
  194. if (strArray.ordinality() < 2)
  195. printf("Invalid configuration: %s", s.str());
  196. else
  197. {
  198. service.clear().append(strArray.item(0));
  199. method .clear().append(strArray.item(1));
  200. }
  201. }
  202. it = s_requestNameMap.find( config );
  203. if (it != s_requestNameMap.end())
  204. request.append( (*it).second.c_str() );
  205. else
  206. request.append(config).append("Request");
  207. rc = true;
  208. }
  209. return rc;
  210. }
  211. /*
  212. QUERY: GBGroup[User[ReferenceCode(RJVCC)GLBPurpose(1)DLPurpose(1)]SearchBy[RequestDetails[Profile(Sweden)]Person[Title(Mr)FirstName(Floren)Gender(Female)]Addresses[Address1[AddressLayout(5)BuildingNumber(2)Street(Norgardsplan)Country(Sweden)ZipPCode(55337)]]]]"
  213. */
  214. static bool expandConciseRequest(const char* concise, StringBuffer& service, StringBuffer& method,
  215. StringBuffer& request, StringBuffer& xml, StringBuffer& msg)
  216. {
  217. method.clear();
  218. msg.clear();
  219. xml.clear();
  220. if (!concise)
  221. return false;
  222. StringStack tagStack;
  223. StringBuffer tag;
  224. const char* p;
  225. const char* q;
  226. const char* errP=NULL;
  227. bool bValue=false;
  228. int indent = 4;
  229. p = q = concise;
  230. while (*q && errP==NULL)
  231. {
  232. switch (*q)
  233. {
  234. case '[':
  235. if (bValue)
  236. {
  237. msg.appendf("Invalid concise XML: no matching ')' for tag %s:\n", tag.str());
  238. errP = q;
  239. }
  240. else
  241. {
  242. tag.clear().append( q-p, p).trim();
  243. if (p==concise)
  244. {
  245. const char* r = strstr(tag.str(), "Request");
  246. if (r)
  247. {
  248. method.append( r-tag.str(), tag.str());
  249. StringBuffer config(method);
  250. lookupMethod(config.str(), service, method, request);
  251. }
  252. else
  253. {
  254. StringBuffer config(tag);
  255. lookupMethod(tag.str(), service, tag, request);
  256. method.append( tag );
  257. tag.clear().append(request);
  258. }
  259. }
  260. //printf("tag: [%s]\n", tag);
  261. tagStack.push_back(tag.str());
  262. indent += 2;
  263. xml.appendN(indent, ' ');
  264. xml.append('<').append(tag).append(">\n");
  265. p=++q;
  266. }
  267. break;
  268. case ']':
  269. if (bValue)
  270. {
  271. msg.appendf("Invalid concise XML: no matching ')' for tag %s:\n", tag.str());
  272. errP = q;
  273. }
  274. else if (tagStack.empty())
  275. {
  276. msg.appendf("Invalid concise XML: no matching tag for ']':\n");
  277. errP = q;
  278. }
  279. else
  280. {
  281. xml.appendN(indent, ' ');
  282. indent -= 2;
  283. xml.append("</").append(tagStack.back().c_str()).append(">\n");
  284. tagStack.pop_back();
  285. p=++q;
  286. }
  287. break;
  288. case '(':
  289. tag.clear().append( q-p, p).trim();
  290. //printf("tag: [%s]\n", tag);
  291. p = ++q;
  292. bValue = true;
  293. if (q==NULL)
  294. {
  295. msg.appendf("Invalid input: no ending '(' for <").append(tag).append('>');
  296. errP = q;
  297. }
  298. break;
  299. case ')':
  300. bValue = false;
  301. xml.appendN(indent+2, ' ');
  302. xml.append('<').append(tag).append('>');
  303. xml.append( q-p, p).append("</").append(tag).append(">\n");
  304. p = ++q;
  305. break;
  306. default:
  307. q++;
  308. break;
  309. }
  310. }
  311. if (errP)
  312. {
  313. msg.append(errP-concise+1, concise).append("<<ERROR<<");
  314. xml.clear();
  315. }
  316. return errP == NULL;
  317. }
  318. bool loadEspLog(const char* logFileName, HttpClient& httpClient, HttpStat& httpStat)
  319. {
  320. if (!logFileName || !*logFileName)
  321. {
  322. ERRLOG("Input log file name not specified.");
  323. return false;
  324. }
  325. typedef std::map<std::string, int> InstanceMap; /* counts instances of each method*/
  326. InstanceMap instanceMap; /*how many times each method is extracted */
  327. // how many instances of each method to extract:
  328. int maxInstances = httpClient.queryGlobals()->getPropInt("items", -1);
  329. if (maxInstances == 0)
  330. {
  331. ERRLOG("Maximum instances specified with -n option cannot be 0.");
  332. return false;
  333. }
  334. int bytes_read = 0;
  335. int fd = open(logFileName, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO);
  336. Owned<BufferedLineReader> linereader = new BufferedLineReader(fd);
  337. if(fd >= 0)
  338. {
  339. StringBuffer buffer;
  340. StringBuffer xml;
  341. StringBuffer msg;
  342. StringBuffer service;
  343. StringBuffer method;
  344. StringBuffer request;
  345. static bool bMapNotLoaded = true;
  346. if (bMapNotLoaded)
  347. {
  348. LoadMethodMappings();
  349. bMapNotLoaded = false;
  350. }
  351. while ((bytes_read = linereader->readLine(buffer.clear())) > 0)
  352. {
  353. const char* buf = buffer.str();
  354. const char* p = strchr(buf, '\"');
  355. const char* prefix1 = "QUERY: ";
  356. const char* prefix2 = "FULL QUERY: ";
  357. bool bProcess = false;
  358. if (p && !strnicmp(p+1, prefix1, strlen(prefix1))) //QUERY: ...
  359. {
  360. p += strlen(prefix1)+1;
  361. if (expandConciseRequest(p, service.clear(), method.clear(), request.clear(), xml.clear(), msg.clear()))
  362. bProcess = true;
  363. else
  364. puts(msg.str());
  365. }
  366. else if (p && !strnicmp(p+1, prefix2, strlen(prefix2))) //FULL QUERY: ...
  367. {
  368. p += strlen(prefix2)+1;
  369. xml.clear().append(p);
  370. const char* q = p + xml.length() - 1;
  371. while (q > p && (*q == '\"' || *q == '\r' || *q == '\n'))
  372. q--;
  373. xml.setLength( q-p+1 );//lose trailing double quote, CR and LF chars
  374. p = strchr(p, '<');
  375. if (p)
  376. {
  377. q = strchr(++p, '>');
  378. if (q)
  379. {
  380. method.clear().append(q-p, p);
  381. //if the root name does not end with Request then append it
  382. const char* z = strstr(method.str(), "Request");
  383. if (!z)
  384. {
  385. StringBuffer config(method);
  386. lookupMethod(config.str(), service.clear(), method, request);
  387. StringBuffer tag(request);
  388. xml.remove(1, q-p).insert(1, tag);//replace starting root tag
  389. //now find last tag and replace that as well
  390. p = xml.str();
  391. q = p + xml.length() - 1;
  392. while (q > p && *q != '>')
  393. q--;
  394. p = q;
  395. while (p > xml.str() && *p != '/')
  396. p--;
  397. xml.remove(p+1-xml.str(), q-p-1).insert(p+1-xml.str(), tag.str());
  398. }
  399. else
  400. {
  401. method.setLength(z-method.str());
  402. StringBuffer config(method);
  403. lookupMethod(config.str(), service.clear(), method, request);
  404. }
  405. bProcess = true;
  406. }
  407. }
  408. }
  409. if (bProcess && maxInstances > 0)
  410. {
  411. InstanceMap::const_iterator it = instanceMap.find( method.str() );
  412. if (it == instanceMap.end())
  413. instanceMap.insert( std::pair<std::string, int>(method.str(), 1) );
  414. else
  415. {
  416. const int nInstances = (*it).second;
  417. if (nInstances < maxInstances)
  418. instanceMap[method.str()] = nInstances+1;
  419. else
  420. bProcess = false;
  421. }
  422. }
  423. if (bProcess)
  424. {
  425. xml.insert( 0, "<?xml version='1.0' encoding='UTF-8'?>\n"
  426. "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' \n"
  427. " xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/' \n"
  428. " xmlns='urn:hpccsystems:ws:wsaccurint'>\n <soap:Body>\n");
  429. xml.append(" </soap:Body>\n</soap:Envelope>\n");
  430. StringBuffer seqNum;
  431. p = strchr(buf, ' ');
  432. if (p)
  433. seqNum.append(p-buf, buf);
  434. httpClient.addEspRequest(seqNum.str(), service.str(), method.str(), xml, httpStat);
  435. }
  436. }
  437. }
  438. struct stat st;
  439. bool rc = false;
  440. if(fd < 0)
  441. printf("File %s doesn't exist\n", logFileName);
  442. else
  443. if (stat(logFileName, &st) < 0)
  444. printf("stat error - %s\n", strerror(errno));
  445. else
  446. rc = true;
  447. return rc;
  448. }