Http1Session.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. http_parser_settings* Http1Session::cbs = NULL;
  11. Http1Session::Http1Session(http_session_type type) {
  12. if (cbs == NULL) {
  13. cbs = (http_parser_settings*)malloc(sizeof(http_parser_settings));
  14. http_parser_settings_init(cbs);
  15. cbs->on_message_begin = on_message_begin;
  16. cbs->on_url = on_url;
  17. cbs->on_status = on_status;
  18. cbs->on_header_field = on_header_field;
  19. cbs->on_header_value = on_header_value;
  20. cbs->on_headers_complete = on_headers_complete;
  21. cbs->on_body = on_body;
  22. cbs->on_message_complete = on_message_complete;
  23. }
  24. http_parser_init(&parser, HTTP_BOTH);
  25. parser.data = this;
  26. state = HP_START_REQ_OR_RES;
  27. submited = NULL;
  28. parsed = NULL;
  29. }
  30. Http1Session::~Http1Session() {
  31. }
  32. int on_url(http_parser* parser, const char *at, size_t length) {
  33. printd("on_url:%.*s\n", (int)length, at);
  34. Http1Session* hss = (Http1Session*)parser->data;
  35. hss->state = HP_URL;
  36. hss->url.insert(hss->url.size(), at, length);
  37. return 0;
  38. }
  39. int on_status(http_parser* parser, const char *at, size_t length) {
  40. printd("on_status:%.*s\n", (int)length, at);
  41. Http1Session* hss = (Http1Session*)parser->data;
  42. hss->state = HP_STATUS;
  43. return 0;
  44. }
  45. int on_header_field(http_parser* parser, const char *at, size_t length) {
  46. printd("on_header_field:%.*s\n", (int)length, at);
  47. Http1Session* hss = (Http1Session*)parser->data;
  48. hss->handle_header();
  49. hss->state = HP_HEADER_FIELD;
  50. hss->header_field.insert(hss->header_field.size(), at, length);
  51. return 0;
  52. }
  53. int on_header_value(http_parser* parser, const char *at, size_t length) {
  54. printd("on_header_value:%.*s""\n", (int)length, at);
  55. Http1Session* hss = (Http1Session*)parser->data;
  56. hss->state = HP_HEADER_VALUE;
  57. hss->header_value.insert(hss->header_value.size(), at, length);
  58. return 0;
  59. }
  60. int on_body(http_parser* parser, const char *at, size_t length) {
  61. //printd("on_body:%.*s""\n", (int)length, at);
  62. Http1Session* hss = (Http1Session*)parser->data;
  63. hss->state = HP_BODY;
  64. hss->parsed->body.insert(hss->parsed->body.size(), at, length);
  65. return 0;
  66. }
  67. int on_message_begin(http_parser* parser) {
  68. printd("on_message_begin\n");
  69. Http1Session* hss = (Http1Session*)parser->data;
  70. hss->state = HP_MESSAGE_BEGIN;
  71. return 0;
  72. }
  73. int on_headers_complete(http_parser* parser) {
  74. printd("on_headers_complete\n");
  75. Http1Session* hss = (Http1Session*)parser->data;
  76. hss->handle_header();
  77. auto iter = hss->parsed->headers.find("content-type");
  78. if (iter != hss->parsed->headers.end()) {
  79. hss->parsed->content_type = http_content_type_enum(iter->second.c_str());
  80. }
  81. hss->parsed->http_major = parser->http_major;
  82. hss->parsed->http_minor = parser->http_minor;
  83. if (hss->parsed->type == HTTP_REQUEST) {
  84. HttpRequest* req = (HttpRequest*)hss->parsed;
  85. req->method = (http_method)parser->method;
  86. req->url = hss->url;
  87. }
  88. else if (hss->parsed->type == HTTP_RESPONSE) {
  89. HttpResponse* res = (HttpResponse*)hss->parsed;
  90. res->status_code = (http_status)parser->status_code;
  91. }
  92. hss->state = HP_HEADERS_COMPLETE;
  93. return 0;
  94. }
  95. int on_message_complete(http_parser* parser) {
  96. printd("on_message_complete\n");
  97. Http1Session* hss = (Http1Session*)parser->data;
  98. hss->state = HP_MESSAGE_COMPLETE;
  99. return 0;
  100. }