示例#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);
        }
示例#2
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"));
        }
示例#3
0
        /// <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));
        }
示例#4
0
        /// <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);
        }
示例#5
0
文件: TchHelper.cs 项目: zggl/tchapp
        public static TchRequestInfo ParseToRequest(byte[] request_bytes)
        {
            TchRequestInfo request_info = new TchRequestInfo();

            request_info.RawBytes = request_bytes;
            string request_str = Encoding.UTF8.GetString(request_bytes);

            int current_index = 0;
            int scan_index    = request_str.IndexOf(' ');

            request_info.Method = request_str.Substring(current_index, scan_index);
            current_index       = scan_index + 1;

            scan_index       = request_str.IndexOf(' ', current_index);
            request_info.Uri = new Uri(request_str.Substring(current_index, scan_index - current_index));
            current_index    = scan_index + 1;

            scan_index = request_str.IndexOf('\r', current_index);
            request_info.HttpVersion = request_str.Substring(current_index, scan_index - current_index);
            current_index            = scan_index + 2;

            string header_line;
            int    header_scan_index = 0;

            while (current_index != request_str.Length && request_str[current_index] != '\r')
            {
                scan_index  = request_str.IndexOf('\r', current_index);
                header_line = request_str.Substring(current_index, scan_index - current_index);
                if (header_line.IndexOf("Connection") == 0 && header_line.IndexOf("keep-alive") != -1)
                {
                    request_info.KeepAlive = true;
                }
                header_scan_index = header_line.IndexOf(':');
                request_info.Headers.Add(header_line.Substring(0, header_scan_index), header_line.Substring(header_scan_index + 2, header_line.Length - header_scan_index - 2));
                current_index = scan_index + 2;
            }
            if (current_index < request_str.Length)
            {
                current_index += 2;
                string body_str   = request_str.Substring(current_index, request_str.Length - current_index);
                byte[] body_bytes = Encoding.UTF8.GetBytes(body_str);
                request_info.Body.Write(body_bytes, 0, body_bytes.Length);
                request_info.Body.Flush();
                request_info.Body.Position = 0;
                if (!request_info.Headers.ContainsKey("Content-Length"))
                {
                    request_info.Headers.Add("Content-Length", body_bytes.Length.ToString());
                }
            }
            return(request_info);
        }