HttpHeaderProcessorTest.cc 8.9 KB

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