public static void GetContext(System.Net.HttpListener listener, Action <Exception, System.Net.HttpListenerContext> callback) { try { listener.BeginGetContext((result) => { try { var context = listener.EndGetContext(result); Loop.Post(() => { callback(null, context); }); } catch (Exception exception) { Loop.Post(() => { callback(exception, null); }); } }, null); } catch (Exception exception) { callback(exception, null); } }
/// <summary> /// Creates a simple server to test the tunnel /// </summary> /// <param name="listenUrl"></param> /// <param name="expectedResponse"></param> /// <returns></returns> private HttpListener StartTestServer(string listenUrl, string expectedResponse) { HttpListener listener = new HttpListener(); listener.Prefixes.Add(listenUrl); listener.Start(); listener.BeginGetContext(new AsyncCallback((result) => { HttpListener listenr = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; string responseString = expectedResponse; byte[] buffer = Encoding.UTF8.GetBytes(responseString); response.ContentLength64 = buffer.Length; Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); }), listener); listener.Start(); return listener; }
public HTTPServer() { listener = new HttpListener(); listener.Prefixes.Add("http://*:" + WebAPI.dis.Configuration.Instance.Port + "/"); listener.Start(); WhitelistIPs = new List<string>() { }; if(WebAPI.dis.Configuration.Instance.WhitelistIPs.Count > 0) { foreach(var ip in WebAPI.dis.Configuration.Instance.WhitelistIPs) { WhitelistIPs.Add(ip.IP); } } AsyncCallback callback = null; callback = ar => { if (!listener.IsListening) return; new Thread(() => { this.ProcessClient( listener.EndGetContext(ar) ); }).Start(); listener.BeginGetContext(callback, null); }; listener.BeginGetContext(callback, null); }
public void Start(string bindUrl) { Ensure.NotNull(bindUrl, nameof(bindUrl)); _httpListener = new HttpListener { IgnoreWriteExceptions = false }; _httpListener.Prefixes.Add(bindUrl); _httpListener.Start(); AsyncCallback acceptConnection = null; acceptConnection = state => { try { HttpListenerContext clientContext = _httpListener.EndGetContext(state); Log.Info($"New Ecr connection from: {clientContext.Request.RemoteEndPoint}"); ProcessAsync(clientContext); } catch (Exception ex) { Log.Error(ex); } finally { _httpListener.BeginGetContext(acceptConnection, null); } }; _httpListener.BeginGetContext(acceptConnection, null); RegisterActions(); }
private void EndGetRequest(IAsyncResult result) { HttpListenerContext context = null; System.Net.HttpListener listener = (System.Net.HttpListener)result.AsyncState; try { context = listener.EndGetContext(result); using (context.Response) HandleRequest(context); } catch (Exception ex) { Console.Write("Exception in listener: {0}{1}", Environment.NewLine, ex); } finally { try { if (listener.IsListening) { listener.BeginGetContext(EndGetRequest, listener); } } catch { Stop(); } } }
public void ProcessRequest(IAsyncResult result) { if (_isDisposed) { return; } HttpListenerContext nativeContext; try { nativeContext = _listener.EndGetContext(result); } catch (HttpListenerException) { return; } QueueNextRequestPending(); var ambientContext = new AmbientContext(); var context = new HttpListenerCommunicationContext(this, nativeContext); try { using (new ContextScope(ambientContext)) { IncomingRequestReceived(this, new IncomingRequestReceivedEventArgs(context)); } } finally { using (new ContextScope(ambientContext)) { IncomingRequestProcessed(this, new IncomingRequestProcessedEventArgs(context)); } } }
private void connected(IAsyncResult result) { System.Net.HttpListener listener = (System.Net.HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); requestedPath = context.Request.Url.AbsolutePath; context.Response.StatusCode = this.response; context.Response.OutputStream.Close(); }
/// <summary> /// 监听到请求时 /// </summary> private void ListenedRequest(IAsyncResult result) { var context = listener.EndGetContext(result); if (OnRequest != null) { try { OnRequest.Invoke(context); } catch { } } listener.BeginGetContext(ListenedRequest, null); }
private void connected(IAsyncResult result) { System.Net.HttpListener listener = (System.Net.HttpListener)result.AsyncState; HttpListenerContext context = null; try { context = listener.EndGetContext(result); } catch (Exception) { return; } requestedPath = context.Request.Url.AbsolutePath; reqeustedContentType = context.Request.ContentType; using (StreamReader reader = new StreamReader(context.Request.InputStream)) { postData = reader.ReadToEnd(); } lock (this.resWriters) { if (resIdx < this.resWriters.Count) { resWriters[resIdx].writeResponse(context.Response); } else { resWriters[resWriters.Count - 1].writeResponse(context.Response); } resIdx++; } context.Response.OutputStream.Close(); try { listener.BeginGetContext(this.connected, listener); } catch (Exception) { return; } }
static void httpListenerCallback(IAsyncResult result) { System.Net.HttpListener listener = (System.Net.HttpListener)result.AsyncState; try { if (listener.IsListening) { // continue to listen listener.BeginGetContext(new AsyncCallback(httpListenerCallback), listener); // handle the incoming request System.Net.HttpListenerContext context = listener.EndGetContext(result); System.Net.HttpListenerRequest request = context.Request; string responseString; if (string.Compare("/appletv/us/js/application.js", request.Url.LocalPath, true) == 0) { responseString = System.IO.File.ReadAllText(@"D:\projects\local\atv\com.apple.trailers\application.js"); } else if (string.Compare("/appletv/us/nav.xml", request.Url.LocalPath, true) == 0) { responseString = System.IO.File.ReadAllText(@"D:\projects\local\atv\com.apple.trailers\index.xml"); } else if (string.Compare("/appletv/studios/marvel/ironman3/index-hd.xml", request.Url.LocalPath, true) == 0) { responseString = System.IO.File.ReadAllText(@"D:\projects\local\atv\com.apple.trailers\ironman3.index-hd.xml"); } else if (string.Compare("/appletv/studios/marvel/ironman3/videos/trailer1-hd.xml", request.Url.LocalPath, true) == 0) { responseString = System.IO.File.ReadAllText(@"D:\projects\local\atv\com.apple.trailers\ironman3.videos.trailer1-hd.xml"); } else { responseString = System.IO.File.ReadAllText(@"D:\projects\local\atv\atv\index.xml"); } System.Net.HttpListenerResponse response = context.Response; //string responseString = System.IO.File.ReadAllText(@"D:\projects\local\atv\atv\index.xml"); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); } } catch (Exception ex) { } }
private void ContextResult(IAsyncResult ar) { System.Net.HttpListener listener = ar.AsyncState as System.Net.HttpListener; listener.BeginGetContext(ContextResult, listener); HttpListenerContext context = null; try { context = listener.EndGetContext(ar); } catch { } if (context != null) { OnRequest(new HttpRequest(context)); } }
private static void HandleRequest(IAsyncResult result) { SessionState state = (SessionState)result.AsyncState; System.Net.HttpListener server = state.Server._httpServer; try { HttpListenerContext context = server.EndGetContext(result); server.BeginGetContext(HandleRequest, state); Task task = new Task(() => HandleSession(context, state), TaskCreationOptions.LongRunning); task.Start(); } catch (Exception) { if (state.Server.IsRunning) { throw; } } }
private void EndGetRequest(IAsyncResult result) { HttpListenerContext context = null; System.Net.HttpListener listener = (System.Net.HttpListener)result.AsyncState; try { context = listener.EndGetContext(result); HandleRequest(context); } catch { } finally { if (context != null) { context.Response.Close(); } if (listener.IsListening) { listener.BeginGetContext(EndGetRequest, listener); } } }
public void InitialUnicastEventTest () { var eventer = new DummyStateVariableEventer (); var root = CreateRoot (CreateServiceController (new StateVariable ("Foo", "string", new StateVariableOptions { Eventer = eventer }))); eventer.SetValue ("foo"); using (var server = new Server (root)) { server.Start (); var prefix = GeneratePrefix (); using (var listener = new HttpListener ()) { listener.Prefixes.Add (prefix); listener.Start (); Exception exception = null; listener.BeginGetContext (result => { try { var context = listener.EndGetContext (result); using (var reader = new StreamReader (context.Request.InputStream)) { Assert.AreEqual (Xml.SingleEventReport, reader.ReadToEnd ()); } context.Response.Close (); } catch (Exception e) { exception = e; } lock (mutex) { Monitor.Pulse (mutex); } }, null); Subscribe (root, prefix); if (exception != null) { throw exception; } } } }
public void UserHeaderWithDoubleMultiValue () { string uri = "http://localhost:" + NetworkHelpers.FindFreePort () + "/"; var l = new HttpListener (); l.Prefixes.Add (uri); l.Start (); l.BeginGetContext (ar => { var ctx = l.EndGetContext (ar); var response = ctx.Response; response.Headers.Add ("X-Custom-Header", "A"); response.Headers.Add ("X-Custom-Header", "B"); response.Close (); }, null); HttpWebRequest wr = HttpWebRequest.CreateHttp (uri); var resp = wr.GetResponse (); var vls = resp.Headers.GetValues ("X-Custom-Header"); Assert.AreEqual (2, vls.Length); l.Close (); }
public HttpListenerContext EndGetContext(IAsyncResult asyncResult) { return(InnerListener.EndGetContext(asyncResult)); }
public void UnsubscribeUnicastEventTest () { string sid = null; var eventer = new DummyStateVariableEventer (); var root = CreateRoot (CreateServiceController (new StateVariable ("Foo", "string", new StateVariableOptions { Eventer = eventer }))); eventer.SetValue ("foo"); using (var server = new Server (root)) { server.Start (); var prefix = GeneratePrefix (); var url = new Uri (root.UrlBase, "/service/0/event/"); using (var listener = new HttpListener ()) { listener.Prefixes.Add (prefix); listener.Start (); Exception exception = null; listener.BeginGetContext (result => { lock (mutex) { try { var context = listener.EndGetContext (result); using (var reader = new StreamReader (context.Request.InputStream)) { Assert.AreEqual (Xml.SingleEventReport, reader.ReadToEnd ()); } context.Response.Close (); var unsub_request = WebRequest.Create (url); unsub_request.Method = "UNSUBSCRIBE"; unsub_request.Headers.Add ("SID", sid); using (var response = (HttpWebResponse)unsub_request.GetResponse ()) { Assert.AreEqual (HttpStatusCode.OK, response.StatusCode); } listener.BeginGetContext (r => { lock (mutex) { Monitor.Pulse (mutex); } }, null); eventer.SetValue ("foo"); } catch (Exception e) { exception = e; Monitor.Pulse (mutex); } } }, null); var request = WebRequest.Create (url); request.Method = "SUBSCRIBE"; request.Headers.Add ("CALLBACK", string.Format ("<{0}>", prefix)); request.Headers.Add ("NT", "upnp:event"); lock (mutex) { using (var response = (HttpWebResponse)request.GetResponse ()) { Assert.AreEqual (HttpStatusCode.OK, response.StatusCode); Assert.IsNotNull (response.Headers["SID"]); sid = response.Headers["SID"]; } if (Monitor.Wait (mutex, TimeSpan.FromSeconds (10))) { Assert.Fail ("The event server sent updates to an unsubscribed client."); } } if (exception != null) { throw exception; } } } }
private void ServeStaticContent () { if (Debug) Console.WriteLine (); if (Debug) Console.WriteLine ("Serving static content..."); listener = new HttpListener (); listener.Prefixes.Add (BaseUrl); listener.Start (); serving = true; while (!stop_requested) { var async_result = listener.BeginGetContext (result => { var context = listener.EndGetContext (result); var response = context.Response; var path = context.Request.Url.LocalPath; response.StatusCode = 200; response.StatusDescription = "OK"; response.ProtocolVersion = new Version ("1.1"); try { if (Debug) Console.WriteLine ("Serving: {0}", path); if (path == "/") { ServeString (response, resources.Count.ToString () + "\n"); return; } else if (path == "/shutdown") { ServeString (response, "Goodbye\n"); lock (this) { stop_requested = true; } } var resource = resources[Int32.Parse (path.Substring (1))]; response.ContentType = "application/octet-stream"; response.ContentLength64 = resource.Length; response.AppendHeader ("X-Content-MD5-Sum", resource.Checksum); if (context.Request.HttpMethod == "HEAD") { response.Close (); } using (var resource_stream = File.OpenRead (resource.Path)) { var buffer = new byte[32 << 10]; using (response.OutputStream) { while (true) { var read = resource_stream.Read (buffer, 0, buffer.Length); if (read <= 0) { break; } response.OutputStream.Write (buffer, 0, read); } } } } catch { response.StatusCode = 404; response.StatusDescription = "404 Not Found"; ServeString (response, "Invalid resource: " + path + "\n"); } response.Close (); }, null); async_result.AsyncWaitHandle.WaitOne (); } }
static void Main(string[] args) { WebUtils.DefaultTimeout = 10000; WebUtils.NumRetries = 3; WebUtils.WaitBetweenRetries = 5000; WebUtils.ReadWriteTimeout = 30000; ServicePointManager.DefaultConnectionLimit = 8; DateTime startTime = DateTime.MinValue; Logger rootLogger = Logger.GetRootLogger(); rootLogger.LocalLevel = Logger.Level.Debug; string LOG_FILE_NAME = ConfigurationManager.AppSettings["logFileName"]; rootLogger.LocalOutputType = Logger.OutputType.Console; if (LOG_FILE_NAME != null) { rootLogger.LocalOutputWriter = new StreamWriter(LOG_FILE_NAME, /*append=*/true); rootLogger.LocalOutputType |= Logger.OutputType.Writer; } string SOURCES_FILE_NAME = ConfigurationManager.AppSettings["dataSourcesFileName"]; string WEB_SITE_ID = Utils.GetConfigValue("webSiteId", "dacq"); string DB_CONNECTION_STRING = ConfigurationManager.AppSettings["dbConnectionString"]; string SQL_DB_CONNECTION_STRING = ConfigurationManager.AppSettings["SqlDbConnectionString"]; string DB_CONNECTION_STRING_DUMP = ConfigurationManager.AppSettings["dbConnectionStringDump"]; string CLIENT_IP = ConfigurationManager.AppSettings["clientIp"]; string XML_DATA_ROOT = ConfigurationManager.AppSettings["xmlDataRoot"]; string XML_DATA_ROOT_DUMP = ConfigurationManager.AppSettings["xmlDataRootDump"]; string HTML_DATA_ROOT = ConfigurationManager.AppSettings["htmlDataRoot"]; string HTML_DATA_ROOT_DUMP = ConfigurationManager.AppSettings["htmlDataRootDump"]; string XML_DATA_ROOT_NEW = XML_DATA_ROOT == null ? null : (XML_DATA_ROOT.TrimEnd('\\') + "\\" + "New"); string HTML_DATA_ROOT_NEW = HTML_DATA_ROOT == null ? null : (HTML_DATA_ROOT.TrimEnd('\\') + "\\" + "New"); string XML_DATA_ROOT_DUMP_NEW = XML_DATA_ROOT_DUMP == null ? null : (XML_DATA_ROOT_DUMP.TrimEnd('\\') + "\\" + "New"); string HTML_DATA_ROOT_DUMP_NEW = HTML_DATA_ROOT_DUMP == null ? null : (HTML_DATA_ROOT_DUMP.TrimEnd('\\') + "\\" + "New"); string DB_CONNECTION_STRING_NEW = ConfigurationManager.AppSettings["SqlDbConnectionStringNew"]; string tmp = ConfigurationManager.AppSettings["enableZeroMQ"]; bool ENABLE_ZEROMQ = tmp != null && new List<string>(new string[] { "true", "1", "yes", "on" }).Contains(tmp.ToLower()); const int NUM_WRITERS = 8; const int SLEEP_BETWEEN_POLLS = 15 * 60000; // 15 minutes ArrayList<StreamDataProducerPoll> dataReaders = new ArrayList<StreamDataProducerPoll>(); ArrayList<StreamDataConsumer> dataConsumers = new ArrayList<StreamDataConsumer>(); Dictionary<IWorkflowComponent, Guid> components = new Dictionary<IWorkflowComponent, Guid>(); // init logging Logger logger = Logger.GetLogger("Latino.Workflows.Dacq"); // start HTTP server bool exit = false; bool httpServerRunning = false; if (CLIENT_IP != null) { new Thread(new ThreadStart(delegate() { HttpListener listener = new HttpListener(); listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; listener.Prefixes.Add(string.Format("http://localhost/{0}/", WEB_SITE_ID)); listener.Prefixes.Add(string.Format("http://first.ijs.si/{0}/", WEB_SITE_ID)); listener.Start(); logger.Info("Main.HttpServer", "HTTP server started."); httpServerRunning = true; DateTime prevRequestTime = DateTime.MinValue; while (!exit) { try { HttpListenerContext ctx = null; listener.BeginGetContext(new AsyncCallback(delegate(IAsyncResult ar) { try { ctx = listener.EndGetContext(ar); } catch { } }), /*state=*/null); while (!exit && ctx == null) { Thread.Sleep(500); } if (!exit) { // process requests one by one ctx.Response.AppendHeader("Content-Type", "application/xml"); XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; settings.CheckCharacters = false; settings.Indent = true; XmlWriter w = XmlTextWriter.Create(ctx.Response.OutputStream, settings); w.WriteStartElement("DacqResponse"); w.WriteElementString("DacqStartTime", startTime.ToString(TIME_FORMAT)); if (prevRequestTime == DateTime.MinValue) { prevRequestTime = startTime; } DateTime thisRequestTime = DateTime.Now; string command = GetHttpRequestCommand(ctx.Request.Url.ToString()); if (command == "components") { w.WriteElementString("PreviousRequestTime", prevRequestTime.ToString(TIME_FORMAT)); w.WriteElementString("ThisRequestTime", thisRequestTime.ToString(TIME_FORMAT)); } w.WriteElementString("Request", ctx.Request.Url.ToString()); w.WriteElementString("Command", command); w.WriteStartElement("ResponseBody"); if (command == "help") { WriteSupportedCommands(w); } else if (command == "components") { WriteComponentInfo(w, components, thisRequestTime, prevRequestTime); prevRequestTime = thisRequestTime; } else if (command == "sources") { WriteRssInfo(w, components); } w.WriteEndElement(); // ResponseBody w.WriteEndElement(); // DacqResponse w.Close(); ctx.Response.Close(); } } catch (Exception e) { logger.Warn("Main.HttpServer", e); } } listener.Stop(); logger.Info("Main.HttpServer", "HTTP server stopped."); httpServerRunning = false; })).Start(); } Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { logger.Info("Main", "*** Ctrl-C command received. ***"); e.Cancel = true; exit = true; string componentsStr = ""; foreach (StreamDataProducerPoll c in dataReaders) { if (c.IsRunning) { componentsStr += "\r\n" + c.GetType() + " : " + c.Name; } } foreach (StreamDataConsumer dataConsumer in dataConsumers) { if (dataConsumer.IsRunning) { componentsStr += "\r\n" + dataConsumer.GetType() + " : " + dataConsumer.Load; } } logger.Info("Main", "Active components:" + componentsStr); }; logger.Info("Main", "Starting Dacq ..."); // initialize database writers PassOnComponent lb = new PassOnComponent(); // data load balancer lb.DispatchPolicy = DispatchPolicy.BalanceLoadMax; dataConsumers.Add(lb); ZeroMqEmitterComponent zmq = null; if (ENABLE_ZEROMQ) { zmq = new ZeroMqEmitterComponent(); dataConsumers.Add(zmq); } DatabaseConnection dbConnection = new DatabaseConnection(); if (DB_CONNECTION_STRING != null) { dbConnection.ConnectionString = DB_CONNECTION_STRING; dbConnection.Connect(); UrlTreeBoilerplateRemoverComponent.InitializeHistory(dbConnection); dbConnection.Disconnect(); RssFeedComponent.DatabaseConnectionString = SQL_DB_CONNECTION_STRING; } for (int i = 0; i < NUM_WRITERS; i++) { DocumentCorpusWriterComponent dcw = new DocumentCorpusWriterComponent(DB_CONNECTION_STRING_DUMP, /*xmlDataRoot=*/null); DocumentWriterComponent dwc = new DocumentWriterComponent(/*connectionString=*/null, XML_DATA_ROOT_DUMP_NEW, HTML_DATA_ROOT_DUMP_NEW); dcw.IsDumpWriter = true; UrlTreeBoilerplateRemoverComponent bpr = new UrlTreeBoilerplateRemoverComponent(DB_CONNECTION_STRING); DocumentCorpusWriterComponent cw = new DocumentCorpusWriterComponent(DB_CONNECTION_STRING, XML_DATA_ROOT); DocumentWriterComponent dw = new DocumentWriterComponent(DB_CONNECTION_STRING_NEW, XML_DATA_ROOT_NEW, HTML_DATA_ROOT_NEW); HtmlTokenizerComponent htc = new HtmlTokenizerComponent(); SentenceSplitterComponent ssc = new SentenceSplitterComponent(); EnglishTokenizerComponent tok = new EnglishTokenizerComponent(); EnglishLemmatizerComponent lem = new EnglishLemmatizerComponent(EnglishLemmatizerComponent.Type.Both); EnglishPosTaggerComponent pt = new EnglishPosTaggerComponent(); LanguageDetectorComponent ld = new LanguageDetectorComponent(); DocumentFilterComponent df = new DocumentFilterComponent(); df.OnFilterDocument += new DocumentFilterComponent.FilterDocumentHandler(delegate(Document document, Logger dfLogger) { string docId = document.Features.GetFeatureValue("guid"); // remove items without link in RSS document if (document.Features.GetFeatureValue("contentType") != "Text") { dfLogger.Info("OnFilterDocument", "Document rejected: contentType not Text (id={0}).", docId); return false; } // remove items with blacklisted URL if (document.Features.GetFeatureValue("blacklisted") == "True") { dfLogger.Info("OnFilterDocument", "Document rejected: responseUrl blacklisted (id={0}).", docId); return false; } // remove items with not enough content if (Convert.ToInt32(document.Features.GetFeatureValue("bprContentCharCount")) < 100) { dfLogger.Info("OnFilterDocument", "Document rejected: bprContentCharCount < 100 (id={0}).", docId); return false; } // remove non-English items if (document.Features.GetFeatureValue("detectedCharRange") != "Basic Latin") { dfLogger.Info("OnFilterDocument", "Document rejected: detectedCharRange not Basic Latin (id={0}).", docId); return false; } if (document.Features.GetFeatureValue("detectedLanguage") != "English") { dfLogger.Info("OnFilterDocument", "Document rejected: detectedLanguage not English (id={0}).", docId); return false; } // remove exact duplicates if (document.Features.GetFeatureValue("unseenContent") == "No") { dfLogger.Info("OnFilterDocument", "Document rejected: no unseen content (id={0}).", docId); return false; } return true; }); ld.BlockSelector = "TextBlock/Content"; // due to problems with itar-tass.com df.SubscribeDumpConsumer(dcw); df.SubscribeDumpConsumer(dwc); lb.Subscribe(htc); htc.Subscribe(bpr); bpr.Subscribe(ld); ld.Subscribe(df); df.Subscribe(ssc); ssc.Subscribe(tok); tok.Subscribe(lem); lem.Subscribe(pt); pt.Subscribe(cw); pt.Subscribe(dw); if (ENABLE_ZEROMQ) { pt.Subscribe(zmq); } dataConsumers.AddRange(new StreamDataConsumer[] { dcw, dwc, df, ld, htc, ssc, tok, pt, cw, dw, lem, bpr }); } // initialize stream simulator string offlineSource = ConfigurationManager.AppSettings["offlineSource"]; if (offlineSource != null) { DocumentStreamReaderComponent dsr = new DocumentStreamReaderComponent(offlineSource); dsr.Name = offlineSource; dataReaders.Add(dsr); dsr.Subscribe(lb); } // initialize RSS feed components int j = 0; RssFeedComponent rssComp = null; Set<string> sites = new Set<string>(); if (SOURCES_FILE_NAME != null) { string[] sources = File.ReadAllLines(SOURCES_FILE_NAME); foreach (string _url in sources) { string url = _url.Trim(); if (url != "" && !url.StartsWith("#")) { Match m; if ((m = Regex.Match(url, @"^site\s*:(?<siteId>.*)$", RegexOptions.IgnoreCase)).Success) { string siteId = m.Result("${siteId}").Trim().ToLower(); if (sites.Contains(siteId)) { throw new Exception(string.Format("Duplicated site identifier ({0}).", siteId)); } sites.Add(siteId); rssComp = new RssFeedComponent(siteId); rssComp.MaxDocsPerCorpus = Convert.ToInt32(Utils.GetConfigValue("MaxDocsPerCorpus", "50")); rssComp.RandomDelayAtStart = new ArrayList<string>("yes,on,true,1,y".Split(',')) .Contains(Utils.GetConfigValue("RandomDelayAtStart", "true").ToLower()); rssComp.Name = siteId; rssComp.TimeBetweenPolls = SLEEP_BETWEEN_POLLS; rssComp.IncludeRssXml = true; if (SQL_DB_CONNECTION_STRING != null) { rssComp.Initialize(SQL_DB_CONNECTION_STRING); } rssComp.IncludeRawData = true; rssComp.Subscribe(lb); dataReaders.Add(rssComp); j++; } else if (rssComp != null) { rssComp.AddSource(url); } } } } foreach (StreamDataProducerPoll c in dataReaders) { c.Start(); } foreach (IWorkflowComponent obj in dataReaders) { components.Add(obj, Guid.NewGuid()); } foreach (IWorkflowComponent obj in dataConsumers) { components.Add(obj, Guid.NewGuid()); } startTime = DateTime.Now; while (!exit) { Thread.Sleep(500); } // shut down gracefully logger.Info("Main", "Please wait while shutting down ..."); // wait for HTTP server shutdown while (httpServerRunning) { Thread.Sleep(500); } // stop RSS components foreach (StreamDataProducerPoll c in dataReaders) { c.Stop(); } foreach (StreamDataProducerPoll c in dataReaders) { while (c.IsRunning) { Thread.Sleep(500); } } // wait for all data consumers to finish foreach (StreamDataConsumer dataConsumer in dataConsumers) { if (dataConsumer.IsRunning) { while (!dataConsumer.IsSuspended) { Thread.Sleep(500); } } dataConsumer.Dispose(); } logger.Info("Main", "Dacq successfully stopped."); }
private void Login() { myLifetimes.Next(lifetime => { myClient.UserLogin = null; myClient.GetTokenAsync( login => { var tcpListener = new TcpListener(IPAddress.Loopback, 0); int port; try { tcpListener.Start(); port = ((IPEndPoint) tcpListener.LocalEndpoint).Port; } finally { tcpListener.Stop(); } var callbackUri = string.Format("http://{0}:{1}/", "localhost", port); var server = new HttpListener(); server.Prefixes.Add(callbackUri); server.Start(); lifetime.AddDispose(server); server.BeginGetContext(ar => { myClient.Logger.CatchAsOuterDataError(() => { if (lifetime.IsTerminated) return; var context = server.EndGetContext(ar); // Write a response. using (var writer = new StreamWriter(context.Response.OutputStream)) { string response = @"<html> <head> <title>JetBox - OAuth Authentication</title> </head> <body> <h1>Authorization for JetBox</h1> <p>The application has received your response. You can close this window now.</p> <script type='text/javascript'> window.setTimeout(function() { window.open('', '_self', ''); window.close(); }, 100); if (window.opener) { window.opener.checkToken(); } </script> </body> </html>"; writer.WriteLine(response); writer.Flush(); } context.Response.OutputStream.Flush(); context.Response.OutputStream.Close(); context.Response.Close(); GetInfo(); }); }, null); Environment.OpensUri.OpenUri(new Uri(myClient.BuildAuthorizeUrl(callbackUri))); }, myClient.LogException); }); }
public static IDisposable Create(AppDelegate app, int port, string path) { app = ErrorPage.Middleware(app); var effectivePath = path ?? ""; if (!effectivePath.EndsWith("/")) effectivePath += "/"; var listener = new System.Net.HttpListener(); listener.Prefixes.Add(string.Format("http://+:{0}{1}", port, effectivePath)); listener.Start(); Action go = () => { }; go = () => listener.BeginGetContext( ar => { HttpListenerContext context; try { context = listener.EndGetContext(ar); } finally { // ReSharper disable AccessToModifiedClosure go(); // ReSharper restore AccessToModifiedClosure } var requestPathBase = effectivePath; if (requestPathBase == "/" || requestPathBase == null) requestPathBase = ""; var requestPath = context.Request.Url.AbsolutePath; if (string.IsNullOrEmpty(requestPath)) requestPath = "/"; if (requestPath.StartsWith(requestPathBase, StringComparison.OrdinalIgnoreCase)) requestPath = requestPath.Substring(requestPathBase.Length); var requestQueryString = context.Request.Url.GetComponents(UriComponents.Query, UriFormat.UriEscaped); var requestHeaders = context.Request.Headers.AllKeys .ToDictionary(x => x, x => (IEnumerable<string>)context.Request.Headers.GetValues(x), StringComparer.OrdinalIgnoreCase); var env = new Dictionary<string, object> { {OwinConstants.Version, "1.0"}, {OwinConstants.RequestMethod, context.Request.HttpMethod}, {OwinConstants.RequestScheme, context.Request.Url.Scheme}, {OwinConstants.RequestPathBase, requestPathBase}, {OwinConstants.RequestPath, requestPath}, {OwinConstants.RequestQueryString, requestQueryString}, {OwinConstants.RequestHeaders, requestHeaders}, {OwinConstants.RequestBody, RequestBody(context.Request.InputStream)}, {"System.Net.HttpListenerContext", context}, {"server.CLIENT_IP", context.Request.RemoteEndPoint.Address.ToString()}, }; app(env, (status, headers, body) => { context.Response.StatusCode = int.Parse(status.Substring(0, 3)); context.Response.StatusDescription = status.Substring(4); foreach (var kv in headers) { // these may not be assigned via header collection if (string.Equals(kv.Key, "Content-Length", StringComparison.OrdinalIgnoreCase)) { context.Response.ContentLength64 = long.Parse(kv.Value.Single()); } else if (string.Equals(kv.Key, "Keep-Alive", StringComparison.OrdinalIgnoreCase)) { context.Response.KeepAlive = true; } //else if (string.Equals(kv.Key, "Transfer-Encoding", StringComparison.OrdinalIgnoreCase)) //{ // // not sure what can be done about this //} //else if (string.Equals(kv.Key, "WWW-Authenticate", StringComparison.OrdinalIgnoreCase)) //{ // // not sure what httplistener properties to assign //} else { // all others are foreach (var v in kv.Value) { context.Response.Headers.Add(kv.Key, v); } } } var pipeResponse = new PipeResponse( context.Response.OutputStream, ex => context.Response.Close(), () => context.Response.Close()); pipeResponse.Go(body); }, ex => { // This should never be called throw new NotImplementedException(); }); }, null); go(); return new Disposable(() => { go = () => { }; listener.Stop(); }); }
public HttpContext EndGetContext(IAsyncResult asyncResult) { HttpListenerContext listenerContext = _listener.EndGetContext(asyncResult); return(new HttpContext(listenerContext, _serializer)); }
HttpListener CreateListener (Action<HttpListenerContext> contextAssert) { var l = new HttpListener (); l.Prefixes.Add (string.Format ("http://+:{0}/", port)); l.Start (); l.BeginGetContext (ar => { var ctx = l.EndGetContext (ar); try { if (contextAssert != null) contextAssert (ctx); } finally { ctx.Response.Close (); } }, null); return l; }
public static void ProcessData(WebKit webKit, HttpListener listener, IAsyncResult result) { try { var context = listener.EndGetContext(result); var response = context.Response; var request = context.Request.Url.AbsolutePath; response.Headers.Set(HttpResponseHeader.Server, AGENT); var ipAddress = context.Request.RemoteEndPoint.Address.ToString(); if (ipAddress != null) ipAddress = ipAddress.Split(':')[0]; if ((request.StartsWith("/"))) request = request.Substring(1); if ((request.EndsWith("/") || request.Equals(String.Empty))) request = request + WebServer.IndexPage; request = WebServer.RootPath + Path.DirectorySeparatorChar + request.Replace('/', Path.DirectorySeparatorChar); if (!CheckAuthenticity(context, webKit, request, ipAddress)) return; if (!Json.ProcessJsonHeader(webKit, context, context.User.Identity.Name, ipAddress)) ProcessResponse(request, context); } catch (ObjectDisposedException) { } catch (HttpListenerException) { } catch (Exception ex) { ProgramLog.Log(ex); } }
private void OnAuthButtonClicked(object sender, EventArgs args) { if (listener != null && listener.IsListening) { listener.Stop(); listener.Close(); } // TODO: Move this if (Auth == null) { Auth = new Api.OAuth(); } string rootUri = Server + "/api/1.0"; try { RootInfo root = RootInfo.GetRoot(rootUri, new Api.AnonymousConnection()); Auth.AuthorizeLocation = root.AuthorizeUrl; Auth.AccessTokenBaseUrl = root.AccessTokenUrl; Auth.RequestTokenBaseUrl = root.RequestTokenUrl; Auth.ConsumerKey = "anyone"; Auth.ConsumerSecret = "anyone"; Auth.Realm = "Snowy"; } catch (Exception e) { Logger.Error("Failed to get Root resource " + rootUri + ". Exception was: " + e.ToString()); authButton.Label = Catalog.GetString("Server not responding. Try again later."); oauth = null; return; } if (!Auth.IsAccessToken) { listener = new HL.HttpListener(); int portToTry = 8000; string callbackUrl = string.Empty; while (!listener.IsListening && portToTry < 9000) { callbackUrl = String.Format("http://localhost:{0}/tomboy-web-sync/", portToTry); try { listener.Prefixes.Add(callbackUrl); } catch (Exception e) { Logger.Error("Exception while trying to add {0} as an HttpListener Prefix", callbackUrl); Logger.Error(e.ToString()); break; } try { listener.Start(); Auth.CallbackUrl = callbackUrl; } catch { listener.Prefixes.Clear(); portToTry++; } } if (!listener.IsListening) { Logger.Error("Unable to start HttpListener on any port between 8000-8999"); authButton.Label = Catalog.GetString("Server not responding. Try again later."); oauth = null; return; } Logger.Debug("Listening on {0} for OAuth callback", callbackUrl); string authUrl = string.Empty; try { authUrl = Auth.GetAuthorizationUrl(); } catch (Exception e) { listener.Stop(); listener.Close(); Logger.Error("Failed to get auth URL from " + Server + ". Exception was: " + e.ToString()); // Translators: The web service supporting Tomboy WebSync is not responding as expected authButton.Label = Catalog.GetString("Server not responding. Try again later."); oauth = null; return; } IAsyncResult result = listener.BeginGetContext(delegate(IAsyncResult localResult) { HL.HttpListenerContext context; try { context = listener.EndGetContext(localResult); } catch (Exception e) { // TODO: Figure out why this error occurs Logger.Error("Error processing OAuth callback. Could be a sign that you pressed the button to reset the connection. Exception details:"); Logger.Error(e.ToString()); return; } // Assuming if we got here user clicked Allow Logger.Debug("Context request uri query section: " + context.Request.Url.Query); // oauth_verifier is required in OAuth 1.0a, not 1.0 var qs = HttpUtility.ParseQueryString(context.Request.Url.Query); if (!String.IsNullOrEmpty(qs ["oauth_verifier"])) { Auth.Verifier = qs ["oauth_verifier"]; } try { if (!Auth.GetAccessAfterAuthorization()) { throw new ApplicationException("Unknown error getting access token"); } Logger.Debug("Successfully authorized web sync"); } catch (Exception e) { listener.Stop(); listener.Close(); Logger.Error("Failed to authorize web sync, with exception:"); Logger.Error(e.ToString()); Gtk.Application.Invoke(delegate { authButton.Label = Catalog.GetString("Authorization Failed, Try Again"); authButton.Sensitive = true; }); oauth = null; return; } string htmlResponse = String.Format(callbackHtmlTemplate, // Translators: Title of web page presented to user after they authorized Tomboy for sync Catalog.GetString("Tomboy Web Authorization Successful"), // Translators: Body of web page presented to user after they authorized Tomboy for sync Catalog.GetString("Please return to the Tomboy Preferences window and press Save to start synchronizing.")); using (var writer = new System.IO.StreamWriter(context.Response.OutputStream)) writer.Write(htmlResponse); listener.Stop(); listener.Close(); if (Auth.IsAccessToken) { Gtk.Application.Invoke(delegate { authButton.Sensitive = false; authButton.Label = Catalog.GetString("Connected. Press Save to start synchronizing"); }); } }, null); Logger.Debug("Launching browser to authorize web sync: " + authUrl); authButton.Label = Catalog.GetString("Authorizing in browser (Press to reset connection)"); try { Services.NativeApplication.OpenUrl(authUrl, Screen); } catch (Exception e) { listener.Stop(); listener.Close(); Logger.Error("Exception opening URL: " + e.Message); // Translators: Sometimes a user's default browser is not set, so we recommend setting it and trying again authButton.Label = Catalog.GetString("Set the default browser and try again"); return; } // Translators: The user must take action in their web browser to continue the authorization process authButton.Label = Catalog.GetString("Authorizing in browser (Press to reset connection)"); } }
private void OnAuthButtonClicked (object sender, EventArgs args) { if (listener != null && listener.IsListening) { listener.Stop (); listener.Close (); } // TODO: Move this if (Auth == null) Auth = new Api.OAuth (); string rootUri = Server.TrimEnd('/') + "/api/1.0"; try { RootInfo root = RootInfo.GetRoot (rootUri, new Api.AnonymousConnection ()); Auth.AuthorizeLocation = root.AuthorizeUrl; Auth.AccessTokenBaseUrl = root.AccessTokenUrl; Auth.RequestTokenBaseUrl = root.RequestTokenUrl; Auth.ConsumerKey = "anyone"; Auth.ConsumerSecret = "anyone"; Auth.Realm = "Snowy"; } catch (Exception e) { Logger.Error ("Failed to get Root resource " + rootUri + ". Exception was: " + e.ToString()); authButton.Label = Catalog.GetString ("Server not responding. Try again later."); oauth = null; return; } if (!Auth.IsAccessToken) { listener = new HL.HttpListener (); int portToTry = 8000; string callbackUrl = string.Empty; while (!listener.IsListening && portToTry < 9000) { callbackUrl = String.Format ("http://localhost:{0}/tomboy-web-sync/", portToTry); try { listener.Prefixes.Add (callbackUrl); } catch (Exception e) { Logger.Error ("Exception while trying to add {0} as an HttpListener Prefix", callbackUrl); Logger.Error (e.ToString ()); break; } try { listener.Start (); Auth.CallbackUrl = callbackUrl; } catch { listener.Prefixes.Clear (); portToTry++; } } if (!listener.IsListening) { Logger.Error ("Unable to start HttpListener on any port between 8000-8999"); authButton.Label = Catalog.GetString ("Server not responding. Try again later."); oauth = null; return; } Logger.Debug ("Listening on {0} for OAuth callback", callbackUrl); string authUrl = string.Empty; try { authUrl = Auth.GetAuthorizationUrl (); } catch (Exception e) { listener.Stop (); listener.Close (); Logger.Error ("Failed to get auth URL from " + Server + ". Exception was: " + e.ToString ()); // Translators: The web service supporting Tomboy WebSync is not responding as expected authButton.Label = Catalog.GetString ("Server not responding. Try again later."); oauth = null; return; } IAsyncResult result = listener.BeginGetContext (delegate (IAsyncResult localResult) { HL.HttpListenerContext context; try { context = listener.EndGetContext (localResult); } catch (Exception e) { // TODO: Figure out why this error occurs Logger.Error ("Error processing OAuth callback. Could be a sign that you pressed the button to reset the connection. Exception details:"); Logger.Error (e.ToString ()); return; } // Assuming if we got here user clicked Allow Logger.Debug ("Context request uri query section: " + context.Request.Url.Query); // oauth_verifier is required in OAuth 1.0a, not 1.0 var qs = HttpUtility.ParseQueryString (context.Request.Url.Query); if (!String.IsNullOrEmpty (qs ["oauth_verifier"])) Auth.Verifier = qs ["oauth_verifier"]; try { if (!Auth.GetAccessAfterAuthorization ()) throw new ApplicationException ("Unknown error getting access token"); Logger.Debug ("Successfully authorized web sync"); } catch (Exception e) { listener.Stop (); listener.Close (); Logger.Error ("Failed to authorize web sync, with exception:"); Logger.Error (e.ToString ()); Gtk.Application.Invoke (delegate { authButton.Label = Catalog.GetString ("Authorization Failed, Try Again"); authButton.Sensitive = true; }); oauth = null; return; } string htmlResponse = String.Format (callbackHtmlTemplate, // Translators: Title of web page presented to user after they authorized Tomboy for sync Catalog.GetString ("Tomboy Web Authorization Successful"), // Translators: Body of web page presented to user after they authorized Tomboy for sync Catalog.GetString ("Please return to the Tomboy Preferences window and press Save to start synchronizing.")); using (var writer = new System.IO.StreamWriter (context.Response.OutputStream)) writer.Write (htmlResponse); listener.Stop (); listener.Close (); if (Auth.IsAccessToken) { Gtk.Application.Invoke (delegate { authButton.Sensitive = false; authButton.Label = Catalog.GetString ("Connected. Press Save to start synchronizing"); }); } }, null); Logger.Debug ("Launching browser to authorize web sync: " + authUrl); authButton.Label = Catalog.GetString ("Authorizing in browser (Press to reset connection)"); try { Services.NativeApplication.OpenUrl (authUrl, Screen); } catch (Exception e) { listener.Stop (); listener.Close (); Logger.Error ("Exception opening URL: " + e.Message); // Translators: Sometimes a user's default browser is not set, so we recommend setting it and trying again authButton.Label = Catalog.GetString ("Set the default browser and try again"); return; } // Translators: The user must take action in their web browser to continue the authorization process authButton.Label = Catalog.GetString ("Authorizing in browser (Press to reset connection)"); } }
protected void Start(Action action, Func<IWebDriver, bool> preExtraTest = null, Func<IWebDriver, bool> postExtraTest = null, TimeSpan? timeout = null) { if (timeout == null) { timeout = TimeSpan.FromSeconds(1000); } var method = CecilHelper.GetMethod(action.Method); var jsResult = Js.CreateFrom(method, this.Verbose, true); var js = jsResult.Js; if (this.Verbose) { Console.WriteLine(js); } var completeHtml = this.MakeHtml(js); using (var http = new HttpListener()) { http.Prefixes.Add("http://localhost:7890/"); http.Start(); AsyncCallback cb = null; cb = asyncState => { if (!http.IsListening) { return; } HttpListenerContext context; try { context = http.EndGetContext(asyncState); } catch (HttpListenerException) { return; } catch (ObjectDisposedException) { return; } using (var response = context.Response) { var output = response.OutputStream; var path = context.Request.Url.AbsolutePath; string responseString; if (path == "/") { responseString = completeHtml; } else { var request = context.Request; using (var ms = new MemoryStream()) { request.InputStream.CopyTo(ms); var bytes = ms.ToArray(); Func<byte[], string> fn; urls.TryGetValue(path, out fn); responseString = fn != null ? fn(bytes) : ""; } } var bHtml = Encoding.UTF8.GetBytes(responseString); response.ContentLength64 = bHtml.LongLength; output.Write(bHtml, 0, bHtml.Length); } http.BeginGetContext(cb, null); }; http.BeginGetContext(cb, null); //var usingNamespace = NamespaceSetup.Chrome != null; //var chrome = usingNamespace ? NamespaceSetup.Chrome : new ChromeDriver(); using (var chrome = NamespaceSetup.ChromeService != null ? new RemoteWebDriver(NamespaceSetup.ChromeService.ServiceUrl, DesiredCapabilities.Chrome()) : new ChromeDriver()) { try { this.jsonTypeMap = jsResult.TypeMap; chrome.Manage().Timeouts().ImplicitlyWait(timeout.Value); chrome.Url = "http://localhost:7890/"; bool isPass = true; if (preExtraTest != null) { isPass = preExtraTest(chrome); } if (isPass) { IWebElement done; string doneText = ""; try { done = chrome.FindElementById("__done__"); doneText = done.Text; } catch (NoSuchElementException) { isPass = false; } catch { isPass = false; } isPass = doneText == "pass"; if (!isPass) { if (doneText.StartsWith("ex:")) { throw new Exception(doneText.Substring(3)); } if (doneText.StartsWith("onerror:")) { throw new Exception(doneText.Substring(8)); } } if (isPass && postExtraTest != null) { isPass = postExtraTest(chrome); } } Assert.That(isPass, Is.True); } finally { //if (!usingNamespace) { chrome.Quit(); chrome.Dispose(); //} this.jsonTypeMap = null; } } http.Abort(); } }
public static IDisposable Create(AppDelegate app, int port, string path) { app = ErrorPage.Middleware(app); var effectivePath = path ?? ""; if (!effectivePath.EndsWith("/")) effectivePath += "/"; var listener = new System.Net.HttpListener(); listener.Prefixes.Add(string.Format("http://+:{0}{1}", port, effectivePath)); listener.Start(); Action go = () => { }; go = () => listener.BeginGetContext( ar => { HttpListenerContext context; try { context = listener.EndGetContext(ar); } finally { // ReSharper disable AccessToModifiedClosure go(); // ReSharper restore AccessToModifiedClosure } CallParameters call; var requestPathBase = effectivePath; if (requestPathBase == "/" || requestPathBase == null) requestPathBase = ""; var requestPath = context.Request.Url.AbsolutePath; if (string.IsNullOrEmpty(requestPath)) requestPath = "/"; if (requestPath.StartsWith(requestPathBase, StringComparison.OrdinalIgnoreCase)) requestPath = requestPath.Substring(requestPathBase.Length); var requestQueryString = context.Request.Url.GetComponents(UriComponents.Query, UriFormat.UriEscaped); call.Headers = context.Request.Headers.AllKeys .ToDictionary(x => x, x => context.Request.Headers.GetValues(x), StringComparer.OrdinalIgnoreCase); call.Environment = new Dictionary<string, object> { {OwinConstants.Version, "1.0"}, {OwinConstants.RequestMethod, context.Request.HttpMethod}, {OwinConstants.RequestScheme, context.Request.Url.Scheme}, {OwinConstants.RequestPathBase, requestPathBase}, {OwinConstants.RequestPath, requestPath}, {OwinConstants.RequestQueryString, requestQueryString}, {"System.Net.HttpListenerContext", context}, }; call.Body = context.Request.InputStream; try { Task<ResultParameters> appTask = app(call); // No real error handling, just close the connection. appTask.ContinueWith(task => context.Response.Abort(), TaskContinuationOptions.NotOnRanToCompletion); // Success appTask.Then( result => { context.Response.StatusCode = result.Status; // context.Response.StatusDescription = ; foreach (var kv in result.Headers) { // these may not be assigned via header collection if (string.Equals(kv.Key, "Content-Length", StringComparison.OrdinalIgnoreCase)) { context.Response.ContentLength64 = long.Parse(kv.Value.Single()); } else if (string.Equals(kv.Key, "Keep-Alive", StringComparison.OrdinalIgnoreCase)) { context.Response.KeepAlive = true; } //else if (string.Equals(kv.Key, "Transfer-Encoding", StringComparison.OrdinalIgnoreCase)) //{ // // not sure what can be done about this //} //else if (string.Equals(kv.Key, "WWW-Authenticate", StringComparison.OrdinalIgnoreCase)) //{ // // not sure what httplistener properties to assign //} else { // all others are foreach (var v in kv.Value) { context.Response.Headers.Add(kv.Key, v); } } } if (result.Body != null) { try { Task bodyTask = result.Body(context.Response.OutputStream); bodyTask.ContinueWith(task => context.Response.Abort(), TaskContinuationOptions.NotOnRanToCompletion); bodyTask.Then(() => context.Response.Close()); } catch (Exception) { context.Response.Abort(); } } }); } catch (Exception) { context.Response.Abort(); } }, null); go(); return new Disposable(() => { go = () => { }; listener.Stop(); }); }
HttpListener AddListenerContext (HttpListener l, Action<HttpListenerContext> contextAssert) { l.BeginGetContext (ar => { var ctx = l.EndGetContext (ar); try { if (contextAssert != null) contextAssert (ctx); } finally { ctx.Response.Close (); } }, null); return l; }
/// <summary> /// Starts the local server. /// </summary> private void startLocalServer() { // Start the server on localhost. canExit = false; newHttpListener = new System.Net.HttpListener(); newHttpListener.Prefixes.Add(LOCALHOST_ADDRESS); newHttpListener.Start(); // Start the listener. serverThread = new Thread(delegate() { while (!canExit) { IAsyncResult serverWaitHandle = newHttpListener.BeginGetContext( new AsyncCallback(handlePageRequest), newHttpListener); bool waitResult = serverWaitHandle.AsyncWaitHandle.WaitOne(1000); if (waitResult) { newHttpListener.EndGetContext(serverWaitHandle); } } }); serverThread.Start(); }
public static HttpListenerContext GetContextWithTimeout (HttpListener listener, int timeout, out bool timed_out) { IAsyncResult ares = listener.BeginGetContext (null, null); timed_out = !ares.AsyncWaitHandle.WaitOne (timeout, false); if (timed_out) return null; return listener.EndGetContext (ares); }
public void HttpClientIsDisconnectedCheckForWriteException() { string uri = "http://localhost:" + NetworkHelpers.FindFreePort () + "/"; AutoResetEvent exceptionOccuredEvent = new AutoResetEvent (false); HttpListener listener = new HttpListener { IgnoreWriteExceptions = false }; listener.Prefixes.Add (uri); listener.Start (); listener.BeginGetContext (result => { HttpListenerContext context = listener.EndGetContext (result); context.Response.SendChunked = true; context.Request.InputStream.Close (); var bytes = new byte [1024]; using(Stream outputStream = context.Response.OutputStream) { try { while (true) outputStream.Write (bytes, 0, bytes.Length); } catch { exceptionOccuredEvent.Set (); } } }, null); Task.Factory.StartNew (() => { var webRequest = (HttpWebRequest)WebRequest.Create (uri); webRequest.Method = "POST"; webRequest.KeepAlive = false; Stream requestStream = webRequest.GetRequestStream (); requestStream.WriteByte (1); requestStream.Close (); using (WebResponse response = webRequest.GetResponse ()) using (Stream stream = response.GetResponseStream ()) { byte[] clientBytes = new byte [1024]; Assert.IsNotNull (stream, "#01"); stream.Read (clientBytes, 0, clientBytes.Length); } }); Assert.IsTrue (exceptionOccuredEvent.WaitOne (15 * 1000), "#02"); }