Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2020-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <httpserver.h>6#include <netaddress.h>7#include <test/fuzz/FuzzedDataProvider.h>8#include <test/fuzz/fuzz.h>9#include <test/fuzz/util.h>10#include <util/signalinterrupt.h>11#include <util/strencodings.h>13#include <cassert>14#include <cstdint>15#include <string>16#include <vector>19std::string_view RequestMethodString(HTTPRequestMethod m);21FUZZ_TARGET(http_request)22{23using util::LineReader;24using namespace bitcoin_http;26FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};27const std::string http_buffer{fuzzed_data_provider.ConsumeRandomLengthString(4096)};29HTTPRequest http_request;30LineReader reader(http_buffer, MAX_HEADERS_SIZE);31try {32if (!http_request.LoadControlData(reader)) return;33if (!http_request.LoadHeaders(reader)) return;34if (!http_request.LoadBody(reader)) return;35} catch (const std::runtime_error&) {36return;37}39const HTTPRequestMethod request_method = http_request.GetRequestMethod();40(void)RequestMethodString(request_method);41(void)http_request.GetURI();42(void)http_request.GetHeader("Host");43std::string header = fuzzed_data_provider.ConsumeRandomLengthString(16);44(void)http_request.GetHeader(header);45(void)http_request.WriteHeader(std::string(header), fuzzed_data_provider.ConsumeRandomLengthString(16));46(void)http_request.GetHeader(header);47// Reaching here means LoadControlData/LoadHeaders/LoadBody all succeeded, so the48// parsed body must be consistent with the message framing. Before libevent was49// replaced with HTTPRequest (#35182), ReadBody() always returned an50// empty string here; LoadBody now populates the body per RFC 9112 framing, so mirror51// its branch logic to assert the body matches the framing that produced it.52const std::string body = http_request.ReadBody();53const auto transfer_encoding = http_request.GetHeader("Transfer-Encoding");54const auto content_length = http_request.GetHeader("Content-Length");55if (transfer_encoding && ToLower(*transfer_encoding) == "chunked") {56// A chunked body is the concatenation of the decoded chunks, bounded by MAX_BODY_SIZE.57assert(body.size() <= MAX_BODY_SIZE);58} else if (content_length) {59// A Content-Length body is exactly that many bytes.60const auto parsed_length{ToIntegral<uint64_t>(*content_length)};61assert(parsed_length);62assert(body.size() == *parsed_length);63} else {64// Absent both framing headers there is no body.65assert(body.empty());66}67}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.