brpc_http_parser_unittest.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include <gtest/gtest.h>
  18. #include <iostream>
  19. #include "butil/time.h"
  20. #include "butil/logging.h"
  21. #include "brpc/details/http_parser.h"
  22. #include "brpc/builtin/common.h" // AppendFileName
  23. using brpc::http_parser;
  24. using brpc::http_parser_init;
  25. using brpc::http_parser_settings;
  26. class HttpParserTest : public testing::Test {
  27. protected:
  28. void SetUp() {}
  29. void TearDown() {}
  30. };
  31. TEST_F(HttpParserTest, init_perf) {
  32. const size_t loops = 10000000;
  33. butil::Timer timer;
  34. timer.start();
  35. for (size_t i = 0; i < loops; ++i) {
  36. http_parser parser;
  37. http_parser_init(&parser, brpc::HTTP_REQUEST);
  38. }
  39. timer.stop();
  40. std::cout << "It takes " << timer.n_elapsed() / loops
  41. << "ns to init a http_parser"
  42. << std::endl;
  43. }
  44. int on_message_begin(http_parser *) {
  45. LOG(INFO) << "Start parsing message";
  46. return 0;
  47. }
  48. int on_url(http_parser *, const char *at, const size_t length) {
  49. LOG(INFO) << "Get url " << std::string(at, length);
  50. return 0;
  51. }
  52. int on_headers_complete(http_parser *) {
  53. LOG(INFO) << "Header complete";
  54. return 0;
  55. }
  56. int on_message_complete(http_parser *) {
  57. LOG(INFO) << "Message complete";
  58. return 0;
  59. }
  60. int on_header_field(http_parser *, const char *at, const size_t length) {
  61. LOG(INFO) << "Get header field " << std::string(at, length);
  62. return 0;
  63. }
  64. int on_header_value(http_parser *, const char *at, const size_t length) {
  65. LOG(INFO) << "Get header value " << std::string(at, length);
  66. return 0;
  67. }
  68. int on_body(http_parser *, const char *at, const size_t length) {
  69. LOG(INFO) << "Get body " << std::string(at, length);
  70. return 0;
  71. }
  72. TEST_F(HttpParserTest, http_example) {
  73. const char *http_request =
  74. "GET /path/file.html?sdfsdf=sdfs HTTP/1.0\r\n"
  75. "From: someuser@jmarshall.com\r\n"
  76. "User-Agent: HTTPTool/1.0\r\n"
  77. "Content-Type: json\r\n"
  78. "Content-Length: 19\r\n"
  79. "Host: sdlfjslfd\r\n"
  80. "Accept: */*\r\n"
  81. "\r\n"
  82. "Message Body sdfsdf\r\n"
  83. ;
  84. std::cout << http_request << std::endl;
  85. http_parser parser;
  86. http_parser_init(&parser, brpc::HTTP_REQUEST);
  87. http_parser_settings settings;
  88. memset(&settings, 0, sizeof(settings));
  89. settings.on_message_begin = on_message_begin;
  90. settings.on_url = on_url;
  91. settings.on_headers_complete = on_headers_complete;
  92. settings.on_message_complete = on_message_complete;
  93. settings.on_header_field = on_header_field;
  94. settings.on_header_value = on_header_value;
  95. settings.on_body = on_body;
  96. LOG(INFO) << http_parser_execute(&parser, &settings, http_request, strlen(http_request));
  97. }
  98. TEST_F(HttpParserTest, append_filename) {
  99. std::string dir;
  100. dir = "/home/someone/.bsvn/..";
  101. brpc::AppendFileName(&dir, "..");
  102. ASSERT_EQ("/home", dir);
  103. dir = "/home/someone/.bsvn/../";
  104. brpc::AppendFileName(&dir, "..");
  105. ASSERT_EQ("/home", dir);
  106. dir = "/home/someone/./..";
  107. brpc::AppendFileName(&dir, "..");
  108. ASSERT_EQ("/", dir);
  109. dir = "/home/someone/./../";
  110. brpc::AppendFileName(&dir, "..");
  111. ASSERT_EQ("/", dir);
  112. dir = "/foo/bar";
  113. brpc::AppendFileName(&dir, "..");
  114. ASSERT_EQ("/foo", dir);
  115. dir = "/foo/bar/";
  116. brpc::AppendFileName(&dir, "..");
  117. ASSERT_EQ("/foo", dir);
  118. dir = "/foo";
  119. brpc::AppendFileName(&dir, ".");
  120. ASSERT_EQ("/foo", dir);
  121. dir = "/foo/";
  122. brpc::AppendFileName(&dir, ".");
  123. ASSERT_EQ("/foo/", dir);
  124. dir = "foo";
  125. brpc::AppendFileName(&dir, "..");
  126. ASSERT_EQ("", dir);
  127. dir = "foo/";
  128. brpc::AppendFileName(&dir, "..");
  129. ASSERT_EQ("", dir);
  130. dir = "foo/..";
  131. brpc::AppendFileName(&dir, "..");
  132. ASSERT_EQ("..", dir);
  133. dir = "foo/../";
  134. brpc::AppendFileName(&dir, "..");
  135. ASSERT_EQ("..", dir);
  136. dir = "/foo";
  137. brpc::AppendFileName(&dir, "..");
  138. ASSERT_EQ("/", dir);
  139. dir = "/foo/";
  140. brpc::AppendFileName(&dir, "..");
  141. ASSERT_EQ("/", dir);
  142. }