example_tc_cgi.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_cgi.h"
  17. #include "util/tc_common.h"
  18. using namespace tars;
  19. void testStdin()
  20. {
  21. string sFileName = "/data/tars/test/util/" + TC_Common::now2str() + ".tmp";
  22. TC_Cgi cgi;
  23. cout << TC_Cgi::htmlHeader("text/html");
  24. cgi.setUpload(sFileName, 2, 1024*1024*10);
  25. cgi.parseCgi();
  26. cout << cgi.setCookie(cgi.getValue("cookie1"), cgi.getValue("cookie2"), TC_Common::now2str());
  27. cout << "value:" << cgi.getValue("value") << endl;
  28. string s = "abc 中文";
  29. cout << TC_Cgi::encodeURL(s) << "<br/>";
  30. cout << TC_Cgi::decodeURL(s) << "<br/>";;
  31. cout << TC_Cgi::encodeHTML(cgi.getValue("D1"), true) ;
  32. cout << TC_Common::tostr(cgi.getParamMap()) << "<br/>";
  33. cout << "<br/>";
  34. cout << TC_Common::tostr(cgi.getEnvMap()) << "<br/>";
  35. }
  36. void parseNormal(multimap<string, string> &mmpParams, const string& sBuffer)
  37. {
  38. int iFlag = 0;
  39. string sName;
  40. string sValue;
  41. string sTmp;
  42. string::size_type len = sBuffer.length();
  43. string::size_type pos = 0;
  44. while (pos < len)
  45. {
  46. sTmp = "";
  47. if(iFlag == 0)
  48. {
  49. while ( (sBuffer[pos] != '=') && (pos < len) )
  50. {
  51. sTmp += (sBuffer[pos] == '+') ? ' ' : sBuffer[pos];
  52. ++pos;
  53. }
  54. }
  55. else
  56. {
  57. while ( (sBuffer[pos] != '&') && (pos < len) )
  58. {
  59. sTmp += (sBuffer[pos] == '+') ? ' ' : sBuffer[pos];
  60. ++pos;
  61. }
  62. }
  63. if (iFlag == 0) //param name
  64. {
  65. sName = TC_Cgi::decodeURL(sTmp);
  66. if ( (sBuffer[pos] != '=') || (pos == len - 1) )
  67. {
  68. sValue = "";
  69. mmpParams.insert(multimap<string, string>::value_type(sName, sValue));
  70. }
  71. else
  72. {
  73. iFlag = 1;
  74. }
  75. }
  76. else
  77. {
  78. sValue = TC_Cgi::decodeURL(sTmp);
  79. mmpParams.insert(multimap<string, string>::value_type(sName, sValue));
  80. iFlag = 0;
  81. }
  82. ++pos;
  83. }
  84. }
  85. void testCgiParam(const string &buffer)
  86. {
  87. multimap<string, string> m;
  88. parseNormal(m, buffer);
  89. cout << TC_Common::tostr(m) << endl;
  90. }
  91. int main(int argc, char *argv[])
  92. {
  93. try
  94. {
  95. // testStdin();
  96. // testCgiParam("a=b&c=d&e=f=");
  97. // testCgiParam("abc&=b&c=d&e=f=");
  98. // testCgiParam("a++&=b&c=d&e=f=");
  99. // testCgiParam("=b&c=d===&e=f=");
  100. // testCgiParam("=b&c=d++=&e=f=");
  101. testCgiParam("db=sina&bid=161423,230233,235253&cid=0,0,0&sid=224587&advid=1293&camid=25420&show=ignore&url=http://bbs.fangyou.com/viewthread.php?tid=1021196&pi=sina-langshou-wzl1");
  102. }
  103. catch(exception &ex)
  104. {
  105. cout << ex.what() << endl;
  106. }
  107. return 0;
  108. }