Exemplo n.º 1
0
        /// <summary>
        /// The handler for the TeX equation requests
        /// </summary>
        /// <param name="sender">Web server instance</param>
        /// <param name="e">URL containing the TeX formula</param>
        public static void WebServer_UrlRequested(object sender, UrlRequestedEventArgs e)
        {
            if (String.IsNullOrEmpty(e.TeXEquation))
            {
                return;
            }

            string tempFile = String.Empty;
            byte[] response = new byte[0];

            e.MimeType = "image/gif";
            e.Redirect = false;

            lock (typeof(MimeTeX))
            {
                try
                {
                    tempFile = Path.GetTempFileName();

                    File.Delete(tempFile);

                    CreateGifFromEq(e.TeXEquation, tempFile);

                    response = File.ReadAllBytes(tempFile);
                }
                catch
                {
                }
                finally
                {
                    try
                    {
                        File.Delete(tempFile);
                    }
                    catch
                    {
                    }

                    e.Response = response;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets called whenever the browser control requests a URL from the web server
        /// </summary>
        /// <param name="sender">Web server instance</param>
        /// <param name="e">Request parameters</param>
        private void Web_UrlRequested(object sender, UrlRequestedEventArgs e)
        {
            string response = "Not found";
            string redirect = String.Empty;

            if (!String.IsNullOrEmpty(e.TeXEquation))
            {
                return;
            }

            string topic = e.TopicName.Replace('_', ' ').Trim();

            if (topic.Contains("#"))
            {
                topic = topic.Substring(0, topic.IndexOf('#')).Trim();
            }

            PageInfo page = hitsBox.SelectedItem as PageInfo;

            if (page != null &&
                topic.Equals(page.Name, StringComparison.InvariantCultureIgnoreCase) &&
                e.IndexName.Equals(page.Indexer.File, StringComparison.InvariantCultureIgnoreCase) &&
                !IsCircularRedirect(page))
            {
                response = page.GetFormattedContent();
                redirect = page.RedirectToTopic;
            }
            else
            {
                List<Indexer> searchArea = new List<Indexer>();

                // This is an internal link

                if (String.IsNullOrEmpty(e.IndexName))
                {
                    if (page != null)
                    {
                        searchArea.Add(page.Indexer);
                    }
                    else
                    {
                        foreach (Indexer ixr in indexes.Values)
                        {
                            searchArea.Add(ixr);
                        }
                    }
                }
                else
                {
                    foreach (Indexer ixr in indexes.Values)
                    {
                        if (ixr.File.Equals(e.IndexName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            searchArea.Add(ixr);

                            break;
                        }
                    }
                }

                if (searchArea.Count > 0)
                {
                    HitCollection hits = Indexer.Search(topic, searchArea, 100);

                    bool exactTopicLocated = false;

                    foreach (PageInfo pi in hits)
                    {
                        if (pi.Name.Trim().Equals(topic, StringComparison.InvariantCultureIgnoreCase) &&
                            !IsCircularRedirect(pi))
                        {
                            response = pi.GetFormattedContent();
                            redirect = pi.RedirectToTopic;

                            exactTopicLocated = true;

                            break;
                        }
                    }

                    if (hits.Count > 0 &&
                        !exactTopicLocated)
                    {
                        foreach (PageInfo pi in hits)
                        {
                            if (String.IsNullOrEmpty(pi.RedirectToTopic))
                            {
                                response = pi.GetFormattedContent();
                                redirect = pi.RedirectToTopic;

                                exactTopicLocated = true;

                                break;
                            }
                        }

                        if (!exactTopicLocated)
                        {
                            foreach (PageInfo pi in hits)
                            {
                                if (!IsCircularRedirect(pi))
                                {
                                    response = pi.GetFormattedContent();
                                    redirect = pi.RedirectToTopic;

                                    break;
                                }
                            }
                        }
                    }
                }
            }

            e.Redirect = !String.IsNullOrEmpty(redirect);
            e.RedirectTarget = redirect;
            e.Response = Encoding.UTF8.GetBytes(response);
            e.MimeType = "text/html";

            if (String.IsNullOrEmpty(redirect))
            {
                autoRedirects.Clear();
            }
            else
            {
                autoRedirects.Push(redirect);
            }
        }
Exemplo n.º 3
0
 private void WebServer_UrlRequested(object sender, UrlRequestedEventArgs e)
 {
     Invoke(new UrlRequestedHandler(Web_UrlRequested), sender, e);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Processes requests coming from the web browser
        /// </summary>
        public void ProcessRequests()
        {
            Socket socket = null;

            try
            {
                socket = listener.AcceptSocket();

                Thread t = new Thread(ProcessRequests);

                lock (this)
                {
                    threads.Add(t);
                }

                t.Start();

                if (socket.Connected)
                {
                    socket.ReceiveTimeout = 200;
                    socket.SendTimeout = 200;
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);

                    byte[] buf = new byte[2048];
                    int receivedLen = 0;

                    while ((receivedLen = socket.Receive(buf)) > 0)
                    {
                        string[] requestStrings = Encoding.ASCII.GetString(buf, 0, receivedLen).Split('\r', '\n');

                        if (requestStrings.Length < 2)
                        {
                            throw new Exception(Properties.Resources.HTTPRequestTooFewStrings);
                        }

                        string[] requestParts = requestStrings[0].Split(' ');

                        if (requestParts.Length != 3)
                        {
                            throw new Exception(Properties.Resources.HTTPRequestNoStringParts);
                        }

                        if (!String.Equals(requestParts[0], "GET"))
                        {
                            throw new Exception(String.Format(Properties.Resources.UnknownHTTPRequestType, requestParts[0]));
                        }

                        string httpVersion = requestParts[2];
                        string url = requestParts[1];

                        byte[] response = new byte[0];
                        string redirectUrl = String.Empty;
                        string mime = "text/html";

                        if (UrlRequested != null)
                        {
                            UrlRequestedEventArgs urea = new UrlRequestedEventArgs(url);

                            UrlRequested(this, urea);

                            redirectUrl = urea.Redirect ? urea.RedirectUrl(port) : String.Empty;

                            if (urea.Response != null)
                            {
                                response = urea.Response;
                            }

                            mime = urea.MimeType;
                        }

                        SendHeader(httpVersion, response.Length, redirectUrl, socket, mime);

                        socket.Send(response);
                    }

                    lock (this)
                    {
                        threads.Remove(Thread.CurrentThread);
                    }
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                try
                {
                    if (socket != null)
                    {
                        socket.Shutdown(SocketShutdown.Both);

                        socket.Close(0);
                    }
                }
                catch (Exception)
                {
                }
            }
        }