Esempio n. 1
0
 private void _freeRequestData()
 {
     if (this.m_requestData != null)
     {
         this.m_requestData.Dispose();
         this.m_requestData = null;
     }
 }
Esempio n. 2
0
 public static void MakeFTPRequest(Session oSession, ref PipeReadBuffer buffBody, out HTTPResponseHeaders oRH)
 {
     FtpWebResponse response;
     if ((oSession.oRequest == null) || (oSession.oRequest.headers == null))
     {
         throw new ArgumentException("Session missing request objects.");
     }
     if (buffBody == null)
     {
         throw new ArgumentException("Response Stream may not be null.");
     }
     string fullUrl = oSession.fullUrl;
     FtpWebRequest request = (FtpWebRequest) WebRequest.Create(fullUrl);
     request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
     if (fullUrl.EndsWith("/"))
     {
         request.Method = "LIST";
     }
     else
     {
         request.Method = "RETR";
         if (oSession.oFlags.ContainsKey("FTP-UseASCII"))
         {
             request.UseBinary = false;
         }
         else
         {
             request.UseBinary = FiddlerApplication.Prefs.GetBoolPref("fiddler.ftp.UseBinary", true);
         }
     }
     if (!string.IsNullOrEmpty(oSession.oRequest.headers._uriUserInfo))
     {
         string sString = Utilities.UrlDecode(Utilities.TrimAfter(oSession.oRequest.headers._uriUserInfo, '@'));
         string userName = Utilities.TrimAfter(sString, ':');
         string password = sString.Contains(":") ? Utilities.TrimBefore(sString, ':') : string.Empty;
         request.Credentials = new NetworkCredential(userName, password);
     }
     else if (oSession.oRequest.headers.ExistsAndContains("Authorization", "Basic "))
     {
         string s = oSession.oRequest.headers["Authorization"].Substring(6);
         s = Encoding.UTF8.GetString(Convert.FromBase64String(s));
         string str6 = Utilities.TrimAfter(s, ':');
         string str7 = Utilities.TrimBefore(s, ':');
         request.Credentials = new NetworkCredential(str6, str7);
     }
     else if (oSession.oFlags.ContainsKey("x-AutoAuth") && oSession.oFlags["x-AutoAuth"].Contains(":"))
     {
         string str8 = Utilities.TrimAfter(oSession.oFlags["x-AutoAuth"], ':');
         string str9 = Utilities.TrimBefore(oSession.oFlags["x-AutoAuth"], ':');
         request.Credentials = new NetworkCredential(str8, str9);
     }
     else if (FiddlerApplication.Prefs.GetBoolPref("fiddler.ftp.AlwaysDemandCredentials", false))
     {
         byte[] bytes = Encoding.UTF8.GetBytes("Please provide login credentials for this FTP server".PadRight(0x200, ' '));
         buffBody.Write(bytes, 0, bytes.Length);
         oRH = new HTTPResponseHeaders();
         oRH.HTTPResponseCode = 0x191;
         oRH.HTTPResponseStatus = "401 Need Creds";
         oRH.Add("Content-Length", buffBody.Length.ToString());
         oRH.Add("WWW-Authenticate", "Basic realm=\"ftp://" + oSession.host + "\"");
         return;
     }
     request.UsePassive = FiddlerApplication.Prefs.GetBoolPref("fiddler.ftp.UsePassive", true);
     request.Proxy = null;
     try
     {
         response = (FtpWebResponse) request.GetResponse();
     }
     catch (WebException exception)
     {
         byte[] buffer2;
         FtpWebResponse response2 = (FtpWebResponse) exception.Response;
         if (response2 != null)
         {
             if (FtpStatusCode.NotLoggedIn == response2.StatusCode)
             {
                 buffer2 = Encoding.UTF8.GetBytes("This FTP server requires login credentials".PadRight(0x200, ' '));
                 buffBody.Write(buffer2, 0, buffer2.Length);
                 oRH = new HTTPResponseHeaders();
                 oRH.HTTPResponseCode = 0x191;
                 oRH.HTTPResponseStatus = "401 Need Creds";
                 oRH.Add("Content-Length", buffBody.Length.ToString());
                 oRH.Add("WWW-Authenticate", "Basic realm=\"ftp://" + oSession.host + "\"");
                 return;
             }
             buffer2 = Encoding.UTF8.GetBytes(string.Format("{0}{1}{2}", "Fiddler was unable to act as a HTTP-to-FTP gateway for this response. ", response2.StatusDescription, string.Empty.PadRight(0x200, ' ')));
             buffBody.Write(buffer2, 0, buffer2.Length);
         }
         else
         {
             buffer2 = Encoding.UTF8.GetBytes(string.Format("{0}{1}{2}", "Fiddler was unable to act as a HTTP-to-FTP gateway for this response. ", exception.Message, string.Empty.PadRight(0x200, ' ')));
             buffBody.Write(buffer2, 0, buffer2.Length);
         }
         oRH = new HTTPResponseHeaders();
         oRH.HTTPResponseCode = 0x1f8;
         oRH.HTTPResponseStatus = "504 HTTP-FTP Gateway failed";
         oRH.Add("Content-Length", buffBody.Length.ToString());
         return;
     }
     Stream responseStream = response.GetResponseStream();
     byte[] buffer3 = new byte[0x2000];
     for (int i = responseStream.Read(buffer3, 0, 0x2000); i > 0; i = responseStream.Read(buffer3, 0, 0x2000))
     {
         buffBody.Write(buffer3, 0, i);
     }
     oRH = new HTTPResponseHeaders();
     oRH.HTTPResponseCode = 200;
     oRH.HTTPResponseStatus = "200 OK";
     oRH.Add("FTP-Status", Utilities.ConvertCRAndLFToSpaces(response.StatusDescription));
     oRH.Add("Content-Length", buffBody.Length.ToString());
     response.Close();
 }
Esempio n. 3
0
 internal ServerChatter(Session oSession)
 {
     this._lngLastChunkInfoOffset = -1L;
     this.m_session = oSession;
     this.m_responseData = new PipeReadBuffer(false);
 }
Esempio n. 4
0
 internal bool ReadResponse()
 {
     if (this.pipeServer == null)
     {
         if (this.m_session.isFTP && !this.m_session.isFlagSet(SessionFlags.SentToGateway))
         {
             FTPGateway.MakeFTPRequest(this.m_session, ref this.m_responseData, out this.m_inHeaders);
             return true;
         }
         return false;
     }
     int iMaxByteCount = 0;
     bool flag = false;
     bool flag2 = false;
     bool flag3 = false;
     bool flag4 = false;
     byte[] arrBuffer = new byte[_cbServerReadBuffer];
     do
     {
         try
         {
             iMaxByteCount = this.pipeServer.Receive(arrBuffer);
             if (0L == this.m_session.Timers.ServerBeginResponse.Ticks)
             {
                 this.m_session.Timers.ServerBeginResponse = DateTime.Now;
             }
             if (iMaxByteCount <= 0)
             {
                 flag = true;
                 FiddlerApplication.DoReadResponseBuffer(this.m_session, arrBuffer, 0);
                 if (CONFIG.bDebugSpew)
                 {
                     FiddlerApplication.DebugSpew(string.Format("READ FROM {0}: returned 0 signaling end-of-stream", this.pipeServer));
                 }
             }
             else
             {
                 if (CONFIG.bDebugSpew)
                 {
                     FiddlerApplication.DebugSpew(string.Format("READ FROM {0}:\n{1}", this.pipeServer, Utilities.ByteArrayToHexView(arrBuffer, 0x20, iMaxByteCount)));
                 }
                 if (!FiddlerApplication.DoReadResponseBuffer(this.m_session, arrBuffer, iMaxByteCount))
                 {
                     flag2 = true;
                 }
                 this.m_responseData.Write(arrBuffer, 0, iMaxByteCount);
                 this.m_responseTotalDataCount += iMaxByteCount;
                 if ((this.m_inHeaders == null) && this.GetHeaders())
                 {
                     this.m_session.Timers.FiddlerGotResponseHeaders = DateTime.Now;
                     if ((this.m_session.state == SessionStates.Aborted) && this.m_session.isAnyFlagSet(SessionFlags.ProtocolViolationInResponse))
                     {
                         return false;
                     }
                     FiddlerApplication.DoResponseHeadersAvailable(this.m_session);
                     string inStr = this.m_inHeaders["Content-Type"];
                     if (inStr.OICStartsWithAny(new string[] { "text/event-stream", "multipart/x-mixed-replace" }) && FiddlerApplication.Prefs.GetBoolPref("fiddler.network.streaming.AutoStreamByMIME", true))
                     {
                         this.m_session.bBufferResponse = false;
                     }
                     else if (CONFIG.bStreamAudioVideo && inStr.OICStartsWithAny(new string[] { "video/", "audio/", "application/x-mms-framed" }))
                     {
                         this.m_session.bBufferResponse = false;
                     }
                     if (!this.m_session.bBufferResponse && this.m_session.HTTPMethodIs("CONNECT"))
                     {
                         this.m_session.bBufferResponse = true;
                     }
                     if (!this.m_session.bBufferResponse && (0x65 == this.m_inHeaders.HTTPResponseCode))
                     {
                         this.m_session.bBufferResponse = true;
                     }
                     if (!this.m_session.bBufferResponse && (this.m_session.oRequest.pipeClient == null))
                     {
                         this.m_session.bBufferResponse = true;
                     }
                     if ((!this.m_session.bBufferResponse && ((0x191 == this.m_inHeaders.HTTPResponseCode) || (0x197 == this.m_inHeaders.HTTPResponseCode))) && this.m_session.oFlags.ContainsKey("x-AutoAuth"))
                     {
                         this.m_session.bBufferResponse = true;
                     }
                     this.m_session.SetBitFlag(SessionFlags.ResponseStreamed, !this.m_session.bBufferResponse);
                     if (!this.m_session.bBufferResponse)
                     {
                         if (this.m_session.oFlags.ContainsKey("response-trickle-delay"))
                         {
                             int num2 = int.Parse(this.m_session.oFlags["response-trickle-delay"]);
                             this.m_session.oRequest.pipeClient.TransmitDelay = num2;
                         }
                         if (this.m_session.oFlags.ContainsKey("log-drop-response-body") || FiddlerApplication.Prefs.GetBoolPref("fiddler.network.streaming.ForgetStreamedData", false))
                         {
                             flag3 = true;
                         }
                     }
                 }
                 if ((this.m_inHeaders != null) && this.m_session.isFlagSet(SessionFlags.ResponseStreamed))
                 {
                     if (!flag4 && !this.LeakResponseBytes())
                     {
                         flag4 = true;
                     }
                     if (flag3)
                     {
                         this.m_session.SetBitFlag(SessionFlags.ResponseBodyDropped, true);
                         if (this._lngLastChunkInfoOffset > -1L)
                         {
                             this.ReleaseStreamedChunkedData();
                         }
                         else if (this.m_inHeaders.ExistsAndContains("Transfer-Encoding", "chunked"))
                         {
                             this.ReleaseStreamedChunkedData();
                         }
                         else
                         {
                             this.ReleaseStreamedData();
                         }
                     }
                 }
             }
         }
         catch (SocketException exception)
         {
             flag2 = true;
             if (exception.ErrorCode == 0x274c)
             {
                 this.m_session["X-ServerPipeError"] = "Timed out while reading response.";
             }
             else
             {
                 FiddlerApplication.Log.LogFormat("fiddler.network.readresponse.failure> Session #{0} raised exception {1}", new object[] { this.m_session.id, Utilities.DescribeException(exception) });
             }
         }
         catch (Exception exception2)
         {
             flag2 = true;
             if (exception2 is OperationCanceledException)
             {
                 this.m_session.state = SessionStates.Aborted;
                 FiddlerApplication.Log.LogFormat("fiddler.network.readresponse.failure> Session #{0} was aborted {1}", new object[] { this.m_session.id, Utilities.DescribeException(exception2) });
             }
             else if (exception2 is OutOfMemoryException)
             {
                 FiddlerApplication.ReportException(exception2);
                 this.m_session.state = SessionStates.Aborted;
                 FiddlerApplication.Log.LogFormat("fiddler.network.readresponse.failure> Session #{0} Out of Memory", new object[] { this.m_session.id });
             }
             else
             {
                 FiddlerApplication.Log.LogFormat("fiddler.network.readresponse.failure> Session #{0} raised exception {1}", new object[] { this.m_session.id, Utilities.DescribeException(exception2) });
             }
         }
     }
     while ((!flag && !flag2) && ((this.m_inHeaders == null) || !this.isResponseBodyComplete()));
     this.m_session.Timers.ServerDoneResponse = DateTime.Now;
     if (this.m_session.isFlagSet(SessionFlags.ResponseStreamed))
     {
         this.m_session.Timers.ClientDoneResponse = this.m_session.Timers.ServerDoneResponse;
     }
     if ((0L == this.m_responseTotalDataCount) && (this.m_inHeaders == null))
     {
         flag2 = true;
     }
     arrBuffer = null;
     if (flag2)
     {
         this.m_responseData.Dispose();
         this.m_responseData = null;
         return false;
     }
     if (this.m_inHeaders == null)
     {
         FiddlerApplication.HandleHTTPError(this.m_session, SessionFlags.ProtocolViolationInResponse, true, true, "The Server did not return properly-formatted HTTP Headers. Maybe missing altogether (e.g. HTTP/0.9), maybe only \\r\\r instead of \\r\\n\\r\\n?\n");
         this.m_session.SetBitFlag(SessionFlags.ResponseStreamed, false);
         this.m_inHeaders = new HTTPResponseHeaders(CONFIG.oHeaderEncoding);
         this.m_inHeaders.HTTPVersion = "HTTP/1.0";
         this.m_inHeaders.HTTPResponseCode = 200;
         this.m_inHeaders.HTTPResponseStatus = "200 This buggy server did not return headers";
         this.iEntityBodyOffset = 0;
         return true;
     }
     return true;
 }
Esempio n. 5
0
 internal void Initialize(bool bAllocatePipeReadBuffer)
 {
     if (bAllocatePipeReadBuffer)
     {
         this.m_responseData = new PipeReadBuffer(false);
     }
     else
     {
         this.m_responseData = null;
     }
     this._lngLeakedOffset = this._iBodySeekProgress = this.iEntityBodyOffset = 0;
     this._lngLastChunkInfoOffset = -1L;
     this.m_inHeaders = null;
     this._bLeakedHeaders = false;
     this.pipeServer = null;
     this._bWasForwarded = false;
     this.m_session.SetBitFlag(SessionFlags.ServerPipeReused, false);
 }
Esempio n. 6
0
 internal void FreeResponseDataBuffer()
 {
     this.m_responseData.Dispose();
     this.m_responseData = null;
 }
Esempio n. 7
0
 private void _deleteInformationalMessage()
 {
     this.m_inHeaders = null;
     byte[] buffer = new byte[this.m_responseData.Length - this.iEntityBodyOffset];
     this.m_responseData.Position = this.iEntityBodyOffset;
     this.m_responseData.Read(buffer, 0, buffer.Length);
     this.m_responseData.Dispose();
     this.m_responseData = new PipeReadBuffer(buffer.Length);
     this.m_responseData.Write(buffer, 0, buffer.Length);
     this.m_responseTotalDataCount = 0L;
     this.iEntityBodyOffset = this._iBodySeekProgress = 0;
 }
Esempio n. 8
0
 private void ReleaseStreamedData()
 {
     this.m_responseData = new PipeReadBuffer(false);
     this._lngLeakedOffset = 0L;
     if (this.iEntityBodyOffset > 0)
     {
         this.m_responseTotalDataCount -= this.iEntityBodyOffset;
         this.iEntityBodyOffset = 0;
     }
 }
Esempio n. 9
0
 private void ReleaseStreamedChunkedData()
 {
     long num;
     if (this.iEntityBodyOffset > this._lngLastChunkInfoOffset)
     {
         this._lngLastChunkInfoOffset = this.iEntityBodyOffset;
     }
     Utilities.IsChunkedBodyComplete(this.m_session, this.m_responseData, this._lngLastChunkInfoOffset, out this._lngLastChunkInfoOffset, out num);
     int iDefaultCapacity = (int) (this.m_responseData.Length - this._lngLastChunkInfoOffset);
     PipeReadBuffer buffer = new PipeReadBuffer(iDefaultCapacity);
     buffer.Write(this.m_responseData.GetBuffer(), (int) this._lngLastChunkInfoOffset, iDefaultCapacity);
     this.m_responseData = buffer;
     this._lngLeakedOffset = iDefaultCapacity;
     this._lngLastChunkInfoOffset = 0L;
     this.iEntityBodyOffset = 0;
 }
Esempio n. 10
0
        internal bool ReadRequest()
        {
            if (this.m_requestData != null)
            {
                FiddlerApplication.ReportException(new InvalidOperationException("ReadRequest called when requestData buffer already existed."));
                return(false);
            }
            if (this.pipeClient == null)
            {
                FiddlerApplication.ReportException(new InvalidOperationException("ReadRequest called after pipeClient was null'd."));
                return(false);
            }
            this.m_requestData = new PipeReadBuffer(true);
            this.m_session.SetBitFlag(SessionFlags.ClientPipeReused, this.pipeClient.iUseCount > 0u);
            this.pipeClient.IncrementUse(0);
            this.pipeClient.setReceiveTimeout();
            int  num   = 0;
            bool flag  = false;
            bool flag2 = false;

            byte[] array = new byte[ClientChatter._cbClientReadBuffer];
            while (true)
            {
                try
                {
                    num = this.pipeClient.Receive(array);
                    goto IL_1F1;
                }
                catch (Exception ex)
                {
                    if (CONFIG.bDebugSpew)
                    {
                        FiddlerApplication.DebugSpew(string.Format("ReadRequest {0} threw {1}", (this.pipeClient == null) ? "Null pipeClient" : this.pipeClient.ToString(), ex.Message));
                    }
                    flag = true;
                    goto IL_1F1;
                }
                goto IL_D2;
IL_1DE:
                if (flag2 || flag)
                {
                    goto IL_22F;
                }
                if (this.isRequestComplete())
                {
                    goto Block_17;
                }
                continue;
IL_D2:
                flag2 = true;
                FiddlerApplication.DoReadRequestBuffer(this.m_session, array, 0);
                if (CONFIG.bDebugSpew)
                {
                    FiddlerApplication.DebugSpew(string.Format("ReadRequest {0} returned {1}", (this.pipeClient == null) ? "Null pipeClient" : this.pipeClient.ToString(), num));
                    goto IL_1DE;
                }
                goto IL_1DE;
IL_1F1:
                if (num <= 0)
                {
                    goto IL_D2;
                }
                if (CONFIG.bDebugSpew)
                {
                    FiddlerApplication.DebugSpew(string.Format("READ FROM {0}:\n{1}", this.pipeClient, Utilities.ByteArrayToHexView(array, 32, num)));
                }
                if (!FiddlerApplication.DoReadRequestBuffer(this.m_session, array, num))
                {
                    flag = true;
                }
                if (0L == this.m_requestData.Length)
                {
                    this.m_session.Timers.ClientBeginRequest = DateTime.Now;
                    if (1u == this.pipeClient.iUseCount && num > 2)
                    {
                        if (array[0] == 5)
                        {
                            break;
                        }
                        if (array[0] == 5)
                        {
                            break;
                        }
                    }
                    int i;
                    for (i = 0; i < num; i++)
                    {
                        if (13 != array[i] && 10 != array[i])
                        {
                            break;
                        }
                    }
                    this.m_requestData.Write(array, i, num - i);
                    goto IL_1DE;
                }
                this.m_requestData.Write(array, 0, num);
                goto IL_1DE;
            }
            goto IL_1FD;
Block_17:
            goto IL_22F;
IL_1FD:
            FiddlerApplication.Log.LogFormat("It looks like someone is trying to send SOCKS traffic to us.\r\n{0}", new object[]
            {
                Utilities.ByteArrayToHexView(array, 16, Math.Min(num, 256))
            });
            return(false);

IL_22F:
            array = null;
            if (!flag)
            {
                if (this.m_requestData.Length != 0L)
                {
                    if (this.m_headers == null || this.m_session.state >= SessionStates.Done)
                    {
                        this._freeRequestData();
                        return(false);
                    }
                    if ("CONNECT" == this.m_headers.HTTPMethod)
                    {
                        this.m_session.isTunnel = true;
                        this.m_sHostFromURI     = this.m_session.PathAndQuery;
                    }
                    if (this.m_sHostFromURI != null)
                    {
                        if (this.m_headers.Exists("Host"))
                        {
                            if (!Utilities.areOriginsEquivalent(this.m_sHostFromURI, this.m_headers["Host"], this.m_session.isHTTPS ? 443 : (this.m_session.isFTP ? 21 : 80)) && (!this.m_session.isTunnel || !Utilities.areOriginsEquivalent(this.m_sHostFromURI, this.m_headers["Host"], 443)))
                            {
                                this.m_session.oFlags["X-Original-Host"] = this.m_headers["Host"];
                                this.m_session.oFlags["X-URI-Host"]      = this.m_sHostFromURI;
                                if (FiddlerApplication.Prefs.GetBoolPref("fiddler.network.SetHostHeaderFromURL", true))
                                {
                                    this.m_headers["Host"] = this.m_sHostFromURI;
                                }
                            }
                        }
                        else
                        {
                            if ("HTTP/1.1".OICEquals(this.m_headers.HTTPVersion))
                            {
                                this.m_session.oFlags["X-Original-Host"] = string.Empty;
                            }
                            this.m_headers["Host"] = this.m_sHostFromURI;
                        }
                        this.m_sHostFromURI = null;
                    }
                    if (!this.m_headers.Exists("Host"))
                    {
                        this._freeRequestData();
                        return(false);
                    }
                    return(true);
                }
            }
            this._freeRequestData();
            if (this.pipeClient == null)
            {
                return(false);
            }
            if (this.pipeClient.iUseCount < 2u || (this.pipeClient.bIsSecured && this.pipeClient.iUseCount < 3u))
            {
                FiddlerApplication.Log.LogFormat("[Fiddler] No {0} request was received from ({1}) new client socket, port {2}.", new object[]
                {
                    this.pipeClient.bIsSecured ? "HTTPS" : "HTTP",
                    this.m_session.oFlags["X-ProcessInfo"],
                    this.m_session.oFlags["X-CLIENTPORT"]
                });
            }
            return(false);
        }
Esempio n. 11
0
        public static void MakeFTPRequest(Session oSession, ref PipeReadBuffer buffBody, out HTTPResponseHeaders oRH)
        {
            if (oSession.oRequest == null || oSession.oRequest.headers == null)
            {
                throw new ArgumentException("Session missing request objects.");
            }
            if (buffBody == null)
            {
                throw new ArgumentException("Response Stream may not be null.");
            }
            string        fullUrl       = oSession.fullUrl;
            FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(fullUrl);

            ftpWebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
            if (fullUrl.EndsWith("/"))
            {
                ftpWebRequest.Method = "LIST";
            }
            else
            {
                ftpWebRequest.Method = "RETR";
                if (oSession.oFlags.ContainsKey("FTP-UseASCII"))
                {
                    ftpWebRequest.UseBinary = false;
                }
                else
                {
                    ftpWebRequest.UseBinary = FiddlerApplication.Prefs.GetBoolPref("fiddler.ftp.UseBinary", true);
                }
            }
            if (!string.IsNullOrEmpty(oSession.oRequest.headers._uriUserInfo))
            {
                string text = Utilities.TrimAfter(oSession.oRequest.headers._uriUserInfo, '@');
                text = Utilities.UrlDecode(text);
                string userName = Utilities.TrimAfter(text, ':');
                string password = text.Contains(":") ? Utilities.TrimBefore(text, ':') : string.Empty;
                ftpWebRequest.Credentials = new NetworkCredential(userName, password);
            }
            else
            {
                if (oSession.oRequest.headers.ExistsAndContains("Authorization", "Basic "))
                {
                    string text2 = oSession.oRequest.headers["Authorization"].Substring(6);
                    text2 = Encoding.UTF8.GetString(Convert.FromBase64String(text2));
                    string userName2 = Utilities.TrimAfter(text2, ':');
                    string password2 = Utilities.TrimBefore(text2, ':');
                    ftpWebRequest.Credentials = new NetworkCredential(userName2, password2);
                }
                else
                {
                    if (oSession.oFlags.ContainsKey("x-AutoAuth") && oSession.oFlags["x-AutoAuth"].Contains(":"))
                    {
                        string userName3 = Utilities.TrimAfter(oSession.oFlags["x-AutoAuth"], ':');
                        string password3 = Utilities.TrimBefore(oSession.oFlags["x-AutoAuth"], ':');
                        ftpWebRequest.Credentials = new NetworkCredential(userName3, password3);
                    }
                    else
                    {
                        if (FiddlerApplication.Prefs.GetBoolPref("fiddler.ftp.AlwaysDemandCredentials", false))
                        {
                            byte[] bytes = Encoding.UTF8.GetBytes("Please provide login credentials for this FTP server".PadRight(512, ' '));
                            buffBody.Write(bytes, 0, bytes.Length);
                            oRH = new HTTPResponseHeaders();
                            oRH.SetStatus(401, "Need Creds");
                            oRH.Add("Content-Length", buffBody.Length.ToString());
                            oRH.Add("WWW-Authenticate", "Basic realm=\"ftp://" + oSession.host + "\"");
                            return;
                        }
                    }
                }
            }
            ftpWebRequest.UsePassive = FiddlerApplication.Prefs.GetBoolPref("fiddler.ftp.UsePassive", true);
            ftpWebRequest.Proxy      = null;
            FtpWebResponse ftpWebResponse;

            try
            {
                ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
            }
            catch (WebException ex)
            {
                FtpWebResponse ftpWebResponse2 = (FtpWebResponse)ex.Response;
                if (ftpWebResponse2 != null)
                {
                    byte[] bytes2;
                    if (FtpStatusCode.NotLoggedIn == ftpWebResponse2.StatusCode)
                    {
                        bytes2 = Encoding.UTF8.GetBytes("This FTP server requires login credentials".PadRight(512, ' '));
                        buffBody.Write(bytes2, 0, bytes2.Length);
                        oRH = new HTTPResponseHeaders();
                        oRH.SetStatus(401, "Need Creds");
                        oRH.Add("Content-Length", buffBody.Length.ToString());
                        oRH.Add("WWW-Authenticate", "Basic realm=\"ftp://" + oSession.host + "\"");
                        return;
                    }
                    bytes2 = Encoding.UTF8.GetBytes(string.Format("{0}{1}{2}", "Fiddler was unable to act as a HTTP-to-FTP gateway for this response. ", ftpWebResponse2.StatusDescription, string.Empty.PadRight(512, ' ')));
                    buffBody.Write(bytes2, 0, bytes2.Length);
                }
                else
                {
                    byte[] bytes2 = Encoding.UTF8.GetBytes(string.Format("{0}{1}{2}", "Fiddler was unable to act as a HTTP-to-FTP gateway for this response. ", ex.Message, string.Empty.PadRight(512, ' ')));
                    buffBody.Write(bytes2, 0, bytes2.Length);
                }
                oRH = new HTTPResponseHeaders();
                oRH.SetStatus(504, "HTTP-FTP Gateway failed");
                oRH.Add("Content-Length", buffBody.Length.ToString());
                return;
            }
            Stream responseStream = ftpWebResponse.GetResponseStream();

            byte[] buffer = new byte[8192];
            for (int i = responseStream.Read(buffer, 0, 8192); i > 0; i = responseStream.Read(buffer, 0, 8192))
            {
                buffBody.Write(buffer, 0, i);
            }
            oRH = new HTTPResponseHeaders();
            oRH.SetStatus(200, "OK");
            oRH.Add("FTP-Status", Utilities.ConvertCRAndLFToSpaces(ftpWebResponse.StatusDescription));
            oRH.Add("Content-Length", buffBody.Length.ToString());
            ftpWebResponse.Close();
        }
Esempio n. 12
0
 private void _freeRequestData()
 {
     if (this.m_requestData != null)
     {
         this.m_requestData.Dispose();
         this.m_requestData = null;
     }
 }
Esempio n. 13
0
 internal bool ReadRequest()
 {
     if (this.m_requestData != null)
     {
         FiddlerApplication.ReportException(new InvalidOperationException("ReadRequest called when requestData buffer already existed."));
         return false;
     }
     if (this.pipeClient == null)
     {
         FiddlerApplication.ReportException(new InvalidOperationException("ReadRequest called after pipeClient was null'd."));
         return false;
     }
     this.m_requestData = new PipeReadBuffer(true);
     this.m_session.SetBitFlag(SessionFlags.ClientPipeReused, this.pipeClient.iUseCount > 0);
     this.pipeClient.IncrementUse(0);
     this.pipeClient.setReceiveTimeout();
     int num = 0;
     bool flag = false;
     bool flag2 = false;
     byte[] arrBuffer = new byte[_cbClientReadBuffer];
     do
     {
         try
         {
             num = this.pipeClient.Receive(arrBuffer);
         }
         catch (Exception exception)
         {
             if (CONFIG.bDebugSpew)
             {
                 FiddlerApplication.DebugSpew(string.Format("ReadRequest {0} threw {1}", (this.pipeClient == null) ? "Null pipeClient" : this.pipeClient.ToString(), exception.Message));
             }
             flag = true;
         }
         if (num <= 0)
         {
             flag2 = true;
             FiddlerApplication.DoReadRequestBuffer(this.m_session, arrBuffer, 0);
             if (CONFIG.bDebugSpew)
             {
                 FiddlerApplication.DebugSpew(string.Format("ReadRequest {0} returned {1}", (this.pipeClient == null) ? "Null pipeClient" : this.pipeClient.ToString(), num));
             }
         }
         else
         {
             if (CONFIG.bDebugSpew)
             {
                 FiddlerApplication.DebugSpew(string.Format("READ FROM {0}:\n{1}", this.pipeClient, Utilities.ByteArrayToHexView(arrBuffer, 0x20, num)));
             }
             if (!FiddlerApplication.DoReadRequestBuffer(this.m_session, arrBuffer, num))
             {
                 flag = true;
             }
             if (0L == this.m_requestData.Length)
             {
                 this.m_session.Timers.ClientBeginRequest = DateTime.Now;
                 if (((1 == this.pipeClient.iUseCount) && (num > 2)) && ((arrBuffer[0] == 5) || (arrBuffer[0] == 5)))
                 {
                     FiddlerApplication.Log.LogFormat("It looks like someone is trying to send SOCKS traffic to us.\r\n{0}", new object[] { Utilities.ByteArrayToHexView(arrBuffer, 0x10, Math.Min(num, 0x100)) });
                     return false;
                 }
                 int index = 0;
                 while ((index < num) && ((13 == arrBuffer[index]) || (10 == arrBuffer[index])))
                 {
                     index++;
                 }
                 this.m_requestData.Write(arrBuffer, index, num - index);
             }
             else
             {
                 this.m_requestData.Write(arrBuffer, 0, num);
             }
         }
     }
     while ((!flag2 && !flag) && !this.isRequestComplete());
     arrBuffer = null;
     if (flag || (this.m_requestData.Length == 0L))
     {
         this._freeRequestData();
         if ((this.pipeClient != null) && ((this.pipeClient.iUseCount < 2) || (this.pipeClient.bIsSecured && (this.pipeClient.iUseCount < 3))))
         {
             FiddlerApplication.Log.LogFormat("[Fiddler] No {0} request was received from ({1}) new client socket, port {2}.", new object[] { this.pipeClient.bIsSecured ? "HTTPS" : "HTTP", this.m_session.oFlags["X-ProcessInfo"], this.m_session.oFlags["X-CLIENTPORT"] });
         }
         return false;
     }
     if ((this.m_headers == null) || (this.m_session.state >= SessionStates.Done))
     {
         this._freeRequestData();
         return false;
     }
     if ("CONNECT" == this.m_headers.HTTPMethod)
     {
         this.m_session.isTunnel = true;
         this.m_sHostFromURI = this.m_session.PathAndQuery;
     }
     if (this.m_sHostFromURI != null)
     {
         if (this.m_headers.Exists("Host"))
         {
             if (!Utilities.areOriginsEquivalent(this.m_sHostFromURI, this.m_headers["Host"], this.m_session.isHTTPS ? 0x1bb : (this.m_session.isFTP ? 0x15 : 80)) && (!this.m_session.isTunnel || !Utilities.areOriginsEquivalent(this.m_sHostFromURI, this.m_headers["Host"], 0x1bb)))
             {
                 this.m_session.oFlags["X-Original-Host"] = this.m_headers["Host"];
                 this.m_session.oFlags["X-URI-Host"] = this.m_sHostFromURI;
                 if (FiddlerApplication.Prefs.GetBoolPref("fiddler.network.SetHostHeaderFromURL", true))
                 {
                     this.m_headers["Host"] = this.m_sHostFromURI;
                 }
             }
         }
         else
         {
             if ("HTTP/1.1".OICEquals(this.m_headers.HTTPVersion))
             {
                 this.m_session.oFlags["X-Original-Host"] = string.Empty;
             }
             this.m_headers["Host"] = this.m_sHostFromURI;
         }
         this.m_sHostFromURI = null;
     }
     if (!this.m_headers.Exists("Host"))
     {
         this._freeRequestData();
         return false;
     }
     return true;
 }