request.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*Software License
  2. Copyright(C) 2024[liuyingjie]
  3. License Terms
  4. Usage Rights
  5. Any individual or entity is free to use, copy, and distribute the binary form of this software without modification to the source code, without the need to disclose the source code.
  6. If the source code is modified, the modifications must be open - sourced under the same license.This means that the modifications must be disclosed and accompanied by a copy of this license.
  7. Future Versions Updates
  8. From this version onwards, all future releases will be governed by the terms of the latest version of the license.This license will automatically be nullified and replaced by the new version.
  9. Users must comply with the terms of the new license issued in future releases.
  10. Liability and Disclaimer
  11. This software is provided “as is”, without any express or implied warranties, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non - infringement.In no event shall the author or copyright holder be liable for any claims, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.
  12. Contact Information
  13. If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com.
  14. */
  15. #include "request.h"
  16. #include "net/http_reqpack.h"
  17. #include "session.h"
  18. module::request::request(network::http::request* request) :m_request(request)
  19. {
  20. }
  21. module::request::~request()
  22. {
  23. if (m_session != nullptr)
  24. delete m_session;
  25. }
  26. module::session* module::request::session(const std::string& token)
  27. {
  28. if (m_session == nullptr)
  29. m_session = new module::session(&m_request->session(token));
  30. return m_session;
  31. }
  32. sol::table module::request::body_param(sol::this_state s)
  33. {
  34. sol::state_view lua(s);
  35. sol::table result_table = lua.create_table();
  36. auto map = m_request->parser()->body_param();
  37. for_iter(iter,map)
  38. result_table[iter->first] = iter->second;
  39. return result_table;
  40. }
  41. sol::table module::request::url_param(sol::this_state s)
  42. {
  43. sol::state_view lua(s);
  44. sol::table result_table = lua.create_table();
  45. auto map = m_request->parser()->url_param();
  46. for_iter(iter, map)
  47. result_table[iter->first] = iter->second;
  48. return result_table;
  49. }
  50. std::string module::request::body()
  51. {
  52. return m_request->parser()->text();
  53. }
  54. void* module::request::website()
  55. {
  56. return m_request->website();
  57. }
  58. sol::table module::request::files(sol::this_state s)
  59. {
  60. auto names = m_request->parser()->form()->names();
  61. sol::state_view lua(s);
  62. sol::table result_table = lua.create_table();
  63. int count = 1;
  64. for (size_t i = 0; i < names.size(); i++)
  65. {
  66. sol::table file = lua.create_table();
  67. network::http::form_info fi;
  68. if (m_request->parser()->form()->get(names[i], fi))
  69. {
  70. file["type"] = fi.content_type;
  71. file["size"] = fi.data.size();
  72. file["filename"] = fi.filename;
  73. result_table[count] = file;
  74. count++;
  75. }
  76. }
  77. return result_table;
  78. }
  79. bool module::request::write_file(const std::string& name, const std::string& filepath)
  80. {
  81. return m_request->parser()->form()->write_file(name,filepath);
  82. }
  83. void module::request::regist(sol::state* lua)
  84. {
  85. // 绑定 Request 类到 Lua
  86. lua->new_usertype<module::request>("module_request",
  87. "header", &module::request::header,
  88. "method", &module::request::method,
  89. "filepath", &module::request::filepath,
  90. "host", &module::request::host,
  91. "remote_ipaddress", &module::request::remote_ipaddress,
  92. "remote_port", &module::request::remote_port,
  93. "param", &module::request::param,
  94. "session", &module::request::session,
  95. "body_param", &module::request::body_param,
  96. "url_param", &module::request::url_param,
  97. "body", &module::request::body,
  98. "files", &module::request::files,
  99. "write_file", &module::request::write_file
  100. );
  101. (*lua)["GET"] = (int)network::http::GET;
  102. (*lua)["POST"] = (int)network::http::POST;
  103. (*lua)["DEL"] = (int)network::http::DEL;
  104. (*lua)["HEAD"] = (int)network::http::HEAD;
  105. (*lua)["PUT"] = (int)network::http::PUT;
  106. }
  107. std::string module::request::header(const std::string& name)
  108. {
  109. std::string value;
  110. m_request->header(name, value);
  111. return value;
  112. }
  113. ylib::network::http::method module::request::method()
  114. {
  115. return m_request->method();
  116. }
  117. std::string module::request::filepath()
  118. {
  119. return m_request->filepath();
  120. }
  121. std::string module::request::host()
  122. {
  123. return m_request->host();
  124. }
  125. sol::object module::request::param(const std::string& name, bool throw_,sol::this_state s)
  126. {
  127. std::string value;
  128. bool result = request_param(name, value);
  129. if (result == false){
  130. if (throw_)
  131. throw ylib::exception("request parameter '" + name + "' was not found");
  132. else
  133. return sol::make_object(s, sol::nil);
  134. }
  135. return sol::make_object(s,value);
  136. }
  137. std::string module::request::remote_ipaddress()
  138. {
  139. return m_request->remote_ipaddress(false);
  140. }
  141. ushort module::request::remote_port()
  142. {
  143. return m_request->remote_port();
  144. }
  145. bool module::request::request_param(const std::string& name, std::string& value)
  146. {
  147. if (m_request->parser()->url_param(name, value) == false)
  148. return m_request->parser()->body_param(name, value);
  149. return true;
  150. }