Example #1
0
        /// <summary>
        /// 原始web请求处理委托,web请求处理器的执行中心,调用方为cef3
        /// </summary>
        /// <param name="handler">cef3通信参数</param>
        /// <param name="request_datas">未解析的请求数据本机指针</param>
        /// <param name="size">数据大小</param>
        /// <param name="on_resource_response">回送处理结果委托</param>
        /// <returns></returns>
        int onTchRequest(IntPtr handler, IntPtr request_datas, int size, ResourceResponseDelegate on_resource_response)
        {
            TchResponseInfo response_info;

            try
            {
                TchRequestInfo tch_request_info = TchHelper.ParseToRequest(request_datas, size);
                response_info = this.tchRequestProcessor(tch_request_info);
            }
            catch (Exception ex)
            {
                Application.This.OnError(-1, $"request exception:{ex.Message}");
                response_info = TchHelper.ParseToResponse($"<html><head><meta charset=\"UTF-8\"/><title>处理请求错误</title></head><body><h1>处理请求发生异常,{ex.Message}</h1></body></html>");
            }
            try
            {
                //把请求结果回送本机到本机内存
                IntPtr response_datas_ptr = Marshal.AllocHGlobal(response_info.Body.Length);
                for (int i = 0; i < response_info.Body.Length; ++i)
                {
                    Marshal.WriteByte(response_datas_ptr + i, response_info.Body[i]);
                }
                //回送结果到cef3
                on_resource_response(handler, response_info.StatusCode, response_info.StatusText, response_info.MimeType, response_info.HeaderMap, response_datas_ptr, response_info.Body.Length);
                Marshal.FreeHGlobal(response_datas_ptr);
            }
            catch (Exception ex)
            {
                Application.This.OnError(-1, $"request exception:{ex.Message}");
            }
            return(0);
        }
        /// <summary>
        /// 封装后的web请求默认处理器
        /// </summary>
        /// <param name="tch_request_info">解析过的web请求数据</param>
        /// <returns></returns>
        public TchResponseInfo ProcessTchRequest(TchRequestInfo tch_request_info)
        {
            TchResponseInfo tch_response_info = this.GetResponseInfoFormFilter(tch_request_info.Uri.AbsolutePath);

            if (tch_response_info != null)
            {
                return(tch_response_info);
            }

            tch_response_info = this.GetResponseInfoFormFile(tch_request_info);
            if (tch_response_info != null)
            {
                return(tch_response_info);
            }

            tch_response_info = this.GetResponseInfoFormResource(tch_request_info);
            if (tch_response_info != null)
            {
                return(tch_response_info);
            }

            //未找到任何资源
            Application.This.OnError(-1, $"[{tch_request_info.Uri.AbsolutePath}] File Not Found");
            return(TchHelper.ParseToResponse(Encoding.UTF8.GetBytes($"<html><head><meta charset=\"UTF-8\"/><title>处理请求错误</title></head><body><h1>{tch_request_info.Uri.AbsolutePath}没找到</h1></body></html>"), 404, "File Not Found"));
        }
        /// <summary>
        /// 从本地文件中构造web请求结果
        /// </summary>
        /// <param name="file_path">本地文件路径</param>
        /// <returns>未找到文件返回null</returns>
        public TchResponseInfo GetResponseInfoFormFile(TchRequestInfo tch_request_info)
        {
            var file_path = $".{tch_request_info.Uri.LocalPath}";

            if (!File.Exists(file_path))
            {
                return(null);
            }
            var raw_datas = File.ReadAllBytes(file_path);

            byte[] datas;
            //判断是否utf8签名
            if (raw_datas[0] == 239 && raw_datas[1] == 187 && raw_datas[2] == 191)
            {
                datas = new byte[raw_datas.Length - 3];
                for (int i = 0; i < datas.Length; ++i)
                {
                    datas[i] = raw_datas[i + 3];
                }
            }
            else
            {
                datas = raw_datas;
            }
            var mimeType = tch_request_info.Headers["Accept"].Split(';')[0].Split(',')[0];

            return(TchHelper.ParseToResponse(datas, mime_type: mimeType));
        }
        /// <summary>
        /// 从TchApp.TchClient.Client对象的资源集合中构造web请求结果
        /// </summary>
        /// <param name="tch_request_info">web请求</param>
        /// <returns>未找到资源返回null</returns>
        public TchResponseInfo GetResponseInfoFormResource(TchRequestInfo tch_request_info)
        {
            //从程序集中查找
            string resource_name   = tch_request_info.Uri.AbsolutePath.Remove(0, 1).Replace("/", ".");
            Stream resource_stream = null;

            foreach (var assembly in Application.This.ResourceAssemblySet)
            {
                resource_stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{resource_name}");
                if (resource_stream != null)
                {
                    break;
                }
            }
            if (resource_stream != null)
            {
                using (resource_stream)
                {
                    byte[] bytes = new byte[resource_stream.Length];
                    resource_stream.Read(bytes, 0, bytes.Length);
                    var mimeType = tch_request_info.Headers["Accept"].Split(';')[0].Split(',')[0];
                    return(TchHelper.ParseToResponse(bytes, mime_type: mimeType));
                }
            }
            return(null);
        }
        /// <summary>
        /// 从本地文件中构造web请求结果
        /// </summary>
        /// <param name="file_path">本地文件路径</param>
        /// <returns>未找到文件返回null</returns>
        public TchResponseInfo GetResponseInfoFormFile(string file_path)
        {
            if (!File.Exists(file_path))
            {
                return(null);
            }
            var raw_datas = File.ReadAllBytes(file_path);

            byte[] datas;
            //判断是否utf8签名
            if (raw_datas[0] == 239 && raw_datas[1] == 187 && raw_datas[2] == 191)
            {
                datas = new byte[raw_datas.Length - 3];
                for (int i = 0; i < datas.Length; ++i)
                {
                    datas[i] = raw_datas[i + 3];
                }
            }
            else
            {
                datas = raw_datas;
            }
            return(TchHelper.ParseToResponse(datas));
        }