Http1Session.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. return 0;
  62. }
  63. int Http1Session::InitResponse(HttpResponse* res) {
  64. res->Reset();
  65. parsed = res;
  66. http_parser_init(&parser, HTTP_RESPONSE);
  67. return 0;
  68. }
  69. int Http1Session::GetError() {
  70. return parser.http_errno;
  71. }
  72. const char* Http1Session::StrError(int error) {
  73. return http_errno_description((enum http_errno)error);
  74. }
  75. http_parser_settings* Http1Session::cbs = NULL;
  76. int on_url(http_parser* parser, const char *at, size_t length) {
  77. printd("on_url:%.*s\n", (int)length, at);
  78. Http1Session* hss = (Http1Session*)parser->data;
  79. hss->state = HP_URL;
  80. hss->url.insert(hss->url.size(), at, length);
  81. return 0;
  82. }
  83. int on_status(http_parser* parser, const char *at, size_t length) {
  84. printd("on_status:%.*s\n", (int)length, at);
  85. Http1Session* hss = (Http1Session*)parser->data;
  86. hss->state = HP_STATUS;
  87. return 0;
  88. }
  89. int on_header_field(http_parser* parser, const char *at, size_t length) {
  90. printd("on_header_field:%.*s\n", (int)length, at);
  91. Http1Session* hss = (Http1Session*)parser->data;
  92. hss->handle_header();
  93. hss->state = HP_HEADER_FIELD;
  94. hss->header_field.insert(hss->header_field.size(), at, length);
  95. return 0;
  96. }
  97. int on_header_value(http_parser* parser, const char *at, size_t length) {
  98. printd("on_header_value:%.*s""\n", (int)length, at);
  99. Http1Session* hss = (Http1Session*)parser->data;
  100. hss->state = HP_HEADER_VALUE;
  101. hss->header_value.insert(hss->header_value.size(), at, length);
  102. return 0;
  103. }
  104. int on_body(http_parser* parser, const char *at, size_t length) {
  105. //printd("on_body:%.*s""\n", (int)length, at);
  106. Http1Session* hss = (Http1Session*)parser->data;
  107. hss->state = HP_BODY;
  108. hss->parsed->body.insert(hss->parsed->body.size(), at, length);
  109. return 0;
  110. }
  111. int on_message_begin(http_parser* parser) {
  112. printd("on_message_begin\n");
  113. Http1Session* hss = (Http1Session*)parser->data;
  114. hss->state = HP_MESSAGE_BEGIN;
  115. return 0;
  116. }
  117. int on_headers_complete(http_parser* parser) {
  118. printd("on_headers_complete\n");
  119. Http1Session* hss = (Http1Session*)parser->data;
  120. hss->handle_header();
  121. auto iter = hss->parsed->headers.find("content-type");
  122. if (iter != hss->parsed->headers.end()) {
  123. hss->parsed->content_type = http_content_type_enum(iter->second.c_str());
  124. }
  125. hss->parsed->http_major = parser->http_major;
  126. hss->parsed->http_minor = parser->http_minor;
  127. if (hss->parsed->type == HTTP_REQUEST) {
  128. HttpRequest* req = (HttpRequest*)hss->parsed;
  129. req->method = (http_method)parser->method;
  130. req->url = hss->url;
  131. }
  132. else if (hss->parsed->type == HTTP_RESPONSE) {
  133. HttpResponse* res = (HttpResponse*)hss->parsed;
  134. res->status_code = (http_status)parser->status_code;
  135. }
  136. hss->state = HP_HEADERS_COMPLETE;
  137. return 0;
  138. }
  139. int on_message_complete(http_parser* parser) {
  140. printd("on_message_complete\n");
  141. Http1Session* hss = (Http1Session*)parser->data;
  142. hss->state = HP_MESSAGE_COMPLETE;
  143. return 0;
  144. }