Beispiel #1
0
 private void echoData(IAppSocket appSocket, string toEcho)
 {
     byte[] msg = Encoding.ASCII.GetBytes(toEcho);
     appSocket.Send(msg);
     _optionalLogger?.Info("Sent Back the data, closing the connection...");
     appSocket.Disconnect(SocketShutdown.Both);
 }
Beispiel #2
0
        public void HandleRequest(IAppSocket socket)
        {
            var bytes           = SocketReader.ReadSocket(socket);
            var request         = DataParser.Parse(bytes);
            var requestWithBody = SocketReader.ReadBody(socket, request);
            var response        = RequestProcessor.HandleRequest(requestWithBody);

            socket.Send(response.ToByteArray());
            socket.Disconnect(SocketShutdown.Both);
        }
Beispiel #3
0
        public byte[] ReadSocket(IAppSocket socket)
        {
            var bytesToReturn = new byte[0];

            while (!Encoding.UTF8.GetString(bytesToReturn).Contains("\r\n\r\n"))
            {
                var(bytesRead, readByteCount) = socket.Receive(1);
                Array.Resize(ref bytesToReturn, bytesToReturn.Length + readByteCount);
                bytesToReturn[bytesToReturn.Length - readByteCount] = bytesRead[0];
            }

            return(bytesToReturn);
        }
Beispiel #4
0
        public Request ReadBody(IAppSocket socket, Request req)
        {
            if (!req.ContainsHeader("Content-Length"))
            {
                return(req);
            }
            var contentLength = int.Parse(req.GetHeader("Content-Length").Value);

            var(bytesRead, readByteCount) = socket.Receive(contentLength);
            var body = Encoding.UTF8.GetString(bytesRead);

            return(new Request(req, body));
        }
Beispiel #5
0
        private string retrieveData(IAppSocket appSocket)
        {
            var bufferText   = "";
            var receivedData = new byte[0];

            while (bufferText.IndexOf("\n") == -1)
            {
                var(data, dataLength) = appSocket.Receive(1);
                var originalLength = receivedData.Length;
                Array.Resize(ref receivedData, originalLength + dataLength);
                Array.Copy(data, 0, receivedData, originalLength, dataLength);
                bufferText = Encoding.ASCII.GetString(receivedData, 0, receivedData.Length);
            }
            _optionalLogger?.Info($"Got Data: {bufferText}");
            return(bufferText);
        }
Beispiel #6
0
        public void HandleRequest(IAppSocket appSocket)
        {
            var retrievedData = retrieveData(appSocket);

            echoData(appSocket, retrievedData);
        }