example_tc_http.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_socket.h"
  17. #include "util/tc_clientsocket.h"
  18. #include "util/tc_http.h"
  19. #include "util/tc_file.h"
  20. #include "util/tc_epoller.h"
  21. #include "util/tc_common.h"
  22. #include <iostream>
  23. #include <arpa/inet.h>
  24. #include <fcntl.h>
  25. #include <netdb.h>
  26. #include <zlib.h>
  27. using namespace tars;
  28. void testTC_HTTPClient()
  29. {
  30. TC_HttpRequest stHttpReq;
  31. stHttpReq.setCacheControl("no-cache");
  32. stHttpReq.setUserAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1; .NET CLR 1.1.4322)");
  33. // stHttpReq.setGetRequest("http://www.baidu.com");
  34. // stHttpReq.setGetRequest("http://www.google.com");
  35. stHttpReq.setGetRequest("http://s39.cnzz.com/stat.php?id=533357&web_id=533357&show=pic2");
  36. // stHttpReq.setGetRequest("http://172.23.154.13:8080/");
  37. // stHttpReq.setGetRequest("http://172.25.38.67:8080");
  38. // stHttpReq.setGetRequest("http://172.27.31.50:8090/mqqservice/jiaxin.jsp?qq=39067484&type=4");
  39. string sSendBuffer = stHttpReq.encode();
  40. cout << sSendBuffer << endl;
  41. cout << "***********************************" << endl;
  42. TC_HttpResponse stHttpRep;
  43. int iRet = stHttpReq.doRequest(stHttpRep, 3000);
  44. if(iRet != 0)
  45. {
  46. cout << iRet << endl;
  47. }
  48. cout << TC_Common::tostr(stHttpRep.getHeaders()) << endl;
  49. string r = stHttpRep.encode();
  50. // cout << r << endl;
  51. cout << "***********************************" << endl;
  52. TC_File::save2file("tmp.html", stHttpRep.getContent());
  53. cout << "***********************************" << endl;
  54. cout << stHttpRep.getContent().length() << endl;
  55. }
  56. void testHttpHeader1()
  57. {
  58. string s = "HTTP/1.1 200 OK\r\n";
  59. s += "Server:nginx/0.6.39\r\n";
  60. s += "Accept-Ranges: bytes\r\n";
  61. s += "Accept-Ranges: bytes\r\n";
  62. TC_Http::http_header_type m;
  63. TC_Http::parseHeader(s.c_str(), m);
  64. cout << TC_Common::tostr(m) << endl;
  65. TC_Http t;
  66. t.setHeader("abc", "def");
  67. t.setHeader("abC", "def");
  68. m = t.getHeaders();
  69. cout << TC_Common::tostr(m) << endl;
  70. }
  71. void testHttp1()
  72. {
  73. TC_HttpRequest stHttpReq;
  74. // stHttpReq.setGetRequest("http://www.baidu.com");
  75. // stHttpReq.setGetRequest("http://www.google.com");
  76. // stHttpReq.setGetRequest("https://s39.cnzz.com:8080/stat.php?id=533357&web_id=533357&show=pic2", true);
  77. string a;
  78. stHttpReq.setPostRequest("https://s39.cnzz.com:8080/stat.php?id=533357&web_id=533357&show=pic2", a, true );
  79. cout << stHttpReq.encode() << endl;
  80. cout << "----------------------------------------" << endl;
  81. cout << stHttpReq.getHost() << endl;
  82. cout << stHttpReq.getOriginRequest() << endl;
  83. cout << stHttpReq.getRequest() << endl;
  84. cout << stHttpReq.getRequestParam() << endl;
  85. cout << stHttpReq.getRequestUrl() << endl;
  86. cout << "----------------------------------------" << endl;
  87. string sSendBuffer = stHttpReq.encode();
  88. cout << sSendBuffer << endl;
  89. cout << "***********************************" << endl;
  90. }
  91. void testHttp2()
  92. {
  93. TC_HttpRequest stHttpReq;
  94. stHttpReq.setGetRequest("http://s39.cnzz.com:8080/stat.php?id=533357&web_id=533357&show=pic2", true);
  95. string sSendBuffer = stHttpReq.encode();
  96. cout << sSendBuffer << endl;
  97. string sHost;
  98. uint32_t iPort;
  99. stHttpReq.getHostPort(sHost, iPort);
  100. cout << "-----" << sHost << ", " << iPort <<endl;
  101. cout << stHttpReq.getHost() << endl;
  102. cout << stHttpReq.getOriginRequest() << endl;
  103. cout << stHttpReq.getRequest() << endl;
  104. cout << stHttpReq.getRequestParam() << endl;
  105. cout << stHttpReq.getRequestUrl() << endl;
  106. cout << "***********************************" << endl;
  107. }
  108. void testHttpHeader2()
  109. {
  110. string s = string("GET /stat.php?id=533357&web_id=533357&show=pic2 HTTP/1.1\r\n")
  111. +string("Host: s39.cnzz.com:8080\r\n")
  112. +string("User-Agent: TC_Http\r\n")
  113. +string("\r\n");
  114. TC_HttpRequest stHttpReq;
  115. stHttpReq.decode(s);
  116. string sDomain;
  117. uint32_t iPort;
  118. stHttpReq.getHostPort(sDomain, iPort);
  119. cout << "getHostPort:" << sDomain << ":" << iPort <<endl;
  120. cout << stHttpReq.getHost() << endl;
  121. assert(stHttpReq.getHost() == "s39.cnzz.com:8080");
  122. cout << stHttpReq.getOriginRequest() << endl;
  123. assert(stHttpReq.getOriginRequest() == "http://s39.cnzz.com:8080/stat.php?id=533357&web_id=533357&show=pic2");
  124. cout << stHttpReq.getRequest() << endl;
  125. assert(stHttpReq.getRequest() == "/stat.php?id=533357&web_id=533357&show=pic2");
  126. cout << stHttpReq.getRequestParam() << endl;
  127. assert(stHttpReq.getRequestParam() == "id=533357&web_id=533357&show=pic2");
  128. cout << stHttpReq.getRequestUrl() << endl;
  129. assert(stHttpReq.getRequestUrl() == "/stat.php");
  130. cout << "***********************************" << endl;
  131. }
  132. void testHttpHeader3()
  133. {
  134. string s = string("GET /stat.php?id=533357&web_id=533357&show=pic2 HTTP/1.1\r\n")
  135. +string("Host: s39.cnzz.com\r\n")
  136. +string("User-Agent: TC_Http\r\n")
  137. +string("\r\n");
  138. TC_HttpRequest stHttpReq;
  139. stHttpReq.decode(s);
  140. string sDomain;
  141. uint32_t iPort;
  142. stHttpReq.getHostPort(sDomain, iPort);
  143. cout << "getHostPort:" << sDomain << ":" << iPort <<endl;
  144. cout << stHttpReq.getHost() << endl;
  145. assert(stHttpReq.getHost() == "s39.cnzz.com");
  146. cout << stHttpReq.getOriginRequest() << endl;
  147. assert(stHttpReq.getOriginRequest() == "http://s39.cnzz.com/stat.php?id=533357&web_id=533357&show=pic2");
  148. cout << stHttpReq.getRequest() << endl;
  149. assert(stHttpReq.getRequest() == "/stat.php?id=533357&web_id=533357&show=pic2");
  150. cout << stHttpReq.getRequestParam() << endl;
  151. assert(stHttpReq.getRequestParam() == "id=533357&web_id=533357&show=pic2");
  152. cout << stHttpReq.getRequestUrl() << endl;
  153. assert(stHttpReq.getRequestUrl() == "/stat.php");
  154. cout << "***********************************" << endl;
  155. }
  156. void testHttpHeader4()
  157. {
  158. string s = string("GET http://s39.cnzz.com:80/stat.php?id=533357&web_id=533357&show=pic2 HTTP/1.1\r\n")
  159. +string("User-Agent: TC_Http\r\n")
  160. +string("\r\n");
  161. TC_HttpRequest stHttpReq;
  162. stHttpReq.decode(s);
  163. string sDomain;
  164. uint32_t iPort;
  165. stHttpReq.getHostPort(sDomain, iPort);
  166. cout << "getHostPort:" << sDomain << ":" << iPort <<endl;
  167. cout << stHttpReq.getHost() << endl;
  168. assert(stHttpReq.getHost() == "s39.cnzz.com");
  169. cout << stHttpReq.getOriginRequest() << endl;
  170. assert(stHttpReq.getOriginRequest() == "http://s39.cnzz.com/stat.php?id=533357&web_id=533357&show=pic2");
  171. cout << stHttpReq.getRequest() << endl;
  172. assert(stHttpReq.getRequest() == "/stat.php?id=533357&web_id=533357&show=pic2");
  173. cout << stHttpReq.getRequestParam() << endl;
  174. assert(stHttpReq.getRequestParam() == "id=533357&web_id=533357&show=pic2");
  175. cout << stHttpReq.getRequestUrl() << endl;
  176. assert(stHttpReq.getRequestUrl() == "/stat.php");
  177. cout << "***********************************" << endl;
  178. }
  179. void testHttpsHeader2()
  180. {
  181. string s = string("GET https://s39.cnzz.com:8080/stat.php?id=533357&web_id=533357&show=pic2 HTTP/1.1\r\n")
  182. +string("Host: s39.cnzz.com:8080\r\n")
  183. +string("User-Agent: TC_Http\r\n")
  184. +string("\r\n");
  185. TC_HttpRequest stHttpReq;
  186. stHttpReq.decode(s);
  187. string sDomain;
  188. uint32_t iPort;
  189. stHttpReq.getHostPort(sDomain, iPort);
  190. cout << "getHostPort:" << sDomain << ":" << iPort <<endl;
  191. cout << stHttpReq.getHost() << endl;
  192. assert(stHttpReq.getHost() == "s39.cnzz.com:8080");
  193. cout << stHttpReq.getOriginRequest() << endl;
  194. assert(stHttpReq.getOriginRequest() == "https://s39.cnzz.com:8080/stat.php?id=533357&web_id=533357&show=pic2");
  195. cout << stHttpReq.getRequest() << endl;
  196. assert(stHttpReq.getRequest() == "/stat.php?id=533357&web_id=533357&show=pic2");
  197. cout << stHttpReq.getRequestParam() << endl;
  198. assert(stHttpReq.getRequestParam() == "id=533357&web_id=533357&show=pic2");
  199. cout << stHttpReq.getRequestUrl() << endl;
  200. assert(stHttpReq.getRequestUrl() == "/stat.php");
  201. cout << "***********************************" << endl;
  202. }
  203. void testEastMoney()
  204. {
  205. string s = string("GET / HTTP/1.1\r\n")
  206. +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
  207. +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
  208. +string("Accept-Encoding: gzip\r\n")
  209. +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
  210. +string("Connection: close\r\n")
  211. +string("Host: www.eastmoney.com\r\n")
  212. +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
  213. +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
  214. +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  215. TC_HttpRequest stHttpReq;
  216. stHttpReq.decode(s);
  217. string sSendBuffer = stHttpReq.encode();
  218. cout << sSendBuffer << endl;
  219. cout << "***********************************" << endl;
  220. TC_HttpResponse stHttpRep;
  221. int iRet = stHttpReq.doRequest(stHttpRep, 10000);
  222. if(iRet != 0)
  223. {
  224. cout << iRet << endl;
  225. }
  226. cout << TC_Common::tostr(stHttpRep.getHeaders()) << endl;
  227. // string r = stHttpRep.encode();
  228. // cout << r << endl;
  229. cout << "***********************************" << endl;
  230. TC_File::save2file("tmp.html", stHttpRep.getContent());
  231. cout << "***********************************" << endl;
  232. cout << stHttpRep.getContent().length() << endl;
  233. }
  234. void testHttpResponse()
  235. {
  236. string s = "HTTP/1.1 200 aaa \r\n";// 200 Aouut Error\r\n";
  237. s += "Server:nginx/0.6.39\r\n";
  238. s += "Accept-Ranges: bytes\r\n";
  239. s += "Accept-Ranges: bytes\r\n\r\n";
  240. TC_HttpResponse rsp;
  241. rsp.decode(s);
  242. cout << rsp.getAbout() << endl;
  243. cout << rsp.getStatus() << endl;
  244. cout << rsp.getVersion() << endl;
  245. }
  246. void testURL(TC_URL &url, const string &sURL, const string &sURL1)
  247. {
  248. cout << sURL << endl;
  249. url.parseURL(sURL);
  250. cout << "[" << sURL << "] to [" << url.getURL() << "] rootpath:" << url.getRootPath() << " relativePath:" << url.getRelativePath() << endl;
  251. assert(url.getURL() == sURL1);
  252. }
  253. void testBuildURL(TC_URL &url, const string &sURL, const string &sURL1, const string &sURL2)
  254. {
  255. url.parseURL(sURL);
  256. TC_URL u = url.buildWithRelativePath(sURL1);
  257. cout << url.getPath() << endl;
  258. cout << "[" << sURL << "] " << sURL1 << " to [ " << u.getURL() << "] " << endl;
  259. // assert(u.getURL() == sURL2);
  260. }
  261. string simplifyDirectory(const string& path)
  262. {
  263. //所有./都去掉
  264. size_t pos = 0;
  265. string sNewPath = path;
  266. while (true)
  267. {
  268. size_t dotPos = sNewPath.find("./", pos);
  269. if (dotPos != string::npos)
  270. {
  271. if ((dotPos == 0) || (sNewPath.at(dotPos - 1) == '/'))
  272. {
  273. sNewPath.erase(dotPos, 2);
  274. }
  275. else
  276. {
  277. pos = dotPos + 2;
  278. }
  279. }
  280. else
  281. {
  282. break;
  283. }
  284. }
  285. //如果路径是以.结尾的, 则.去掉
  286. if (((sNewPath.length() >= 2) && (sNewPath.substr(sNewPath.length()-2) == "/.")) || (sNewPath == "."))
  287. {
  288. sNewPath.erase(sNewPath.length() - 1);
  289. }
  290. //处理/../的形式
  291. pos = 0;
  292. size_t startPos = 0;
  293. while (1)
  294. {
  295. size_t slashDot = sNewPath.find("/../", pos);
  296. if (slashDot != string::npos)
  297. {
  298. if (0 == slashDot)
  299. {
  300. sNewPath.erase(0, 3);
  301. continue;
  302. }
  303. if ( (slashDot > 1) && (sNewPath.substr(slashDot - 2, 2) == "..") )
  304. {
  305. pos = slashDot + 4;
  306. continue;
  307. }
  308. startPos = sNewPath.rfind('/', slashDot - 1);
  309. if (startPos == string::npos) startPos = 0;
  310. sNewPath.erase(startPos, slashDot + 4 - startPos - 1);
  311. }
  312. else
  313. {
  314. break;
  315. }
  316. }
  317. //处理/..结尾的情况
  318. if ((sNewPath.size() >= 3) && (sNewPath.substr(sNewPath.size() - 3, 3) == "/.."))
  319. {
  320. size_t slashDot = sNewPath.size() - 3;
  321. if (!((slashDot > 1) && (sNewPath.substr(slashDot - 2, 2) == "..")))
  322. {
  323. startPos = sNewPath.rfind ('/', slashDot - 1);
  324. if (startPos == string::npos) startPos = 0;
  325. sNewPath.erase (startPos + 1);
  326. }
  327. }
  328. return sNewPath;
  329. /*
  330. string result = path;
  331. string::size_type pos;
  332. pos = 0;
  333. while((pos = result.find("//", pos)) != string::npos)
  334. {
  335. result.erase(pos, 1);
  336. }
  337. pos = 0;
  338. while((pos = result.find("/./", pos)) != string::npos)
  339. {
  340. result.erase(pos, 2);
  341. }
  342. pos = 0;
  343. while((pos = result.find("/../", pos)) != string::npos)
  344. {
  345. result.erase(pos, 3);
  346. }
  347. while(result.substr(0, 4) == "/../")
  348. {
  349. result.erase(0, 3);
  350. }
  351. if(result == "/.")
  352. {
  353. return result.substr(0, result.size() - 1);
  354. }
  355. if(result.size() >= 2 && result.substr(result.size() - 2, 2) == "/.")
  356. {
  357. result.erase(result.size() - 2, 2);
  358. }
  359. if(result == "/")
  360. {
  361. return result;
  362. }
  363. if(result.size() >= 1 && result[result.size() - 1] == '/')
  364. {
  365. result.erase(result.size() - 1);
  366. }
  367. if(result == "/..")
  368. {
  369. result = "/";
  370. }
  371. return result;
  372. */
  373. }
  374. void testURL()
  375. {
  376. TC_URL url;
  377. /*
  378. testURL(url, ":pass@www.google.com", "http://www.google.com/");
  379. testURL(url, "http://:pass@www.google.com", "http://www.google.com/");
  380. testURL(url, "http://www.google.com/abc/def", "http://www.google.com/abc/def");
  381. cout << url.getPath() << endl;
  382. cout << url.getQuery() << endl;
  383. testURL(url, "http://user:pass@google.com:99/foo;bar?q=a#ref", "http://user:pass@google.com:99/foo;bar?q=a#ref");
  384. testURL(url, "http://user@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "http://user@www.qq.com:8080/abc/dev/query?a=b&c=3#ref");
  385. testURL(url, "http://@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "http://www.qq.com:8080/abc/dev/query?a=b&c=3#ref");
  386. testURL(url, "http://www.qq.com/abc/dev/query?a=b&c=3#ref", "http://www.qq.com/abc/dev/query?a=b&c=3#ref");
  387. testURL(url, "http://www.qq.com?a=b&c=3#ref", "http://www.qq.com/?a=b&c=3#ref");
  388. testURL(url, "http://www.qq.com#ref", "http://www.qq.com/#ref");
  389. testURL(url, "http://www.qq.com?#ref", "http://www.qq.com/#ref");
  390. testURL(url, "http://:pass@www.google.com", "http://www.google.com/"),
  391. testURL(url, "http://:@www.google.com", "http://www.google.com/"),
  392. testURL(url, "ftp://user:password@www.qq.com:21/abc/dev/query?a=b&c=3#ref", "ftp://user:password@www.qq.com/abc/dev/query?a=b&c=3#ref");
  393. testURL(url, "https://user:password@www.qq.com:433/abc/dev/query?a=b&c=3#ref", "https://user:password@www.qq.com/abc/dev/query?a=b&c=3#ref");
  394. testURL(url, "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref");
  395. */
  396. testBuildURL(url, "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "abc/def", "https://user:password@www.qq.com:8080/abc/dev/abc/def");
  397. testBuildURL(url, "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "/abc/def", "https://user:password@www.qq.com:8080/abc/def");
  398. testBuildURL(url, "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "../../abc/def", "https://user:password@www.qq.com:8080/abc/def");
  399. testBuildURL(url, "https://user:password@www.qq.com:8080/abc/dev/query?a=b&c=3#ref", "#abc", "https://user:password@www.qq.com:8080/abc/def");
  400. testBuildURL(url, "http://www.qq.com", "#abc", "http://www.qq.com/#abc");
  401. testBuildURL(url, "http://www.qq.com/a/b", "#abc", "http://www.qq.com/#abc");
  402. testBuildURL(url, "http://www.qq.com/a/b", "../../#abc", "http://www.qq.com/#abc");
  403. testBuildURL(url, "http://www.qq.com/a/b", "./../../#abc", "http://www.qq.com/#abc");
  404. testBuildURL(url, "http://www.qq.com/a/b/", "../#abc", "http://www.qq.com/#abc");
  405. testBuildURL(url, "http://www.qq.com/a/b/", "../abc/def/..", "http://www.qq.com/#abc");
  406. testBuildURL(url, "http://www.qq.com/a/b", "../abc/def/../../.", "http://www.qq.com/#abc");
  407. }
  408. void testCookie(const string &sRspURL, const string &sReqURL, const vector<string> &vsCookie)
  409. {
  410. cout << sRspURL << "=>" << sReqURL << "-----------------------------------" << endl;
  411. TC_HttpCookie cookie;
  412. cookie.addCookie(sRspURL, vsCookie);
  413. list<TC_HttpCookie::Cookie> vCookie = cookie.getAllCookie();
  414. cout << "All Cookie:" << sRspURL << "-----------------------------------" << endl;
  415. list<TC_HttpCookie::Cookie>::iterator it = vCookie.begin();
  416. while(it != vCookie.end())
  417. {
  418. cout << TC_Common::tostr(it->_data.begin(), it->_data.end(), "; ") << ", " << it->_expires << ", " << it->_path << endl;
  419. ++it;
  420. }
  421. cout << "-----------------------------------" << endl << endl;
  422. string sCookie;
  423. cookie.getCookieForURL(sReqURL, sCookie);
  424. cout << TC_Common::tostr(sCookie) << endl;
  425. cout << "-----------------------------------" << endl << endl;
  426. }
  427. void testCookie()
  428. {
  429. assert(TC_HttpCookie::matchDomain("qq.com", "www.qq.com") == true);
  430. assert(TC_HttpCookie::matchDomain(".qq.com", "www.qq.com") == true);
  431. assert(TC_HttpCookie::matchDomain(".qq.com", "qq.com") == true);
  432. assert(TC_HttpCookie::matchDomain("t.qq.com", "www.qq.com") == false);
  433. assert(TC_HttpCookie::matchDomain(".t.qq.com", "www.qq.com") == false);
  434. assert(TC_HttpCookie::matchDomain(".t.qq.com", "t.qq.com") == true);
  435. assert(TC_HttpCookie::matchDomain(".com", "www.qq.com") == false);
  436. assert(TC_HttpCookie::matchDomain(".com", "qq.com") == false);
  437. assert(TC_HttpCookie::matchDomain(".y.qq.com", "x.y.qq.com") == true);
  438. assert(TC_HttpCookie::matchDomain(".x.y.qq.com", "x.y.qq.com") == true);
  439. assert(TC_HttpCookie::matchDomain(".qq.com", "x.y.qq.com") == true);
  440. assert(TC_HttpCookie::matchDomain(".qq.com", "y.qq.com") == true);
  441. assert(TC_HttpCookie::matchDomain("qq.com", "y.qq.com") == true);
  442. cout << TC_Common::now2GMTstr() << endl;
  443. string gmt = TC_Common::tm2GMTstr(time(NULL) + 10);
  444. string s = "HTTP/1.1 200 OK\r\n";// 200 Aouut Error\r\n";
  445. s += "Set-Cookie: n1=1; a=1; c=d; Path=/; Domain=qq.com; Expires=" + gmt + "\r\n";
  446. s += "Set-Cookie: n2=2; a=0; c=d; Path=/abc/def; Domain=.qq.com; Expires=" + gmt + "\r\n";
  447. s += "Set-Cookie: n3=3; a=5; c=d; Path=/abc/def/aaa; Domain=.qq.com; Expires=" + gmt + "\r\n";
  448. s += "Set-Cookie: n4=4; a=6; c=d; Path=/abc; Domain=.qq.com; Expires=" + gmt + "\r\n";
  449. s += "Set-Cookie: n5=5; a=2; c=d; Path=/; Domain=.qq.com; Expires=" + gmt + "\r\n";
  450. s += "Set-Cookie: n6=6; c=3; Path=/; Domain=y.qq.com; Expires=" + gmt + "\r\n";
  451. s += "Set-Cookie: n7=7; c=3; Path=/abc; Domain=.y.qq.com; Expires=" + gmt + "\r\n";
  452. s += "Set-Cookie: n8=6; c=3; Path=/; Domain=x.y.qq.com; Expires=" + gmt + "\r\n";
  453. s += "Set-Cookie: n9=7; c=4; Path=/; Domain=.x.y.qq.com; Expires=" + gmt + "\r\n";
  454. s += "Set-Cookie: n10=7; c=4; Path=/; Domain=qqq.com; Expires=" + gmt + "\r\n";
  455. s += "Set-Cookie: n11=7; c=4; Path=/; Domain=.qqq.com; Expires=" + gmt + "\r\n";
  456. s += "Set-Cookie: n12=8; c=4; Expires=" + gmt + "\r\n";
  457. s += "Accept-Ranges: bytes\r\n\r\n";
  458. TC_HttpResponse rsp;
  459. rsp.decode(s);
  460. cout << "-----------------------------------" << endl;
  461. vector<string> vsCookie = rsp.getSetCookie();
  462. cout << TC_Common::tostr(vsCookie.begin(), vsCookie.end(), "\r\n") << endl << endl;
  463. testCookie("http://www.qq.com", "http://www.qq.com", vsCookie);
  464. testCookie("http://www.qq.com/abc/def", "http://www.qq.com", vsCookie);
  465. testCookie("http://www.qq.com/abc/def", "http://www.qq.com/abc", vsCookie);
  466. cout << endl;
  467. // sleep(11);
  468. testCookie("http://www.qq.com", "http://qq.com", vsCookie);
  469. testCookie("http://www.qq.com/abc/def/aaa", "http://www.qq.com/abc/def/aaa", vsCookie);
  470. testCookie("http://www.qq.com/abc/def/aaa", "http://www.qq.com", vsCookie);
  471. testCookie("http://www.qq.com", "http://www.qq.com/abc/def", vsCookie);
  472. testCookie("http://qq.com", "http://qq.com/abc/def", vsCookie);
  473. testCookie("http://qq.com", "http://t.qq.com/abc/def", vsCookie);
  474. testCookie("http://qq.com", "http://y.qq.com/", vsCookie);
  475. testCookie("http://qq.com", "http://y.qq.com/abc", vsCookie);
  476. testCookie("http://x.y.qq.com", "http://x.y.qq.com", vsCookie);
  477. }
  478. int main(int argc, char *argv[])
  479. {
  480. try
  481. {
  482. // testHttp1();
  483. // testHttp2();
  484. // return 0;
  485. cout << "testURL-----------------------------------" << endl;
  486. testURL();
  487. return 0;
  488. cout << "testHttpHeader2-----------------------------------" << endl;
  489. testHttpHeader2();
  490. cout << "-----------------------------------" << endl;
  491. testHttpHeader3();
  492. cout << "testHttpHeader3-----------------------------------" << endl;
  493. testHttpHeader4();
  494. cout << "testHttpHeader4-----------------------------------" << endl;
  495. testHttpsHeader2();
  496. cout << "testCookie-----------------------------------" << endl;
  497. // testCookie();
  498. // testHttpResponse();
  499. // testEastMoney();
  500. // testHttp2();
  501. // testTC_HTTPClient();
  502. }
  503. catch(exception &ex)
  504. {
  505. cout << ex.what() << endl;
  506. }
  507. return 0;
  508. }