HttpModule Extends WebDAV - Throwing 500

半腔热情 提交于 2019-12-13 03:05:29

问题


I have an HttpModule with the following event handler installed on our WebDAV server. Many of our clients PUT files up to our server using the file creation semantic, they append the name of the file to create on the url, and the file is created by WebDAV on the server. Standard WebDAV. Some of our clients however don't have systems which can generate a dynamic file name on every call. They want a more traditional Webhook to POST (not PUT) to a static URL. This event handler, which fires after a successful authentication takes care of this. This event handler is working for its intended purpose. WebDAV PUTs, DELETEs, PROPFINDs are all working. The POST is handled by this function. files are uploading to our server via either PUT or POST. So far so good.

One thing has stopped working though. I am no longer able to open an Excel file via my Windows drive which is mapped via WebDAV. I can copy files to and from however when I try to open an Excel directly from the WebDAV drive mapping document I get an error. While this is a fringe use case, I care more about the fact that something isn't 100% correct on the server and I'd like to get this corrected if I can. Logs and diagnostics are below the code snippet.

A couple of points about the code.

  1. It specifically looks for the POST verb. If the current transaction isn't a POST it exits without doing anything. So I can't see how this would interfere with anything other than a POST.

  2. The entire function is wrapped within a try block with an empty catch handler. I don't see how its possible to get a server 500 error out of this function.

    private void OnPostAuthenticateRequest(object sender, EventArgs e)
    {
        try
        {
            HttpContext ctx = HttpContext.Current;
    
            if (ctx.Request.RequestType == "POST")
            {
                try
                {
                    string folder = ctx.Request.PhysicalPath;
                    if (folder.EndsWith("\\"))
                    {
                        folder = folder.Substring(0, folder.Length - 1);
                    }
                    string[] subs = folder.Split('\\');
                    string[] logon = ctx.Request.LogonUserIdentity.Name.Split('\\');
    
                    if (logon.Length > 0 && subs.Length > 1 && subs[subs.Length - 2].ToLower() == logon[logon.Length - 1].ToLower())
                    {
                        string body;
                        using (Stream inputStream = ctx.Request.InputStream)
                        {
                            inputStream.Position = 0;
                            using (StreamReader readStream = new StreamReader(inputStream, Encoding.UTF8))
                            {
                                body = readStream.ReadToEnd();
                            }
                        }
    
                        string file = ctx.Request.Params["filename"];
    
                        if (file == null || file.Trim() == "")
                        {
                            file = ctx.Request.Headers["filename"];
                        }
    
                        if (file == null || file.Trim() == "")
                        {
                            file = logon[logon.Length - 1].Replace("_", "") + "_" + subs[subs.Length - 1].ToLower() + "_" + Guid.NewGuid() + ".txt";
                        }
    
                        if (File.Exists(Path.Combine(folder, file)))
                        {
                            ctx.Response.StatusCode = 409;
                        }
                        else
                        {
                            File.WriteAllText(Path.Combine(folder, file), body);
                            ctx.Response.StatusCode = 200;
                        }
                    }
                    else
                    {
                        ctx.Response.StatusCode = 400;
                    }
    
                    ctx.ApplicationInstance.CompleteRequest();
                }
                catch (Exception ex)
                {
                    ctx.Response.StatusCode = 400;
                    ctx.ApplicationInstance.CompleteRequest();
                }
            }
        }
        catch
        {
            // Exit gracefully
        }
    }
    

Three log entries for each attempted open of the workbook. First one returns a 401 and then the second two, which are accompanied by credentials, and will thus enter into my Webhook module, both fail with 500's.

2019-05-09 03:04:25 W3SVC1 myhost 10.0.0.6 HEAD /Admin/workbook.xlsm - 443 - 68.199.109.17 HTTP/1.1 Microsoft+Office+Excel+2014 _ga=GA1.2.904248333.1551983251 - myhost.mydomain.com 401 2 5 293 321 15
2019-05-09 03:04:25 W3SVC1 myhost 10.0.0.6 HEAD /Admin/workbook.xlsm - 443 myaccount 68.199.109.17 HTTP/1.1 Microsoft+Office+Excel+2014 _ga=GA1.2.904248333.1551983251 - myhost.mydomain.com 500 0 0 325 345 62
2019-05-09 03:04:25 W3SVC1 myhost 10.0.0.6 OPTIONS /Admin/workbook.xlsm - 443 myaccount 68.199.109.17 HTTP/1.1 Microsoft+Office/16.0+(Windows+NT+10.0;+Microsoft+Excel+16.0.11601;+Pro) _ga=GA1.2.904248333.1551983251 - myhost.mydomain.com 500 0 0 3733 390 31

As mentioned above, I don't see how it's possible to get a 500 out of my event handler because of the covering try/catch construct.

I have Failed Request Tracing enabled. Here is just one small abstract from the verbose output: (I changed angle brackets to square brackets.)

[EventData]
   [Data Name="ContextId"]{80007F2B-0001-F800-B63F-84710C7967BB}[/Data]
   [Data Name="ModuleName"]Webhook[/Data]
   [Data Name="Notification"]2[/Data]
   [Data Name="HttpStatus"]500[/Data]
   [Data Name="HttpReason"]Internal Server Error[/Data]
   [Data Name="HttpSubStatus"]0[/Data]
   [Data Name="ErrorCode"]0[/Data]
   [Data Name="ConfigExceptionInfo"][/Data]
[/EventData]

So that's my module - Webhook. But I have no idea how it could be throwing an exception which would be the cause of the 500.

If you tell me what to look for or have a specific question I can provide additional info.

Thank you for any advice which you can provide!

来源:https://stackoverflow.com/questions/56052000/httpmodule-extends-webdav-throwing-500

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!