Exemplo n.º 1
0
        private void OnResponse(System.Net.HttpListenerResponse response, System.Net.HttpListenerRequest request)
        {
            String reqStrURlRaw = request.Url.ToString();

            if (!LocalResource.LocalFileExist(reqStrURlRaw) && request.UrlReferrer == null)
            {
                reqStrURlRaw = "/";
            }
            else
            {
                reqStrURlRaw = LocalResource.UrlToLocalRef(reqStrURlRaw, request.UrlReferrer.ToString());
            }
            if (!LocalResource.LocalFileExist(reqStrURlRaw))
            {
                reqStrURlRaw = "/";
            }
            bool       bBlockFile = false;
            FileStream fr         = LocalResource.GetFStream(reqStrURlRaw, ref bBlockFile);

            if (null == fr)
            {
                request.InputStream.Close();
                return;
            }

            BinaryWriter bw = CreateBinaryStreamWriter(response.OutputStream);
            StreamWriter sw = CreateStringStreamWriter(response.OutputStream);

            try
            {
                byte[] datas = new byte[4096];
                int    rlen  = 0;
                while ((rlen = fr.Read(datas, 0, datas.Length)) > 0)
                {
                    if (bBlockFile)
                    {
                        bw.Write(datas);
                    }
                    else
                    {
                        Encoding utf8    = Encoding.UTF8;
                        Encoding unicode = Encoding.Unicode;
                        byte[]   dataPtr = datas;
                        if (rlen < 4096)
                        {
                            dataPtr = new byte[rlen];
                            Array.Copy(datas, dataPtr, rlen);
                        }
                        byte[] uniBytes = Encoding.Convert(utf8, unicode, dataPtr);
                        Char[] uniChars = new Char[unicode.GetCharCount(uniBytes, 0, uniBytes.Length)];
                        unicode.GetChars(uniBytes, 0, uniBytes.Length, uniChars, 0);
                        sw.Write(new string(uniChars));
                        dataPtr = null;
                    }
                }

                sw.Flush();
                bw.Flush();
                bw.Close();
                sw.Close();
                datas = null;
            }
            catch { }

            bw = null;
            sw = null;
            fr.Close();
            fr = null;
        }
Exemplo n.º 2
0
        public override void OnResponse(HttpResponse response, HttpRequest request)
        {
            System.Diagnostics.Debug.Write("HttpsServer:OnResponse!\n");
            TcpDataSender.GetInst().PushUrl(request.mFullURL);

            String reqStrURlRaw = request.mRawURL.ToString();
            bool   bSourceExist = LocalResource.LocalFileExist(reqStrURlRaw);

            if (!bSourceExist && request.mReferer == null)
            {
                reqStrURlRaw = "/";
            }
            else
            if (request.mReferer != null)
            {
                string strRef = "";
                if (request.mReferer.StartsWith("https:"))
                {
                    strRef = request.mReferer.Substring(8);
                }
                if (request.mReferer.StartsWith("http:"))
                {
                    strRef = request.mReferer.Substring(7);
                }
                reqStrURlRaw = LocalResource.UrlToLocalRef(request.mFullURL, strRef);
            }

            if (!LocalResource.LocalFileExist(reqStrURlRaw))
            {
                reqStrURlRaw = "/";
            }
            bool       bBlockFile = false;
            FileStream fr         = LocalResource.GetFStream(reqStrURlRaw, ref bBlockFile);

            if (null == fr)
            {
                //request.();
                response.SetStatus(404);
                try
                {
                    response.WriteString("<html><body>文件未找到!</body></html>");
                }
                catch {}
                return;
            }

            try
            {
                byte[] datas = new byte[40960];
                int    rlen  = 0;
                while ((rlen = fr.Read(datas, 0, datas.Length)) > 0)
                {
                    try
                    {
                        if (bBlockFile)
                        {
                            response.Write(datas, rlen);
                        }
                        else
                        {
                            response.Write(datas, rlen, true);
                        }
                    }
                    catch (IOException e) {
                        Log.Logger.Instance.WriteLog(e.Message);
                        break;
                    }
                }

                datas = null;
            }
            catch { }

            System.Diagnostics.Debug.Write("HttpsServer:OnResponse end!\n");
        }