From ad244ea8b03654bffd44fe375b4c07b8e1976998 Mon Sep 17 00:00:00 2001 From: neru Date: Sat, 21 Mar 2026 19:30:11 -0300 Subject: [PATCH] feat: forward more info on callback events --- src/unlocker/proxy.cpp | 75 +++++++++++++++++++++++++----------------- src/unlocker/proxy.h | 4 +-- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/unlocker/proxy.cpp b/src/unlocker/proxy.cpp index ed67b97..bab118f 100644 --- a/src/unlocker/proxy.cpp +++ b/src/unlocker/proxy.cpp @@ -331,55 +331,66 @@ void Proxy::handleClient(SOCKET hClientSocket) removeHeader(headers, "Expect"); headers.insert(headers.size() - 2, "Accept-Encoding: identity\r\n"); - OnClientRequest.run(url, headers); - SSL_write(remoteSSL, headers.data(), (int)headers.size()); - - clientStream.buffer.erase(0, clientStream.headersEnd + 4); + 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) - clientStream.reset(); - else - break; + } + + 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 - clientStream.reset(); + break; } } } @@ -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"); diff --git a/src/unlocker/proxy.h b/src/unlocker/proxy.h index d79f5cc..e6bba4f 100644 --- a/src/unlocker/proxy.h +++ b/src/unlocker/proxy.h @@ -25,8 +25,8 @@ class Proxy bool Init(); void Shutdown(); - CallbackEvent OnClientRequest; - CallbackEvent OnServerResponse; + CallbackEvent OnClientRequest; + CallbackEvent OnServerResponse; private: void loop();