Exemple #1
0
        private async Task <(EventRequestModel, String)> FormatRequest(HttpRequest request, string transactionId)
        {
            // Request headers
            var reqHeaders = LoggerHelper.ToHeaders(request.Headers, debug);

            // RequestBody
            request.EnableBuffering(bufferThreshold: 1000000);
            string bodyAsText = null;

            string contentEncoding     = "";
            string contentLength       = "";
            int    parsedContentLength = 100000;

            reqHeaders.TryGetValue("Content-Encoding", out contentEncoding);
            reqHeaders.TryGetValue("Content-Length", out contentLength);
            int.TryParse(contentLength, out parsedContentLength);

            bodyAsText = await LoggerHelper.GetRequestContents(bodyAsText, request, contentEncoding, parsedContentLength, debug);

            // Add Transaction Id to the Request Header
            bool disableTransactionId = LoggerHelper.GetConfigBoolValues(moesifOptions, "DisableTransactionId", false);

            if (!disableTransactionId)
            {
                transactionId = LoggerHelper.GetOrCreateTransactionId(reqHeaders, "X-Moesif-Transaction-Id");
                reqHeaders    = LoggerHelper.AddTransactionId("X-Moesif-Transaction-Id", transactionId, reqHeaders);
            }

            // Serialize request body
            var bodyWrapper = LoggerHelper.Serialize(bodyAsText, request.ContentType, logBody, debug);

            // Client Ip Address
            string ip  = clientIpHelper.GetClientIp(reqHeaders, request);
            var    uri = new Uri(request.GetDisplayUrl()).ToString();

            string apiVersion     = null;
            var    apiVersion_out = new object();
            var    getApiVersion  = moesifOptions.TryGetValue("ApiVersion", out apiVersion_out);

            if (getApiVersion)
            {
                apiVersion = apiVersion_out.ToString();
            }

            var eventReq = new EventRequestModel()
            {
                Time             = DateTime.UtcNow,
                Uri              = uri,
                Verb             = request.Method,
                ApiVersion       = apiVersion,
                IpAddress        = ip,
                Headers          = reqHeaders,
                Body             = bodyWrapper.Item1,
                TransferEncoding = bodyWrapper.Item2
            };

            return(eventReq, transactionId);
        }
        private async Task <EventRequestModel> FormatRequest(IOwinRequest request)
        {
            var body = request.Body;

            string requestBody = await new StreamReader(request.Body).ReadToEndAsync();

            // Read request Body
            byte[] requestData = Encoding.UTF8.GetBytes(requestBody);
            request.Body = new MemoryStream(requestData);

            var reqHeaders = new Dictionary <string, string>();

            try
            {
                reqHeaders = request.Headers.ToDictionary(k => k.Key, k => k.Value.First(), StringComparer.OrdinalIgnoreCase);
            }
            catch (Exception inst)
            {
                if (debug)
                {
                    Console.WriteLine("error encountered while copying request header");
                    Console.WriteLine(inst);
                }
            }

            // Add Transaction Id to the Request Header
            var transation_id_out    = new Object();
            var captureTransactionId = moesifOptions.TryGetValue("DisableTransactionId", out transation_id_out);

            bool GetCaptureTransactionId = false;

            if (captureTransactionId)
            {
                GetCaptureTransactionId = (bool)(transation_id_out);
            }

            if (!GetCaptureTransactionId)
            {
                if (reqHeaders.ContainsKey("X-Moesif-Transaction-Id"))
                {
                    string reqTransId = reqHeaders["X-Moesif-Transaction-Id"];
                    if (!string.IsNullOrEmpty(reqTransId))
                    {
                        transactionId = reqTransId;
                        if (string.IsNullOrEmpty(transactionId))
                        {
                            transactionId = Guid.NewGuid().ToString();
                        }
                    }
                    else
                    {
                        transactionId = Guid.NewGuid().ToString();
                    }
                }
                else
                {
                    transactionId = Guid.NewGuid().ToString();
                }
                // Add Transaction Id to the Request Header
                reqHeaders["X-Moesif-Transaction-Id"] = transactionId;
            }

            var reqBody = new object();

            reqBody = null;
            string requestTransferEncoding;

            requestTransferEncoding = null;
            if (logBody)
            {
                try
                {
                    reqBody = ApiHelper.JsonDeserialize <object>(requestBody);
                }
                catch (Exception inst)
                {
                    if (debug)
                    {
                        Console.WriteLine("About to parse Request body as Base64 encoding");
                    }
                    // Get Request Body
                    reqBody = Base64Encode(requestBody);
                    requestTransferEncoding = "base64";
                }
            }

            string ip = null;

            try
            {
                List <string> proxyHeaders = new List <string> {
                    "X-Client-Ip", "X-Forwarded-For", "Cf-Connecting-Ip", "True-Client-Ip",
                    "X-Real-Ip", "X-Cluster-Client-Ip", "X-Forwarded", "Forwarded-For", "Forwarded"
                };

                if (!proxyHeaders.Intersect(reqHeaders.Keys.ToList(), StringComparer.OrdinalIgnoreCase).Any())
                {
                    ip = request.RemoteIpAddress.ToString();
                }
                else
                {
                    ClientIp helpers = new ClientIp();
                    ip = helpers.GetClientIp(reqHeaders, request);
                }
            }
            catch (Exception inst)
            {
                Console.WriteLine("error encountered while trying to get the client IP address");
                Console.WriteLine(inst);
            }

            var uri = request.Uri.ToString();

            string apiVersion     = null;
            var    apiVersion_out = new object();
            var    getApiVersion  = moesifOptions.TryGetValue("ApiVersion", out apiVersion_out);

            if (getApiVersion)
            {
                apiVersion = apiVersion_out.ToString();
            }

            var eventReq = new EventRequestModel()
            {
                Time             = DateTime.UtcNow,
                Uri              = uri,
                Verb             = request.Method,
                ApiVersion       = apiVersion,
                IpAddress        = ip,
                Headers          = reqHeaders,
                Body             = reqBody,
                TransferEncoding = requestTransferEncoding
            };

            return(eventReq);
        }
Exemple #3
0
        private async Task <EventRequestModel> FormatRequest(HttpRequest request)
        {
            var body = request.Body;

            request.EnableRewind();

            var buffer = new byte[Convert.ToInt32(request.ContentLength)];

            await request.Body.ReadAsync(buffer, 0, buffer.Length);

            var bodyAsText = Encoding.UTF8.GetString(buffer);

            request.Body = body;

            var reqHeaders = new Dictionary <string, string>();

            try
            {
                reqHeaders = request.Headers.ToDictionary(k => k.Key, k => k.Value.ToString(), StringComparer.OrdinalIgnoreCase);
            }
            catch (Exception inst)
            {
                if (debug)
                {
                    Console.WriteLine("error encountered while copying request header");
                    Console.WriteLine(inst);
                }
            }

            // Add Transaction Id to the Request Header
            var transation_id_out    = new Object();
            var captureTransactionId = moesifOptions.TryGetValue("DisableTransactionId", out transation_id_out);

            bool GetCaptureTransactionId = false;

            if (captureTransactionId)
            {
                GetCaptureTransactionId = (bool)(transation_id_out);
            }

            if (!GetCaptureTransactionId)
            {
                if (reqHeaders.ContainsKey("X-Moesif-Transaction-Id"))
                {
                    string reqTransId = reqHeaders["X-Moesif-Transaction-Id"];
                    if (!string.IsNullOrEmpty(reqTransId))
                    {
                        transactionId = reqTransId;
                        if (string.IsNullOrEmpty(transactionId))
                        {
                            transactionId = Guid.NewGuid().ToString();
                        }
                    }
                    else
                    {
                        transactionId = Guid.NewGuid().ToString();
                    }
                }
                else
                {
                    transactionId = Guid.NewGuid().ToString();
                }
                // Add Transaction Id to the Request Header
                reqHeaders["X-Moesif-Transaction-Id"] = transactionId;
            }

            var reqBody = new object();

            reqBody = null;
            try
            {
                reqBody = ApiHelper.JsonDeserialize <object>(bodyAsText);
            }
            catch (Exception inst)
            {
                Console.WriteLine("error encountered while trying to serialize request body");
                Console.WriteLine(inst);
            }

            string ip = null;

            try
            {
                List <string> proxyHeaders = new List <string> {
                    "X-Client-Ip", "X-Forwarded-For", "Cf-Connecting-Ip", "True-Client-Ip",
                    "X-Real-Ip", "X-Cluster-Client-Ip", "X-Forwarded", "Forwarded-For", "Forwarded"
                };

                if (!proxyHeaders.Intersect(reqHeaders.Keys.ToList(), StringComparer.OrdinalIgnoreCase).Any())
                {
                    ip = request.HttpContext.Connection.RemoteIpAddress.ToString();
                }
                else
                {
                    ClientIp helpers = new ClientIp();
                    ip = helpers.GetClientIp(reqHeaders, request);
                }
            }
            catch (Exception inst)
            {
                Console.WriteLine("error encountered while trying to get the client IP address");
                Console.WriteLine(inst);
            }

            var uri = new Uri(request.GetDisplayUrl()).ToString();

            string apiVersion     = null;
            var    apiVersion_out = new object();
            var    getApiVersion  = moesifOptions.TryGetValue("ApiVersion", out apiVersion_out);

            if (getApiVersion)
            {
                apiVersion = apiVersion_out.ToString();
            }

            var eventReq = new EventRequestModel()
            {
                Time       = DateTime.UtcNow,
                Uri        = uri,
                Verb       = request.Method,
                ApiVersion = apiVersion,
                IpAddress  = ip,
                Headers    = reqHeaders,
                Body       = reqBody
            };

            return(eventReq);
        }
        private async Task <(EventRequestModel, String)> ToRequest(IOwinRequest request, string transactionId)
        {
            // Request headers
            var reqHeaders = LoggerHelper.ToHeaders(request.Headers, debug);

            // RequestBody
            string contentEncoding     = "";
            string contentLength       = "";
            int    parsedContentLength = 100000;

            string body = null;

            reqHeaders.TryGetValue("Content-Encoding", out contentEncoding);
            reqHeaders.TryGetValue("Content-Length", out contentLength);
            int.TryParse(contentLength, out parsedContentLength);
            try
            {
                body = await LoggerHelper.GetRequestContents(request, contentEncoding, parsedContentLength, disableStreamOverride);
            }
            catch
            {
                LoggerHelper.LogDebugMessage(debug, "Cannot read request body.");
            }

            var bodyWrapper = LoggerHelper.Serialize(body, request.ContentType);

            // Add Transaction Id to the Request Header
            bool disableTransactionId = LoggerHelper.GetConfigBoolValues(moesifOptions, "DisableTransactionId", false);

            if (!disableTransactionId)
            {
                transactionId = LoggerHelper.GetOrCreateTransactionId(reqHeaders, "X-Moesif-Transaction-Id");
                reqHeaders    = LoggerHelper.AddTransactionId("X-Moesif-Transaction-Id", transactionId, reqHeaders);
            }

            string ip  = clientIpHelper.GetClientIp(reqHeaders, request);
            var    uri = request.Uri.ToString();

            string apiVersion     = null;
            var    apiVersion_out = new object();
            var    getApiVersion  = moesifOptions.TryGetValue("ApiVersion", out apiVersion_out);

            if (getApiVersion)
            {
                apiVersion = apiVersion_out.ToString();
            }

            var eventReq = new EventRequestModel()
            {
                Time             = DateTime.UtcNow,
                Uri              = uri,
                Verb             = request.Method,
                ApiVersion       = apiVersion,
                IpAddress        = ip,
                Headers          = reqHeaders,
                Body             = bodyWrapper.Item1,
                TransferEncoding = bodyWrapper.Item2
            };

            return(eventReq, transactionId);
        }