HttpContext.h 4.6 KB

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