Ejemplo n.º 1
0
 public static string Stringify(HttpStatusCode code)
 {
     switch (code)
     {
         case HttpStatusCode.Accepted: return "Accepted";
         case HttpStatusCode.Ambiguous: return "Multiple Choices";
         case HttpStatusCode.BadGateway: return "Bad Gateway";
         case HttpStatusCode.BadRequest: return "Bad Request";
         case HttpStatusCode.Conflict: return "Conflict";
         case HttpStatusCode.Continue: return "Continue";
         case HttpStatusCode.Created: return "Created";
         case HttpStatusCode.ExpectationFailed: return "Expectation Failed";
         case HttpStatusCode.Forbidden: return "Forbidden";
         case HttpStatusCode.Found: return "Found";
         case HttpStatusCode.GatewayTimeout: return "Gateway Timeout";
         case HttpStatusCode.Gone: return "Gone";
         case HttpStatusCode.HttpVersionNotSupported: return "HTTP Version Not Supported";
         case HttpStatusCode.InternalServerError: return "Internal Server Error";
         case HttpStatusCode.LengthRequired: return "Length Required";
         case HttpStatusCode.MethodNotAllowed: return "Method Not Allowed";
         case HttpStatusCode.Moved: return "Moved";
         case HttpStatusCode.NoContent: return "No Content";
         case HttpStatusCode.NonAuthoritativeInformation: return "Non Authoritative Information";
         case HttpStatusCode.NotAcceptable: return "Not Acceptable";
         case HttpStatusCode.NotFound: return "Not Found";
         case HttpStatusCode.NotImplemented: return "Not Implemented";
         case HttpStatusCode.NotModified: return "Not Modified";
         case HttpStatusCode.OK: return "OK";
         case HttpStatusCode.PartialContent: return "Partial Content";
         case HttpStatusCode.PaymentRequired: return "Payment Required";
         case HttpStatusCode.PreconditionFailed: return "Precondition Failed";
         case HttpStatusCode.ProxyAuthenticationRequired: return "Proxy Authentication Required";
         case HttpStatusCode.RedirectKeepVerb: return "Redirect Keep Verb";
         case HttpStatusCode.RedirectMethod: return "Redirect Method";
         case HttpStatusCode.RequestedRangeNotSatisfiable: return "Requested Range Not Satisfiable";
         case HttpStatusCode.RequestEntityTooLarge: return "Request Entity Too Large";
         case HttpStatusCode.RequestTimeout: return "Request Timeout";
         case HttpStatusCode.RequestUriTooLong: return "Request URI Too Long";
         case HttpStatusCode.ResetContent: return "Reset Content";
         case HttpStatusCode.ServiceUnavailable: return "Service Unavailable";
         case HttpStatusCode.SwitchingProtocols: return "Switching Protocols";
         case HttpStatusCode.Unauthorized: return "Unauthorized";
         case HttpStatusCode.UnsupportedMediaType: return "Unsupported Media Type";
         case HttpStatusCode.Unused: return "Unused";
         case HttpStatusCode.UseProxy: return "Use Proxy";
         default: return code.ToString();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Send a response.
        /// </summary>
        /// <param name="httpVersion">Either <see cref="HttpHelper.HTTP10"/> or <see cref="HttpHelper.HTTP11"/></param>
        /// <param name="statusCode">HTTP status code</param>
        /// <param name="reason">reason for the status code.</param>
        /// <param name="body">HTML body contents, can be null or empty.</param>
        /// <param name="contentType">A content type to return the body as, i.e. 'text/html' or 'text/plain', defaults to 'text/html' if null or empty</param>
        /// <exception cref="ArgumentException">If <paramref name="httpVersion"/> is invalid.</exception>
        public void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body, string contentType)
        {
            if (string.IsNullOrEmpty(contentType))
                contentType = "text/html";

            if (string.IsNullOrEmpty(httpVersion) || !httpVersion.StartsWith("HTTP/1"))
                throw new ArgumentException("Invalid HTTP version");

            if (string.IsNullOrEmpty(reason))
                reason = statusCode.ToString();

            string response = string.IsNullOrEmpty(body)
                                  ? httpVersion + " " + (int) statusCode + " " + reason + "\r\n\r\n"
                                  : string.Format("{0} {1} {2}\r\nContent-Type: {5}\r\nContent-Length: {3}\r\n\r\n{4}",
                                                  httpVersion, (int) statusCode, reason ?? statusCode.ToString(),
                                                  body.Length, body, contentType);
            byte[] buffer = Encoding.ASCII.GetBytes(response);

            Send(buffer);
        }
        /// <summary>
        /// Send a response.
        /// </summary>
        /// <param name="httpVersion">Either HttpHelper.HTTP10 or HttpHelper.HTTP11</param>
        /// <param name="statusCode">http status code</param>
        /// <param name="reason">reason for the status code.</param>
        /// <param name="body">html body contents, can be null or empty.</param>
        /// <param name="contentType">A content type to return the body as, ie 'text/html' or 'text/plain', defaults to 'text/html' if null or empty</param>
        /// <exception cref="ArgumentException">If httpVersion is invalid.</exception>
        public void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body, string contentType)
        {
            if (string.IsNullOrEmpty(contentType))
                contentType = "text/html";

            if (string.IsNullOrEmpty(httpVersion))
                throw new ArgumentException("Invalid HTTP version");

            if (string.IsNullOrEmpty(reason))
                reason = statusCode.ToString();

            byte[] buffer;
            if (string.IsNullOrEmpty(body))
            {
                buffer = Encoding.ASCII.GetBytes(String.Format("{0} {1} {2}\r\n\r\n",
                    httpVersion, (int)statusCode, reason));
            }
            else
            {
                buffer = Encoding.ASCII.GetBytes(String.Format("{0} {1} {2}\r\nContent-Type: {3}\r\nContent-Length: {4}\r\n\r\n{5}",
                    httpVersion, (int)statusCode, reason, contentType, body.Length, body));
            }

            Send(buffer);
        }