HttpContext.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #ifndef HV_HTTP_CONTEXT_H_
  2. #define HV_HTTP_CONTEXT_H_
  3. #include "hexport.h"
  4. #include "HttpMessage.h"
  5. #include "HttpResponseWriter.h"
  6. namespace hv {
  7. struct HttpService;
  8. struct HV_EXPORT HttpContext {
  9. HttpService* service;
  10. HttpRequestPtr request;
  11. HttpResponsePtr response;
  12. HttpResponseWriterPtr writer;
  13. void* userdata;
  14. HttpContext() {
  15. service = NULL;
  16. userdata = NULL;
  17. }
  18. // HttpRequest aliases
  19. // return request->xxx
  20. std::string ip() {
  21. return request->client_addr.ip;
  22. }
  23. int port() {
  24. return request->client_addr.port;
  25. }
  26. http_method method() {
  27. return request->method;
  28. }
  29. std::string url() {
  30. return request->url;
  31. }
  32. std::string path() {
  33. return request->Path();
  34. }
  35. std::string fullpath() {
  36. return request->FullPath();
  37. }
  38. std::string host() {
  39. return request->Host();
  40. }
  41. const http_headers& headers() {
  42. return request->headers;
  43. }
  44. std::string header(const char* key, const std::string& defvalue = hv::empty_string) {
  45. return request->GetHeader(key, defvalue);
  46. }
  47. const hv::QueryParams& params() {
  48. return request->query_params;
  49. }
  50. std::string param(const char* key, const std::string& defvalue = hv::empty_string) {
  51. return request->GetParam(key, defvalue);
  52. }
  53. const HttpCookie& cookie(const char* name) {
  54. return request->GetCookie(name);
  55. }
  56. int length() {
  57. return request->ContentLength();
  58. }
  59. http_content_type type() {
  60. return request->ContentType();
  61. }
  62. bool is(http_content_type content_type) {
  63. return request->ContentType() == content_type;
  64. }
  65. bool is(const char* content_type) {
  66. return request->ContentType() == http_content_type_enum(content_type);
  67. }
  68. std::string& body() {
  69. return request->body;
  70. }
  71. #ifndef WITHOUT_HTTP_CONTENT
  72. // Content-Type: application/json
  73. const hv::Json& json() {
  74. return request->GetJson();
  75. }
  76. // Content-Type: multipart/form-data
  77. const hv::MultiPart& form() {
  78. return request->GetForm();
  79. }
  80. std::string form(const char* name, const std::string& defvalue = hv::empty_string) {
  81. return request->GetFormData(name, defvalue);
  82. }
  83. // Content-Type: application/x-www-form-urlencoded
  84. const hv::KeyValue& urlencoded() {
  85. return request->GetUrlEncoded();
  86. }
  87. std::string urlencoded(const char* key, const std::string& defvalue = hv::empty_string) {
  88. return request->GetUrlEncoded(key, defvalue);
  89. }
  90. // T=[bool, int, int64_t, float, double]
  91. template<typename T>
  92. T get(const char* key, T defvalue = 0) {
  93. return request->Get(key, defvalue);
  94. }
  95. std::string get(const char* key, const std::string& defvalue = hv::empty_string) {
  96. return request->GetString(key, defvalue);
  97. }
  98. #endif
  99. // HttpResponse aliases
  100. // response->xxx = xxx
  101. void setStatus(http_status status) {
  102. response->status_code = status;
  103. }
  104. void setContentType(http_content_type type) {
  105. response->content_type = type;
  106. }
  107. void setContentType(const char* type) {
  108. response->content_type = http_content_type_enum(type);
  109. }
  110. void setHeader(const char* key, const std::string& value) {
  111. response->headers[key] = value;
  112. if (stricmp(key, "Content-Type") == 0) {
  113. setContentType(value.c_str());
  114. }
  115. }
  116. void setCookie(const HttpCookie& cookie) {
  117. response->AddCookie(cookie);
  118. }
  119. void setBody(const std::string& body) {
  120. response->body = body;
  121. }
  122. // response->sendXxx
  123. int send() {
  124. if (writer) {
  125. writer->End();
  126. }
  127. return response->status_code;
  128. }
  129. int send(const std::string& str, http_content_type type = APPLICATION_JSON) {
  130. response->content_type = type;
  131. response->body = str;
  132. return send();
  133. }
  134. int sendString(const std::string& str) {
  135. response->String(str);
  136. return send();
  137. }
  138. int sendData(void* data, int len, bool nocopy = true) {
  139. response->Data(data, len, nocopy);
  140. return send();
  141. }
  142. int sendFile(const char* filepath) {
  143. response->File(filepath);
  144. return send();
  145. }
  146. #ifndef WITHOUT_HTTP_CONTENT
  147. // T=[bool, int, int64_t, float, double, string]
  148. template<typename T>
  149. void set(const char* key, const T& value) {
  150. response->Set(key, value);
  151. }
  152. // @see HttpMessage::Json
  153. // @usage https://github.com/nlohmann/json
  154. template<typename T>
  155. int sendJson(const T& t) {
  156. response->Json(t);
  157. return send();
  158. }
  159. #endif
  160. int redirect(const std::string& location, http_status status = HTTP_STATUS_FOUND) {
  161. response->Redirect(location, status);
  162. return send();
  163. }
  164. int close() {
  165. return writer ? writer->close(true) : -1;
  166. }
  167. };
  168. } // end namespace hv
  169. typedef std::shared_ptr<hv::HttpContext> HttpContextPtr;
  170. #endif // HV_HTTP_CONTEXT_H_