Esempio n. 1
0
        /// <summary>
        /// 发送已缓存
        /// </summary>
        /// <param name="requestData"></param>
        /// <param name="cache"></param>
        private static void SendCache(RequestData requestData, CacheManager.Cache cache)
        {
            if (String.Compare(requestData.GetEtag(), cache.Key, StringComparison.OrdinalIgnoreCase) == 0 || requestData.IfModifiedSince() == cache.LastWriteTime)
            {
                byte[] array = ResultFactory.BuildCacheResult(cache.Key, cache.LastWriteTime, requestData.IsKeepAlive(), 0);
                requestData.Socket.Write(array, new Action <OwinSocket, int, Exception, object>(SendCompleteCallback), requestData);
                return;
            }
            Tuple <int, int> tuple = requestData.IfRange();

            if (tuple != null)
            {
                int num;
                int num2;
                GetNextSize(tuple, cache.FileBytes.Length, out num, out num2);
                byte[] array2 = new byte[num2];
                Buffer.BlockCopy(cache.FileBytes, num, array2, 0, num2);
                string s2 = ResultFactory.BuildResult(206, cache.HttpMimeType, "", num, checked (num + num2), cache.FileBytes.Length, false, requestData.IsKeepAlive());
                requestData.Socket.WriteForPost(Encoding.ASCII.GetBytes(s2), array2, new Action <OwinSocket, int, Exception, object>(SendCompleteCallback), requestData);
                return;
            }
            byte[] array3 = (cache.FileBytesCompress != null) ? cache.FileBytesCompress : cache.FileBytes;
            byte[] array4 = ResultFactory.BuildSuccessResult(cache.HttpMimeType, array3.Length, cache.Key, cache.LastWriteTime, requestData.IsKeepAlive(), cache.FileBytesCompress != null);
            requestData.Socket.WriteForPost(array4, array3, new Action <OwinSocket, int, Exception, object>(SendCompleteCallback), requestData);
        }
Esempio n. 2
0
            public void Run()
            {
                try
                {
                    _fs = File.OpenRead(_fileName);
                }
                catch
                {
                    EndRequest(_req.Socket, 500, "can't open file.");
                    _req.SaveToPoll();
                    _req = null;
                    return;
                }
                string           extention = CommonUtil.GetFileExtention(_fileName);
                string           mimeType  = HttpMimeTypeManage.GetHttpMimeType(extention);
                Tuple <int, int> tuple     = _req.IfRange();
                int num;
                int num2;

                if (tuple == null)
                {
                    byte[] headDomain = ResultFactory.BuildSuccessResult(mimeType, _fileSize, "", CommonUtil.GetFileTime(_modifyTime), _req.IsKeepAlive(), false);
                    byte[] body       = new byte[32768];
                    _fs.Read(body, 0, body.Length);
                    _sendedSize += body.Length;
                    _offsetSize -= body.Length;
                    _req.Socket.WriteForPost(headDomain, body, new Action <OwinSocket, int, Exception, object>(CompilteCallback), null);
                    return;
                }
                //续传
                GetNextSize(tuple, checked ((int)_fileSize), out num, out num2);
                _sendedSize = num;
                _offsetSize = num2;
                string s = ResultFactory.BuildResult(206, mimeType, "", _sendedSize, _offsetSize, _fileSize, false, _req.IsKeepAlive());

                byte[] array3 = (_offsetSize > 32768L) ? new byte[32768] : new byte[_offsetSize];
                _fs.Seek(_sendedSize, SeekOrigin.Begin);
                _fs.Read(array3, (int)_sendedSize, array3.Length);
                _sendedSize += array3.Length;
                _offsetSize -= array3.Length;
                _req.Socket.WriteForPost(Encoding.ASCII.GetBytes(s), array3, new Action <OwinSocket, int, Exception, object>(CompilteCallback), null);
            }
Esempio n. 3
0
        private static void SendFile(RequestData req, string file, long mtime, long fsize)
        {
            byte[] array = null;
            try
            {
                array = File.ReadAllBytes(file);
            }
            catch
            {
                EndRequest(req.Socket, 500, "Can't Open File.");
                req.SaveToPoll();
                req = null;
                return;
            }
            string fileExtention = CommonUtil.GetFileExtention(file);
            string mimeType      = HttpMimeTypeManage.GetHttpMimeType(fileExtention);

            Tuple <int, int> tuple = req.IfRange();

            if (tuple == null)
            {
                byte[] array2 = ResultFactory.BuildSuccessResult(mimeType, fsize, CacheManager.FileLastTimeAndFileLengthString(file, fsize, mtime), CommonUtil.GetFileTime(mtime), req.IsKeepAlive(), false);
                req.Socket.WriteForPost(array2, array, new Action <OwinSocket, int, Exception, object>(SendCompleteCallback), req);
                CacheManager.Save(req.RequestUrl, file, array, req.SafeRequestUrl);
                return;
            }

            int num3 = 0;
            int num4 = checked ((int)fsize);

            GetNextSize(tuple, fsize, out num3, out num4);
            byte[] array3 = new byte[num4];
            Buffer.BlockCopy(array, num3, array3, 0, num4);
            //206的续传回应
            string s = ResultFactory.BuildResult(206, mimeType, "", num3, checked (num3 + num4), fsize, false, req.IsKeepAlive());

            byte[] bytes = Encoding.ASCII.GetBytes(s);
            req.Socket.WriteForPost(bytes, array3, new Action <OwinSocket, int, Exception, object>(SendCompleteCallback), req);
        }