RequestData.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #include "RequestData.h"
  5. #include "regex"
  6. #include "strop.h"
  7. using namespace std;
  8. namespace xc {
  9. namespace utils {
  10. RequestData::RequestData(string url, string method, map<string, string> headers, string body) {
  11. this->url = url;
  12. this->method = method;
  13. this->headers = headers;
  14. this->body = body;
  15. if (this->url.length() == 0) {
  16. this->url = "/";
  17. } else {
  18. if (this->url[0] != '/') {
  19. this->url = '/' + this->url;
  20. }
  21. }
  22. }
  23. string RequestData::getURL() const {
  24. return this->url;
  25. }
  26. string RequestData::getMethod() const {
  27. return this->method;
  28. }
  29. string RequestData::getHeader(string name) {
  30. return this->headers[name];
  31. }
  32. string RequestData::getBody() const {
  33. return this->body;
  34. }
  35. string RequestData::getURLPath() const {
  36. return this->url.substr(0, this->url.find_first_of('?'));
  37. }
  38. static unsigned char dec_tab[256] = {
  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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
  43. 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  45. 0, 10, 11, 12, 13, 14, 15, 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. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. };
  56. static char *urlDecode(const char *str){
  57. int len = (int) strlen(str);
  58. char *tmp = (char *)malloc(len + 1);
  59. int i = 0, pos = 0;
  60. for (i = 0; i < len; i++) {
  61. if (str[i] != '%')
  62. tmp[pos] = str[i];
  63. else if (i + 2 >= len) {
  64. tmp[pos++] = '%';
  65. if (++i >= len)
  66. break;
  67. tmp[pos] = str[i];
  68. break;
  69. } else if (isalnum(str[i + 1]) && isalnum(str[i + 2])) {
  70. tmp[pos] = (dec_tab[(unsigned char) str[i + 1]] << 4)
  71. + dec_tab[(unsigned char) str[i + 2]];
  72. i += 2;
  73. } else
  74. tmp[pos] = str[i];
  75. pos++;
  76. }
  77. tmp[pos] = '\0';
  78. return tmp;
  79. }
  80. string RequestData::getURLArgument(string key) const {
  81. smatch result;
  82. string res;
  83. if (regex_search(this->url.cbegin(), this->url.cend(), result, regex(key + "=(.*?)&"))) {
  84. res = result[1];
  85. } else if (regex_search(this->url.cbegin(), this->url.cend(), result, regex(key + "=(.*)"))) {
  86. res = result[1];
  87. } else {
  88. return string();
  89. }
  90. char *newStr = urlDecode(res.c_str());
  91. res = string(newStr);
  92. ::free(newStr);
  93. return res;
  94. }
  95. string RequestData::getCookie(string key) {
  96. string cookies = this->getHeader("Cookie");
  97. if (cookies.empty()) { return ""; }
  98. vector<string> cookieList = split(cookies, ";");
  99. for (string cookie : cookieList) {
  100. auto items = split(cookie, "=");
  101. if (items.size() == 2) {
  102. string name = items[0];
  103. string value = items[1];
  104. trim(name);
  105. if (name == key) {
  106. return value;
  107. }
  108. }
  109. }
  110. return "";
  111. }
  112. } // xc
  113. } // utils