private void InitConnection(HttpTunnelStatus tunnelStatus) { if (IsUseProxy()) { if ((oldProxyServer != ProxySettings.Server) || (oldProxyPort != ProxySettings.Port)) { base.Close(); } oldProxyServer = ProxySettings.Server; oldProxyPort = ProxySettings.Port; } if (!StringUtils.IsEmpty(Url.Host) && (Url.Host != "*")) { if ((base.Server != Url.Host) || (base.Port != Url.Port)) { base.Close(); } base.Server = Url.Host; base.Port = Url.Port; } base.Open(); }
private void WriteRequestHeader(HttpTunnelStatus tunnelStatus, StringCollection requestHeader, Stream requestBody) { bool active = base.Active; InitConnection(tunnelStatus); PrepareRequestHeader(tunnelStatus, requestHeader, requestBody); try { base.Connection.WriteString(requestHeader.ToString()); } catch (SocketError error) { if (!active || (error.ErrorCode != 0x2745)) { throw; } base.Close(); base.Open(); base.Connection.WriteString(requestHeader.ToString()); } }
private void PrepareRequestHeader(HttpTunnelStatus tunnelStatus, StringCollection requestHeader, Stream requestBody) { string str2; RemoveRequestLine(requestHeader); RemoveHeaderTrailer(requestHeader); string absolutePath = Url.AbsolutePath; if (IsUseProxy()) { switch (tunnelStatus) { case HttpTunnelStatus.None: absolutePath = Url.AbsoluteUri; break; case HttpTunnelStatus.Connect: absolutePath = string.Format("{0}:{1}", Url.Host, Url.Port); break; } } if (StringUtils.IsEmpty(absolutePath)) { absolutePath = "/"; } requestHeader.Insert(0, string.Format("{0} {1} {2}", method, absolutePath, httpVersions[(int)HttpVersion])); var fieldList = new HeaderFieldList(); HeaderFieldList.GetHeaderFieldList(0, requestHeader, fieldList); HeaderFieldList.RemoveHeaderField(requestHeader, fieldList, "Host"); fieldList.Clear(); HeaderFieldList.GetHeaderFieldList(0, requestHeader, fieldList); if (AllowCompression && StringUtils.IsEmpty(HeaderFieldList.GetHeaderFieldValue(requestHeader, fieldList, "Accept-Encoding"))) { HeaderFieldList.AddHeaderField(requestHeader, "Accept-Encoding", "gzip, deflate"); } if (!StringUtils.IsEmpty(Url.Host)) { str2 = Url.Host + GetPortIfNeed(Url.Port); } else { str2 = base.Server + GetPortIfNeed(base.Port); } HeaderFieldList.AddHeaderField(requestHeader, "Host", str2); if (StringUtils.IsEmpty(HeaderFieldList.GetHeaderFieldValue(requestHeader, fieldList, "User-Agent"))) { HeaderFieldList.AddHeaderField(requestHeader, "User-Agent", UserAgent); } string val = HeaderFieldList.GetHeaderFieldValue(requestHeader, fieldList, "Content-Length"); if (requestBody.Length == 0L) { HeaderFieldList.RemoveHeaderField(requestHeader, fieldList, "Content-Length"); fieldList.Clear(); HeaderFieldList.GetHeaderFieldList(0, requestHeader, fieldList); HeaderFieldList.RemoveHeaderField(requestHeader, fieldList, "Content-Type"); fieldList.Clear(); HeaderFieldList.GetHeaderFieldList(0, requestHeader, fieldList); } else if (StringUtils.IsEmpty(val) && ((tunnelStatus == HttpTunnelStatus.Connect) || (requestBody.Length > 0L))) { HeaderFieldList.AddHeaderField(requestHeader, "Content-Length", requestBody.Length.ToString()); } if (!AllowCaching && StringUtils.IsEmpty(HeaderFieldList.GetHeaderFieldValue(requestHeader, fieldList, cacheFields[(int)HttpVersion]))) { HeaderFieldList.AddHeaderField(requestHeader, cacheFields[(int)HttpVersion], "no-cache"); } if (KeepConnection && StringUtils.IsEmpty(HeaderFieldList.GetHeaderFieldValue(requestHeader, fieldList, connFields[IsUseProxy() ? 1 : 0]))) { HeaderFieldList.AddHeaderField(requestHeader, connFields[IsUseProxy() ? 1 : 0], "Keep-Alive"); } if (AllowCookies) { Cookies.SetRequestCookies(requestHeader); } requestHeader.Add(""); }
private string[] InternalSendRequest(string method, string[] requestHeader, Stream requestBody, Stream responseBody) { string[] strArray2; using (var stream = new MemoryStream()) { var ex = new StringCollectionEx(); if (requestHeader != null) { ex.AddRange(requestHeader); } var responseHeader = new StringCollectionEx(); this.method = method; if (requestBody == null) { requestBody = Stream.Null; } if (responseBody == null) { responseBody = Stream.Null; } string[] responseText = null; long position = requestBody.Position; bool flag = false; int num2 = 0; int num3 = 0; int num4 = 0; long extraSize = 0L; HttpTunnelStatus none = HttpTunnelStatus.None; try { while (true) { none = GetTunnelStatus(); WriteRequestHeader(none, ex, requestBody); requestBody.Position = position; WriteRequestData(requestBody); OnRequestSent(new HttpRequestEventArgs(this.method, Url.Url, ex.ToArray())); ReadResponseHeader(responseHeader, stream); extraSize = stream.Length - stream.Position; flag = !GetKeepAlive(); if ((((StatusCode / 100) == 3) && (StatusCode != 0x130)) && (StatusCode != 0x131)) { num2++; responseText = ReadResponseText(responseHeader, extraSize, stream); if ((MaxRedirects > 0) && (num2 > MaxRedirects)) { RaiseHttpError(StatusCode, responseHeader, responseText); } string str = this.method; if (!AllowRedirects || !Redirect(ex, responseText, ref str)) { RaiseHttpError(StatusCode, responseHeader, responseText); } if (string.Compare(str, "GET", true, CultureInfo.InvariantCulture) == 0) { requestBody = Stream.Null; } this.method = str; if (IsUseProxy()) { flag = true; } } else if (StatusCode == 0x191) { num3++; responseText = ReadResponseText(responseHeader, extraSize, stream); if ((MaxAuthRetries > 0) && (num3 > MaxAuthRetries)) { RaiseHttpError(StatusCode, responseHeader, responseText); } } else if (StatusCode == 0x197) { num4++; responseText = ReadResponseText(responseHeader, extraSize, stream); if ((MaxAuthRetries > 0) && (num4 > MaxAuthRetries)) { RaiseHttpError(StatusCode, responseHeader, responseText); } } else if (StatusCode >= 400) { responseText = ReadResponseText(responseHeader, extraSize, stream); RaiseHttpError(StatusCode, responseHeader, responseText); } else { ReadResponseBody(responseHeader, extraSize, stream, responseBody); goto Label_032C; } if (flag) { base.Close(); } } } finally { if (flag && (none != HttpTunnelStatus.Connect)) { base.Close(); } } Label_032C: strArray2 = responseHeader.ToArray(); } return(strArray2); }