Exemple #1
0
        public static HttpPacket BuildPacket(string data)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                return(null);
            }

            try
            {
                HttpPacket result = new HttpPacket();

                string[] splitted = data.Split('\n');

                // There are no headers... nothing to do here...
                if (splitted.Length < 2)
                {
                    return(null);
                }

                for (int i = 1; i < splitted.Length; i++)
                {
                    string header = splitted[i];

                    // End of HTTP headers (newline)
                    if (string.IsNullOrWhiteSpace(header))
                    {
                        break;
                    }

                    string[] splittedHeader = header.Split(':');

                    if (splittedHeader.Length < 2)
                    {
                        continue;
                    }

                    // header name
                    string fieldName = splittedHeader[0].Trim().ToLowerInvariant();

                    // header value
                    string value = string.Empty;
                    for (int j = 1; j < splittedHeader.Length; j++)
                    {
                        value = value + splittedHeader[j];
                    }

                    result.AddHeader(fieldName, value.Trim());
                }

                return(result);
            }
            catch (Exception)
            {
                return(null);
            }
        }
        private void FirstPacketReadCallback(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                return;
            }

            TcpStateContainer asyncState = asyncResult.AsyncState as TcpStateContainer;

            if (asyncState == null)
            {
                return;
            }

            TcpHost clientMachine = asyncState.Client;

            byte[] data = asyncState.DataBuffer;

            try
            {
                int    numBytes   = clientMachine.Stream.EndRead(asyncResult);
                string stringData = Encoding.UTF8.GetString(data, 0, numBytes);

                HttpPacket packet = HttpPacketBuilder.BuildPacket(stringData);

                if (packet == null)
                {
                    clientMachine.Close();
                    return;
                }

                Host serverMachineHost = packet.IsWebSocketPacket
                    ? _configuration.WebSocketHost
                    : _configuration.HttpHost;

                if (serverMachineHost != null && serverMachineHost.IsSpecified)
                {
                    TcpHost serverMachine = TcpHost.ManufactureDefault(serverMachineHost.IpAddress,
                                                                       serverMachineHost.Port);

                    serverMachine.Send(data, numBytes);

                    TcpRoute route = new TcpRoute(clientMachine, serverMachine);
                    _tcpConnectionManager.AddRoute(route);
                }
                else
                {
                    clientMachine.Close();
                }
            }
            catch (IOException)
            {
            }
        }
        public static HttpPacket BuildPacket(string data)
        {
            if (string.IsNullOrWhiteSpace(data))
                return null;

            try
            {
                HttpPacket result = new HttpPacket();

                string[] splitted = data.Split('\n');

                // There are no headers... nothing to do here...
                if (splitted.Length < 2)
                    return null;

                for (int i = 1; i < splitted.Length; i++)
                {
                    string header = splitted[i];

                    // End of HTTP headers (newline)
                    if (string.IsNullOrWhiteSpace(header))
                        break;

                    string[] splittedHeader = header.Split(':');

                    if (splittedHeader.Length < 2)
                        continue;

                    // header name
                    string fieldName = splittedHeader[0].Trim().ToLowerInvariant();

                    // header value
                    string value = string.Empty;
                    for (int j = 1; j < splittedHeader.Length; j++)
                    {
                        value = value + splittedHeader[j];
                    }

                    result.AddHeader(fieldName, value.Trim());
                }

                return result;

            }
            catch (Exception)
            {
                return null;
            }
        }