HttpHeaderProcessorTest.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "HttpHeaderProcessor.h"
  2. #include <iostream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "HttpHeader.h"
  5. #include "DlRetryEx.h"
  6. #include "DlAbortEx.h"
  7. namespace aria2 {
  8. class HttpHeaderProcessorTest : public CppUnit::TestFixture {
  9. CPPUNIT_TEST_SUITE(HttpHeaderProcessorTest);
  10. CPPUNIT_TEST(testParse1);
  11. CPPUNIT_TEST(testParse2);
  12. CPPUNIT_TEST(testParse3);
  13. CPPUNIT_TEST(testGetLastBytesProcessed);
  14. CPPUNIT_TEST(testGetLastBytesProcessed_nullChar);
  15. CPPUNIT_TEST(testGetHttpResponseHeader);
  16. CPPUNIT_TEST(testGetHttpResponseHeader_statusOnly);
  17. CPPUNIT_TEST(testGetHttpResponseHeader_insufficientStatusLength);
  18. CPPUNIT_TEST(testGetHttpResponseHeader_nameStartsWs);
  19. CPPUNIT_TEST(testGetHttpResponseHeader_teAndCl);
  20. CPPUNIT_TEST(testBeyondLimit);
  21. CPPUNIT_TEST(testGetHeaderString);
  22. CPPUNIT_TEST(testGetHttpRequestHeader);
  23. CPPUNIT_TEST_SUITE_END();
  24. public:
  25. void testParse1();
  26. void testParse2();
  27. void testParse3();
  28. void testGetLastBytesProcessed();
  29. void testGetLastBytesProcessed_nullChar();
  30. void testGetHttpResponseHeader();
  31. void testGetHttpResponseHeader_statusOnly();
  32. void testGetHttpResponseHeader_insufficientStatusLength();
  33. void testGetHttpResponseHeader_nameStartsWs();
  34. void testGetHttpResponseHeader_teAndCl();
  35. void testBeyondLimit();
  36. void testGetHeaderString();
  37. void testGetHttpRequestHeader();
  38. };
  39. CPPUNIT_TEST_SUITE_REGISTRATION(HttpHeaderProcessorTest);
  40. void HttpHeaderProcessorTest::testParse1()
  41. {
  42. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  43. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  44. CPPUNIT_ASSERT(!proc.parse(hd1));
  45. CPPUNIT_ASSERT(proc.parse("\r\n"));
  46. }
  47. void HttpHeaderProcessorTest::testParse2()
  48. {
  49. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  50. std::string hd1 = "HTTP/1.1 200 OK\n";
  51. CPPUNIT_ASSERT(!proc.parse(hd1));
  52. CPPUNIT_ASSERT(proc.parse("\n"));
  53. }
  54. void HttpHeaderProcessorTest::testParse3()
  55. {
  56. HttpHeaderProcessor proc(HttpHeaderProcessor::SERVER_PARSER);
  57. std::string s = "GET / HTTP/1.1\r\n"
  58. "Host: aria2.sourceforge.net\r\n"
  59. "Connection: close \r\n" // trailing white space (BWS)
  60. "Accept-Encoding: text1\r\n" // Multi-line header
  61. " text2\r\n"
  62. " text3\r\n"
  63. "Authorization: foo\r\n"
  64. "Authorization: bar\r\n"
  65. "Content-Type:\r\n"
  66. "\r\n";
  67. CPPUNIT_ASSERT(proc.parse(s));
  68. auto h = proc.getResult();
  69. CPPUNIT_ASSERT_EQUAL(std::string("close"), h->find(HttpHeader::CONNECTION));
  70. CPPUNIT_ASSERT_EQUAL(std::string("text1 text2 text3"),
  71. h->find(HttpHeader::ACCEPT_ENCODING));
  72. CPPUNIT_ASSERT_EQUAL(std::string("foo"),
  73. h->findAll(HttpHeader::AUTHORIZATION)[0]);
  74. CPPUNIT_ASSERT_EQUAL(std::string("bar"),
  75. h->findAll(HttpHeader::AUTHORIZATION)[1]);
  76. CPPUNIT_ASSERT_EQUAL(std::string(""), h->find(HttpHeader::CONTENT_TYPE));
  77. CPPUNIT_ASSERT(h->defined(HttpHeader::CONTENT_TYPE));
  78. }
  79. void HttpHeaderProcessorTest::testGetLastBytesProcessed()
  80. {
  81. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  82. std::string hd1 = "HTTP/1.1 200 OK\r\n"
  83. "\r\nputbackme";
  84. CPPUNIT_ASSERT(proc.parse(hd1));
  85. CPPUNIT_ASSERT_EQUAL((size_t)19, proc.getLastBytesProcessed());
  86. proc.clear();
  87. std::string hd2 = "HTTP/1.1 200 OK\n"
  88. "\nputbackme";
  89. CPPUNIT_ASSERT(proc.parse(hd2));
  90. CPPUNIT_ASSERT_EQUAL((size_t)17, proc.getLastBytesProcessed());
  91. }
  92. void HttpHeaderProcessorTest::testGetLastBytesProcessed_nullChar()
  93. {
  94. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  95. const char x[] = "HTTP/1.1 200 OK\r\n"
  96. "foo: foo\0bar\r\n"
  97. "\r\nputbackme";
  98. std::string hd1(&x[0], &x[sizeof(x) - 1]);
  99. CPPUNIT_ASSERT(proc.parse(hd1));
  100. CPPUNIT_ASSERT_EQUAL((size_t)33, proc.getLastBytesProcessed());
  101. }
  102. void HttpHeaderProcessorTest::testGetHttpResponseHeader()
  103. {
  104. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  105. std::string hd = "HTTP/1.1 404 Not Found\r\n"
  106. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  107. "Server: Apache/2.2.3 (Debian)\r\n"
  108. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  109. "ETag: \"594065-23e3-50825cc0\"\r\n"
  110. "Accept-Ranges: bytes\r\n"
  111. "Content-Length: 9187\r\n"
  112. "Connection: close\r\n"
  113. "Content-Type: text/html; charset=UTF-8\r\n"
  114. "\r\n"
  115. "Content-Encoding: body";
  116. CPPUNIT_ASSERT(proc.parse(hd));
  117. auto header = proc.getResult();
  118. CPPUNIT_ASSERT_EQUAL(404, header->getStatusCode());
  119. CPPUNIT_ASSERT_EQUAL(std::string("Not Found"), header->getReasonPhrase());
  120. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), header->getVersion());
  121. CPPUNIT_ASSERT_EQUAL(std::string("9187"),
  122. header->find(HttpHeader::CONTENT_LENGTH));
  123. CPPUNIT_ASSERT_EQUAL(std::string("text/html; charset=UTF-8"),
  124. header->find(HttpHeader::CONTENT_TYPE));
  125. CPPUNIT_ASSERT(!header->defined(HttpHeader::CONTENT_ENCODING));
  126. }
  127. void HttpHeaderProcessorTest::testGetHttpResponseHeader_statusOnly()
  128. {
  129. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  130. std::string hd = "HTTP/1.1 200\r\n\r\n";
  131. CPPUNIT_ASSERT(proc.parse(hd));
  132. auto header = proc.getResult();
  133. CPPUNIT_ASSERT_EQUAL(200, header->getStatusCode());
  134. }
  135. void HttpHeaderProcessorTest::
  136. testGetHttpResponseHeader_insufficientStatusLength()
  137. {
  138. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  139. std::string hd = "HTTP/1.1 20\r\n\r\n";
  140. try {
  141. proc.parse(hd);
  142. CPPUNIT_FAIL("Exception must be thrown.");
  143. }
  144. catch (DlAbortEx& ex) {
  145. // Success
  146. }
  147. }
  148. void HttpHeaderProcessorTest::testGetHttpResponseHeader_nameStartsWs()
  149. {
  150. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  151. std::string hd = "HTTP/1.1 200\r\n"
  152. " foo:bar\r\n"
  153. "\r\n";
  154. try {
  155. proc.parse(hd);
  156. CPPUNIT_FAIL("Exception must be thrown.");
  157. }
  158. catch (DlAbortEx& ex) {
  159. // Success
  160. }
  161. proc.clear();
  162. hd = "HTTP/1.1 200\r\n"
  163. ":foo:bar\r\n"
  164. "\r\n";
  165. try {
  166. proc.parse(hd);
  167. CPPUNIT_FAIL("Exception must be thrown.");
  168. }
  169. catch (DlAbortEx& ex) {
  170. // Success
  171. }
  172. proc.clear();
  173. hd = "HTTP/1.1 200\r\n"
  174. ":foo\r\n"
  175. "\r\n";
  176. try {
  177. proc.parse(hd);
  178. CPPUNIT_FAIL("Exception must be thrown.");
  179. }
  180. catch (DlAbortEx& ex) {
  181. // Success
  182. }
  183. }
  184. void HttpHeaderProcessorTest::testGetHttpResponseHeader_teAndCl()
  185. {
  186. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  187. std::string hd = "HTTP/1.1 200\r\n"
  188. "Content-Length: 200\r\n"
  189. "Transfer-Encoding: chunked\r\n"
  190. "Content-Range: 1-200/300\r\n"
  191. "\r\n";
  192. CPPUNIT_ASSERT(proc.parse(hd));
  193. auto httpHeader = proc.getResult();
  194. CPPUNIT_ASSERT_EQUAL(std::string("chunked"),
  195. httpHeader->find(HttpHeader::TRANSFER_ENCODING));
  196. CPPUNIT_ASSERT(!httpHeader->defined(HttpHeader::CONTENT_LENGTH));
  197. CPPUNIT_ASSERT(!httpHeader->defined(HttpHeader::CONTENT_RANGE));
  198. }
  199. void HttpHeaderProcessorTest::testBeyondLimit()
  200. {
  201. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  202. std::string hd1 = "HTTP/1.1 200 OK\r\n";
  203. std::string hd2 = std::string(1025, 'A');
  204. proc.parse(hd1);
  205. try {
  206. proc.parse(hd2);
  207. CPPUNIT_FAIL("Exception must be thrown.");
  208. }
  209. catch (DlAbortEx& ex) {
  210. // Success
  211. }
  212. }
  213. void HttpHeaderProcessorTest::testGetHeaderString()
  214. {
  215. HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  216. std::string hd = "HTTP/1.1 200 OK\r\n"
  217. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  218. "Server: Apache/2.2.3 (Debian)\r\n"
  219. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  220. "ETag: \"594065-23e3-50825cc0\"\r\n"
  221. "Accept-Ranges: bytes\r\n"
  222. "Content-Length: 9187\r\n"
  223. "Connection: close\r\n"
  224. "Content-Type: text/html; charset=UTF-8\r\n"
  225. "\r\nputbackme";
  226. CPPUNIT_ASSERT(proc.parse(hd));
  227. CPPUNIT_ASSERT_EQUAL(
  228. std::string("HTTP/1.1 200 OK\r\n"
  229. "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
  230. "Server: Apache/2.2.3 (Debian)\r\n"
  231. "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
  232. "ETag: \"594065-23e3-50825cc0\"\r\n"
  233. "Accept-Ranges: bytes\r\n"
  234. "Content-Length: 9187\r\n"
  235. "Connection: close\r\n"
  236. "Content-Type: text/html; charset=UTF-8\r\n"
  237. "\r\n"),
  238. proc.getHeaderString());
  239. }
  240. void HttpHeaderProcessorTest::testGetHttpRequestHeader()
  241. {
  242. HttpHeaderProcessor proc(HttpHeaderProcessor::SERVER_PARSER);
  243. std::string request = "GET /index.html HTTP/1.1\r\n"
  244. "Host: host\r\n"
  245. "Connection: close\r\n"
  246. "\r\n"
  247. "Content-Encoding: body";
  248. CPPUNIT_ASSERT(proc.parse(request));
  249. auto httpHeader = proc.getResult();
  250. CPPUNIT_ASSERT_EQUAL(std::string("GET"), httpHeader->getMethod());
  251. CPPUNIT_ASSERT_EQUAL(std::string("/index.html"),
  252. httpHeader->getRequestPath());
  253. CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), httpHeader->getVersion());
  254. CPPUNIT_ASSERT_EQUAL(std::string("close"),
  255. httpHeader->find(HttpHeader::CONNECTION));
  256. CPPUNIT_ASSERT(!httpHeader->defined(HttpHeader::CONTENT_ENCODING));
  257. }
  258. } // namespace aria2