Beispiel #1
0
        private HttpParseResult OnMessageComplete(IntPtr connId)
        {
            if (httpsServer.IsUpgrade(connId))
            {
                return(HttpParseResult.Ok);
            }

            var arr = httpsServer.GetAllCookies(connId);

            bool isSkipBody = httpsServer.GetMethod(connId) == "HEAD";

            uint sequence = 0;

            if (uint.TryParse(httpsServer.GetCookie(connId, "requestSequence"), out sequence))
            {
                sequence++;
            }

            string cookie = string.Format("requestSequence={0}; path=/", sequence);

            string body = GetHeaderSummary(connId, "    ", 0, false);

            if (isSkipBody)
            {
                body = "";
            }

            // 这里的utf8 对应Content-Type里的charset, 能保证中文不乱码
            var bytes = Encoding.UTF8.GetBytes(body);

            THeader[] headers =
            {
                // 这里请参照http协议
                new THeader()
                {
                    Name = "Content-Type", Value = "text/plain;charset=utf-8"
                },
                new THeader()
                {
                    Name = "Content-Length", Value = bytes.Length.ToString()
                },
                new THeader()
                {
                    Name = "Set-Cookie", Value = cookie
                },
            };

            // 回复请求
            httpsServer.SendResponse(connId, HttpStatusCode.Ok, "Http Server OK", headers, bytes, bytes.Length);

            // 不是长连接就断开
            if (httpsServer.IsKeepAlive(connId) == false)
            {
                httpsServer.Release(connId);
            }

            return(HttpParseResult.Ok);
        }