public void ContainsTest() { ContentType.Load(); Assert.IsTrue(ContentType.Contains("incl.patch")); Assert.IsTrue(ContentType.Contains("KEYGEN.ONLY")); Assert.IsFalse(ContentType.Contains("non-exist")); }
/// <summary> /// Initializes a new instance of the <see cref="WebRequest"/> class. /// </summary> /// <param name="uri">The uri<see cref="Uri"/>.</param> /// <param name="method">The method<see cref="string"/>.</param> /// <param name="headers">The headers<see cref="NameValueCollection"/>.</param> /// <param name="postData">The postData<see cref="byte[]"/>.</param> /// <param name="uploadFiles">The uploadFiles<see cref="string[]"/>.</param> /// <param name="cefRequest">The cefRequest<see cref="CefRequest"/>.</param> public WebRequest(Uri uri, string method, NameValueCollection headers, byte[] postData, string[] uploadFiles, CefRequest cefRequest) { Uri = uri; Method = method; Headers = headers; RawData = postData; UploadFiles = uploadFiles; RawRequest = cefRequest; QueryString = ProcessQueryString(uri.Query); if (ContentType != null && ContentType.Contains(CONTENT_TYPE_FORM_URL_ENCODED) && RawData != null) { FormData = ProcessFormData(RawData); } else { FormData = new NameValueCollection(); } if (IsJson && RawData != null) { try { JsonData = JsonValue.Parse(Encoding.UTF8.GetString(RawData)); } catch { JsonData = string.Empty; } } }
private void EnrichSiteWithTypes(SiteMetaData site, List <PageMetaData> pages) { var blockedTypes = new ContentType[] { ContentType.Unknown, ContentType.Page }; var types = pages .Where(x => !blockedTypes.Contains(x.Type)) .Select(x => x.Type) .Distinct(); foreach (var type in types) { var typeFiles = pages.Where(x => /*x.Type != null && */ x.Type.Equals(type)).ToArray(); site.Types.Add(type.ToString(), typeFiles); } }
public FileType IdentifyFileType(IFormFile file) { var fileType = file switch { var f when ".gif".Equals(Path.GetExtension(f.FileName), StringComparison.InvariantCultureIgnoreCase) => FileType.GIF, var f when f.ContentType.Contains("image", StringComparison.InvariantCultureIgnoreCase) => FileType.IMAGE, var f when f.ContentType.Contains("video", StringComparison.InvariantCultureIgnoreCase) => FileType.VIDEO, var f when f.ContentType.Contains("text", StringComparison.InvariantCultureIgnoreCase) => FileType.TEXT, var f when f.ContentType.Contains("audio", StringComparison.InvariantCultureIgnoreCase) => FileType.AUDIO, _ => FileType.UNKNOWN }; return(fileType); } }
public SystemHttpRequest(System.Net.HttpListenerRequest request) { _request = request; if (!String.IsNullOrEmpty(ContentType) && ContentType.Contains("multipart/form-data")) { multipartFormDataParser = MultipartFormDataParser.Parse(InputStream); _form = multipartFormDataParser.Parameters; _files = multipartFormDataParser.Files; } else { _form = new List <ParameterPart>(); _files = new List <FilePart>(); } }
/// <summary> /// Add Attachment to <see cref="AS4Message" /> /// </summary> /// <param name="attachment"></param> /// <exception cref="InvalidOperationException">Throws when there already exists an <see cref="Attachment"/> with the same id</exception> public void AddAttachment(Attachment attachment) { if (attachment == null) { throw new ArgumentNullException(nameof(attachment)); } if (!_attachmens.Contains(attachment)) { _attachmens.Add(attachment); if (!ContentType.Contains(Constants.ContentTypes.Mime)) { UpdateContentTypeHeader(); } } else { throw new InvalidOperationException( $"Cannot add attachment because there already exists an 'Attachment' with the Id={attachment.Id}"); } }
private bool IsRequiredGZip(object sendobject) { if (ContentType.Contains("image")) { return(false); } if (ContentType.Contains("video")) { return(false); } if (sendobject is byte[]) { return(((byte[])sendobject).Length >= 640); } var s = sendobject as Stream; try { return(s.Length >= 640); } catch { return(true); } }
public IEnumerable<Content> GetContents(string siteId, string category, out int total , bool includeSubCategories = false , bool? published = null, ContentType[] contentTypes = null, DateTime? startDate = null, DateTime? endDate = null , int pi = 0, int ps = 0) { IQueryable<Content> query = null; total = 0; if (includeSubCategories) { var obj = repoCategory.Query(o => o.SiteId == siteId && o.Code == category).FirstOrDefault(); if (obj != null) { query = from o in repoContent.Query(null) join cc in repoContentCategory.Query(null) on o.ContentId equals cc.ContentId join c in repoCategory.Query(null) on cc.CategoryId equals c.CategoryId where o.SiteId == siteId && o.CreateAsRelated == false && c.FlatId.StartsWith(obj.CategoryId) select o; } } else { query = from o in repoContent.Query(null) join cc in repoContentCategory.Query(null) on o.ContentId equals cc.ContentId join c in repoCategory.Query(null) on cc.CategoryId equals c.CategoryId where o.SiteId == siteId && o.CreateAsRelated == false && c.Code == category select o; } if (query == null) { return Enumerable.Empty<Content>(); } if (published.HasValue) { query = query.Where(o => o.Published == published.Value); if (published.Value) { if (startDate.HasValue) query = query.Where(o => o.PublishTime > startDate.Value); if (endDate.HasValue) query = query.Where(o => o.PublishTime < endDate.Value); } else { if (startDate.HasValue) query = query.Where(o => o.CreateTime > startDate.Value); if (endDate.HasValue) query = query.Where(o => o.CreateTime < endDate.Value); } } else { if (startDate.HasValue) query = query.Where(o => o.CreateTime > startDate.Value); if (endDate.HasValue) query = query.Where(o => o.CreateTime < endDate.Value); } if (contentTypes != null && contentTypes.Length > 0) query = query.Where(o => contentTypes.Contains(o.ContentType)); query = query.OrderBy(o => o.ShowOrder).ThenByDescending(o => o.PublishTime).ThenByDescending(o => o.CreateTime); total = query.Count(); if (pi >= 0 && ps > 0) { query = query.Skip(pi * ps).Take(ps); } return query; }
public override string ToString() { var cType = ContentType.Contains("App") ? "" : $"[{ContentType}]"; return(Toolbelt.RIC($"{cType}[{Region}] {Name}")); }
public LxwResponseHeader(string header, Action <string> CookieMethod = null) { Header = header; var headers = Regex.Split(header, "\r\n"); foreach (string key in headers) { if (key.StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase)) { var arrs = key.Split(' '); HttpVer = arrs[0]; Statue = int.Parse(arrs[1]); continue; } if (key.StartsWith("Content-Type:", StringComparison.OrdinalIgnoreCase)) { ContentType = key.Substring("Content-Type:".Length).Trim(); if (ContentType.Contains(";")) { var temp = ContentType.Split(';')[1].Trim(); if (temp.ToLower().Contains("Charset=")) { Charset = temp.Split('=')[0]; } ContentType = ContentType.Split(';')[0].Trim(); } continue; } //Set-Cookie: wxsid=; Domain=wx.qq.com; Path=/; Expires=Thu, 01-Jan-1970 00:00:30 GMT if (key.StartsWith("Set-Cookie:", StringComparison.OrdinalIgnoreCase)) { CookieMethod?.Invoke(key.Substring("Set-Cookie:".Length).Trim()); continue; } //Content-Encoding: gzip //deflate if (key.StartsWith("Content-Encoding:", StringComparison.OrdinalIgnoreCase)) { GZip = key.ToLower().Contains("gzip"); Deflate = key.ToLower().Contains("deflate"); continue; } if (key.StartsWith("Content-Length:", StringComparison.OrdinalIgnoreCase)) { ContentLength = int.Parse(key.Substring("Content-Length:".Length).Trim()); continue; } if (key.StartsWith("TRANSFER-ENCODING:", StringComparison.OrdinalIgnoreCase)) { TransferEncoding = key.Substring("TRANSFER-ENCODING:".Length).ToLower().Contains("chunked"); continue; } //Content-Disposition: attachment; filename="1.sql" if (key.StartsWith("Content-Disposition:", StringComparison.OrdinalIgnoreCase)) { var arr = Regex.Split(key, "filename=\"", RegexOptions.IgnoreCase); if (arr.Length > 1) { AttachmentFilername = arr[1].TrimEnd(new char[] { '"' }); try { AttachmentFilername = HttpUtility.UrlDecode(AttachmentFilername, Encoding.UTF8); } catch { } } continue; } } }
private HttpRequestConvertFormDataHanlder GetConvertFormDataHanlder() { var formDataType = FormData.GetType(); var key = new HttpConvertDataHanlderKey(formDataType, ContentType); HttpRequestConvertFormDataHanlder hanlder = null; if (_convertFormDataHanlders.ContainsKey(key)) { hanlder = _convertFormDataHanlders[key]; } if (hanlder == null) { var closestAncestor = formDataType.FindClosestAncestor(_convertFormDataHanlders.Where(t => ContentType.Contains(t.Key.ContentType, StringComparison.OrdinalIgnoreCase)).Select(t => t.Key.Type)); if (closestAncestor != null) { key = new HttpConvertDataHanlderKey(closestAncestor, ContentType); hanlder = _convertFormDataHanlders[key]; } } if (hanlder == null) { throw new Exception($"未注册ContentType为{ContentType},DataType为{formDataType.FullName}的序列化Hanlder"); } return(hanlder); }
private HttpReponseConvertDataHanlder GetConvertResultHanlder() { var key = new HttpConvertDataHanlderKey(typeof(TResult), ContentType); HttpReponseConvertDataHanlder hanlder = null; if (_convertHanlders.ContainsKey(key)) { hanlder = _convertHanlders[key]; } if (hanlder == null) { var closestAncestor = typeof(TResult).FindClosestAncestor(_convertHanlders.Where(t => ContentType.Contains(t.Key.ContentType, StringComparison.OrdinalIgnoreCase)).Select(t => t.Key.Type)); if (closestAncestor != null) { key = new HttpConvertDataHanlderKey(closestAncestor, ContentType); hanlder = _convertHanlders[key]; } } if (hanlder == null) { throw new Exception($"无法反序列化ContentType为{ContentType}的结果到{typeof(TResult).FullName},因为未注册Hanlder"); } return(hanlder); }
/// <summary> /// MEthod to change user picture /// </summary> public void ChangeUserPic(HttpListenerRequest Hrequest, string UserEmail) { string boundary = "--" + Hrequest.ContentType.Split(';')[1].Split('=')[1]; Stream inputStream = Hrequest.InputStream; Encoding contentEnc = Hrequest.ContentEncoding; Byte[] boundaryBytes = contentEnc.GetBytes(boundary); int boundaryLen = boundaryBytes.Length; using (FileStream output = new FileStream(Environment.CurrentDirectory + "/web/img/users/" + UserEmail + ".png", FileMode.Create, FileAccess.Write)) { Byte[] buffer = new Byte[1024]; int len = inputStream.Read(buffer, 0, 1024); int startPos = -1; string ContentType; // Find start pos while (true) { if (len == 0) { throw new Exception("Start Boundaray Not Found"); } startPos = IndexOf(buffer, len, boundaryBytes); if (startPos >= 0) { break; } else { Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen); len = inputStream.Read(buffer, boundaryLen, 1024 - boundaryLen); } } // Skip four lines (Boundary, Content-Disposition, Content-Type, and a blank) for (int i = 0; i < 4; i++) { int lastPos; byte[] ContentTypebyte; while (true) { if (len == 0) { throw new Exception("Preamble not Found."); } lastPos = startPos; startPos = Array.IndexOf(buffer, contentEnc.GetBytes("\n")[0], startPos); if (i == 2) { int masSize = startPos - lastPos - 1; ContentTypebyte = new byte[masSize]; Array.Copy(buffer, lastPos, ContentTypebyte, 0, masSize); ContentType = Encoding.UTF8.GetString(ContentTypebyte); if (ContentType.Contains("Content-Type:")) { ContentType = ContentType.Split('/')[1]; if (ContentType != "jpeg" && ContentType != "png") { break; } } else { return; } } if (startPos >= 0) { startPos++; break; } else { len = inputStream.Read(buffer, 0, 1024); } } } Array.Copy(buffer, startPos, buffer, 0, len - startPos); len = len - startPos; while (true) { int endPos = IndexOf(buffer, len, boundaryBytes); if (endPos >= 0) { if (endPos > 0) { output.Write(buffer, 0, endPos - 2); } break; } else if (len <= boundaryLen) { throw new Exception("End Boundaray Not Found"); } else { output.Write(buffer, 0, len - boundaryLen); Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen); len = inputStream.Read(buffer, boundaryLen, 1024 - boundaryLen) + boundaryLen; } } } int IndexOf(Byte[] buffer, int len, Byte[] boundarBytes) { for (int i = 0; i <= len - boundarBytes.Length; i++) { Boolean match = true; for (int j = 0; j < boundarBytes.Length && match; j++) { match = buffer[i + j] == boundarBytes[j]; } if (match) { return(i); } } return(-1); } }