Beispiel #1
0
        private void ProcessRequest(object state)
        {
            var context = (IHttpListenerContext)state;

            _Logger.Log(LogLevel.Informational, context.Request.HttpMethod + " " + context.Request.RemoteEndPoint + ": " + context.Request.Url);
            try
            {
                try
                {
                    string method = context.Request.HttpMethod;
                    IWebDAVMethodHandler methodHandler;
                    if (!_MethodHandlers.TryGetValue(method, out methodHandler))
                    {
                        throw new HttpMethodNotAllowedException(string.Format(CultureInfo.InvariantCulture, "%s ({0})", context.Request.HttpMethod));
                    }

                    context.Response.AppendHeader("DAV", "1,2,1#extend");
                    methodHandler.ProcessRequest(this, context, Store, _Logger);
                }
                catch (HttpException)
                {
                    throw;
                }
                catch (FileNotFoundException ex)
                {
                    throw new HttpNotFoundException(innerException: ex);
                }
                catch (DirectoryNotFoundException ex)
                {
                    throw new HttpNotFoundException(innerException: ex);
                }
                catch (NotImplementedException ex)
                {
                    throw new HttpNotImplementedException(innerException: ex);
                }
                catch (Exception ex)
                {
                    throw new HttpInternalServerException(innerException: ex);
                }
            }
            catch (HttpException ex)
            {
                context.Response.StatusCode        = ex.StatusCode;
                context.Response.StatusDescription = HttpStatusCodes.GetName(ex.StatusCode);
                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.Close();
            }
            finally
            {
                _Logger.Log(LogLevel.Informational, context.Response.StatusCode + " " + context.Response.StatusDescription + ": " + context.Request.HttpMethod + " " + context.Request.RemoteEndPoint + ": " + context.Request.Url);
            }
        }
Beispiel #2
0
        private static string GetMessage(int statusCode, string message)
        {
            string format = "%s";

            if (!StringEx.IsNullOrWhiteSpace(message))
            {
                format = message;
            }

            return(format.Replace("%s", HttpStatusCodes.GetName(statusCode)));
        }
Beispiel #3
0
        /// <summary>
        /// Sends a simple response with a specified HTTP status code but no content.
        /// </summary>
        /// <param name="context">
        /// The <see cref="IHttpListenerContext"/> to send the response through.
        /// </param>
        /// <param name="statusCode">
        /// The HTTP status code for the response.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="context"/> is <c>null</c>.</para>
        /// </exception>
        public static void SendSimpleResponse(this IHttpListenerContext context, int statusCode = HttpStatusCodes.Successful.OK)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            context.Response.StatusCode        = statusCode;
            context.Response.StatusDescription = HttpStatusCodes.GetName(statusCode);
            context.Response.Close();
        }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HttpException"/> class.
 /// </summary>
 /// <param name="statusCode">
 /// The HTTP status code that this <see cref="HttpException"/> maps to.
 /// </param>
 /// <param name="message">
 /// The exception message stating the reason for the exception being thrown.
 /// </param>
 /// <param name="innerException">
 /// The <see cref="Exception"/> that is the cause for this exception;
 /// or <c>null</c> if no inner exception is specified.
 /// </param>
 public HttpException(int statusCode, string message = null, Exception innerException = null)
     : base(GetMessage(statusCode, message), innerException)
 {
     StatusCode        = statusCode;
     StatusDescription = HttpStatusCodes.GetName(statusCode);
 }
Beispiel #5
0
        public void GetDescription_ForSampleCodes_ReturnsCorrectDescription(int id, string expected)
        {
            string output = HttpStatusCodes.GetName(id);

            Assert.That(output, Is.EqualTo(expected));
        }