Example #1
0
 public void Exception_NotFound_Test()
 {
     var exception = new WebDavNotFoundException();
     Assert.AreEqual((int)HttpStatusCode.NotFound, exception.StatusCode);
 }
        private void ProcessRequest(object state)
        {
            DateTime startTime = DateTime.Now;

            IHttpListenerContext context = (IHttpListenerContext) state;
            string url = context.Request.Url.ToString().ToLower();

            if (url.EndsWith("desktop.ini") ||
                url.EndsWith("folder.gif") ||
                url.EndsWith("folder.jpg") ||
                url.EndsWith("thumbs.db"))
            {
                WebDavNotFoundException ex = new WebDavNotFoundException();
                context.Response.StatusCode = ex.StatusCode;
                context.Response.StatusDescription = ex.StatusDescription;
                if (ex.Message != context.Response.StatusDescription)
                {
                    byte[] buffer = Encoding.UTF8.GetBytes(ex.Message);
                    context.Response.ContentEncoding = Encoding.UTF8;
                    context.Response.ContentLength64 = buffer.Length;
                    context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                    context.Response.OutputStream.Flush();
                }

                context.Response.Close();
                return;
            }

            var ident = _listener.GetIdentity(context);
            Thread.SetData(Thread.GetNamedDataSlot(WebDavServer.HttpUser), ident);
            _store.UserAuthenticated(ident);

            string method = context.Request.HttpMethod;

#if DEBUG
            
            _log.Warn("");
            _log.Warn("!!!!!!!!!!!!!!!!!!!!!! BEGIN Method Handler: " + method + "!!!!!!!!!!!!!!!!!!!!!!");
#endif
            try
            {
                try
                {
                    IWebDavMethodHandler methodHandler;

                    if (!_methodHandlers.TryGetValue(method, out methodHandler))
                        throw new WebDavMethodNotAllowedException(string.Format(CultureInfo.InvariantCulture, "%s ({0})", context.Request.HttpMethod));

                    context.Response.AppendHeader("DAV", "1,2,1#extend");

                    methodHandler.ProcessRequest(_server, context, _store);
                }
                catch (WebDavException)
                {
                    throw;
                }
                catch (UnauthorizedAccessException)
                {
                    throw new WebDavUnauthorizedException();
                }
                catch (FileNotFoundException ex)
                {
#if DEBUG
                    _log.Warn(ex.Message);
#endif
                    throw new WebDavNotFoundException(innerException: ex);
                }
                catch (DirectoryNotFoundException ex)
                {
#if DEBUG
                    _log.Warn(ex.Message);
#endif
                    throw new WebDavNotFoundException(innerException: ex);
                }
                catch (NotImplementedException ex)
                {
#if DEBUG
                    _log.Warn(ex.Message);
#endif
                    throw new WebDavNotImplementedException(innerException: ex);
                }
                catch (Exception ex)
                {
#if DEBUG
                    _log.Warn(ex.Message);
#endif
                    throw new WebDavInternalServerException(innerException: ex);
                }
            }
            catch (WebDavException ex)
            {
                if (ex.Message != "Not Found")
                {
                    Console.WriteLine(ex.Message + ex.StackTrace);
                    if (ex.InnerException != null)
                        Console.WriteLine(ex.InnerException.Message + ex.InnerException.StackTrace);
                }
#if DEBUG
                _log.Warn(ex.StatusCode + " " + ex.Message);
#else
                //Console.WriteLine("Method: " + method + " Processing URL: " + context.Request.Url + " Threw Exception: " + ex.Message);
#endif
                context.Response.StatusCode = ex.StatusCode;
                context.Response.StatusDescription = ex.StatusDescription;
                if (ex.Message != context.Response.StatusDescription)
                {
                    byte[] buffer = Encoding.UTF8.GetBytes(ex.Message);
                    context.Response.ContentEncoding = Encoding.UTF8;
                    context.Response.ContentLength64 = buffer.Length;
                    context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                    context.Response.OutputStream.Flush();
                }

                context.Response.Close();
            }

            finally
            {
#if DEBUG
                _log.Warn("!!!!!!!!!!!!!!!!!!!!!! END Method Handler: " + method + "!!!!!!!!!!!!!!!!!!!!!! ------------->" + ((DateTime.Now - startTime).Milliseconds / 1000.00) + " seconds.");
#endif
            }
        }