Esempio n. 1
0
        static void LoadGeminiLink(Uri uri)
        {
            string         result;
            bool           retrieved = false;
            GeminiResponse resp;

            resp = new GeminiResponse();

            try
            {
                resp      = (GeminiResponse)Gemini.Fetch(uri);
                retrieved = true;
            }
            catch (Exception e)
            {
                //the native gui.cs Messagebox does not resize to show enough content
                //so we use our own that is better
                Dialogs.MsgBoxOK("Gemini error", uri.AbsoluteUri + "\n\n" + e.Message);
            }

            if (retrieved)
            {
                if (resp.codeMajor == '2')
                {
                    //examine the first component of the media type up to any semi colon
                    switch (resp.mime.Split(';')[0].Trim())
                    {
                    case "text/gemini":
                    case "text/plain":
                    case "text/html":
                    {
                        string body = Encoding.UTF8.GetString(resp.bytes.ToArray());

                        result = (body);

                        if (!resp.mime.StartsWith("text/gemini"))
                        {
                            //display as preformatted text
                            result = "```\n" + result + "\n```\n";
                        }

                        break;
                    }

                    default:     // report the mime type only for now
                        result = ("Some " + resp.mime + " content was received, but cannot currently be displayed.");
                        break;
                    }

                    //render the content and add to history
                    SetAsCurrent(resp.uri);             //remember the final URI, since it may have been redirected.

                    if (_history.Count > 0)
                    {
                        _history.Peek().top      = _lineView.TopItem;
                        _history.Peek().selected = _lineView.SelectedItem;        //remember the line offset of the current page
                    }
                    _history.Push(new CachedPage(resp.uri, result, 0, 0));

                    RenderGemini(resp.uri.AbsoluteUri, result, _lineView);
                }
                else if (resp.codeMajor == '1')
                {
                    //input requested from server
                    var userResponse = Dialogs.SingleLineInputBox("Input request from: " + uri.Authority, resp.meta, "");

                    if ((userResponse.ButtonPressed == TextDialogResponse.Buttons.Ok) && (userResponse.Text != ""))
                    {
                        var ub = new UriBuilder(uri);
                        ub.Query = userResponse.Text;

                        LoadGeminiLink(ub.Uri);
                    }
                }
                else if ((resp.codeMajor == '5') && (resp.codeMinor == '1'))
                {
                    //not found
                    Dialogs.MsgBoxOK("Not found", "The resource was not found on the server: \n\n" + resp.uri.AbsoluteUri);
                }
                else
                {
                    Dialogs.MsgBoxOK("Gemini server response", uri.AbsoluteUri + "\n\n" + "Status: " + resp.codeMajor + resp.codeMinor + ": " + resp.meta);
                }
            }
        }
Esempio n. 2
0
        //navigate to a url but get some user input first
        public void NavigateGeminiWithInput(System.Windows.Navigation.NavigatingCancelEventArgs e, GeminiResponse geminiResponse)
        {
            //position input box approx in middle of main window

            var targetUri = geminiResponse.uri;
            var message   = geminiResponse.meta;

            var windowCentre = WindowGeometry.WindowCentre(mMainWindow);
            var inputPrompt  = "Input request from Gemini server\n\n" +
                               "  " + targetUri.Host + targetUri.LocalPath + "\n\n" +
                               message;

            string input = Interaction.InputBox(inputPrompt, "Server input request", "", windowCentre.Item1, windowCentre.Item2);

            if (input != "")
            {
                //encode the query
                var b = new UriBuilder();
                b.Scheme = targetUri.Scheme;
                b.Host   = targetUri.Host;
                if (targetUri.Port != -1)
                {
                    b.Port = targetUri.Port;
                }
                b.Path = targetUri.LocalPath;
                //!%22%C2%A3$%25%5E&*()_+1234567890-=%7B%7D:@~%3C%3E?[];'#,./
                b.Query = Uri.EscapeDataString(input);      //escape the query result

                //ToastNotify(b.ToString());

                mWebBrowser.Navigate(b.ToString());
            }
            else
            {
                //dont do anything further with navigating the browser
                e.Cancel = true;
            }
        }
Esempio n. 3
0
        static void LoadLink(Uri uri)
        {
            string result;

            //special treatment for about: scheme
            if (uri.Scheme == "about")
            {
                result = ReadAboutSchemeFile(uri);
                RenderGemini(uri.AbsoluteUri, result, _lineView);
                _history.Push(new CachedPage(uri, result, 0, 0));
                SetAsCurrent(uri);
                return;
            }



            bool           retrieved = false;
            GeminiResponse resp;

            resp = new GeminiResponse();

            try
            {
                resp      = (GeminiResponse)Gemini.Fetch(uri);
                retrieved = true;
            }
            catch (Exception e)
            {
                //seems to be a bug that the native Messagebox does not resize to show enough content
                MsgBoxOK("Retrieval error", e.Message);
            }


            if (retrieved)
            {
                if (resp.codeMajor == '2')
                {
                    //examine the first component of the media type up to any semi colon
                    switch (resp.mime.Split(';')[0].Trim())
                    {
                    case "text/gemini":
                    case "text/plain":
                    case "text/html":
                    {
                        string body = Encoding.UTF8.GetString(resp.bytes.ToArray());

                        result = (body);

                        if (!resp.mime.StartsWith("text/gemini"))
                        {
                            //display as preformatted text
                            result = "```\n" + result + "\n```\n";
                        }

                        break;
                    }

                    default:                             // report the mime type only for now

                        result = ("Some " + resp.mime + " content was received, but cannot currently be displayed.");
                        break;
                    }


                    //render the content and add to history
                    SetAsCurrent(resp.uri);                             //remember the final URI, since it may have been redirected.

                    if (_history.Count > 0)
                    {
                        _history.Peek().top      = _lineView.TopItem;
                        _history.Peek().selected = _lineView.SelectedItem;                                //remember the line offset of the current page
                    }
                    _history.Push(new CachedPage(resp.uri, result, 0, 0));

                    RenderGemini(resp.uri.AbsoluteUri, result, _lineView);
                }
                else if (resp.codeMajor == '1')
                {
                    //input requested from server
                    var userText = InputBox("Input request from: " + uri.Authority, resp.meta, "");

                    if (userText != "")
                    {
                        var ub = new UriBuilder(uri);
                        ub.Query = userText;

                        LoadLink(ub.Uri);
                    }
                }
                else
                {
                    MsgBoxOK("Gemini error", "Status: " + resp.codeMajor + resp.codeMinor + ": " + resp.meta);
                }
            }
        }