private void HandleHttpRequests()
        {
            while (_listener.IsListening)
            {
                try
                {
                    var ctx = _listener.GetContext();
                    OnRequestReceived(ctx);

                    if (ShouldDeserializeTraces)
                    {
                        var spans = MessagePackSerializer.Deserialize <IList <IList <Span> > >(ctx.Request.InputStream);
                        OnRequestDeserialized(spans);

                        lock (this)
                        {
                            // we only need to lock when replacing the span collection,
                            // not when reading it because it is immutable
                            Spans          = Spans.AddRange(spans.SelectMany(trace => trace));
                            RequestHeaders = RequestHeaders.Add(new NameValueCollection(ctx.Request.Headers));
                        }
                    }

                    ctx.Response.ContentType = "application/json";
                    var buffer = Encoding.UTF8.GetBytes("{}");
                    ctx.Response.OutputStream.Write(buffer, 0, buffer.Length);
                    ctx.Response.Close();
                }
                catch (HttpListenerException)
                {
                    // listener was stopped,
                    // ignore to let the loop end and the method return
                }
            }
        }
예제 #2
0
        private void HandleHttpRequests()
        {
            while (_listener.IsListening)
            {
                try
                {
                    var ctx = _listener.GetContext();
                    OnRequestReceived(ctx);

                    if (ShouldDeserializeTraces)
                    {
                        var spans = MessagePackSerializer.Deserialize <IList <IList <Span> > >(ctx.Request.InputStream);
                        OnRequestDeserialized(spans);

                        lock (this)
                        {
                            // we only need to lock when replacing the span collection,
                            // not when reading it because it is immutable
                            Spans          = Spans.AddRange(spans.SelectMany(trace => trace));
                            RequestHeaders = RequestHeaders.Add(new NameValueCollection(ctx.Request.Headers));
                        }
                    }

                    // NOTE: HttpStreamRequest doesn't support Transfer-Encoding: Chunked
                    // (Setting content-length avoids that)

                    ctx.Response.ContentType = "application/json";
                    var buffer = Encoding.UTF8.GetBytes("{}");
                    ctx.Response.ContentLength64 = buffer.LongLength;
                    ctx.Response.OutputStream.Write(buffer, 0, buffer.Length);
                    ctx.Response.Close();
                }
                catch (HttpListenerException)
                {
                    // listener was stopped,
                    // ignore to let the loop end and the method return
                }
                catch (ObjectDisposedException)
                {
                    // the response has been already disposed.
                }
                catch (InvalidOperationException)
                {
                    // this can occur when setting Response.ContentLength64, with the framework claiming that the response has already been submitted
                    // for now ignore, and we'll see if this introduces downstream issues
                }
                catch (Exception) when(!_listener.IsListening)
                {
                    // we don't care about any exception when listener is stopped
                }
            }
        }
        /// <summary>
        ///     Merges spans etc. from the other rule set into this rule set.
        /// </summary>
        public void MergeFrom(HighlightRuleSet ruleSet)
        {
            for (var i = 0; i < Delimiters.Length; i++)
            {
                Delimiters[i] |= ruleSet.Delimiters[i];
            }
            // insert merged spans in front of old spans
            var oldSpans = Spans;

            Spans = ruleSet.Spans.ToList();
            Spans.AddRange(oldSpans);
            //keyWords.MergeFrom(ruleSet.keyWords);
            //prevMarkers.MergeFrom(ruleSet.prevMarkers);
            //nextMarkers.MergeFrom(ruleSet.nextMarkers);
        }
예제 #4
0
        private void HandleHttpRequests()
        {
            while (true)
            {
                try
                {
                    var getCtxTask = Task.Run(() => _listener.GetContext());
                    getCtxTask.Wait(_listenerCts.Token);

                    var ctx = getCtxTask.Result;
                    OnRequestReceived(ctx);

                    if (ShouldDeserializeTraces)
                    {
                        using (var reader = new StreamReader(ctx.Request.InputStream))
                        {
                            var zspans = JsonConvert.DeserializeObject <List <Span> >(reader.ReadToEnd());
                            if (zspans != null)
                            {
                                IList <IMockSpan> spans = (IList <IMockSpan>)zspans.ConvertAll(x => (IMockSpan)x);
                                OnRequestDeserialized(spans);

                                Spans = Spans.AddRange(spans);
                            }

                            RequestHeaders = RequestHeaders.Add(new NameValueCollection(ctx.Request.Headers));
                        }
                    }

                    ctx.Response.ContentType = "application/json";
                    var buffer = Encoding.UTF8.GetBytes("{}");
                    ctx.Response.OutputStream.Write(buffer, 0, buffer.Length);
                    ctx.Response.Close();
                }
                catch (Exception ex) when(ex is HttpListenerException || ex is OperationCanceledException || ex is AggregateException)
                {
                    lock (_listener)
                    {
                        if (!_listener.IsListening)
                        {
                            return;
                        }
                    }

                    throw;
                }
            }
        }
    private void HandleHttpRequests()
    {
        while (_listener.IsListening)
        {
            try
            {
                var ctx = _listener.GetContext();
                OnRequestReceived(ctx);

                if (ctx.Request.RawUrl.Equals("/healthz", StringComparison.OrdinalIgnoreCase))
                {
                    CreateHealthResponse(ctx);

                    continue;
                }

                if (ShouldDeserializeTraces)
                {
                    using (var reader = new StreamReader(ctx.Request.InputStream))
                    {
                        var zspans = JsonConvert.DeserializeObject <List <ZSpanMock> >(reader.ReadToEnd());
                        if (zspans != null)
                        {
                            IList <IMockSpan> spans = zspans.ConvertAll(x => (IMockSpan)x);
                            OnRequestDeserialized(spans);

                            lock (this)
                            {
                                // we only need to lock when replacing the span collection,
                                // not when reading it because it is immutable
                                Spans          = Spans.AddRange(spans);
                                RequestHeaders = RequestHeaders.Add(new NameValueCollection(ctx.Request.Headers));
                            }
                        }
                    }
                }

                // NOTE: HttpStreamRequest doesn't support Transfer-Encoding: Chunked
                // (Setting content-length avoids that)

                ctx.Response.ContentType = "application/json";
                var buffer = Encoding.UTF8.GetBytes("{}");
                ctx.Response.ContentLength64 = buffer.LongLength;
                ctx.Response.OutputStream.Write(buffer, 0, buffer.Length);
                ctx.Response.Close();
            }
            catch (HttpListenerException)
            {
                // listener was stopped,
                // ignore to let the loop end and the method return
            }
            catch (ObjectDisposedException)
            {
                // the response has been already disposed.
            }
            catch (InvalidOperationException)
            {
                // this can occur when setting Response.ContentLength64, with the framework claiming that the response has already been submitted
                // for now ignore, and we'll see if this introduces downstream issues
            }
            catch (Exception) when(!_listener.IsListening)
            {
                // we don't care about any exception when listener is stopped
            }
        }
    }