1
0

Http1Session.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "Http1Session.h"
  2. static int on_url(http_parser* parser, const char *at, size_t length);
  3. static int on_status(http_parser* parser, const char *at, size_t length);
  4. static int on_header_field(http_parser* parser, const char *at, size_t length);
  5. static int on_header_value(http_parser* parser, const char *at, size_t length);
  6. static int on_body(http_parser* parser, const char *at, size_t length);
  7. static int on_message_begin(http_parser* parser);
  8. static int on_headers_complete(http_parser* parser);
  9. static int on_message_complete(http_parser* parser);
  10. Http1Session::Http1Session(http_session_type type) {
  11. if (cbs == NULL) {
  12. cbs = (http_parser_settings*)malloc(sizeof(http_parser_settings));
  13. http_parser_settings_init(cbs);
  14. cbs->on_message_begin = on_message_begin;
  15. cbs->on_url = on_url;
  16. cbs->on_status = on_status;
  17. cbs->on_header_field = on_header_field;
  18. cbs->on_header_value = on_header_value;
  19. cbs->on_headers_complete = on_headers_complete;
  20. cbs->on_body = on_body;
  21. cbs->on_message_complete = on_message_complete;
  22. }
  23. http_parser_init(&parser, HTTP_BOTH);
  24. parser.data = this;
  25. state = HP_START_REQ_OR_RES;
  26. submited = NULL;
  27. parsed = NULL;
  28. }
  29. Http1Session::~Http1Session() {
  30. }
  31. int Http1Session::GetSendData(char** data, size_t* len) {
  32. if (!submited) {
  33. *data = NULL;
  34. *len = 0;
  35. return 0;
  36. }
  37. sendbuf = submited->Dump(true, true);
  38. submited = NULL;
  39. *data = (char*)sendbuf.data();
  40. *len = sendbuf.size();
  41. return sendbuf.size();
  42. }
  43. int Http1Session::FeedRecvData(const char* data, size_t len) {
  44. return http_parser_execute(&parser, cbs, data, len);
  45. }
  46. bool Http1Session::WantRecv() {
  47. return state != HP_MESSAGE_COMPLETE;
  48. }
  49. int Http1Session::SubmitRequest(HttpRequest* req) {
  50. submited = req;
  51. return 0;
  52. }
  53. int Http1Session::SubmitResponse(HttpResponse* res) {
  54. submited = res;
  55. return 0;
  56. }
  57. int Http1Session::InitRequest(HttpRequest* req) {
  58. req->Reset();
  59. parsed = req;
  60. http_parser_init(&parser, HTTP_REQUEST);
  61. state = HP_START_REQ_OR_RES;
  62. url.clear();
  63. header_field.clear();
  64. header_value.clear();
  65. return 0;
  66. }
  67. int Http1Session::InitResponse(HttpResponse* res) {
  68. res->Reset();
  69. parsed = res;
  70. http_parser_init(&parser, HTTP_RESPONSE);
  71. url.clear();
  72. header_field.clear();
  73. header_value.clear();
  74. return 0;
  75. }
  76. int Http1Session::GetError() {
  77. return parser.http_errno;
  78. }
  79. const char* Http1Session::StrError(int error) {
  80. return http_errno_description((enum http_errno)error);
  81. }
  82. http_parser_settings* Http1Session::cbs = NULL;
  83. int on_url(http_parser* parser, const char *at, size_t length) {
  84. printd("on_url:%.*s\n", (int)length, at);
  85. Http1Session* hss = (Http1Session*)parser->data;
  86. hss->state = HP_URL;
  87. hss->url.insert(hss->url.size(), at, length);
  88. return 0;
  89. }
  90. int on_status(http_parser* parser, const char *at, size_t length) {
  91. printd("on_status:%.*s\n", (int)length, at);
  92. Http1Session* hss = (Http1Session*)parser->data;
  93. hss->state = HP_STATUS;
  94. return 0;
  95. }
  96. int on_header_field(http_parser* parser, const char *at, size_t length) {
  97. printd("on_header_field:%.*s\n", (int)length, at);
  98. Http1Session* hss = (Http1Session*)parser->data;
  99. hss->handle_header();
  100. hss->state = HP_HEADER_FIELD;
  101. hss->header_field.insert(hss->header_field.size(), at, length);
  102. return 0;
  103. }
  104. int on_header_value(http_parser* parser, const char *at, size_t length) {
  105. printd("on_header_value:%.*s""\n", (int)length, at);
  106. Http1Session* hss = (Http1Session*)parser->data;
  107. hss->state = HP_HEADER_VALUE;
  108. hss->header_value.insert(hss->header_value.size(), at, length);
  109. return 0;
  110. }
  111. int on_body(http_parser* parser, const char *at, size_t length) {
  112. //printd("on_body:%.*s""\n", (int)length, at);
  113. Http1Session* hss = (Http1Session*)parser->data;
  114. hss->state = HP_BODY;
  115. hss->parsed->body.insert(hss->parsed->body.size(), at, length);
  116. return 0;
  117. }
  118. int on_message_begin(http_parser* parser) {
  119. printd("on_message_begin\n");
  120. Http1Session* hss = (Http1Session*)parser->data;
  121. hss->state = HP_MESSAGE_BEGIN;
  122. return 0;
  123. }
  124. int on_headers_complete(http_parser* parser) {
  125. printd("on_headers_complete\n");
  126. Http1Session* hss = (Http1Session*)parser->data;
  127. hss->handle_header();
  128. auto iter = hss->parsed->headers.find("content-type");
  129. if (iter != hss->parsed->headers.end()) {
  130. hss->parsed->content_type = http_content_type_enum(iter->second.c_str());
  131. }
  132. hss->parsed->http_major = parser->http_major;
  133. hss->parsed->http_minor = parser->http_minor;
  134. if (hss->parsed->type == HTTP_REQUEST) {
  135. HttpRequest* req = (HttpRequest*)hss->parsed;
  136. req->method = (http_method)parser->method;
  137. req->url = hss->url;
  138. }
  139. else if (hss->parsed->type == HTTP_RESPONSE) {
  140. HttpResponse* res = (HttpResponse*)hss->parsed;
  141. res->status_code = (http_status)parser->status_code;
  142. }
  143. hss->state = HP_HEADERS_COMPLETE;
  144. return 0;
  145. }
  146. int on_message_complete(http_parser* parser) {
  147. printd("on_message_complete\n");
  148. Http1Session* hss = (Http1Session*)parser->data;
  149. hss->state = HP_MESSAGE_COMPLETE;
  150. return 0;
  151. }