Beispiel #1
0
 private static void OnEavesdropperResponse(EavesdropperResponseEventArgs e)
 {
     EavesdropperResponse?.Invoke(null, e);
 }
Beispiel #2
0
        private static void HandleClientResponse(EavesdropperRequestEventArgs requestArgs, Stream clientStream)
        {
            WebResponse webResponse = null;
            try { webResponse = requestArgs.Request.GetResponse(); }
            catch (WebException e) { webResponse = e.Response; }

            if (webResponse == null) return;
            using (WebResponse response = webResponse)
            {
                if (IsCacheDisabled)
                    response.Headers["Cache-Control"] = "no-cache, no-store";

                byte[] responsePayload = null;
                using (var responseStream = response.GetResponseStream())
                using (var responseBufferStream = new MemoryStream())
                {
                    responseStream.CopyTo(responseBufferStream);
                    responsePayload = responseBufferStream.ToArray();
                }

                var responseArgs = new EavesdropperResponseEventArgs(response);
                responseArgs.Payload = responsePayload;

                OnEavesdropperResponse(responseArgs);
                if (!responseArgs.Cancel)
                {
                    string responseCommand = response.Headers.ToString();

                    var hResponse = (response as HttpWebResponse);
                    if (hResponse != null && !responseArgs.IsResponseOK)
                    {
                        responseCommand = $"HTTP/{hResponse.ProtocolVersion} {(int)hResponse.StatusCode} " +
                            $"{hResponse.StatusDescription}\r\n{responseCommand}";
                    }
                    else responseCommand = "HTTP/1.1 200 OK\r\n" + responseCommand;

                    string responseCookies = FormatResponseCookies(response);
                    if (!string.IsNullOrWhiteSpace(responseCookies))
                    {
                        string innerCookies = responseCommand.GetChild("Set-Cookie: ", '\r');

                        responseCommand = responseCommand
                            .Replace("Set-Cookie: " + innerCookies + "\r\n", responseCookies);
                    }

                    if (responseCommand.Contains("Content-Length"))
                    {
                        string contentLengthChild =
                            responseCommand.GetChild("Content-Length: ", '\r');

                        responseCommand = responseCommand.Replace(
                            contentLengthChild + "\r", (responseArgs.Payload?.Length ?? 0) + "\r");
                    }

                    byte[] responseCommandPayload = Encoding.ASCII.GetBytes(responseCommand);
                    clientStream.Write(responseCommandPayload,
                        0, responseCommandPayload.Length);

                    if (responseArgs.Payload != null)
                    {
                        clientStream.Write(responseArgs.Payload,
                            0, responseArgs.Payload.Length);
                    }
                }
            }
        }