|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2019 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <string> |
| 22 | + |
| 23 | +#include "./httpserver.hpp" |
| 24 | +#include "httpserver/create_test_request.hpp" |
| 25 | +#include "httpserver/detail/modded_request.hpp" |
| 26 | +#include "httpserver/detail/webserver_impl.hpp" |
| 27 | + |
| 28 | +#include "./littletest.hpp" |
| 29 | + |
| 30 | +// post_iterator is the MHD post-processor callback defined as a static |
| 31 | +// member of webserver_impl (it lives in the dispatch helper header under |
| 32 | +// the class's public: section). The no-file branch funnels into |
| 33 | +// handle_post_form_arg, which is the function issue #375 hardens. We drive |
| 34 | +// the public callback so the test exercises the same entry point MHD does. |
| 35 | +// |
| 36 | +// modded_request::dhr is a unique_ptr<http_request> with the default |
| 37 | +// deleter, and http_request's move constructor is private (friended only |
| 38 | +// to create_test_request), so the request cannot be make_unique'd. Instead |
| 39 | +// we own an http_request on the stack -- built via the public test builder |
| 40 | +// using guaranteed copy elision -- and point dhr at it, detaching dhr |
| 41 | +// before destruction so the stack object is not double-freed. |
| 42 | +namespace { |
| 43 | +struct request_fixture { |
| 44 | + httpserver::http_request req = |
| 45 | + httpserver::create_test_request().build(); |
| 46 | + httpserver::detail::modded_request mr; |
| 47 | + |
| 48 | + request_fixture() { |
| 49 | + // ws is never read on the no-file form-arg path, so leave it null. |
| 50 | + mr.dhr.reset(&req); |
| 51 | + } |
| 52 | + |
| 53 | + ~request_fixture() { |
| 54 | + // Detach before mr (and its unique_ptr) is destroyed: req is |
| 55 | + // stack-owned and must not be deleted through dhr. |
| 56 | + mr.dhr.release(); |
| 57 | + } |
| 58 | + |
| 59 | + MHD_Result feed(const char* key, const char* data, uint64_t off, |
| 60 | + size_t size) { |
| 61 | + return httpserver::detail::webserver_impl::post_iterator( |
| 62 | + &mr, MHD_POSTDATA_KIND, key, /*filename=*/nullptr, |
| 63 | + /*content_type=*/nullptr, /*transfer_encoding=*/nullptr, |
| 64 | + data, off, size); |
| 65 | + } |
| 66 | +}; |
| 67 | +} // namespace |
| 68 | + |
| 69 | +LT_BEGIN_SUITE(post_iterator_null_key_suite) |
| 70 | + void set_up() { |
| 71 | + } |
| 72 | + |
| 73 | + void tear_down() { |
| 74 | + } |
| 75 | +LT_END_SUITE(post_iterator_null_key_suite) |
| 76 | + |
| 77 | +// Regression test for issue #375: MHD may invoke the post iterator with a |
| 78 | +// null key on a continuation chunk (off > 0) because the field name was |
| 79 | +// only supplied on the first call. The previous implementation passed the |
| 80 | +// raw key pointer into std::string, which throws std::logic_error on null |
| 81 | +// and aborts the process via std::terminate (the throw escapes a C |
| 82 | +// callback). The guard must instead accept and silently skip the chunk. |
| 83 | +LT_BEGIN_AUTO_TEST(post_iterator_null_key_suite, null_key_continuation_does_not_throw) |
| 84 | + request_fixture f; |
| 85 | + MHD_Result r = MHD_NO; |
| 86 | + LT_CHECK_NOTHROW(r = f.feed(/*key=*/nullptr, "value", /*off=*/5, 5)); |
| 87 | + // MHD_YES keeps the request alive; MHD_NO would abort it. |
| 88 | + LT_CHECK_EQ(r, MHD_YES); |
| 89 | + // Nothing was stored: there was no field name to key the value under. |
| 90 | + LT_CHECK_EQ(f.mr.dhr->get_args().size(), static_cast<size_t>(0)); |
| 91 | +LT_END_AUTO_TEST(null_key_continuation_does_not_throw) |
| 92 | + |
| 93 | +// Same guard on the initial-chunk path (off == 0). MHD should not normally |
| 94 | +// hand us a null key here, but the guard is unconditional, so pin it. |
| 95 | +LT_BEGIN_AUTO_TEST(post_iterator_null_key_suite, null_key_initial_does_not_throw) |
| 96 | + request_fixture f; |
| 97 | + MHD_Result r = MHD_NO; |
| 98 | + LT_CHECK_NOTHROW(r = f.feed(/*key=*/nullptr, "value", /*off=*/0, 5)); |
| 99 | + LT_CHECK_EQ(r, MHD_YES); |
| 100 | + LT_CHECK_EQ(f.mr.dhr->get_args().size(), static_cast<size_t>(0)); |
| 101 | +LT_END_AUTO_TEST(null_key_initial_does_not_throw) |
| 102 | + |
| 103 | +// Happy path: a non-null key on the initial chunk stores the value under |
| 104 | +// that field name, proving the guard did not regress normal form handling. |
| 105 | +LT_BEGIN_AUTO_TEST(post_iterator_null_key_suite, valid_key_stores_arg) |
| 106 | + request_fixture f; |
| 107 | + MHD_Result r = f.feed("field", "value", /*off=*/0, 5); |
| 108 | + LT_CHECK_EQ(r, MHD_YES); |
| 109 | + LT_CHECK_EQ(std::string(f.mr.dhr->get_arg_flat("field")), |
| 110 | + std::string("value")); |
| 111 | +LT_END_AUTO_TEST(valid_key_stores_arg) |
| 112 | + |
| 113 | +// Continuation chunk with a (repeated) non-null key appends to the value |
| 114 | +// MHD started on the first call - the legitimate large-field split that |
| 115 | +// commit 1b5fe8f (issue #337) introduced grow_last_arg to handle. |
| 116 | +LT_BEGIN_AUTO_TEST(post_iterator_null_key_suite, valid_key_continuation_appends) |
| 117 | + request_fixture f; |
| 118 | + LT_CHECK_EQ(f.feed("field", "hel", /*off=*/0, 3), MHD_YES); |
| 119 | + LT_CHECK_EQ(f.feed("field", "lo", /*off=*/3, 2), MHD_YES); |
| 120 | + LT_CHECK_EQ(std::string(f.mr.dhr->get_arg_flat("field")), |
| 121 | + std::string("hello")); |
| 122 | +LT_END_AUTO_TEST(valid_key_continuation_appends) |
| 123 | + |
| 124 | +LT_BEGIN_AUTO_TEST_ENV() |
| 125 | + AUTORUN_TESTS() |
| 126 | +LT_END_AUTO_TEST_ENV() |
0 commit comments