RequestData.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #include "RequestData.h"
  5. #include "regex"
  6. using namespace std;
  7. namespace xc {
  8. namespace utils {
  9. RequestData::RequestData(string url, string method, map<string, string> headers, string body) {
  10. this->url = url;
  11. this->method = method;
  12. this->headers = headers;
  13. this->body = body;
  14. if (this->url.length() == 0) {
  15. this->url = "/";
  16. } else {
  17. if (this->url[0] != '/') {
  18. this->url = '/' + this->url;
  19. }
  20. }
  21. }
  22. string RequestData::getURL() const {
  23. return this->url;
  24. }
  25. string RequestData::getMethod() const {
  26. return this->method;
  27. }
  28. string RequestData::getHeader(string name) {
  29. return this->headers[name];
  30. }
  31. string RequestData::getBody() const {
  32. return this->body;
  33. }
  34. string RequestData::getURLPath() const {
  35. return this->url.substr(0, this->url.find_first_of('?'));
  36. }
  37. static unsigned char dec_tab[256] = {
  38. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  39. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  40. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  41. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
  42. 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44. 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  45. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  46. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  47. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  54. };
  55. static char *urlDecode(const char *str){
  56. int len = (int) strlen(str);
  57. char *tmp = (char *)malloc(len + 1);
  58. int i = 0, pos = 0;
  59. for (i = 0; i < len; i++) {
  60. if (str[i] != '%')
  61. tmp[pos] = str[i];
  62. else if (i + 2 >= len) {
  63. tmp[pos++] = '%';
  64. if (++i >= len)
  65. break;
  66. tmp[pos] = str[i];
  67. break;
  68. } else if (isalnum(str[i + 1]) && isalnum(str[i + 2])) {
  69. tmp[pos] = (dec_tab[(unsigned char) str[i + 1]] << 4)
  70. + dec_tab[(unsigned char) str[i + 2]];
  71. i += 2;
  72. } else
  73. tmp[pos] = str[i];
  74. pos++;
  75. }
  76. tmp[pos] = '\0';
  77. return tmp;
  78. }
  79. string RequestData::getURLArgument(string key) const {
  80. smatch result;
  81. string res;
  82. if (regex_search(this->url.cbegin(), this->url.cend(), result, regex(key + "=(.*?)&"))) {
  83. res = result[1];
  84. } else if (regex_search(this->url.cbegin(), this->url.cend(), result, regex(key + "=(.*)"))) {
  85. res = result[1];
  86. } else {
  87. return string();
  88. }
  89. char *newStr = urlDecode(res.c_str());
  90. res = string(newStr);
  91. ::free(newStr);
  92. return res;
  93. }
  94. vector<string> split(const string& str, const string& delim) {
  95. vector<string> res;
  96. if ("" == str) return res;
  97. char * strs = new char[str.length() + 1];
  98. strcpy(strs, str.c_str());
  99. char * d = new char[delim.length() + 1];
  100. strcpy(d, delim.c_str());
  101. char *p = strtok(strs, d);
  102. while(p) {
  103. string s = p;
  104. res.push_back(s);
  105. p = strtok(NULL, d);
  106. }
  107. return res;
  108. }
  109. std::string& trim(std::string &s) {
  110. if (s.empty()) { return s; }
  111. s.erase(0,s.find_first_not_of(" "));
  112. s.erase(s.find_last_not_of(" ") + 1);
  113. return s;
  114. }
  115. string RequestData::getCookie(string key) {
  116. string cookies = this->getHeader("Cookie");
  117. if (cookies.empty()) { return ""; }
  118. vector<string> cookieList = split(cookies, ";");
  119. for (string cookie : cookieList) {
  120. auto items = split(cookies, "=");
  121. if (items.size() == 2) {
  122. string name = items[0];
  123. string value = items[1];
  124. trim(name);
  125. if (name == key) {
  126. return value;
  127. }
  128. }
  129. }
  130. return "";
  131. }
  132. } // xc
  133. } // utils