feat: forward more info on callback events
This commit is contained in:
+42
-27
@@ -331,56 +331,67 @@ void Proxy::handleClient(SOCKET hClientSocket)
|
||||
removeHeader(headers, "Expect");
|
||||
headers.insert(headers.size() - 2, "Accept-Encoding: identity\r\n");
|
||||
|
||||
OnClientRequest.run(url, headers);
|
||||
if (clientStream.contentLength == 0 || (clientStream.contentLength < 0 && !clientStream.isChunked))
|
||||
{
|
||||
std::string emptyBody = "";
|
||||
OnClientRequest.run(url, emptyBody, headers);
|
||||
SSL_write(remoteSSL, headers.data(), (int)headers.size());
|
||||
|
||||
clientStream.buffer.erase(0, clientStream.headersEnd + 4);
|
||||
clientStream.reset();
|
||||
}
|
||||
}
|
||||
|
||||
if (clientStream.isReceivingBody)
|
||||
{
|
||||
size_t bodyStart = clientStream.headersEnd + 4;
|
||||
std::string url = pendingUrls.back();
|
||||
std::string headers = clientStream.buffer.substr(0, bodyStart);
|
||||
removeHeader(headers, "Accept-Encoding");
|
||||
removeHeader(headers, "Expect");
|
||||
headers.insert(headers.size() - 2, "Accept-Encoding: identity\r\n");
|
||||
|
||||
bool complete = false;
|
||||
std::string body;
|
||||
|
||||
if (clientStream.isChunked)
|
||||
{
|
||||
size_t idx = 0;
|
||||
size_t idx = bodyStart;
|
||||
while (idx < clientStream.buffer.size())
|
||||
{
|
||||
size_t le = clientStream.buffer.find("\r\n", idx);
|
||||
if (le == std::string::npos) break;
|
||||
|
||||
int chunkSz = safe_stoi(clientStream.buffer.substr(idx, le - idx), 0, 16);
|
||||
size_t totalChunkSz = (le - idx) + 2 + chunkSz + 2;
|
||||
if (idx + totalChunkSz > clientStream.buffer.size()) break;
|
||||
|
||||
SSL_write(remoteSSL, clientStream.buffer.data() + idx, (int)totalChunkSz);
|
||||
idx += totalChunkSz;
|
||||
|
||||
if (chunkSz == 0)
|
||||
int cs = safe_stoi(clientStream.buffer.substr(idx, le - idx), 0, 16);
|
||||
if (idx + (le - idx) + 2 + cs + 2 > clientStream.buffer.size()) break;
|
||||
body.append(clientStream.buffer, le + 2, cs);
|
||||
idx = le + 2 + cs + 2;
|
||||
if (cs == 0)
|
||||
{
|
||||
clientStream.reset();
|
||||
complete = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (idx > 0) clientStream.buffer.erase(0, idx);
|
||||
if (!clientStream.isReceivingBody) continue;
|
||||
break;
|
||||
}
|
||||
else if (clientStream.contentLength >= 0)
|
||||
{
|
||||
size_t ts = (std::min)((size_t)clientStream.contentLength, clientStream.buffer.size());
|
||||
if (ts > 0)
|
||||
if (clientStream.buffer.size() >= bodyStart + clientStream.contentLength)
|
||||
{
|
||||
SSL_write(remoteSSL, clientStream.buffer.data(), (int)ts);
|
||||
clientStream.buffer.erase(0, ts);
|
||||
clientStream.contentLength -= (int)ts;
|
||||
body = clientStream.buffer.substr(bodyStart, clientStream.contentLength);
|
||||
complete = true;
|
||||
}
|
||||
if (clientStream.contentLength <= 0)
|
||||
}
|
||||
|
||||
if (complete)
|
||||
{
|
||||
OnClientRequest.run(url, body, headers);
|
||||
SSL_write(remoteSSL, headers.data(), (int)headers.size());
|
||||
SSL_write(remoteSSL, clientStream.buffer.data() + bodyStart,
|
||||
(int)(clientStream.buffer.size() - bodyStart));
|
||||
clientStream.buffer.clear();
|
||||
clientStream.reset();
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
else
|
||||
clientStream.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -527,11 +538,15 @@ void Proxy::handleClient(SOCKET hClientSocket)
|
||||
int sc =
|
||||
(firstSpace != std::string::npos) ? safe_stoi(respHeaders.substr(firstSpace + 1, 3)) : 0;
|
||||
|
||||
OnServerResponse.run(url, body);
|
||||
OnServerResponse.run(url, body, respHeaders);
|
||||
|
||||
removeHeader(respHeaders, "Transfer-Encoding");
|
||||
removeHeader(respHeaders, "Content-Length");
|
||||
if (sc != 204 && sc != 304 && sc != 205)
|
||||
|
||||
size_t fs = respHeaders.find(' ');
|
||||
int scFinal = (fs != std::string::npos) ? safe_stoi(respHeaders.substr(fs + 1, 3)) : 0;
|
||||
|
||||
if (scFinal != 204 && scFinal != 304 && scFinal != 205)
|
||||
respHeaders.insert(respHeaders.size() - 2,
|
||||
"Content-Length: " + std::to_string(body.size()) + "\r\n");
|
||||
|
||||
|
||||
@@ -25,8 +25,8 @@ class Proxy
|
||||
bool Init();
|
||||
void Shutdown();
|
||||
|
||||
CallbackEvent<const std::string&, std::string&> OnClientRequest;
|
||||
CallbackEvent<const std::string&, std::string&> OnServerResponse;
|
||||
CallbackEvent<const std::string&, const std::string&, std::string&> OnClientRequest;
|
||||
CallbackEvent<const std::string&, std::string&, std::string&> OnServerResponse;
|
||||
|
||||
private:
|
||||
void loop();
|
||||
|
||||
Reference in New Issue
Block a user