HttpContext.h 4.8 KB

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