//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有 // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权 // CSDN博客:https://blog.csdn.net/qq_40374647 // 哔哩哔哩视频:https://space.bilibili.com/94253567 // Gitee源代码仓库:https://gitee.com/RRQM_Home // Github源代码仓库:https://github.com/RRQM // API首页:https://www.yuque.com/rrqm/touchsocket/index // 交流QQ群:234762506 // 感谢您的下载和使用 //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ using System; using System.IO; using System.Text; using TouchSocket.Core; using TouchSocket.Sockets; namespace TouchSocket.Http { /// /// Http扩展辅助 /// public static class HttpExtensions { /// /// 根据字符串获取枚举 /// /// /// /// /// public static bool GetEnum(string str, out T result) where T : struct { return Enum.TryParse(str, out result); } #region HttpBase #region 设置内容 /// /// 从Json /// /// /// /// public static T FromJson(this T httpRequest, string value) where T : HttpBase { httpRequest.SetContent(Encoding.UTF8.GetBytes(value)); httpRequest.SetHeader(HttpHeaders.ContentType, "application/json;charset=UTF-8"); return httpRequest; } /// /// 从文本 /// /// /// /// public static T FromText(this T httpRequest, string value) where T : HttpBase { httpRequest.SetContent(Encoding.UTF8.GetBytes(value)); httpRequest.SetHeader(HttpHeaders.ContentType, "text/plain;charset=UTF-8"); return httpRequest; } /// /// 从Xml格式 /// /// /// /// public static T FromXML(this T httpRequest, string value) where T : HttpBase { httpRequest.SetContent(Encoding.UTF8.GetBytes(value)); httpRequest.SetHeader(HttpHeaders.ContentType, "application/xml;charset=UTF-8"); return httpRequest; } #endregion 设置内容 /// /// 获取Body的字符串 /// /// /// public static string GetBody(this HttpBase httpBase) { if (httpBase.TryGetContent(out byte[] data)) { return Encoding.UTF8.GetString(data); } throw new Exception("获取数据体错误。"); } /// /// 当数据类型为multipart/form-data时,获取boundary /// /// /// /// public static string GetBoundary(this HttpBase httpBase) { if (httpBase.ContentType.IsNullOrEmpty()) { return string.Empty; } string[] strs = httpBase.ContentType.Split(';'); if (strs.Length == 2) { strs = strs[1].Split('='); if (strs.Length == 2) { return strs[1].Trim(); } } return string.Empty; } /// /// 设置内容 /// /// /// /// /// public static T SetContent(this T httpBase, string content, Encoding encoding = null) where T : HttpBase { httpBase.SetContent(Encoding.UTF8.GetBytes(content)); return httpBase; } /// /// 设置数据体长度 /// /// /// public static T SetContentLength(this T httpBase, long value) where T : HttpBase { httpBase.ContentLength = value; return httpBase; } /// /// 从扩展名设置内容类型,必须以“.”开头 /// /// /// /// public static T SetContentTypeByExtension(this T httpBase, string extension) where T : HttpBase { string type = HttpTools.GetContentTypeFromExtension(extension); httpBase.SetHeader(HttpHeaders.ContentType.GetDescription(), type); httpBase.ContentType = type; return httpBase; } /// /// 设置头值 /// /// /// /// public static T SetHeader(this T httpBase, HttpHeaders header, string value) where T : HttpBase { httpBase.SetHeaderByKey(header.GetDescription(), value); return httpBase; } /// /// 设置头值 /// /// /// /// public static T SetHeader(this T httpBase, string fieldName, string value) where T : HttpBase { httpBase.SetHeaderByKey(fieldName.ToLower(), value); return httpBase; } /// /// 写入 /// /// /// public static T WriteContent(this T httpBase, byte[] buffer) where T : HttpBase { httpBase.WriteContent(buffer, 0, buffer.Length); return httpBase; } #endregion HttpBase #region HttpRequest /// /// 获取多文件集合。如果不存在,则返回null。 /// /// /// /// public static MultifileCollection GetMultifileCollection(this TRequest request) where TRequest : HttpRequest { if (request.GetBoundary().IsNullOrEmpty()) { return null; } else { return new MultifileCollection(request); } } /// /// 初始化常规的请求头。 /// 包含: /// /// Connection:keep-alive /// Pragma:no-cache /// UserAgent:TouchSocket.Http /// Accept:*/* /// AcceptEncoding:deflate, br /// /// /// /// public static TRequest InitHeaders(this TRequest request) where TRequest : HttpRequest { request.SetHeader(HttpHeaders.Connection, "keep-alive"); request.SetHeader(HttpHeaders.Pragma, "no-cache"); request.SetHeader(HttpHeaders.UserAgent, "TouchSocket.Http"); request.SetHeader(HttpHeaders.Accept, "*/*"); request.SetHeader(HttpHeaders.AcceptEncoding, "deflate, br"); return request; } /// /// 添加Host请求头 /// /// /// /// public static TRequest SetHost(this TRequest request, string host) where TRequest : HttpRequest { request.SetHeader(HttpHeaders.Host, host); return request; } /// /// 对比不包含参数的Url。其中有任意一方为null,则均返回False。 /// /// /// /// public static bool UrlEquals(this TRequest httpRequest, string url) where TRequest : HttpRequest { if (string.IsNullOrEmpty(httpRequest.RelativeURL) || string.IsNullOrEmpty(url)) { return false; } if (httpRequest.RelativeURL.Equals(url, StringComparison.CurrentCultureIgnoreCase)) { return true; } return false; } #region 设置函数 /// /// 作为Delete访问 /// /// /// public static TRequest AsDelete(this TRequest httpRequest) where TRequest : HttpRequest { httpRequest.Method = TouchSocketHttpUtility.Delete; return httpRequest; } /// /// 作为Get访问 /// /// /// public static TRequest AsGet(this TRequest httpRequest) where TRequest : HttpRequest { httpRequest.Method = TouchSocketHttpUtility.Get; return httpRequest; } /// /// 作为指定函数 /// /// /// /// public static TRequest AsMethod(this TRequest request, string method) where TRequest : HttpRequest { request.Method = method; return request; } /// /// 作为Post访问 /// /// /// public static TRequest AsPost(this TRequest httpRequest) where TRequest : HttpRequest { httpRequest.Method = TouchSocketHttpUtility.Post; return httpRequest; } /// /// 作为Put访问 /// /// /// public static TRequest AsPut(this TRequest httpRequest) where TRequest : HttpRequest { httpRequest.Method = TouchSocketHttpUtility.Put; return httpRequest; } #endregion 设置函数 #endregion HttpRequest #region HttpResponse /// /// 路径文件没找到 /// /// /// public static TResponse UrlNotFind(this TResponse response) where TResponse : HttpResponse { response.SetContent("

404 -RRQM Not Found

"); response.StatusCode = "404"; response.ContentType = "text/html;charset=utf-8"; return response; } /// /// 从文件响应。 /// 当response支持持续写入时,会直接回复响应。并阻塞执行,直到完成。所以在执行该方法之前,请确保已设置完成所有状态字 /// 当response不支持持续写入时,会填充Content,且不会响应,需要自己执行Build,并发送。 /// /// 响应 /// 请求头,用于尝试续传,为null时则不续传。 /// 文件路径 /// 文件名,不设置时会获取路径文件名 /// 最大速度(仅企业版有效)。 /// 读取长度。 /// /// /// public static TResponse FromFile(this TResponse response, string filePath, HttpRequest request, string fileName = null, int maxSpeed = 1024 * 1024 * 10, int bufferLen = 1024 * 64) where TResponse : HttpResponse { using (var reader = FilePool.GetReader(filePath)) { response.SetContentTypeByExtension(Path.GetExtension(filePath)); var contentDisposition = "attachment;" + "filename=" /*+ System.Web.HttpUtility.UrlEncode(fileName == null ? Path.GetFileName(filePath) : fileName)*/; response.SetHeader(HttpHeaders.ContentDisposition, contentDisposition) .SetHeader(HttpHeaders.AcceptRanges, "bytes"); if (response.CanWrite) { HttpRange httpRange; string range = request?.GetHeader(HttpHeaders.Range); if (string.IsNullOrEmpty(range)) { response.SetStatus(); response.ContentLength = reader.FileStorage.FileInfo.Length; httpRange = new HttpRange() { Start = 0, Length = reader.FileStorage.FileInfo.Length }; } else { httpRange = HttpRange.GetRange(range, reader.FileStorage.FileInfo.Length); if (httpRange == null) { response.ContentLength = reader.FileStorage.FileInfo.Length; httpRange = new HttpRange() { Start = 0, Length = reader.FileStorage.FileInfo.Length }; } else { response.SetContentLength(httpRange.Length) .SetStatus("206", "Partial Content") .SetHeader(HttpHeaders.ContentRange, string.Format("bytes {0}-{1}/{2}", httpRange.Start, httpRange.Length + httpRange.Start - 1, reader.FileStorage.FileInfo.Length)); } } reader.Position = httpRange.Start; long surLen = httpRange.Length; FlowGate flowGate = new FlowGate(); flowGate.Maximum = maxSpeed; using (ByteBlock block = new ByteBlock(bufferLen)) { while (surLen > 0) { int r = reader.Read(block.Buffer, 0, (int)Math.Min(bufferLen, surLen)); if (r == 0) { break; } flowGate.AddCheckWait(r); response.WriteContent(block.Buffer, 0, r); surLen -= r; } } } else { if (reader.FileStorage.FileInfo.Length > 1024 * 1024) { throw new OverlengthException("当该对象不支持写入时,仅支持1Mb以内的文件。"); } using (ByteBlock byteBlock = new ByteBlock((int)reader.FileStorage.FileInfo.Length)) { using (ByteBlock block = new ByteBlock(bufferLen)) { while (true) { int r = reader.Read(block.Buffer, 0, bufferLen); if (r == 0) { break; } byteBlock.Write(block.Buffer, 0, r); } response.SetContent(byteBlock.ToArray()); } } } } return response; } /// /// 设置文件类型。 /// /// /// /// public static TResponse SetContentTypeFromFileName(this TResponse response, string fileName) where TResponse : HttpResponse { var contentDisposition = "attachment;" + "filename=" /*+ System.Web.HttpUtility.UrlEncode(fileName)*/; response.SetHeader(HttpHeaders.ContentDisposition, contentDisposition); return response; } /// /// 设置状态,并且附带时间戳。 /// /// /// /// /// public static TResponse SetStatus(this TResponse response, string status = "200", string msg = "Success") where TResponse : HttpResponse { response.StatusCode = status; response.StatusMessage = msg; response.SetHeader(HttpHeaders.Server, $"TouchSocket.Http {HttpBase.ServerVersion}"); response.SetHeader(HttpHeaders.Date, DateTime.Now.ToGMTString("r")); return response; } #endregion HttpResponse } }