/// <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; var 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; } } }
/// <summary> /// Gets called to render a URI. /// </summary> /// <param name="e">Request parameters.</param> private void ServeWikiURLRenderPage(UrlRequestedEventArgs e) { string response = "Not found"; string redirect = String.Empty; PageInfo page = null; if (page == null || !e.Url.Equals(page.Name, StringComparison.InvariantCultureIgnoreCase)) { HitCollection hits = Indexer.Search(e.Url, RCLocalProxy.WikiIndices.Values, 1); page = null; if (hits.Count > 0) { page = hits[0]; } } if (page != null) { response = page.GetFormattedContent(); redirect = page.RedirectToTopic; } e.Redirect = !String.IsNullOrEmpty(redirect); e.RedirectTarget = redirect; e.Response = response; }
/// <summary> /// Serves a Wikipedia page using the Wiki renderer. /// </summary> /// <returns>Status of the handler.</returns> private int ServeWikiURI() { string response = String.Empty; string redirectUrl = String.Empty; try { Uri uri = new Uri(RequestUri); // XXX: not sure if we need to Decode again UrlRequestedEventArgs urea = new UrlRequestedEventArgs(HttpUtility.UrlDecode(uri.AbsolutePath.Substring(6))); ServeWikiURLRenderPage(urea); redirectUrl = urea.Redirect ? urea.RedirectTarget : String.Empty; response = urea.Redirect ? "302 Moved" : urea.Response; byte[] sendBuf = Encoding.UTF8.GetBytes(response); SendWikiHeader("HTTP/1.1", sendBuf.Length, redirectUrl, _clientSocket); _clientSocket.Send(sendBuf); } catch (Exception) { return (int)Status.Failed; } return (int)Status.Completed; }
/// <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; PageInfo page = hitsBox.SelectedItem as PageInfo; if (page == null || !e.Url.Equals(page.Name, StringComparison.InvariantCultureIgnoreCase)) { HitCollection hits = Indexer.Search(e.Url, indexes.Values, 1); page = null; if (hits.Count > 0) { page = hits[0]; } } if (page != null) { response = page.GetFormattedContent(); redirect = page.RedirectToTopic; } e.Redirect = !String.IsNullOrEmpty(redirect); e.RedirectTarget = redirect; e.Response = response; }
private void WebServer_UrlRequested(object sender, UrlRequestedEventArgs e) { Invoke(new UrlRequestedHandler(Web_UrlRequested), sender, e); }
/// <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) { } } }
/// <summary> /// Serves a Wikipedia page using the Wiki renderer, if available. /// </summary> /// <param name="requestUri">The request uri.</param> /// <param name="redirectTarget">If there is a redirect, the target will be put in here. /// Otherwise this will be the empty string.</param> /// <returns>The page content or null, if the page is not in the wiki.</returns> public string GetWikiContentIfAvailable(string requestUri, out string redirectTarget) { redirectTarget = ""; // Check, if there could be anything if (!HasWikiIndices()) { return null; } // This only gets content for english wikipedia at the moment if (!requestUri.StartsWith(EN_WIKI_URI)) { return null; } // images aren't currently cached, just return null if (requestUri.StartsWith(EN_WIKI_URI + "File:")) { return null; } // No dump file -> No pages if (_dumpFile.Equals("")) { return null; } // Try to get the page, if available try { Uri uri = new Uri(requestUri); UrlRequestedEventArgs urea = new UrlRequestedEventArgs( HttpUtility.UrlDecode(uri.AbsolutePath.Substring(6))); ServeWikiURLRenderPage(urea); if (urea.Redirect) { redirectTarget = EN_WIKI_URI + urea.RedirectTarget; } return urea.Response; } catch (Exception) { return null; } }
/// <summary> /// Gets called to render a URI. /// </summary> /// <param name="e">Request parameters.</param> private void ServeWikiURLRenderPage(UrlRequestedEventArgs e) { PageInfo page = null; // We cannot make BzReader search case sensitive, so we will look // an the first N results HitCollection hits = Indexer.Search(e.Url, _wikiIndices.Values, FIND_PAGE_MAX_RESULTS); foreach (PageInfo pi in hits) { if (pi.Name.Equals(e.Url.Replace('_',' '))) { page = pi; break; } } if (page != null) { e.Response = page.GetFormattedContent(); e.RedirectTarget = page.RedirectToTopic; e.Redirect = !String.IsNullOrEmpty(e.RedirectTarget); } }
/// <summary> /// Generates the URL for the given request term /// </summary> /// <param name="term">The request term to generate the URL for</param> /// <returns>The URL</returns> public string GenerateUrl(PageInfo page) { return(UrlRequestedEventArgs.Url(page.Indexer.File, page.Name, port)); }
/// <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("Too few strings in the HTTP request"); } string[] requestParts = requestStrings[0].Split(' '); if (requestParts.Length != 3) { throw new Exception("Invalid number of HTTP request string parts"); } if (!String.Equals(requestParts[0], "GET")) { throw new Exception("Unknown HTTP request type " + requestParts[0]); } string httpVersion = requestParts[2]; string url = requestParts[1]; string response = String.Empty; string redirectUrl = String.Empty; if (UrlRequested != null) { Uri uri = new Uri(new Uri(GenerateUrl(String.Empty)), url); UrlRequestedEventArgs urea = new UrlRequestedEventArgs(HttpUtility.UrlDecode(uri.AbsolutePath.Substring(1))); UrlRequested(this, urea); redirectUrl = urea.Redirect ? urea.RedirectTarget : String.Empty; response = urea.Redirect ? "302 Moved" : urea.Response; } byte[] sendBuf = Encoding.UTF8.GetBytes(response); SendHeader(httpVersion, sendBuf.Length, redirectUrl, socket); socket.Send(sendBuf); } lock (this) { threads.Remove(Thread.CurrentThread); } } } catch (Exception) { } finally { try { if (socket != null) { socket.Shutdown(SocketShutdown.Both); socket.Close(0); } } catch (Exception) { } } }
/// <summary> /// Generates a TeX equation URL using the provided equation text /// </summary> /// <param name="equation">Equation text</param> /// <returns>TeX equation image URL</returns> public string GenerateTeXUrl(string equation) { return(UrlRequestedEventArgs.TeXUrl(equation, port)); }
/// <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); } }
/// <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); } }