关键词搜索

源码搜索 ×
×

tars源码分析之5

发布2022-07-03浏览419次

详情内容

cgi的相关实现,一起来看下,还是很常规的:

  1. #include "util/tc_cgi.h"
  2. #include "util/tc_common.h"
  3. #include "util/tc_http.h"
  4. #include <string.h>
  5. namespace tars
  6. {
  7. ostream &operator<<(ostream &os, const TC_Cgi_Upload &tcCgiUpload)
  8. {
  9. os << tcCgiUpload.tostr();
  10. return os;
  11. }
  12. TC_Cgi_Upload::TC_Cgi_Upload(const TC_Cgi_Upload &tcCgiUpload)
  13. {
  14. _sFileName = tcCgiUpload._sFileName;
  15. _sRealFileName = tcCgiUpload._sRealFileName;
  16. _sServerFileName= tcCgiUpload._sServerFileName;
  17. _iSize = tcCgiUpload._iSize;
  18. _bOverSize = tcCgiUpload._bOverSize;
  19. }
  20. TC_Cgi_Upload & TC_Cgi_Upload::operator=(const TC_Cgi_Upload &tcCgiUpload)
  21. {
  22. if(this != &tcCgiUpload)
  23. {
  24. _sFileName = tcCgiUpload._sFileName;
  25. _sRealFileName = tcCgiUpload._sRealFileName;
  26. _sServerFileName= tcCgiUpload._sServerFileName;
  27. _iSize = tcCgiUpload._iSize;
  28. _bOverSize = tcCgiUpload._bOverSize;
  29. }
  30. return *this;
  31. }
  32. string TC_Cgi_Upload::tostr() const
  33. {
  34. string sBuffer;
  35. sBuffer = "[file控件名称:" + _sFileName + "] ";
  36. sBuffer += " [真实文件名称:" + _sRealFileName + "] ";
  37. sBuffer += " [服务器端文件名称:" + _sServerFileName + "] ";
  38. sBuffer += " [大小(字节):" + TC_Common::tostr(_iSize) + "] ";
  39. sBuffer += " [超过大小限制:" + TC_Common::tostr(_bOverSize) + "] ";
  40. return sBuffer;
  41. }
  42. TC_Cgi::TC_Cgi()
  43. : _is(NULL)
  44. , _iMaxUploadFiles(1)
  45. , _iUploadMaxSize(20971520)
  46. , _bOverUploadFiles(false)
  47. , _iMaxContentLength(20971520)
  48. , _bUploadFileOverSize(false)
  49. {
  50. }
  51. TC_Cgi::~TC_Cgi()
  52. {
  53. }
  54. void TC_Cgi::setUpload(const string &sUploadFilePrefix, int iMaxUploadFiles, size_t iUploadMaxSize, size_t iMaxContentLength)
  55. {
  56. _sUploadFilePrefix = sUploadFilePrefix;
  57. _iMaxUploadFiles = iMaxUploadFiles;
  58. _iUploadMaxSize = iUploadMaxSize;
  59. _iMaxContentLength = iMaxContentLength;
  60. }
  61. void TC_Cgi::parseCgi()
  62. {
  63. char **env = environ;
  64. while(*env != NULL)
  65. {
  66. string s(*env);
  67. string::size_type pos = s.find('=');
  68. if(pos != string::npos)
  69. {
  70. _env[s.substr(0, pos)] = s.substr(pos + 1);
  71. }
  72. ++env;
  73. }
  74. _is = &cin;
  75. readCgiInput(_mmpParams, _mpCookies);
  76. }
  77. void TC_Cgi::parseCgi(const TC_HttpRequest &request)
  78. {
  79. setCgiEnv("QUERY_STRING", request.getRequestParam());
  80. setCgiEnv("CONTENT_LENGTH", TC_Common::tostr(request.getContentLength()));
  81. setCgiEnv("HTTP_COOKIE", request.getHeader("Cookie"));
  82. setCgiEnv("CONTENT_TYPE", request.getHeader("Content-Type"));
  83. setCgiEnv("REQUEST_METHOD", request.isGET() ? "GET" : "POST");
  84. _buffer = request.getContent();
  85. _iss.str(_buffer);
  86. _is = &_iss;
  87. readCgiInput(_mmpParams, _mpCookies);
  88. }
  89. void TC_Cgi::getPOST(string &sBuffer)
  90. {
  91. string::size_type iSize = atoi(getCgiEnv(TC_Cgi::ENM_CONTENT_LENGTH).c_str());
  92. if( iSize > _iMaxContentLength)
  93. {
  94. throw TC_Cgi_Exception("[TC_Cgi::getPOST] : CONTENT_LENGTH is too large!");
  95. }
  96. if(iSize <= 0)
  97. {
  98. return;
  99. }
  100. sBuffer = _buffer;
  101. if(sBuffer.length() < iSize)
  102. {
  103. throw TC_Cgi_Exception("[TC_Cgi::getPOST] : Read CONTENT error: content size " + TC_Common::tostr(sBuffer.length()) + "<" + TC_Common::tostr(iSize));
  104. }
  105. }
  106. void TC_Cgi::getGET(string &sBuffer)
  107. {
  108. sBuffer = getCgiEnv(TC_Cgi::ENM_QUERY_STRING);
  109. }
  110. void TC_Cgi::readCgiInput(multimap<string, string> &mmpParams, map<string, string> &mpCooies)
  111. {
  112. string sBuffer = TC_Cgi::getCgiEnv(TC_Cgi::ENM_HTTP_COOKIE);
  113. parseCookies(mpCooies, sBuffer);
  114. if (getCgiEnv(ENM_REQUEST_METHOD).find("POST") != string::npos)
  115. {
  116. //文件上传模式
  117. if (getCgiEnv(ENM_CONTENT_TYPE).find("multipart/form-data") != string::npos)
  118. {
  119. parseUpload(mmpParams);
  120. }
  121. else if (getCgiEnv(ENM_CONTENT_TYPE).find("application/x-www-form-urlencoded") != string::npos)
  122. {
  123. getPOST(sBuffer);
  124. parseNormal(mmpParams, sBuffer);
  125. }
  126. getGET(sBuffer);
  127. }
  128. else if (getCgiEnv(ENM_REQUEST_METHOD).find("GET") != string::npos)
  129. {
  130. getGET(sBuffer);
  131. }
  132. else
  133. {
  134. //default is GET
  135. getGET(sBuffer);
  136. }
  137. if (sBuffer.length() == 0)
  138. {
  139. return ;
  140. }
  141. parseNormal(mmpParams, sBuffer);
  142. }
  143. void TC_Cgi::parseNormal(multimap<string, string> &mmpParams, const string& sBuffer)
  144. {
  145. int iFlag = 0;
  146. string sName;
  147. string sValue;
  148. string sTmp;
  149. string::size_type len = sBuffer.length();
  150. string::size_type pos = 0;
  151. while (pos < len)
  152. {
  153. sTmp = "";
  154. if(iFlag == 0)
  155. {
  156. while ( (sBuffer[pos] != '=') && (pos < len) )
  157. {
  158. sTmp += (sBuffer[pos] == '+') ? ' ' : sBuffer[pos];
  159. ++pos;
  160. }
  161. }
  162. else
  163. {
  164. while ( (sBuffer[pos] != '&') && (pos < len) )
  165. {
  166. sTmp += (sBuffer[pos] == '+') ? ' ' : sBuffer[pos];
  167. ++pos;
  168. }
  169. }
  170. if (iFlag == 0) //param name
  171. {
  172. sName = TC_Cgi::decodeURL(sTmp);
  173. if ( (sBuffer[pos] != '=') || (pos == len - 1) )
  174. {
  175. sValue = "";
  176. mmpParams.insert(multimap<string, string>::value_type(sName, sValue));
  177. }
  178. else
  179. {
  180. iFlag = 1;
  181. }
  182. }
  183. else
  184. {
  185. sValue = TC_Cgi::decodeURL(sTmp);
  186. mmpParams.insert(multimap<string, string>::value_type(sName, sValue));
  187. iFlag = 0;
  188. }
  189. ++pos;
  190. }
  191. }
  192. void TC_Cgi::parseCookies(map<string, string> &mpCookies, const string& sBuffer)
  193. {
  194. string::size_type len = sBuffer.length();
  195. string::size_type pos = 0;
  196. if(len == 0)
  197. {
  198. return;
  199. }
  200. string sName;
  201. string sValue;
  202. int iFlag = 1;
  203. while(pos < len)
  204. {
  205. if(sBuffer[pos] == '=')
  206. {
  207. if( pos == len - 1) //最后一个参数
  208. {
  209. sName = decodeURL(TC_Common::trimleft(sName, " "));
  210. sValue = decodeURL(sValue);
  211. mpCookies[sName] = sValue;
  212. sName = "";
  213. sValue= "";
  214. }
  215. iFlag = 0; //取值
  216. }
  217. else if ( (sBuffer[pos] == ';') || (pos == len - 1) )
  218. {
  219. if(iFlag == 0)
  220. {
  221. if(pos == len - 1)
  222. {
  223. sValue += sBuffer[pos];
  224. }
  225. sName = decodeURL(TC_Common::trimleft(sName, " "));
  226. sValue = decodeURL(sValue);
  227. mpCookies[sName] = sValue;
  228. sName = "";
  229. sValue= "";
  230. iFlag = 1; //取名称
  231. }
  232. }
  233. else if (iFlag)
  234. {
  235. sName += sBuffer[pos];
  236. }
  237. else
  238. {
  239. sValue += sBuffer[pos];
  240. }
  241. ++pos;
  242. }
  243. }
  244. void TC_Cgi::ignoreLine()
  245. {
  246. string sBuffer;
  247. //读取直到有一个空行
  248. while(true)
  249. {
  250. if(!getline(*_is, sBuffer))
  251. {
  252. throw TC_Cgi_Exception("[TC_Cgi::parseFormData] 'multipart/form-data' Format is error");
  253. }
  254. sBuffer = TC_Common::trim(sBuffer, "\r\n");
  255. if(sBuffer.empty())
  256. {
  257. break;
  258. }
  259. }
  260. }
  261. bool TC_Cgi::writeFile(FILE*fp, const string &sFileName, const string &sBuffer, size_t &iTotalWrite)
  262. {
  263. if(!fp)
  264. {
  265. return false;
  266. }
  267. if(_mpUpload[sFileName]._bOverSize
  268. || iTotalWrite > _iUploadMaxSize
  269. || (iTotalWrite + sBuffer.length()) > _iUploadMaxSize)
  270. {
  271. _mpUpload[sFileName]._bOverSize = true;
  272. _bUploadFileOverSize = true;
  273. return false;
  274. }
  275. //内容写入文件
  276. int ret = fwrite(sBuffer.c_str(), 1, sBuffer.length(), fp);
  277. if(ret != (int)sBuffer.length())
  278. {
  279. fclose(fp);
  280. throw TC_Cgi_Exception("[TC_Cgi::parseFormData] upload file '" + _mpUpload[sFileName]._sServerFileName + "' error:" + string(strerror(errno)));
  281. }
  282. iTotalWrite += sBuffer.length();
  283. _mpUpload[sFileName]._iSize = iTotalWrite;
  284. return true;
  285. }
  286. void TC_Cgi::parseFormData(multimap<string, string> &mmpParams, const string &sBoundary)
  287. {
  288. string sBuffer;
  289. if(!getline(*_is, sBuffer))
  290. {
  291. throw TC_Cgi_Exception("[TC_Cgi::parseFormData] 'multipart/form-data' Format is error");
  292. }
  293. string sName;
  294. string sValue;
  295. string::size_type pos1 = string("Content-Disposition: form-data; name=\"").length();
  296. string::size_type pos2 = sBuffer.find('"', pos1);
  297. if(pos2 == string::npos)
  298. {
  299. throw TC_Cgi_Exception("[TC_Cgi::parseFormData] 'multipart/form-data' Format is error");
  300. }
  301. //控件名称
  302. sName = sBuffer.substr(pos1, pos2 - pos1);
  303. pos1 = sBuffer.find("filename=\"");
  304. if(pos1 != string::npos)
  305. {
  306. FILE *fp = NULL;
  307. string sTheFile;
  308. //上传文件的form data
  309. pos1 += 10; //filename=
  310. pos2 = sBuffer.find('"', pos1);
  311. if(pos2 == string::npos)
  312. {
  313. throw TC_Cgi_Exception("[TC_Cgi::parseFormData] 'multipart/form-data' Format is error");
  314. }
  315. //获取文件名
  316. sValue = sBuffer.substr(pos1, pos2 - pos1);
  317. if( sValue.length() > 0)
  318. {
  319. if(_iMaxUploadFiles < 0 || _mpUpload.size() < (size_t)_iMaxUploadFiles)
  320. {
  321. mmpParams.insert(multimap<string, string>::value_type(sName, sValue));
  322. //组成文件名称
  323. string sUploadFileName = _sUploadFilePrefix + "_" + TC_Common::tostr(_mpUpload.size());
  324. //记录上传文件路径
  325. _mpUpload[sName]._sFileName = sName;
  326. _mpUpload[sName]._sRealFileName = sValue;
  327. _mpUpload[sName]._sServerFileName = sUploadFileName;
  328. sTheFile = sName;
  329. //打开文件
  330. if ( (fp = fopen(sUploadFileName.c_str(),"w")) == NULL)
  331. {
  332. mmpParams.clear(); //clear , exception safe
  333. throw TC_Cgi_Exception("[TC_Cgi::parseFormData] Upload File '" + sValue + "' to '" + sUploadFileName +"' error! " + string(strerror(errno)));
  334. }
  335. }
  336. else
  337. {
  338. _bOverUploadFiles = true;
  339. }
  340. }
  341. ignoreLine();
  342. string sLastBuffer;
  343. size_t iTotalWrite = 0;
  344. //读取内容到文件
  345. while(getline(*_is, sBuffer))
  346. {
  347. string sTmp = TC_Common::trimright(sBuffer);
  348. if(sTmp.find(sBoundary) != string::npos)
  349. {
  350. if(sLastBuffer.length() < 2)
  351. {
  352. if(fp)
  353. {
  354. fclose(fp);
  355. }
  356. throw TC_Cgi_Exception("[TC_Cgi::parseFormData] 'multipart/form-data' Format is error");
  357. }
  358. sLastBuffer = sLastBuffer.substr(0, sLastBuffer.length() - 2);
  359. writeFile(fp, sTheFile, sLastBuffer, iTotalWrite);
  360. if(fp)
  361. {
  362. fclose(fp);
  363. fp = NULL;
  364. }
  365. //新的一个form data
  366. if(sTmp == sBoundary)
  367. {
  368. parseFormData(mmpParams, sBoundary);
  369. return;
  370. }
  371. else if(sTmp == sBoundary + "--")
  372. {
  373. return;
  374. }
  375. {
  376. throw TC_Cgi_Exception("[TC_Cgi::parseFormData] 'multipart/form-data' Format is error");
  377. }
  378. }
  379. if(!sLastBuffer.empty())
  380. {
  381. writeFile(fp, sTheFile, sLastBuffer, iTotalWrite);
  382. }
  383. sLastBuffer = sBuffer + "\n";
  384. }
  385. if(fp)
  386. {
  387. fclose(fp);
  388. fp = NULL;
  389. }
  390. }
  391. else
  392. {
  393. ignoreLine();
  394. string sLastBuffer;
  395. while(getline(*_is, sBuffer))
  396. {
  397. string sTmp = TC_Common::trimright(sBuffer);
  398. if(sTmp.find(sBoundary) != string::npos)
  399. {
  400. if(sLastBuffer.length() < 2)
  401. {
  402. throw TC_Cgi_Exception("[TC_Cgi::parseFormData] 'multipart/form-data' Format is error");
  403. }
  404. sLastBuffer = sLastBuffer.substr(0, sLastBuffer.length() - 2);
  405. mmpParams.insert(multimap<string, string>::value_type(sName, sLastBuffer));
  406. //新的一个form data
  407. if(sTmp == sBoundary)
  408. {
  409. parseFormData(mmpParams, sBoundary);
  410. return;
  411. }
  412. else if(sTmp == sBoundary + "--")
  413. {
  414. return;
  415. }
  416. throw TC_Cgi_Exception("[TC_Cgi::parseFormData] 'multipart/form-data' Format is error");
  417. }
  418. sLastBuffer += sBuffer + "\n";
  419. }
  420. }
  421. }
  422. void TC_Cgi::parseUpload(multimap<string, string> &mmpParams)
  423. {
  424. string::size_type iSize = atoi(getCgiEnv(TC_Cgi::ENM_CONTENT_LENGTH).c_str());
  425. if( iSize > _iMaxContentLength)
  426. {
  427. throw TC_Cgi_Exception("[TC_Cgi::parseUpload] : CONTENT_LENGTH is too large!");
  428. }
  429. //get content type
  430. string sContentType = getCgiEnv(TC_Cgi::ENM_CONTENT_TYPE);
  431. string::size_type bPos = sContentType.find("boundary=");
  432. if(bPos == string::npos)
  433. {
  434. return;
  435. }
  436. //get boundary, boundary separete params
  437. string sBoundary(sContentType.substr(bPos + 9));
  438. sBoundary = "--" + sBoundary;
  439. string sBuffer;
  440. if(!getline(*_is, sBuffer))
  441. {
  442. throw TC_Cgi_Exception("[TC_Cgi::parseFormData] 'multipart/form-data' Format is error");
  443. }
  444. sBuffer = TC_Common::trim(sBuffer);
  445. if(sBuffer == sBoundary)
  446. {
  447. parseFormData(mmpParams, sBoundary);
  448. }
  449. else
  450. {
  451. throw TC_Cgi_Exception("[TC_Cgi::parseUpload] 'multipart/form-data' Format is error");
  452. }
  453. }
  454. string &TC_Cgi::operator[](const string &sName)
  455. {
  456. multimap<string, string>::iterator it;
  457. if((it = _mmpParams.find(sName)) != _mmpParams.end())
  458. {
  459. return it->second;
  460. }
  461. return _mmpParams.insert(multimap<string, string>::value_type(sName, ""))->second;
  462. }
  463. string TC_Cgi::getValue(const string& sName) const
  464. {
  465. multimap<string, string>::const_iterator it;
  466. if((it = _mmpParams.find(sName)) != _mmpParams.end())
  467. {
  468. return it->second;
  469. }
  470. return "";
  471. }
  472. const vector<string>& TC_Cgi::getMultiValue(const string& sName, vector<string> &vtValue) const
  473. {
  474. vtValue.clear();
  475. multimap<string, string>::const_iterator itEnd = _mmpParams.end();
  476. for( multimap<string, string>::const_iterator it = _mmpParams.begin(); it != itEnd; ++it)
  477. {
  478. if(it->first == sName)
  479. {
  480. vtValue.push_back(it->second);
  481. }
  482. }
  483. return vtValue;
  484. }
  485. const multimap<string, string> &TC_Cgi::getParamMap() const
  486. {
  487. return _mmpParams;
  488. }
  489. map<string, string> TC_Cgi::getParamMapEx() const
  490. {
  491. map<string, string> mpCgiParam;
  492. multimap<string, string>::const_iterator it = _mmpParams.begin();
  493. multimap<string, string>::const_iterator itEnd = _mmpParams.end();
  494. while(it != itEnd)
  495. {
  496. mpCgiParam[it->first] = it->second;
  497. ++it;
  498. }
  499. return mpCgiParam;
  500. }
  501. const map<string, string> &TC_Cgi::getCookiesMap() const
  502. {
  503. return _mpCookies;
  504. }
  505. bool TC_Cgi::isUploadOverSize() const
  506. {
  507. return _bUploadFileOverSize;
  508. }
  509. bool TC_Cgi::isUploadOverSize(vector<TC_Cgi_Upload> &vtUploads) const
  510. {
  511. vtUploads.clear();
  512. map<string, TC_Cgi_Upload>::const_iterator itEnd = _mpUpload.end();
  513. for( map<string, TC_Cgi_Upload>::const_iterator it = _mpUpload.begin(); it != itEnd; ++it)
  514. {
  515. if(it->second._bOverSize)
  516. {
  517. vtUploads.push_back(it->second);
  518. }
  519. }
  520. return vtUploads.size() > 0;
  521. }
  522. const map<string, TC_Cgi_Upload> &TC_Cgi::getUploadFilesMap() const
  523. {
  524. return _mpUpload;
  525. }
  526. size_t TC_Cgi::getUploadFilesCount() const
  527. {
  528. return _mpUpload.size();
  529. }
  530. string TC_Cgi::getCookie(const string &sName) const
  531. {
  532. map<string, string>::const_iterator it = _mpCookies.find(sName);
  533. if(it != _mpCookies.end())
  534. {
  535. return it->second;
  536. }
  537. return "";
  538. }
  539. string TC_Cgi::setCookie(const string &sName, const string &sValue, const string &sExpires, const string &sPath, const string &sDomain, bool bSecure)
  540. {
  541. if(sName.length() == 0)
  542. {
  543. return "";
  544. }
  545. ostringstream os;
  546. os << "Set-Cookie: %s=%s;",sName.c_str(),sValue.c_str();
  547. if (sExpires.length() > 0)
  548. {
  549. os << "EXPIRES=%s;",sExpires.c_str();
  550. }
  551. if (sPath.length() > 0)
  552. {
  553. os << "PATH=%s;", sPath.c_str();
  554. }
  555. if (sDomain.length() > 0)
  556. {
  557. os << "DOMAIN=%s;", sDomain.c_str();
  558. }
  559. if (bSecure)
  560. {
  561. os << "SECURE";
  562. }
  563. os << endl;
  564. _mpCookies[sName] = sValue;
  565. return os.str();
  566. }
  567. bool TC_Cgi::isParamEmpty() const
  568. {
  569. return _mmpParams.empty();
  570. }
  571. bool TC_Cgi::isParamExist(const string& sName) const
  572. {
  573. return _mmpParams.find(sName) != _mmpParams.end();
  574. }
  575. string TC_Cgi::getCgiEnv(int iEnv)
  576. {
  577. switch(iEnv)
  578. {
  579. case ENM_SERVER_SOFTWARE:
  580. return getCgiEnv("SERVER_SOFTWARE");
  581. case ENM_SERVER_NAME:
  582. return getCgiEnv("SERVER_NAME");
  583. case ENM_GATEWAY_INTERFACE:
  584. return getCgiEnv("GATEWAY_INTERFACE");
  585. case ENM_SERVER_PROTOCOL:
  586. return getCgiEnv("SERVER_PROTOCOL");
  587. case ENM_SERVER_PORT:
  588. return getCgiEnv("SERVER_PORT");
  589. case ENM_REQUEST_METHOD:
  590. return getCgiEnv("REQUEST_METHOD");
  591. case ENM_PATH_INFO:
  592. return getCgiEnv("PATH_INFO");
  593. case ENM_PATH_TRANSLATED:
  594. return getCgiEnv("PATH_TRANSLATED");
  595. case ENM_SCRIPT_NAME:
  596. return getCgiEnv("SCRIPT_NAME");
  597. case ENM_HTTP_COOKIE:
  598. return getCgiEnv("HTTP_COOKIE");;
  599. case ENM_QUERY_STRING:
  600. return getCgiEnv("QUERY_STRING");
  601. case ENM_REMOTE_HOST:
  602. return getCgiEnv("REMOTE_HOST");
  603. case ENM_REMOTE_ADDR:
  604. return getCgiEnv("REMOTE_ADDR");
  605. case ENM_AUTH_TYPE:
  606. return getCgiEnv("AUTH_TYPE");
  607. case ENM_REMOTE_USER:
  608. return getCgiEnv("REMOTE_USER");
  609. case ENM_REMOTE_IDENT:
  610. return getCgiEnv("REMOTE_IDENT");
  611. case ENM_CONTENT_TYPE:
  612. return getCgiEnv("CONTENT_TYPE");
  613. case ENM_CONTENT_LENGTH:
  614. return getCgiEnv("CONTENT_LENGTH");
  615. case ENM_HTTP_USER_AGENT:
  616. return getCgiEnv("HTTP_USER_AGENT");
  617. default:
  618. return "";
  619. }
  620. }
  621. string TC_Cgi::getCgiEnv(const string &sEnv)
  622. {
  623. if(_env.find(sEnv) != _env.end())
  624. {
  625. return _env[sEnv];
  626. }
  627. return "";
  628. }
  629. void TC_Cgi::setCgiEnv(const string &sName, const string &sValue)
  630. {
  631. _env[sName] = sValue;
  632. }
  633. string TC_Cgi::htmlHeader(const string &sHeader)
  634. {
  635. return "Content-type: " + sHeader + "\n\n";
  636. }
  637. string TC_Cgi::decodeURL(const string &sUrl)
  638. {
  639. string sDecodeUrl;
  640. register string::size_type pos = 0;
  641. string::size_type len = sUrl.length();
  642. sDecodeUrl = "";
  643. while (pos < len)
  644. {
  645. if(sUrl[pos] == '+')
  646. {
  647. sDecodeUrl += ' ';
  648. ++pos;
  649. }
  650. else if(sUrl[pos] == '%')
  651. {
  652. sDecodeUrl += TC_Common::x2c(sUrl.substr(pos + 1));
  653. pos += 3;
  654. }
  655. else
  656. {
  657. sDecodeUrl += sUrl[pos];
  658. ++pos;
  659. }
  660. }
  661. return sDecodeUrl;
  662. }
  663. string TC_Cgi::encodeURL(const string &sUrl)
  664. {
  665. static char HEX_TABLE[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  666. string result;
  667. for (size_t i = 0; i < sUrl.length(); i++)
  668. {
  669. char c = sUrl[i];
  670. if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
  671. result.append(1, c);
  672. else if(c == ' ')
  673. {
  674. result.append(1, '+');
  675. }
  676. else
  677. {
  678. result.append(1, '%');
  679. result.append(1, HEX_TABLE[(c >> 4) & 0x0f]);
  680. result.append(1, HEX_TABLE[c & 0x0f]);
  681. }
  682. }
  683. return result;
  684. }
  685. string TC_Cgi::encodeHTML(const string &src, bool blankEncode)
  686. {
  687. if (src == "")
  688. return "";
  689. string result = src;
  690. result = TC_Common::replace(result, "&", "&amp;");
  691. result = TC_Common::replace(result, "<", "&lt;");
  692. result = TC_Common::replace(result, ">", "&gt;");
  693. result = TC_Common::replace(result, "\"", "&quot;");
  694. if (blankEncode)
  695. {
  696. result = TC_Common::replace(result, "\t", " ");
  697. result = TC_Common::replace(result, " ", "&nbsp; ");
  698. result = TC_Common::replace(result, "\r\n", "<br>");
  699. result = TC_Common::replace(result, "\n", "<br>");
  700. }
  701. return result;
  702. }
  703. string TC_Cgi::encodeXML(const string &src)
  704. {
  705. if (src == "")
  706. return "";
  707. string result = src;
  708. result = TC_Common::replace(result, "&", "&amp;");
  709. result = TC_Common::replace(result, "<", "&lt;");
  710. result = TC_Common::replace(result, ">", "&gt;");
  711. result = TC_Common::replace(result, "\"", "&quot;");
  712. result = TC_Common::replace(result, "'", "&apos;");
  713. return result;
  714. }
  715. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载