Exemplo n.º 1
0
        public override async Task <Message> HandleMessage(Message msg)
        {
            // Find the original HTTP request, it'll have the context to respond on.
            WebRequestMessage entry = msg.FirstAncestor <WebRequestMessage>();

            if (entry == null ||
                entry.Context == null ||
                !entry.Context.Response.OutputStream.CanWrite)
            {
                return(msg);
            }

            MessageContext context = new MessageContext(msg, this);

            var parsedStatus = await StatusCode.TrySelectIntAsync(context);

            if (!parsedStatus.Parsed)
            {
                logger.Warn("Failed to parse HTTP status for {0}", this.Name);
                return(null);
            }

            string contentType = await ContentType.SelectStringAsync(context);

            try
            {
                var webContext = entry.Context;
                webContext.Response.StatusCode  = parsedStatus.Value;
                webContext.Response.ContentType = contentType;

                StreamMessage smsg = msg as StreamMessage;
                if (smsg != null && Body is null)
                {
                    var outStream = await smsg.GetStream();

                    if (outStream.CanSeek)
                    {
                        outStream.Position = 0;
                    }
                    outStream.CopyTo(webContext.Response.OutputStream);
                }
                else
                {
                    var payload = await Body.SelectStringAsync(context);

                    var payloadBytes = Encoding.UTF8.GetBytes(payload);
                    webContext.Response.ContentLength64 = payloadBytes.Length;
                    webContext.Response.OutputStream.Write(payloadBytes, 0, payloadBytes.Length);
                }

                webContext.Response.Close();
            }
            catch (Exception ex)
            {
                logger.Warn(ex, "Error sending HTTP response");
            }

            return(msg);
        }
Exemplo n.º 2
0
        public void OnBeginRequest(HttpContext httpContext)
        {
            // TODO: Not sure if this is where this should live but it's the earlist hook point we have
            _contextData.Value = new MessageContext {
                Id = Guid.NewGuid(), Type = "Request"
            };

            var request         = httpContext.Request;
            var requestDateTime = DateTime.UtcNow;

            // build message
            var message = new WebRequestMessage
            {
                Url       = $"{request.Scheme}://{request.Host}{request.PathBase}{request.Path}{request.QueryString}", // TODO: check if there is a better way of doing this
                Method    = request.Method,
                Headers   = request.Headers.ToDictionary(h => h.Key, h => h.Value),
                StartTime = requestDateTime
            };

            // add ajax
            var isAjax = StringValues.Empty;

            httpContext.Request.Headers.TryGetValue("__glimpse-isAjax", out isAjax);
            message.IsAjax = isAjax == "true";

            // add protocol
            message.Protocol = new WebRequestProtocol();
            if (!string.IsNullOrEmpty(request.Protocol))
            {
                var protocol = request.Protocol.Split('/');
                message.Protocol.Identifier = protocol[0];
                if (protocol.Length > 0)
                {
                    message.Protocol.Version = protocol[1];
                }
            }

            _broker.StartOffsetOperation();
            _broker.BeginLogicalOperation(message, requestDateTime);
            _broker.SendMessage(message);
        }
        private async Task <T> SendRequestAsync <T>(WebRequestMessage webRequest)
        {
            var requestJson = JsonConvert.SerializeObject(webRequest, Formatting.None, new JsonSerializerSettings
            {
                NullValueHandling = webRequest.NullValueHandling
            });

            var responseJson = await PostJson(requestJson);

            var webResponse = JsonConvert.DeserializeObject <WebResponseMessage <T> >(responseJson);

            if (webResponse.Error != null)
            {
                throw new DelugeWebClientException(webResponse.Error.Message, webResponse.Error.Code);
            }
            if (webResponse.ResponseId != webRequest.RequestId)
            {
                throw new DelugeWebClientException("Desync.", 0);
            }

            return(webResponse.Result);
        }