~jpgleeson/spawsh

169110e0dc6ad8f6cf1329fce0f8d97226459243 — jpgleeson 10 months ago c527298
Catch host errors & blank request errors.

TCPClient now tries the connection rather than connecting in the constructor. This lets me output an error to be the response if host can't be found. Also changed things around so a blank url doesn't fetch a new page, it just returns the current page.
1 files changed, 31 insertions(+), 14 deletions(-)

M Program.cs
M Program.cs => Program.cs +31 -14
@@ 152,13 152,16 @@ namespace spawsh
                    newInput = linksInPage[selectedLinkIndex];
                }

                Console.WriteLine(newInput);
                buildRequest(newInput);
                LineBuffer = fetchPage();
                if (newInput.Length != 0)
                {
                    buildRequest(newInput);
                    LineBuffer = fetchPage();

                linksInPage = buildLinkSet(LineBuffer);
                    linksInPage = buildLinkSet(LineBuffer);

                selectedLinkIndex = -1;
                    selectedLinkIndex = -1;
                }
                
            }
            else if (keyRead.Key == ConsoleKey.RightArrow)
            {


@@ 183,22 186,36 @@ namespace spawsh

        static string[] fetchPage()
        {
            TcpClient client = new TcpClient(server, 1965);
            string responseData;
            TcpClient client = new TcpClient();
            string responseData = "";

            using (SslStream sslStream = new SslStream(client.GetStream(), false,
                new RemoteCertificateValidationCallback(ValidateServerCertificate), null))
            try
            {
                sslStream.AuthenticateAsClient(server);
                client.Connect(server, 1965);
            }
            catch (SocketException error)
            {
                responseData = error.Message;
            }

                byte[] messageToSend = Encoding.UTF8.GetBytes("gemini://" + server + page + '\r' + '\n');
                sslStream.Write(messageToSend);
            if (client != null && client.Connected)
            {
                using (SslStream sslStream = new SslStream(client.GetStream(), false,
                new RemoteCertificateValidationCallback(ValidateServerCertificate), null))
                {
                    sslStream.AuthenticateAsClient(server);

                    byte[] messageToSend = Encoding.UTF8.GetBytes("gemini://" + server + page + '\r' + '\n');
                    sslStream.Write(messageToSend);

                responseData = ReadMessage(sslStream);
                    responseData = ReadMessage(sslStream);

                handleResponse(responseData);
                    handleResponse(responseData);

                }
            }

            
            client.Close();

            return responseData.Split('\n');