public void HelloWorld() { const string address = "http://localhost:8080/"; var contextsHot = HttpObservable.GetContexts(address); int count = 0; const int expected = 2; try { var contexts = contextsHot.Publish(); contexts.Subscribe(RespondWithHelloWorld); contexts.Subscribe(_ => Interlocked.Increment(ref count)); var waiter = contexts.ToTask(); contexts.Connect(); for (int i = 0; i < expected; i++) { GetRequest(address); } contextsHot.Dispose(); waiter.Wait(TimeSpan.FromSeconds(2)); } finally { contextsHot.Dispose(); } Assert.AreEqual(expected, count); }
public HttpListenerServer(string prefix) { _contexts = HttpObservable.GetContexts(prefix); const string responseString = "<HTML><BODY> Hello world!</BODY></HTML>"; _buffer = System.Text.Encoding.UTF8.GetBytes(responseString); Console.WriteLine("Listening on " + prefix); }
static IEnumerable <HttpFetch <HttpContent> > CrawlImpl(Uri rootUrl, int depth, Func <Uri, bool> followPredicate) { var linkSet = new HashSet <Uri> { rootUrl }; var queue = new Queue <KeyValuePair <int, Uri> >(); queue.Enqueue(0.AsKeyTo(rootUrl)); while (queue.Count > 0) { var dequeued = queue.Dequeue(); var url = dequeued.Value; var level = dequeued.Key; // TODO retry intermittent errors? var fetch = Http.Get(url).WithOptions(HttpOptions.Default.WithReturnErroneousFetch(true)).Buffer().Single(); if (!fetch.IsSuccessStatusCode) { continue; } yield return(fetch); if (level >= depth) { continue; } // If content is HTML then sniff links and add them to the // queue assuming they are from the same domain and pass the // user-supplied condition to follow. var contentMediaType = fetch.Content.Headers.ContentType?.MediaType; if (!"text/html".Equals(contentMediaType, StringComparison.OrdinalIgnoreCase)) { continue; } var lq = from e in HttpObservable.Return(_ => Observable.Return(fetch)).Links().Content() select Uri.TryCreate(e, UriKind.Absolute, out url) ? url : null into e where e != null && (e.Scheme == Uri.UriSchemeHttp || e.Scheme == Uri.UriSchemeHttps) && !linkSet.Contains(e) && rootUrl.Host.Equals(e.Host, StringComparison.OrdinalIgnoreCase) && followPredicate(e) select e; foreach (var e in lq.ToEnumerable()) { if (linkSet.Add(e)) { queue.Enqueue((level + 1).AsKeyTo(e)); } } } }
public static void Main(string args) { var contexts = HttpObservable.GetContexts("http://+:8080/"); contexts.Subscribe(async context => { var response = context.Response; var buffer = Encoding.UTF8.GetBytes("<HTML><BODY> Hello world!</BODY></HTML>"); response.ContentLength64 = buffer.Length; await response.OutputStream.WriteAsync(buffer, 0, buffer.Length); response.OutputStream.Close(); }); Console.ReadKey(); contexts.Dispose(); }
static IHttpObservable Submit(IHttpObservable query, string formSelector, int?formIndex, Uri url, NameValueCollection data) => HttpObservable.Return( from html in query.Html() select HttpQuery.Submit(html.Client, html.Content, formSelector, formIndex, url, data));