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); } } }
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); } } }
/// <summary> /// Writes out the Headers to the stream. /// </summary> /// <param name="stream"></param> private void SendHeaders(BinaryWriter stream) { if (!HasHeader("Host")) { SetHeader("Host", CurrentUri.Host); } 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 ? "Keep-Alive" : "Close"); } if (!HasHeader("Connection")) { AddHeader("Connection", IsKeepAlive ? "Keep-Alive, TE" : "Close, TE"); } if (!HasHeader("TE")) { AddHeader("TE", "identity"); } if (!HasHeader("User-Agent")) { AddHeader("User-Agent", "BestHTTP"); } byte[] entityBody = GetEntityBody(); int contentLength = entityBody != null ? entityBody.Length : 0; if (RawData == null && (FormImpl != null || (FieldCollector != null && !FieldCollector.IsEmpty))) { SelectFormImplementation(); if (FormImpl != null) { FormImpl.PrepareRequest(this); } } if (!HasHeader("Content-Length") && contentLength != 0) { AddHeader("Content-Length", contentLength.ToString()); } // Proxy Authentication if (HasProxy && Proxy.Credentials != null) { switch (Proxy.Credentials.Type) { case AuthenticationTypes.Basic: // With Basic authentication we don't want to wait for a challange, we will send the hash with the first request SetHeader("Proxy-Authorization", string.Concat("Basic ", Convert.ToBase64String(Encoding.UTF8.GetBytes(Credentials.UserName + ":" + Credentials.Password)))); break; case AuthenticationTypes.Unknown: case AuthenticationTypes.Digest: var digest = DigestStore.Get(Proxy.Address); if (digest != null) { string authentication = digest.GenerateResponseHeader(this, Proxy.Credentials); if (!string.IsNullOrEmpty(authentication)) { SetHeader("Proxy-Authorization", authentication); } } break; } } // Server authentication if (Credentials != null) { switch (Credentials.Type) { case AuthenticationTypes.Basic: // With Basic authentication we don't want to wait for a challange, we will send the hash with the first request SetHeader("Authorization", string.Concat("Basic ", Convert.ToBase64String(Encoding.UTF8.GetBytes(Credentials.UserName + ":" + Credentials.Password)))); break; case AuthenticationTypes.Unknown: case AuthenticationTypes.Digest: var digest = DigestStore.Get(this.CurrentUri); if (digest != null) { string authentication = digest.GenerateResponseHeader(this, Credentials); if (!string.IsNullOrEmpty(authentication)) { SetHeader("Authorization", authentication); } } break; } } // Cookies if (IsCookiesEnabled) { var cookies = CookieJar.Get(CurrentUri); // http://tools.ietf.org/html/rfc6265#section-5.4 // -When the user agent generates an HTTP request, the user agent MUST NOT attach more than one Cookie header field. if (cookies != null && cookies.Count > 0) { // TODO: // 2. The user agent SHOULD sort the cookie-list in the following order: // * Cookies with longer paths are listed before cookies with shorter paths. // * Among cookies that have equal-length path fields, cookies with earlier creation-times are listed before cookies with later creation-times. bool first = true; string cookieStr = string.Empty; foreach (var cookie in cookies) { if ((!cookie.IsSecure || (cookie.IsSecure && HTTPProtocolFactory.IsSecureProtocol(CurrentUri))) && (!cookie.IsHttpOnly || (cookie.IsHttpOnly && HTTPProtocolFactory.GetProtocolFromUri(CurrentUri) == SupportedProtocols.HTTP))) { if (!first) { cookieStr += "; "; } else { first = false; } cookieStr += cookie.ToString(); // 3. Update the last-access-time of each cookie in the cookie-list to the current date and time. cookie.LastAccess = DateTime.UtcNow; } } SetHeader("Cookie", cookieStr); } } // Write out the headers to the stream foreach (var kvp in Headers) { byte[] headerName = string.Concat(kvp.Key, ": ").GetASCIIBytes(); for (int i = 0; i < kvp.Value.Count; ++i) { stream.Write(headerName); stream.Write(kvp.Value[i].GetASCIIBytes()); stream.Write(EOL); } } }