Skip to content

Commit 23dec67

Browse files
author
Jesse Nicholson
committed
Fixes
Fixes TechnikEmpire#11
1 parent b0d8276 commit 23dec67

1 file changed

Lines changed: 36 additions & 79 deletions

File tree

src/te/httpengine/mitm/http/BaseHttpTransaction.cpp

Lines changed: 36 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,17 @@ namespace te
191191
{
192192
std::string hdrString{ (std::istreambuf_iterator<char>(&m_headerBuffer)), std::istreambuf_iterator<char>() };
193193

194+
// Pull server requests for HTTP2 upgrade
195+
if (hdrString.size() > 0)
196+
{
197+
auto http2 = boost::string_ref(u8"Upgrade: h2\r\n");
198+
auto http2c = boost::string_ref(u8"Upgrade: h2c\r\n");
199+
200+
boost::erase_all(hdrString, http2);
201+
boost::erase_all(hdrString, http2c);
202+
}
203+
//
204+
194205
// Because boost::asio lies to us and reads more data into the header buffer than just headers, we cannot
195206
// rely on the bytesReceived value here. We must look at the whole buffer.
196207
auto bytesToParse = hdrString.size();
@@ -209,10 +220,10 @@ namespace te
209220
}
210221

211222
if (m_httpParser->upgrade == 1)
212-
{
223+
{
213224
ReportError(u8"In BaseHttpTransaction::Parse(const size_t&) - Upgrade requested. Unsupported.");
214225
ReportInfo(hdrString);
215-
return false;
226+
return false;
216227
}
217228

218229
if (nparsed != bytesToParse)
@@ -312,7 +323,7 @@ namespace te
312323
m_payloadBuffer.resize(PayloadBufferReadSize);
313324
}
314325

315-
return boost::asio::mutable_buffers_1(m_payloadBuffer.data(), PayloadBufferReadSize);
326+
return boost::asio::mutable_buffers_1(m_payloadBuffer.data(), m_payloadBuffer.size());
316327
}
317328

318329
boost::asio::const_buffers_1 BaseHttpTransaction::GetWriteBuffer()
@@ -321,8 +332,12 @@ namespace te
321332
{
322333
auto headersVector = HeadersToVector();
323334
auto newSize = headersVector.size() + m_parsedTransactionData.size();
324-
headersVector.reserve(newSize);
325-
headersVector.insert(headersVector.end(), m_parsedTransactionData.begin(), m_parsedTransactionData.end());
335+
headersVector.reserve(newSize);
336+
337+
if (m_parsedTransactionData.size() > 0)
338+
{
339+
headersVector.insert(headersVector.end(), m_parsedTransactionData.begin(), m_parsedTransactionData.end());
340+
}
326341

327342
m_parsedTransactionData = std::move(headersVector);
328343

@@ -348,29 +363,6 @@ namespace te
348363
RemoveHeader(util::http::headers::TransferEncoding);
349364
RemoveHeader(util::http::headers::ContentEncoding);
350365

351-
/*
352-
auto payloadSize = m_parsedTransactionData.size();
353-
if (
354-
payloadSize >= 4 &&
355-
m_parsedTransactionData[payloadSize - 4] == '\r' &&
356-
m_parsedTransactionData[payloadSize - 3] == '\n' &&
357-
m_parsedTransactionData[payloadSize - 2] == '\r' &&
358-
m_parsedTransactionData[payloadSize - 1] == '\n'
359-
)
360-
{
361-
payloadSize = m_parsedTransactionData.size() - 4;
362-
}
363-
else
364-
{
365-
payloadSize = m_parsedTransactionData.size();
366-
367-
m_parsedTransactionData.push_back('\r');
368-
m_parsedTransactionData.push_back('\n');
369-
m_parsedTransactionData.push_back('\r');
370-
m_parsedTransactionData.push_back('\n');
371-
}
372-
*/
373-
374366
std::string length = std::to_string(m_parsedTransactionData.size());
375367

376368
RemoveHeader(util::http::headers::ContentLength);
@@ -388,29 +380,6 @@ namespace te
388380
RemoveHeader(util::http::headers::TransferEncoding);
389381
RemoveHeader(util::http::headers::ContentEncoding);
390382

391-
/*
392-
auto payloadSize = m_parsedTransactionData.size();
393-
if (
394-
payloadSize >= 4 &&
395-
m_parsedTransactionData[payloadSize - 4] == '\r' &&
396-
m_parsedTransactionData[payloadSize - 3] == '\n' &&
397-
m_parsedTransactionData[payloadSize - 2] == '\r' &&
398-
m_parsedTransactionData[payloadSize - 1] == '\n'
399-
)
400-
{
401-
payloadSize = m_parsedTransactionData.size() - 4;
402-
}
403-
else
404-
{
405-
payloadSize = m_parsedTransactionData.size();
406-
407-
m_parsedTransactionData.push_back('\r');
408-
m_parsedTransactionData.push_back('\n');
409-
m_parsedTransactionData.push_back('\r');
410-
m_parsedTransactionData.push_back('\n');
411-
}
412-
*/
413-
414383
std::string length = std::to_string(m_parsedTransactionData.size());
415384

416385
RemoveHeader(util::http::headers::ContentLength);
@@ -627,19 +596,13 @@ namespace te
627596
return false;
628597
}
629598

630-
if (m_parsedTransactionData.size() > compressed.size())
631-
{
632-
SetPayload(compressed);
599+
SetPayload(compressed);
633600

634-
// Must re-add encoding header AFTER calling SetPayload, because it removes such headers.
635-
std::string gzip(u8"gzip");
636-
RemoveHeader(util::http::headers::ContentEncoding);
637-
AddHeader(util::http::headers::ContentEncoding, gzip);
638-
return true;
639-
}
640-
else {
641-
return false;
642-
}
601+
// Must re-add encoding header AFTER calling SetPayload, because it removes such headers.
602+
std::string gzip(u8"gzip");
603+
RemoveHeader(util::http::headers::ContentEncoding);
604+
AddHeader(util::http::headers::ContentEncoding, gzip);
605+
return true;
643606
}
644607

645608
const bool BaseHttpTransaction::CompressDeflate()
@@ -669,19 +632,13 @@ namespace te
669632
return false;
670633
}
671634

672-
if (m_parsedTransactionData.size() > compressed.size())
673-
{
674-
SetPayload(compressed);
635+
SetPayload(compressed);
675636

676-
// Must re-add encoding header AFTER calling SetPayload, because it removes such headers.
677-
std::string deflate(u8"deflate");
678-
RemoveHeader(util::http::headers::ContentEncoding);
679-
AddHeader(util::http::headers::ContentEncoding, deflate);
680-
return true;
681-
}
682-
else {
683-
return false;
684-
}
637+
// Must re-add encoding header AFTER calling SetPayload, because it removes such headers.
638+
std::string deflate(u8"deflate");
639+
RemoveHeader(util::http::headers::ContentEncoding);
640+
AddHeader(util::http::headers::ContentEncoding, deflate);
641+
return true;
685642
}
686643

687644
const bool BaseHttpTransaction::DecompressPayload()
@@ -1052,9 +1009,6 @@ namespace te
10521009
{
10531010
if (parser != nullptr)
10541011
{
1055-
/*
1056-
1057-
*/
10581012

10591013
BaseHttpTransaction* trans = static_cast<BaseHttpTransaction*>(parser->data);
10601014

@@ -1063,7 +1017,10 @@ namespace te
10631017
throw std::runtime_error(u8"In BaseHttpTransaction::OnBody() - http_parser->data is nullptr when it should contain a pointer the http_parser's owning BaseHttpTransaction object.");
10641018
}
10651019

1066-
trans->m_parsedTransactionData.insert(trans->m_parsedTransactionData.end(), at, at + length);
1020+
trans->m_parsedTransactionData.reserve(trans->m_parsedTransactionData.capacity() + length);
1021+
std::copy(at, at + length, std::back_inserter(trans->m_parsedTransactionData));
1022+
1023+
//trans->m_parsedTransactionData.insert(trans->m_parsedTransactionData.end(), at, at + length);
10671024
}
10681025
else
10691026
{

0 commit comments

Comments
 (0)