HttpContext.h 4.6 KB

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