private void SendHeaders(BinaryWriter stream) { SetHeader("Host", CurrentUri.Host); if (IsRedirected && !HasHeader("Referer")) { AddHeader("Referer", Uri.ToString()); } if (!HasHeader("Accept-Encoding")) { AddHeader("Accept-Encoding", "gzip, deflate, identity"); } if (!HasHeader("Connection")) { AddHeader("Connection", (!IsKeepAlive) ? "Close, TE" : "Keep-Alive, TE"); } if (!HasHeader("TE")) { AddHeader("TE", "chunked, identity"); } byte[] entityBody = GetEntityBody(); int num = (entityBody != null) ? entityBody.Length : 0; if (RawData == null) { byte[] array = ((object)FieldsImpl == null) ? null : FieldsImpl.get_data(); if (array != null && array.Length > 0 && !HasHeader("Content-Type")) { AddHeader("Content-Type", "application/x-www-form-urlencoded"); } } if (!HasHeader("Content-Length") && num != 0) { AddHeader("Content-Length", num.ToString()); } if (Credentials != null) { switch (Credentials.Type) { case AuthenticationTypes.Basic: SetHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(Credentials.UserName + ":" + Credentials.Password))); break; case AuthenticationTypes.Unknown: case AuthenticationTypes.Digest: { Digest digest = DigestStore.Get(CurrentUri); if (digest != null) { string value = digest.GenerateResponseHeader(this); if (!string.IsNullOrEmpty(value)) { SetHeader("Authorization", value); } } break; } } } foreach (KeyValuePair<string, List<string>> header in Headers) { byte[] aSCIIBytes = (header.Key + ": ").GetASCIIBytes(); for (int i = 0; i < header.Value.Count; i++) { stream.Write(aSCIIBytes); stream.Write(header.Value[i].GetASCIIBytes()); stream.Write(EOL); } } }
public void EnumerateHeaders(OnHeaderEnumerationDelegate callback) { if (!HasHeader("Host")) { SetHeader("Host", CurrentUri.Authority); } if (IsRedirected && !HasHeader("Referer")) { AddHeader("Referer", Uri.ToString()); } if (!HasHeader("Accept-Encoding")) { AddHeader("Accept-Encoding", "gzip, identity"); } if (HasProxy && !HasHeader("Proxy-Connection")) { AddHeader("Proxy-Connection", (!IsKeepAlive) ? "Close" : "Keep-Alive"); } if (!HasHeader("Connection")) { AddHeader("Connection", (!IsKeepAlive) ? "Close, TE" : "Keep-Alive, TE"); } if (!HasHeader("TE")) { AddHeader("TE", "identity"); } if (!HasHeader("User-Agent")) { AddHeader("User-Agent", "VRC.Core.BestHTTP"); } long num = -1L; if (UploadStream == null) { byte[] entityBody = GetEntityBody(); num = ((entityBody != null) ? entityBody.Length : 0); if (RawData == null && (FormImpl != null || (FieldCollector != null && !FieldCollector.IsEmpty))) { SelectFormImplementation(); if (FormImpl != null) { FormImpl.PrepareRequest(this); } } } else { num = UploadStreamLength; if (num == -1) { SetHeader("Transfer-Encoding", "Chunked"); } if (!HasHeader("Content-Type")) { SetHeader("Content-Type", "application/octet-stream"); } } if (num != -1 && !HasHeader("Content-Length")) { SetHeader("Content-Length", num.ToString()); } if (HasProxy && Proxy.Credentials != null) { switch (Proxy.Credentials.Type) { case AuthenticationTypes.Basic: SetHeader("Proxy-Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(Proxy.Credentials.UserName + ":" + Proxy.Credentials.Password))); break; case AuthenticationTypes.Unknown: case AuthenticationTypes.Digest: { Digest digest = DigestStore.Get(Proxy.Address); if (digest != null) { string value = digest.GenerateResponseHeader(this, Proxy.Credentials); if (!string.IsNullOrEmpty(value)) { SetHeader("Proxy-Authorization", value); } } break; } } } if (Credentials != null) { switch (Credentials.Type) { case AuthenticationTypes.Basic: SetHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(Credentials.UserName + ":" + Credentials.Password))); break; case AuthenticationTypes.Unknown: case AuthenticationTypes.Digest: { Digest digest2 = DigestStore.Get(CurrentUri); if (digest2 != null) { string value2 = digest2.GenerateResponseHeader(this, Credentials); if (!string.IsNullOrEmpty(value2)) { SetHeader("Authorization", value2); } } break; } } } List <Cookie> list = (!IsCookiesEnabled) ? null : CookieJar.Get(CurrentUri); if (list == null || list.Count == 0) { list = customCookies; } else if (customCookies != null) { for (int i = 0; i < customCookies.Count; i++) { Cookie customCookie = customCookies[i]; int num2 = list.FindIndex((Cookie c) => c.Name.Equals(customCookie.Name)); if (num2 >= 0) { list[num2] = customCookie; } else { list.Add(customCookie); } } } if (list != null && list.Count > 0) { bool flag = true; string text = string.Empty; bool flag2 = HTTPProtocolFactory.IsSecureProtocol(CurrentUri); SupportedProtocols protocolFromUri = HTTPProtocolFactory.GetProtocolFromUri(CurrentUri); foreach (Cookie item in list) { if ((!item.IsSecure || (item.IsSecure && flag2)) && (!item.IsHttpOnly || (item.IsHttpOnly && protocolFromUri == SupportedProtocols.HTTP))) { if (!flag) { text += "; "; } else { flag = false; } text += item.ToString(); item.LastAccess = DateTime.UtcNow; } } SetHeader("Cookie", text); } if (callback != null) { foreach (KeyValuePair <string, List <string> > header in Headers) { callback(header.Key, header.Value); } } }
private void Connect() { Uri uri = !base.CurrentRequest.HasProxy ? base.CurrentRequest.CurrentUri : base.CurrentRequest.Proxy.Address; if (this.Client == null) { this.Client = new TcpClient(); } if (!this.Client.Connected) { this.Client.ConnectTimeout = base.CurrentRequest.ConnectTimeout; if (HTTPManager.Logger.Level == Loglevels.All) { HTTPManager.Logger.Verbose("HTTPConnection", $"'{base.CurrentRequest.CurrentUri.ToString()}' - Connecting to {uri.Host}:{uri.Port.ToString()}"); } this.Client.Connect(uri.Host, uri.Port); if (HTTPManager.Logger.Level <= Loglevels.Information) { HTTPManager.Logger.Information("HTTPConnection", "Connected to " + uri.Host + ":" + uri.Port.ToString()); } } else if (HTTPManager.Logger.Level <= Loglevels.Information) { HTTPManager.Logger.Information("HTTPConnection", "Already connected to " + uri.Host + ":" + uri.Port.ToString()); } base.StartTime = DateTime.UtcNow; if (this.Stream == null) { bool flag = HTTPProtocolFactory.IsSecureProtocol(base.CurrentRequest.CurrentUri); this.Stream = this.Client.GetStream(); if (base.HasProxy && (!base.Proxy.IsTransparent || (flag && base.Proxy.NonTransparentForHTTPS))) { bool flag2; BinaryWriter stream = new BinaryWriter(this.Stream); do { flag2 = false; string str = $"CONNECT {base.CurrentRequest.CurrentUri.Host}:{base.CurrentRequest.CurrentUri.Port} HTTP/1.1"; HTTPManager.Logger.Information("HTTPConnection", "Sending " + str); stream.SendAsASCII(str); stream.Write(HTTPRequest.EOL); stream.SendAsASCII("Proxy-Connection: Keep-Alive"); stream.Write(HTTPRequest.EOL); stream.SendAsASCII("Connection: Keep-Alive"); stream.Write(HTTPRequest.EOL); stream.SendAsASCII($"Host: {base.CurrentRequest.CurrentUri.Host}:{base.CurrentRequest.CurrentUri.Port}"); stream.Write(HTTPRequest.EOL); if (base.HasProxy && (base.Proxy.Credentials != null)) { switch (base.Proxy.Credentials.Type) { case AuthenticationTypes.Basic: stream.Write($"Proxy-Authorization: {("Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(base.Proxy.Credentials.UserName + ":" + base.Proxy.Credentials.Password)))}".GetASCIIBytes()); stream.Write(HTTPRequest.EOL); break; case AuthenticationTypes.Unknown: case AuthenticationTypes.Digest: { Digest digest = DigestStore.Get(base.Proxy.Address); if (digest != null) { string str2 = digest.GenerateResponseHeader(base.CurrentRequest, base.Proxy.Credentials); if (!string.IsNullOrEmpty(str2)) { stream.Write($"Proxy-Authorization: {str2}".GetASCIIBytes()); stream.Write(HTTPRequest.EOL); } } break; } } } stream.Write(HTTPRequest.EOL); stream.Flush(); base.CurrentRequest.ProxyResponse = new HTTPResponse(base.CurrentRequest, this.Stream, false, false); if (!base.CurrentRequest.ProxyResponse.Receive(-1, true)) { throw new Exception("Connection to the Proxy Server failed!"); } if (HTTPManager.Logger.Level <= Loglevels.Information) { object[] objArray1 = new object[] { "Proxy returned - status code: ", base.CurrentRequest.ProxyResponse.StatusCode, " message: ", base.CurrentRequest.ProxyResponse.Message }; HTTPManager.Logger.Information("HTTPConnection", string.Concat(objArray1)); } if (base.CurrentRequest.ProxyResponse.StatusCode == 0x197) { string str3 = DigestStore.FindBest(base.CurrentRequest.ProxyResponse.GetHeaderValues("proxy-authenticate")); if (!string.IsNullOrEmpty(str3)) { Digest orCreate = DigestStore.GetOrCreate(base.Proxy.Address); orCreate.ParseChallange(str3); if (((base.Proxy.Credentials != null) && orCreate.IsUriProtected(base.Proxy.Address)) && (!base.CurrentRequest.HasHeader("Proxy-Authorization") || orCreate.Stale)) { flag2 = true; } } } else if (!base.CurrentRequest.ProxyResponse.IsSuccess) { throw new Exception($"Proxy returned Status Code: " { base.CurrentRequest.ProxyResponse.StatusCode } ", Message: " { base.CurrentRequest.ProxyResponse.Message } " and Response: {base.CurrentRequest.ProxyResponse.DataAsText}"); } }while (flag2); } if (flag) { if (base.CurrentRequest.UseAlternateSSL) { TlsClientProtocol protocol = new TlsClientProtocol(this.Client.GetStream(), new SecureRandom()); List <string> hostNames = new List <string>(1) { base.CurrentRequest.CurrentUri.Host }; protocol.Connect(new LegacyTlsClient(base.CurrentRequest.CurrentUri, (base.CurrentRequest.CustomCertificateVerifyer != null) ? base.CurrentRequest.CustomCertificateVerifyer : new AlwaysValidVerifyer(), base.CurrentRequest.CustomClientCredentialsProvider, hostNames)); this.Stream = protocol.Stream; } else { SslStream stream = new SslStream(this.Client.GetStream(), false, (sender, cert, chain, errors) => base.CurrentRequest.CallCustomCertificationValidator(cert, chain)); if (!stream.IsAuthenticated) { stream.AuthenticateAsClient(base.CurrentRequest.CurrentUri.Host); } this.Stream = stream; } } } }
private void SendHeaders(BinaryWriter stream) { if (!this.HasHeader("Host")) { this.SetHeader("Host", this.CurrentUri.Authority); } if (this.IsRedirected && !this.HasHeader("Referer")) { this.AddHeader("Referer", this.Uri.ToString()); } if (!this.HasHeader("Accept-Encoding")) { this.AddHeader("Accept-Encoding", "gzip, identity"); } if (this.HasProxy && !this.HasHeader("Proxy-Connection")) { this.AddHeader("Proxy-Connection", (!this.IsKeepAlive) ? "Close" : "Keep-Alive"); } if (!this.HasHeader("Connection")) { this.AddHeader("Connection", (!this.IsKeepAlive) ? "Close, TE" : "Keep-Alive, TE"); } if (!this.HasHeader("TE")) { this.AddHeader("TE", "identity"); } if (!this.HasHeader("User-Agent")) { this.AddHeader("User-Agent", "BestHTTP"); } long num; if (this.UploadStream == null) { byte[] entityBody = this.GetEntityBody(); num = (long)((entityBody == null) ? 0 : entityBody.Length); if (this.RawData == null && (this.FormImpl != null || (this.FieldCollector != null && !this.FieldCollector.IsEmpty))) { this.SelectFormImplementation(); if (this.FormImpl != null) { this.FormImpl.PrepareRequest(this); } } } else { num = this.UploadStreamLength; if (num == -1L) { this.SetHeader("Transfer-Encoding", "Chunked"); } if (!this.HasHeader("Content-Type")) { this.SetHeader("Content-Type", "application/octet-stream"); } } if (num != -1L && !this.HasHeader("Content-Length")) { this.SetHeader("Content-Length", num.ToString()); } if (this.HasProxy && this.Proxy.Credentials != null) { switch (this.Proxy.Credentials.Type) { case AuthenticationTypes.Unknown: case AuthenticationTypes.Digest: { Digest digest = DigestStore.Get(this.Proxy.Address); if (digest != null) { string value = digest.GenerateResponseHeader(this, this.Proxy.Credentials); if (!string.IsNullOrEmpty(value)) { this.SetHeader("Proxy-Authorization", value); } } break; } case AuthenticationTypes.Basic: this.SetHeader("Proxy-Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(this.Credentials.UserName + ":" + this.Credentials.Password))); break; } } if (this.Credentials != null) { switch (this.Credentials.Type) { case AuthenticationTypes.Unknown: case AuthenticationTypes.Digest: { Digest digest2 = DigestStore.Get(this.CurrentUri); if (digest2 != null) { string value2 = digest2.GenerateResponseHeader(this, this.Credentials); if (!string.IsNullOrEmpty(value2)) { this.SetHeader("Authorization", value2); } } break; } case AuthenticationTypes.Basic: this.SetHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(this.Credentials.UserName + ":" + this.Credentials.Password))); break; } } List <Cookie> list = (!this.IsCookiesEnabled) ? null : CookieJar.Get(this.CurrentUri); if (list == null) { list = this.customCookies; } else if (this.customCookies != null) { list.AddRange(this.customCookies); } if (list != null && list.Count > 0) { bool flag = true; string text = string.Empty; bool flag2 = HTTPProtocolFactory.IsSecureProtocol(this.CurrentUri); SupportedProtocols protocolFromUri = HTTPProtocolFactory.GetProtocolFromUri(this.CurrentUri); foreach (Cookie current in list) { if ((!current.IsSecure || (current.IsSecure && flag2)) && (!current.IsHttpOnly || (current.IsHttpOnly && protocolFromUri == SupportedProtocols.HTTP))) { if (!flag) { text += "; "; } else { flag = false; } text += current.ToString(); current.LastAccess = DateTime.UtcNow; } } this.SetHeader("Cookie", text); } foreach (KeyValuePair <string, List <string> > current2 in this.Headers) { byte[] aSCIIBytes = (current2.Key + ": ").GetASCIIBytes(); for (int i = 0; i < current2.Value.Count; i++) { stream.Write(aSCIIBytes); stream.Write(current2.Value[i].GetASCIIBytes()); stream.Write(HTTPRequest.EOL); } } }
private void Connect() { Uri uri = (!base.CurrentRequest.HasProxy) ? base.CurrentRequest.CurrentUri : base.CurrentRequest.Proxy.Address; if (Client == null) { Client = new TcpClient(); } if (!Client.Connected) { Client.ConnectTimeout = base.CurrentRequest.ConnectTimeout; Client.Connect(uri.Host, uri.Port); if (HTTPManager.Logger.Level <= Loglevels.Information) { HTTPManager.Logger.Information("HTTPConnection", "Connected to " + uri.Host + ":" + uri.Port.ToString()); } } else if (HTTPManager.Logger.Level <= Loglevels.Information) { HTTPManager.Logger.Information("HTTPConnection", "Already connected to " + uri.Host + ":" + uri.Port.ToString()); } lock (HTTPManager.Locker) { base.StartTime = DateTime.UtcNow; } if (Stream == null) { bool flag = HTTPProtocolFactory.IsSecureProtocol(base.CurrentRequest.CurrentUri); if (base.HasProxy && (!base.Proxy.IsTransparent || (flag && base.Proxy.NonTransparentForHTTPS))) { Stream = Client.GetStream(); BinaryWriter binaryWriter = new BinaryWriter(Stream); bool flag2; do { flag2 = false; binaryWriter.SendAsASCII($"CONNECT {base.CurrentRequest.CurrentUri.Host}:{base.CurrentRequest.CurrentUri.Port} HTTP/1.1"); binaryWriter.Write(HTTPRequest.EOL); binaryWriter.SendAsASCII("Proxy-Connection: Keep-Alive"); binaryWriter.Write(HTTPRequest.EOL); binaryWriter.SendAsASCII("Connection: Keep-Alive"); binaryWriter.Write(HTTPRequest.EOL); binaryWriter.SendAsASCII($"Host: {base.CurrentRequest.CurrentUri.Host}:{base.CurrentRequest.CurrentUri.Port}"); binaryWriter.Write(HTTPRequest.EOL); if (base.HasProxy && base.Proxy.Credentials != null) { switch (base.Proxy.Credentials.Type) { case AuthenticationTypes.Basic: binaryWriter.Write(string.Format("Proxy-Authorization: {0}", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(base.Proxy.Credentials.UserName + ":" + base.Proxy.Credentials.Password))).GetASCIIBytes()); binaryWriter.Write(HTTPRequest.EOL); break; case AuthenticationTypes.Unknown: case AuthenticationTypes.Digest: { Digest digest = DigestStore.Get(base.Proxy.Address); if (digest != null) { string text = digest.GenerateResponseHeader(base.CurrentRequest, base.Proxy.Credentials); if (!string.IsNullOrEmpty(text)) { binaryWriter.Write($"Proxy-Authorization: {text}".GetASCIIBytes()); binaryWriter.Write(HTTPRequest.EOL); } } break; } } } binaryWriter.Write(HTTPRequest.EOL); binaryWriter.Flush(); base.CurrentRequest.ProxyResponse = new HTTPResponse(base.CurrentRequest, Stream, isStreamed: false, isFromCache: false); if (!base.CurrentRequest.ProxyResponse.Receive()) { throw new Exception("Connection to the Proxy Server failed!"); } if (HTTPManager.Logger.Level <= Loglevels.Information) { HTTPManager.Logger.Information("HTTPConnection", "Proxy returned - status code: " + base.CurrentRequest.ProxyResponse.StatusCode + " message: " + base.CurrentRequest.ProxyResponse.Message); } int statusCode = base.CurrentRequest.ProxyResponse.StatusCode; if (statusCode == 407) { string text2 = DigestStore.FindBest(base.CurrentRequest.ProxyResponse.GetHeaderValues("proxy-authenticate")); if (!string.IsNullOrEmpty(text2)) { Digest orCreate = DigestStore.GetOrCreate(base.Proxy.Address); orCreate.ParseChallange(text2); if (base.Proxy.Credentials != null && orCreate.IsUriProtected(base.Proxy.Address) && (!base.CurrentRequest.HasHeader("Proxy-Authorization") || orCreate.Stale)) { flag2 = true; } } } else if (!base.CurrentRequest.ProxyResponse.IsSuccess) { throw new Exception($"Proxy returned Status Code: \"{base.CurrentRequest.ProxyResponse.StatusCode}\", Message: \"{base.CurrentRequest.ProxyResponse.Message}\" and Response: {base.CurrentRequest.ProxyResponse.DataAsText}"); } }while (flag2); } if (flag) { if (base.CurrentRequest.UseAlternateSSL) { TlsClientProtocol tlsClientProtocol = new TlsClientProtocol(Client.GetStream(), new SecureRandom()); List <string> list = new List <string>(1); list.Add(base.CurrentRequest.CurrentUri.Host); TlsClientProtocol tlsClientProtocol2 = tlsClientProtocol; Uri currentUri = base.CurrentRequest.CurrentUri; object verifyer; if (base.CurrentRequest.CustomCertificateVerifyer == null) { ICertificateVerifyer certificateVerifyer = new AlwaysValidVerifyer(); verifyer = certificateVerifyer; } else { verifyer = base.CurrentRequest.CustomCertificateVerifyer; } tlsClientProtocol2.Connect(new LegacyTlsClient(currentUri, (ICertificateVerifyer)verifyer, base.CurrentRequest.CustomClientCredentialsProvider, list)); Stream = tlsClientProtocol.Stream; } else { SslStream sslStream = new SslStream(Client.GetStream(), leaveInnerStreamOpen: false, (object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors) => base.CurrentRequest.CallCustomCertificationValidator(cert, chain)); if (!sslStream.IsAuthenticated) { sslStream.AuthenticateAsClient(base.CurrentRequest.CurrentUri.Host); } Stream = sslStream; } } else { Stream = Client.GetStream(); } } }