⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 request.cs

📁 虚拟WEB 服务器
💻 CS
📖 第 1 页 / 共 3 页
字号:
                        return;

                    case 20:
                        if (!(value == "bytes"))
                        {
                            break;
                        }
                        this._specialCaseStaticFileHeaders = true;
                        return;
                }
                this._responseHeadersBuilder.Append(HttpWorkerRequest.GetKnownResponseHeaderName(index));
                this._responseHeadersBuilder.Append(": ");
                this._responseHeadersBuilder.Append(value);
                this._responseHeadersBuilder.Append("\r\n");
            }
        }

        public override void SendResponseFromFile(IntPtr handle, long offset, long length)
        {
            if (length != 0L)
            {
                FileStream f = null;
                try
                {
                    SafeFileHandle handle2 = new SafeFileHandle(handle, false);
                    f = new FileStream(handle2, FileAccess.Read);
                    this.SendResponseFromFileStream(f, offset, length);
                }
                finally
                {
                    if (f != null)
                    {
                        f.Close();
                        f = null;
                    }
                }
            }
        }

        public override void SendResponseFromFile(string filename, long offset, long length)
        {
            if (length != 0L)
            {
                FileStream f = null;
                try
                {
                    f = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                    this.SendResponseFromFileStream(f, offset, length);
                }
                finally
                {
                    if (f != null)
                    {
                        f.Close();
                    }
                }
            }
        }

        private void SendResponseFromFileStream(FileStream f, long offset, long length)
        {
            long num = f.Length;
            if (length == -1L)
            {
                length = num - offset;
            }
            if (((length != 0L) && (offset >= 0L)) && (length <= (num - offset)))
            {
                if (offset > 0L)
                {
                    f.Seek(offset, SeekOrigin.Begin);
                }
                if (length <= 0x10000L)
                {
                    byte[] buffer = new byte[(int) length];
                    int num2 = f.Read(buffer, 0, (int) length);
                    this.SendResponseFromMemory(buffer, num2);
                }
                else
                {
                    byte[] buffer2 = new byte[0x10000];
                    int num3 = (int) length;
                    while (num3 > 0)
                    {
                        int count = (num3 < 0x10000) ? num3 : 0x10000;
                        int num5 = f.Read(buffer2, 0, count);
                        this.SendResponseFromMemory(buffer2, num5);
                        num3 -= num5;
                        if ((num3 > 0) && (num5 > 0))
                        {
                            this.FlushResponse(false);
                        }
                    }
                }
            }
        }

        public override void SendResponseFromMemory(byte[] data, int length)
        {
            if (length > 0)
            {
                byte[] dst = new byte[length];
                Buffer.BlockCopy(data, 0, dst, 0, length);
                this._responseBodyBytes.Add(dst);
            }
        }

        public override void SendStatus(int statusCode, string statusDescription)
        {
            this._responseStatus = statusCode;
        }

        public override void SendUnknownResponseHeader(string name, string value)
        {
            if (!this._headersSent)
            {
                this._responseHeadersBuilder.Append(name);
                this._responseHeadersBuilder.Append(": ");
                this._responseHeadersBuilder.Append(value);
                this._responseHeadersBuilder.Append("\r\n");
            }
        }

        private void SkipAllPostedContent()
        {
            if ((this._contentLength > 0) && (this._preloadedContentLength < this._contentLength))
            {
                byte[] buffer;
                for (int i = this._contentLength - this._preloadedContentLength; i > 0; i -= buffer.Length)
                {
                    buffer = this._connection.ReadRequestBytes(i);
                    if ((buffer == null) || (buffer.Length == 0))
                    {
                        return;
                    }
                }
            }
        }

        private bool TryNtlmAuthenticate()
        {
            try
            {
                using (NtlmAuth auth = new NtlmAuth())
                {
                    do
                    {
                        string blobString = null;
                        string extraHeaders = this._knownRequestHeaders[0x18];
                        if ((extraHeaders != null) && extraHeaders.StartsWith("NTLM ", StringComparison.Ordinal))
                        {
                            blobString = extraHeaders.Substring(5);
                        }
                        if (blobString != null)
                        {
                            if (!auth.Authenticate(blobString))
                            {
                                this._connection.WriteErrorAndClose(0x193);
                                return false;
                            }
                            if (auth.Completed)
                            {
                                goto Label_009A;
                            }
                            extraHeaders = "WWW-Authenticate: NTLM " + auth.Blob + "\r\n";
                        }
                        else
                        {
                            extraHeaders = "WWW-Authenticate: NTLM\r\n";
                        }
                        this.SkipAllPostedContent();
                        this._connection.WriteErrorWithExtraHeadersAndKeepAlive(0x191, extraHeaders);
                    }
                    while (this.TryParseRequest());
                    return false;
                Label_009A:
                    if (this._host.GetProcessSID() != auth.SID)
                    {
                        this._connection.WriteErrorAndClose(0x193);
                        return false;
                    }
                }
            }
            catch
            {
                try
                {
                    this._connection.WriteErrorAndClose(500);
                }
                catch
                {
                }
                return false;
            }
            return true;
        }

        private bool TryParseRequest()
        {
            this.Reset();
            this.ReadAllHeaders();
            if (!this._connection.IsLocal)
            {
                this._connection.WriteErrorAndClose(0x193);
                return false;
            }
            if (((this._headerBytes == null) || (this._endHeadersOffset < 0)) || ((this._headerByteStrings == null) || (this._headerByteStrings.Count == 0)))
            {
                this._connection.WriteErrorAndClose(400);
                return false;
            }
            this.ParseRequestLine();
            if (this.IsBadPath())
            {
                this._connection.WriteErrorAndClose(400);
                return false;
            }
            if (!this._host.IsVirtualPathInApp(this._path, out this._isClientScriptPath))
            {
                this._connection.WriteErrorAndClose(0x194);
                return false;
            }
            this.ParseHeaders();
            this.ParsePostedContent();
            return true;
        }

        private bool TryReadAllHeaders()
        {
            byte[] src = this._connection.ReadRequestBytes(0x8000);
            if ((src == null) || (src.Length == 0))
            {
                return false;
            }
            if (this._headerBytes != null)
            {
                int num = src.Length + this._headerBytes.Length;
                if (num > 0x8000)
                {
                    return false;
                }
                byte[] dst = new byte[num];
                Buffer.BlockCopy(this._headerBytes, 0, dst, 0, this._headerBytes.Length);
                Buffer.BlockCopy(src, 0, dst, this._headerBytes.Length, src.Length);
                this._headerBytes = dst;
            }
            else
            {
                this._headerBytes = src;
            }
            this._startHeadersOffset = -1;
            this._endHeadersOffset = -1;
            this._headerByteStrings = new ArrayList();
            ByteParser parser = new ByteParser(this._headerBytes);
            while (true)
            {
                ByteString str = parser.ReadLine();
                if (str == null)
                {
                    break;
                }
                if (this._startHeadersOffset < 0)
                {
                    this._startHeadersOffset = parser.CurrentOffset;
                }
                if (str.IsEmpty)
                {
                    this._endHeadersOffset = parser.CurrentOffset;
                    break;
                }
                this._headerByteStrings.Add(str);
            }
            return true;
        }

        private static string UrlEncodeRedirect(string path)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(path);
            int length = bytes.Length;
            int num2 = 0;
            for (int i = 0; i < length; i++)
            {
                if ((bytes[i] & 0x80) != 0)
                {
                    num2++;
                }
            }
            if (num2 > 0)
            {
                byte[] buffer2 = new byte[length + (num2 * 2)];
                int num4 = 0;
                for (int j = 0; j < length; j++)
                {
                    byte num6 = bytes[j];
                    if ((num6 & 0x80) == 0)
                    {
                        buffer2[num4++] = num6;
                    }
                    else
                    {
                        buffer2[num4++] = 0x25;
                        buffer2[num4++] = (byte) IntToHex[(num6 >> 4) & 15];
                        buffer2[num4++] = (byte) IntToHex[num6 & 15];
                    }
                }
                path = Encoding.ASCII.GetString(buffer2);
            }
            if (path.IndexOf(' ') >= 0)
            {
                path = path.Replace(" ", "%20");
            }
            return path;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -