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

📄 request.cs

📁 虚拟WEB 服务器
💻 CS
📖 第 1 页 / 共 3 页
字号:
            if (physicalPath.EndsWith(@"\", StringComparison.Ordinal) && !physicalPath.EndsWith(@":\", StringComparison.Ordinal))
            {
                physicalPath = physicalPath.Substring(0, physicalPath.Length - 1);
            }
            return physicalPath;
        }

        private void ParseHeaders()
        {
            this._knownRequestHeaders = new string[40];
            ArrayList list = new ArrayList();
            for (int i = 1; i < this._headerByteStrings.Count; i++)
            {
                string str = ((ByteString) this._headerByteStrings[i]).GetString();
                int index = str.IndexOf(':');
                if (index >= 0)
                {
                    string header = str.Substring(0, index).Trim();
                    string str3 = str.Substring(index + 1).Trim();
                    int knownRequestHeaderIndex = HttpWorkerRequest.GetKnownRequestHeaderIndex(header);
                    if (knownRequestHeaderIndex >= 0)
                    {
                        this._knownRequestHeaders[knownRequestHeaderIndex] = str3;
                    }
                    else
                    {
                        list.Add(header);
                        list.Add(str3);
                    }
                }
            }
            int num4 = list.Count / 2;
            this._unknownRequestHeaders = new string[num4][];
            int num5 = 0;
            for (int j = 0; j < num4; j++)
            {
                this._unknownRequestHeaders[j] = new string[] { (string) list[num5++], (string) list[num5++] };
            }
            if (this._headerByteStrings.Count > 1)
            {
                this._allRawHeaders = Encoding.UTF8.GetString(this._headerBytes, this._startHeadersOffset, this._endHeadersOffset - this._startHeadersOffset);
            }
            else
            {
                this._allRawHeaders = string.Empty;
            }
        }

        private void ParsePostedContent()
        {
            this._contentLength = 0;
            this._preloadedContentLength = 0;
            string s = this._knownRequestHeaders[11];
            if (s != null)
            {
                try
                {
                    this._contentLength = int.Parse(s, CultureInfo.InvariantCulture);
                }
                catch
                {
                }
            }
            if (this._headerBytes.Length > this._endHeadersOffset)
            {
                this._preloadedContentLength = this._headerBytes.Length - this._endHeadersOffset;
                if (this._preloadedContentLength > this._contentLength)
                {
                    this._preloadedContentLength = this._contentLength;
                }
                if (this._preloadedContentLength > 0)
                {
                    this._preloadedContent = new byte[this._preloadedContentLength];
                    Buffer.BlockCopy(this._headerBytes, this._endHeadersOffset, this._preloadedContent, 0, this._preloadedContentLength);
                }
            }
        }

        private void ParseRequestLine()
        {
            ByteString[] strArray = ((ByteString) this._headerByteStrings[0]).Split(' ');
            if (((strArray == null) || (strArray.Length < 2)) || (strArray.Length > 3))
            {
                this._connection.WriteErrorAndClose(400);
            }
            else
            {
                this._verb = strArray[0].GetString();
                ByteString str2 = strArray[1];
                this._url = str2.GetString();
                if (strArray.Length == 3)
                {
                    this._prot = strArray[2].GetString();
                }
                else
                {
                    this._prot = "HTTP/1.0";
                }
                int index = str2.IndexOf('?');
                if (index > 0)
                {
                    this._queryStringBytes = str2.Substring(index + 1).GetBytes();
                }
                else
                {
                    this._queryStringBytes = new byte[0];
                }
                index = this._url.IndexOf('?');
                if (index > 0)
                {
                    this._path = this._url.Substring(0, index);
                    this._queryString = this._url.Substring(index + 1);
                }
                else
                {
                    this._path = this._url;
                    this._queryStringBytes = new byte[0];
                }
                if (this._path.IndexOf('%') >= 0)
                {
                    this._path = HttpUtility.UrlDecode(this._path, Encoding.UTF8);
                    index = this._url.IndexOf('?');
                    if (index >= 0)
                    {
                        this._url = this._path + this._url.Substring(index);
                    }
                    else
                    {
                        this._url = this._path;
                    }
                }
                int startIndex = this._path.LastIndexOf('.');
                int num3 = this._path.LastIndexOf('/');
                if (((startIndex >= 0) && (num3 >= 0)) && (startIndex < num3))
                {
                    int length = this._path.IndexOf('/', startIndex);
                    this._filePath = this._path.Substring(0, length);
                    this._pathInfo = this._path.Substring(length);
                }
                else
                {
                    this._filePath = this._path;
                    this._pathInfo = string.Empty;
                }
                this._pathTranslated = this.MapPath(this._filePath);
            }
        }

        private void PrepareResponse()
        {
            this._headersSent = false;
            this._responseStatus = 200;
            this._responseHeadersBuilder = new StringBuilder();
            this._responseBodyBytes = new ArrayList();
        }

        public void Process()
        {
            if (this.TryParseRequest())
            {
                if (((this._verb == "POST") && (this._contentLength > 0)) && (this._preloadedContentLength < this._contentLength))
                {
                    this._connection.Write100Continue();
                }
                if (!this._host.RequireAuthentication || this.TryNtlmAuthenticate())
                {
                    if (this._isClientScriptPath)
                    {
                        this._connection.WriteEntireResponseFromFile(this._host.PhysicalClientScriptPath + this._path.Substring(this._host.NormalizedClientScriptPath.Length), false);
                    }
                    else if (this.IsRequestForRestrictedDirectory())
                    {
                        this._connection.WriteErrorAndClose(0x193);
                    }
                    else if (!this.ProcessDirectoryListingRequest())
                    {
                        this.PrepareResponse();
                        HttpRuntime.ProcessRequest(this);
                    }
                }
            }
        }

        private bool ProcessDirectoryListingRequest()
        {
            if (this._verb != "GET")
            {
                return false;
            }
            string path = this._pathTranslated;
            if (this._pathInfo.Length > 0)
            {
                path = this.MapPath(this._path);
            }
            if (!Directory.Exists(path))
            {
                return false;
            }
            if (!this._path.EndsWith("/", StringComparison.Ordinal))
            {
                string str2 = this._path + "/";
                string extraHeaders = "Location: " + UrlEncodeRedirect(str2) + "\r\n";
                string body = "<html><head><title>Object moved</title></head><body>\r\n<h2>Object moved to <a href='" + str2 + "'>here</a>.</h2>\r\n</body></html>\r\n";
                this._connection.WriteEntireResponseFromString(0x12e, extraHeaders, body, false);
                return true;
            }
            foreach (string str5 in defaultFileNames)
            {
                string str6 = path + @"\" + str5;
                if (File.Exists(str6))
                {
                    this._path = this._path + str5;
                    this._filePath = this._path;
                    this._url = (this._queryString != null) ? (this._path + "?" + this._queryString) : this._path;
                    this._pathTranslated = str6;
                    return false;
                }
            }
            FileSystemInfo[] elements = null;
            try
            {
                elements = new DirectoryInfo(path).GetFileSystemInfos();
            }
            catch
            {
            }
            string str7 = null;
            if (this._path.Length > 1)
            {
                int length = this._path.LastIndexOf('/', this._path.Length - 2);
                str7 = (length > 0) ? this._path.Substring(0, length) : "/";
                if (!this._host.IsVirtualPathInApp(str7))
                {
                    str7 = null;
                }
            }
            this._connection.WriteEntireResponseFromString(200, "Content-type: text/html; charset=utf-8\r\n", Messages.FormatDirectoryListing(this._path, str7, elements), false);
            return true;
        }

        private void ReadAllHeaders()
        {
            this._headerBytes = null;
            do
            {
                if (!this.TryReadAllHeaders())
                {
                    return;
                }
            }
            while (this._endHeadersOffset < 0);
        }

        public override int ReadEntityBody(byte[] buffer, int size)
        {
            int count = 0;
            this._connectionPermission.Assert();
            byte[] src = this._connection.ReadRequestBytes(size);
            if ((src != null) && (src.Length > 0))
            {
                count = src.Length;
                Buffer.BlockCopy(src, 0, buffer, 0, count);
            }
            return count;
        }

        private void Reset()
        {
            this._headerBytes = null;
            this._startHeadersOffset = 0;
            this._endHeadersOffset = 0;
            this._headerByteStrings = null;
            this._isClientScriptPath = false;
            this._verb = null;
            this._url = null;
            this._prot = null;
            this._path = null;
            this._filePath = null;
            this._pathInfo = null;
            this._pathTranslated = null;
            this._queryString = null;
            this._queryStringBytes = null;
            this._contentLength = 0;
            this._preloadedContentLength = 0;
            this._preloadedContent = null;
            this._allRawHeaders = null;
            this._unknownRequestHeaders = null;
            this._knownRequestHeaders = null;
            this._specialCaseStaticFileHeaders = false;
        }

        public override void SendCalculatedContentLength(int contentLength)
        {
            if (!this._headersSent)
            {
                this._responseHeadersBuilder.Append("Content-Length: ");
                this._responseHeadersBuilder.Append(contentLength.ToString(CultureInfo.InvariantCulture));
                this._responseHeadersBuilder.Append("\r\n");
            }
        }

        public override void SendKnownResponseHeader(int index, string value)
        {
            if (!this._headersSent)
            {
                switch (index)
                {
                    case 1:
                    case 2:
                    case 0x1a:
                        return;

                    case 0x12:
                    case 0x13:
                        if (!this._specialCaseStaticFileHeaders)
                        {
                            break;
                        }

⌨️ 快捷键说明

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