HttpContext.h 4.3 KB

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