HttpContext.h 4.5 KB

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