void backgroundWorker_Accept(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                Socket handler = sListener.Accept();

                bool flag = false;

                foreach (var currentSocket in sockets)
                {
                    if (handler.RemoteEndPoint == currentSocket.handlerSocket.RemoteEndPoint)
                    {
                        flag = true;

                        break;
                    }
                }

                if (!flag)
                {
                    MessageDelegate     message             = TextList; // делегат указывает на метод Add
                    WriteClientDelegate writeClientDelegate = WriteClient;
                    sockets.Add(new TCPClientReader(handler, message, WriteClient));
                }
            }
        }
Beispiel #2
0
        public static void HandleHttpRequestCallback(
            #region params
            IntPtr conn,
            [MarshalAs(UnmanagedType.LPStr)] string verb,
            [MarshalAs(UnmanagedType.LPStr)] string query,
            [MarshalAs(UnmanagedType.LPStr)] string pathInfo,
            [MarshalAs(UnmanagedType.LPStr)] string pathTranslated,
            [MarshalAs(UnmanagedType.LPStr)] string contentType,

            Int32 bytesDeclared,
            Int32 bytesAvailable, // if available < declared, you need to run `readClient` to get more
            IntPtr data,          // first blush of data, if any

            GetServerVariableDelegate getServerVariable, WriteClientDelegate writeClient, ReadClientDelegate readClient, IntPtr serverSupport)
        #endregion
        {
            try
            {
                if (_proxy == null)
                {
                    _proxy = new DirectServer("C:\\Temp\\WrappedSites\\1_rolling");
                }
                var headerString = TryGetHeaders(conn, getServerVariable);

                var physicalPath = TryGetPhysPath(conn, getServerVariable);

                var head = T.g("head")[T.g("title")[".Net Output"]];

                var body = T.g("body")[
                    T.g("h1")["Hello"],
                    T.g("p")[".Net here!"],
                    T.g("p")["You called me with these properties:"],

                    T.g("dl")[
                        Def("Verb", verb),
                        Def("Query string", query),
                        Def("URL path", pathInfo),
                        Def("Equivalent file path", pathTranslated),
                        Def("App physical path", physicalPath),
                        Def("Requested content type", contentType)
                    ],

                    T.g("p")["Request headers: ",
                             T.g("pre")[headerString]
                    ],

                    T.g("p")["Client supplied " + bytesAvailable + " bytes out of an expected " + bytesDeclared + " bytes"]
                           ];

                var rq = new SerialisableRequest();
                rq.Headers    = SplitToDict(headerString);
                rq.Method     = verb;
                rq.RequestUri = pathInfo;
                if (!string.IsNullOrWhiteSpace(query))
                {
                    rq.RequestUri += "?" + query;
                }
                rq.Content = ReadAllContent(conn, bytesAvailable, bytesDeclared, data, readClient);

                var tx = _proxy.DirectCall(rq);


                // spit out the Huygens response
                if (tx != null)
                {
                    body.Add(T.g("hr/"));

                    var responseHeaderList = T.g("dl");
                    foreach (var header in tx.Headers)
                    {
                        responseHeaderList.Add(Def(header.Key, string.Join(",", header.Value)));
                    }
                    body.Add(
                        T.g("h2")["Huygen proxy response:"],
                        T.g("p")["Headers:"],
                        responseHeaderList,
                        T.g("p")["Status: ", tx.StatusCode + " ", tx.StatusMessage],
                        T.g("p")["Response data (as utf8 string):"],
                        T.g("pre")[Encoding.UTF8.GetString(tx.Content)]
                        );
                }

                var page = T.g("html")[head, body];

                var ms = new MemoryStream();
                page.StreamTo(ms, Encoding.UTF8);
                ms.WriteByte(0);
                ms.Seek(0, SeekOrigin.Begin);
                var msg = ms.ToArray();
                int len = msg.Length;

                TryWriteHeaders(conn, serverSupport);
                writeClient(conn, msg, ref len, 0);
            }
            catch (Exception ex)
            {
                var ms    = new MemoryStream();
                var bytes = Encoding.UTF8.GetBytes("<pre>" + ex + "</pre>");
                ms.Write(bytes, 0, bytes.Length);
                ms.WriteByte(0);
                ms.Seek(0, SeekOrigin.Begin);
                var msg = ms.ToArray();
                int len = msg.Length;

                TryWriteHeaders(conn, serverSupport);
                writeClient(conn, msg, ref len, 0);
            }
        }