The Wayback Machine - https://web.archive.org/web/20201017160337/https://github.com/exceptionless/Exceptionless.Net/issues/97
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add owin request info object via middleware #97

Open
zgredas opened this issue May 18, 2016 · 6 comments
Open

Add owin request info object via middleware #97

zgredas opened this issue May 18, 2016 · 6 comments

Comments

@zgredas
Copy link

@zgredas zgredas commented May 18, 2016

"it’s currently not possible to add the request info via middleware in the current form as we expect an HttpActionContext and use the request object from that.. I almost think that the web api plugin should be refactored a little bit to be a bit more generic and maybe provide overloads for both OwinContext and HttpActionContext as well as helpers for specifying it on the event builder.

One work around for the time being would be to add an extension method to populate it from an OwinContext"

@niemyjski
Copy link
Member

@niemyjski niemyjski commented May 18, 2016

@zgredas as a short term work around you could populate the request info object from the IOwinContext and populate it like this: ex.ToExceptionless().AddObject(Event.KnownDataKeys.RequestInfo, GetRequestInfo(context.Request)).Submit();

Would be a huge help if you could send a snippet of the request info being populated from the IOwinRequest. We could then work this into the client.

@zgredas
Copy link
Author

@zgredas zgredas commented May 19, 2016

@niemyjski please take a look on the following code

    public static RequestInfo ToRequestInfo(this IOwinRequest request, IEnumerable<string> exclusions = null)
    {
        if (request == null)
            return null;

        var info = new RequestInfo
        {
            ClientIpAddress = request.LocalIpAddress,
            HttpMethod = request.Method
        };

        if (request.Headers["User-Agent"] != null)
            info.UserAgent = request.Headers["User-Agent"];

        if (request.Uri != null)
        {
            info.Host = request.Uri.Host;
            info.IsSecure = request.Uri.Scheme == "https";
            info.Path = IsNullOrEmpty(request.Uri.LocalPath) ? "/" : request.Uri.LocalPath;
            info.Port = request.Uri.Port;
        }

        if (request.Headers["Referrer"] != null)
            info.Referrer = request.Headers["Referrer"];

        if (exclusions == null) exclusions = new List<string>();
        var exclusionList = exclusions as string[] ?? exclusions.ToArray();
        info.Cookies = request.Cookies.ToDictionary(exclusionList);
        info.QueryString = request.Uri.ParseQueryString().ToDictionary(exclusionList);

        return info;
    }

Which value should I use for ClientIpAddress?
Is it request.LocalIpAddress or request.RemoteIpAddress?

@niemyjski
Copy link
Member

@niemyjski niemyjski commented May 19, 2016

It should be the clients remote ip address

@zgredas
Copy link
Author

@zgredas zgredas commented May 19, 2016

@niemyjski ok, so it leaves us with the following

public static RequestInfo ToRequestInfo(this IOwinRequest request, IEnumerable<string> exclusions = null)
    {
        if (request == null)
            return null;

        var info = new RequestInfo
        {
            ClientIpAddress = request.RemoteIpAddress,
            HttpMethod = request.Method
        };

        if (request.Headers["User-Agent"] != null)
            info.UserAgent = request.Headers["User-Agent"];

        if (request.Uri != null)
        {
            info.Host = request.Uri.Host;
            info.IsSecure = request.Uri.Scheme == "https";
            info.Path = IsNullOrEmpty(request.Uri.LocalPath) ? "/" : request.Uri.LocalPath;
            info.Port = request.Uri.Port;
        }

        if (request.Headers["Referrer"] != null)
            info.Referrer = request.Headers["Referrer"];

        if (exclusions == null) exclusions = new List<string>();
        var exclusionList = exclusions as string[] ?? exclusions.ToArray();
        info.Cookies = request.Cookies.ToDictionary(exclusionList);
        info.QueryString = request.Uri.ParseQueryString().ToDictionary(exclusionList);

        return info;
    }

    private static readonly List<string> _ignoredFormFields = new List<string> {
        "__*"
    };

    private static readonly List<string> _ignoredCookies = new List<string> {
        ".ASPX*",
        "__*",
        "*SessionId*"
    };

    public static Dictionary<string, string> ToDictionary(this NameValueCollection values, IEnumerable<string> exclusions)
    {
        var d = new Dictionary<string, string>();

        var patternsToMatch = exclusions as string[] ?? exclusions.ToArray();
        foreach (string key in values.AllKeys)
        {
            if (IsNullOrEmpty(key) || key.AnyWildcardMatches(_ignoredFormFields, true) || key.AnyWildcardMatches(patternsToMatch, true))
                continue;

            try
            {
                string value = values.Get(key);
                d.Add(key, value);
            }
            catch (Exception ex)
            {
                if (!d.ContainsKey(key))
                    d.Add(key, ex.Message);
            }
        }

        return d;
    }

    public static Dictionary<string, string> ToDictionary(this RequestCookieCollection cookies, IEnumerable<string> exclusions)
    {
        var d = new Dictionary<string, string>();

        foreach (var cookie in cookies.Where(c => !IsNullOrEmpty(c.Key) && !c.Key.AnyWildcardMatches(_ignoredCookies, true) && !c.Key.AnyWildcardMatches(exclusions, true))) 
        {
                if (!d.ContainsKey(cookie.Key))
                    d.Add(cookie.Key, cookie.Value);
        }

        return d;
    }
@niemyjski
Copy link
Member

@niemyjski niemyjski commented Sep 23, 2016

@niemyjski niemyjski changed the title Add request info object via middleware Add owin request info object via middleware Sep 23, 2016
@niemyjski
Copy link
Member

@niemyjski niemyjski commented Sep 23, 2016

@zgredas would you mind submitting a pr for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.