public static void InsertHeaderFieldIfNeed(StringCollection source, string name, string theValue) { if (!StringUtils.IsEmpty(theValue)) { HeaderFieldList fieldList = new HeaderFieldList(); int index = GetHeaderFieldList(0, source, fieldList); if (!fieldList.ContainsField(name)) { if ((index < 0) || (index > source.Count)) { index = source.Count; } theValue = theValue.Replace("\r\n", "\r\n\t"); if (theValue[theValue.Length - 1] == '\t') { theValue = theValue.Substring(0, theValue.Length - 1); } foreach (string str in StringUtils.GetStringArray(string.Format("{0}: {1}", name, theValue))) { source.Insert(index, str); index++; } } } }
protected virtual void ParseResponseCookie(string cookieData) { if (!StringUtils.IsEmpty(cookieData)) { string str = cookieData; string headerFieldValueItem = ""; int index = cookieData.IndexOf("="); if (index > -1) { str = cookieData.Substring(0, index); headerFieldValueItem = HeaderFieldList.GetHeaderFieldValueItem(cookieData, str.ToLower(CultureInfo.InvariantCulture) + "="); } if (this[str] == null) { CookieItem item = new CookieItem(); this.Add(item); item.Name = str; item.Value = headerFieldValueItem; item.Expires = this.GetExpires(cookieData); item.Domain = HeaderFieldList.GetHeaderFieldValueItem(cookieData, "domain="); item.Path = HeaderFieldList.GetHeaderFieldValueItem(cookieData, "path="); item.Secure = cookieData.ToLower(CultureInfo.InvariantCulture).IndexOf("secure") > -1; item.CookieData = cookieData; } } }
private bool Redirect(StringCollectionEx requestHeader, string[] responseText, ref string method) { string location = ResponseHeader.Location; var e = new HttpRedirectEventArgs(requestHeader.ToArray(), statusCode, ResponseHeader, responseText, method, false, false); string resourcePath = GetResourcePath(); OnRedirecting(e); method = e.Method; if (e.Handled || StringUtils.IsEmpty(location)) { return(e.CanRedirect); } url.Parse(Url.Url, location); if (StringUtils.IsEmpty(Url.AbsoluteUri)) { return(false); } method = GetRedirectMethod(StatusCode, method); if (GetResourcePath().IndexOf(resourcePath) != 0) { var fieldList = new HeaderFieldList(); HeaderFieldList.GetHeaderFieldList(0, requestHeader, fieldList); HeaderFieldList.RemoveHeaderField(requestHeader, fieldList, "Authorization"); } return(true); }
public void ParseHeader(IList header) { this.BeginUpdate(); try { this.Clear(); HeaderFieldList fieldList = new HeaderFieldList(); HeaderFieldList.GetHeaderFieldList(0, header, fieldList); this.InternalParseHeader(header, fieldList); StringCollectionEx ex = new StringCollectionEx(); foreach (HeaderField field in fieldList) { if (!this.knownFields.Contains(field.Name, true)) { ex.Add(field.Name + ": " + HeaderFieldList.GetHeaderFieldValue(header, field)); } } if (ex.Count > 0) { this.ExtraFields = ex.ToArray(); } } finally { this.EndUpdate(); } }
protected virtual void ParseContentType(IList header, HeaderFieldList fieldList) { string source = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Content-Type"); this.ContentType = HeaderFieldList.GetHeaderFieldValueItem(source, string.Empty); this.Boundary = HeaderFieldList.GetHeaderFieldValueItem(source, "boundary="); this.CharSet = HeaderFieldList.GetHeaderFieldValueItem(source, "charset="); }
protected internal override void ParseHeader(IList header, HeaderFieldList fieldList) { base.ParseHeader(header, fieldList); this.ContentType = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Content-Type"); string source = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Content-Disposition"); this.FieldName = HeaderFieldList.GetHeaderFieldValueItem(source, "name="); }
private void SetAuthChallenge(StringCollection header, string fieldName, IEnumerable authChallenge) { if (authChallenge != null) { foreach (string str in authChallenge) { HeaderFieldList.AddHeaderField(header, fieldName, str); } } }
private string GetExpires(string cookieData) { int index = cookieData.ToLower(CultureInfo.InvariantCulture).IndexOf("expires="); if (index > -1) { return(HeaderFieldList.GetHeaderFieldValueItem(cookieData.Substring(index + "expires=".Length).Replace(",", "=="), string.Empty).Replace("==", ",")); } return(string.Empty); }
private void ProcessCookies(StringCollection responseHeader, HeaderFieldList fieldList, string fieldName) { foreach (HeaderField field in fieldList) { if (string.Compare(field.Name, fieldName, true, CultureInfo.InvariantCulture) == 0) { this.ParseResponseCookie(HeaderFieldList.GetHeaderFieldValue(responseHeader, field)); } } }
protected virtual void AssignContentType(StringCollection header) { string contentType = this.ContentType; if (!StringUtils.IsEmpty(contentType)) { contentType = AddHttpFieldItem(AddHttpFieldItem(contentType, "boundary", this.Boundary), "charset", this.CharSet); HeaderFieldList.AddHeaderField(header, "Content-Type", contentType); } }
private HttpRequestItem CreateMultiPartItem(string header) { ClearDataStream(); string[] stringArray = StringUtils.GetStringArray(header); var fieldList = new HeaderFieldList(); HeaderFieldList.GetHeaderFieldList(0, stringArray, fieldList); HttpRequestItem item = CreateItem(stringArray, fieldList); item.ParseHeader(stringArray, fieldList); return(item); }
private string[] GetAuthChallenge(IList header, HeaderFieldList fieldList, string fieldName) { ArrayList list = new ArrayList(); foreach (HeaderField field in fieldList) { if (string.Compare(fieldName, field.Name, true, CultureInfo.InvariantCulture) == 0) { list.Add(HeaderFieldList.GetHeaderFieldValue(header, field)); } } return((string[])list.ToArray(typeof(string))); }
protected override void InternalAssignHeader(StringCollection header) { HeaderFieldList.AddHeaderField(header, "Allow", this.Allow); HeaderFieldList.AddHeaderField(header, "Accept-Ranges", this.AcceptRanges); HeaderFieldList.AddHeaderField(header, "Age", this.Age); HeaderFieldList.AddHeaderField(header, "Content-Range", this.ContentRange); HeaderFieldList.AddHeaderField(header, "ETag", this.ETag); HeaderFieldList.AddHeaderField(header, "Location", this.Location); HeaderFieldList.AddHeaderField(header, "Retry-After", this.RetryAfter); HeaderFieldList.AddHeaderField(header, "Server", this.Server); this.SetAuthChallenge(header, "WWW-Authenticate", this.Authenticate); this.SetAuthChallenge(header, "Proxy-Authenticate", this.ProxyAuthenticate); base.InternalAssignHeader(header); }
protected override void InternalParseHeader(IList header, HeaderFieldList fieldList) { base.InternalParseHeader(header, fieldList); this.AcceptRanges = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Accept-Ranges"); this.Age = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Age"); this.Allow = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Allow"); this.ContentRange = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Content-Range"); this.ETag = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "ETag"); this.Location = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Location"); this.RetryAfter = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Retry-After"); this.Server = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Server"); this.Authenticate = this.GetAuthChallenge(header, fieldList, "WWW-Authenticate"); this.ProxyAuthenticate = this.GetAuthChallenge(header, fieldList, "Proxy-Authenticate"); }
protected override void InternalParseHeader(IList header, HeaderFieldList fieldList) { base.InternalParseHeader(header, fieldList); this.Accept = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Accept"); this.AcceptCharSet = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Accept-Charset"); this.AcceptEncoding = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Accept-Encoding"); this.AcceptLanguage = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Accept-Language"); this.Authorization = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Authorization"); this.Host = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Host"); this.ProxyAuthorization = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Proxy-Authorization"); this.Range = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Range"); this.Referer = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Referer"); this.UserAgent = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "User-Agent"); }
private void RemoveCookies(StringCollection requestHeader) { HeaderFieldList fieldList = new HeaderFieldList(); HeaderFieldList.GetHeaderFieldList(0, requestHeader, fieldList); for (int i = fieldList.Count - 1; i >= 0; i--) { HeaderField field = fieldList[i]; if (string.Compare(field.Name, "cookie", true, CultureInfo.InvariantCulture) == 0) { HeaderFieldList.RemoveHeaderField(requestHeader, fieldList, field); } } }
protected override void InternalAssignHeader(StringCollection header) { HeaderFieldList.AddHeaderField(header, "Accept", this.Accept); HeaderFieldList.AddHeaderField(header, "Accept-Charset", this.AcceptCharSet); HeaderFieldList.AddHeaderField(header, "Accept-Encoding", this.AcceptEncoding); HeaderFieldList.AddHeaderField(header, "Accept-Language", this.AcceptLanguage); HeaderFieldList.AddHeaderField(header, "Range", this.Range); HeaderFieldList.AddHeaderField(header, "Referer", this.Referer); HeaderFieldList.AddHeaderField(header, "Host", this.Host); HeaderFieldList.AddHeaderField(header, "User-Agent", this.UserAgent); HeaderFieldList.AddHeaderField(header, "Authorization", this.Authorization); HeaderFieldList.AddHeaderField(header, "Proxy-Authorization", this.ProxyAuthorization); base.InternalAssignHeader(header); }
public void SetRequestCookies(StringCollection requestHeader) { this.RemoveCookies(requestHeader); string str = ""; foreach (CookieItem item in this) { str = str + this.BuildRequestCookie(item) + "; "; } if (!StringUtils.IsEmpty(str)) { str = str.Substring(0, str.Length - "; ".Length); } HeaderFieldList.AddHeaderField(requestHeader, "Cookie", str); }
public void GetResponseCookies(StringCollection responseHeader) { base.Clear(); HeaderFieldList fieldList = new HeaderFieldList(); HeaderFieldList.GetHeaderFieldList(0, responseHeader, fieldList); if (!StringUtils.IsEmpty(HeaderFieldList.GetHeaderFieldValue(responseHeader, fieldList, "set-cookie2"))) { this.ProcessCookies(responseHeader, fieldList, "set-cookie2"); } else { this.ProcessCookies(responseHeader, fieldList, "set-cookie"); } }
protected virtual void InternalParseHeader(IList header, HeaderFieldList fieldList) { this.CacheControl = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Cache-Control"); this.Connection = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Connection"); this.ContentEncoding = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Content-Encoding"); this.ContentLanguage = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Content-Language"); this.ContentLength = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Content-Length"); this.ContentVersion = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Content-Version"); this.Date = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Date"); this.Expires = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Expires"); this.LastModified = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Last-Modified"); this.ProxyConnection = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Proxy-Connection"); this.TransferEncoding = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Transfer-Encoding"); this.ParseContentType(header, fieldList); }
public static void RemoveHeaderField(StringCollection source, HeaderFieldList fieldList, HeaderField field) { if (field != null) { fieldList.Remove(field); int index = int.Parse(field.Value); source.RemoveAt(index); while (index < source.Count) { if (StringUtils.IsEmpty(source[index]) || ((source[index][0] != '\t') && (source[index][0] != ' '))) { break; } source.RemoveAt(index); } } }
protected virtual HttpRequestItem CreateItem(IList header, HeaderFieldList fieldList) { string source = HeaderFieldList.GetHeaderFieldValue(header, fieldList, "Content-Disposition"); if ( !HeaderFieldList.GetHeaderFieldValueItem(source, "").ToLower(CultureInfo.InvariantCulture).Equals( "form-data")) { return(Items.AddTextData("")); } string headerFieldValueItem = HeaderFieldList.GetHeaderFieldValueItem(source, "filename="); if (!StringUtils.IsEmpty(headerFieldValueItem)) { return(Items.AddSubmitFile(headerFieldValueItem, "")); } return(Items.AddFormField(HeaderFieldList.GetHeaderFieldValueItem(source, "name="), string.Empty)); }
protected virtual void InternalAssignHeader(StringCollection header) { HeaderFieldList.AddHeaderField(header, "Content-Encoding", this.ContentEncoding); HeaderFieldList.AddHeaderField(header, "Content-Language", this.ContentLanguage); HeaderFieldList.AddHeaderField(header, "Content-Length", this.ContentLength); HeaderFieldList.AddHeaderField(header, "Content-Version", this.ContentVersion); this.AssignContentType(header); HeaderFieldList.AddHeaderField(header, "Date", this.Date); HeaderFieldList.AddHeaderField(header, "Expires", this.Expires); HeaderFieldList.AddHeaderField(header, "LastModified", this.LastModified); HeaderFieldList.AddHeaderField(header, "Transfer-Encoding", this.TransferEncoding); HeaderFieldList.AddHeaderField(header, "Cache-Control", this.CacheControl); HeaderFieldList.AddHeaderField(header, "Connection", this.Connection); HeaderFieldList.AddHeaderField(header, "Proxy-Connection", this.ProxyConnection); if (this.ExtraFields != null) { header.AddRange(this.ExtraFields); } }
public static void RemoveHeaderField(ArrayList source, HeaderFieldList fieldList, string name) { HeaderField field = fieldList[name]; if (field != null) { fieldList.Remove(field); int index = int.Parse(field.Value); source.RemoveAt(index); while (index < source.Count) { string str = (string)source[index]; if (StringUtils.IsEmpty(str) || ((str[0] != '\t') && (str[0] != ' '))) { break; } source.RemoveAt(index); } } }
public static int GetHeaderFieldList(int startFrom, IList source, HeaderFieldList fieldList) { if (source == null) { return(startFrom); } for (int i = startFrom; i < source.Count; i++) { string str = (string)source[i]; if (StringUtils.IsEmpty(str)) { return(i); } if ((str[0] != '\t') && (str[0] != ' ')) { int index = str.IndexOf(":"); if (index >= 0) { fieldList.Add(str.Substring(0, index), i.ToString(CultureInfo.InvariantCulture)); } } } return(source.Count); }
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(""); }
public static string GetHeaderFieldValue(IList source, HeaderFieldList fieldList, string name) { return(GetHeaderFieldValue(source, fieldList[name.ToLower(CultureInfo.InvariantCulture)])); }
public static void RemoveHeaderField(StringCollection source, HeaderFieldList fieldList, string name) { RemoveHeaderField(source, fieldList, fieldList[name]); }
protected internal virtual void ParseHeader(IList header, HeaderFieldList fieldList) { }