Example #1
0
 /// <summary>
 /// Create a new HTTPResult when an error occurred.
 /// </summary>
 /// <param name="HTTPRequest"></param>
 /// <param name="HTTPStatusCode"></param>
 /// <param name="Error">The HTTPResponse for this error.</param>
 public HTTPResult(HTTPRequest HTTPRequest, HTTPStatusCode HTTPStatusCode, String Reason = null, String ETag = null)
 {
     this.Error     = null;
     this.Data      = default(TData);
     this.ValidData = false;
     this.ETag      = ETag;
     this.Error     = HTTPErrorResponse(HTTPRequest, HTTPStatusCode, Reason, ETag);
 }
Example #2
0
        public HTTPErrorAttribute(HTTPMethods HTTPMethod, String UriTemplate, HTTPStatusCode myHTTPStatusCode)
        {
            this.HTTPMethod     = de.ahzf.Vanaheimr.Hermod.HTTP.HTTPMethod.ParseEnum(HTTPMethod);

            if (this.HTTPMethod == null)
                throw new ArgumentNullException("Invalid HTTPMethod!");

            this.UriTemplate    = UriTemplate;
            this.HTTPStatusCode = myHTTPStatusCode;
        }
Example #3
0
            public HTTPResponse()
            {
                Version = null;

                StatusCode = null;

                Headers = new List<HTTPHeader>();

                Content = null;
            }
Example #4
0
            public HTTPResponse(string ver, HTTPStatusCode code, List<HTTPHeader> headers)
            {

                Version = ver;

                StatusCode = code;

                Headers = headers;

            }
Example #5
0
 /// <summary>
 /// Create a new HTTP response.
 /// </summary>
 /// <param name="HTTPRequest">The HTTP request for this response.</param>
 /// <param name="HTTPStatusCode">A HTTP status code</param>
 public HTTPResponseBuilder(HTTPRequest     HTTPRequest,
                            HTTPStatusCode  HTTPStatusCode = null)
 {
     this._HTTPRequest       = HTTPRequest;
     this._HTTPStatusCode    = HTTPStatusCode;
     this._Timestamp         = DateTime.Now;
     this.ProtocolName       = "HTTP";
     this.ProtocolVersion    = new HTTPVersion(1, 1);
     this.CancellationToken  = HTTPRequest?.CancellationToken ?? new CancellationTokenSource().Token;
     base.EventTrackingId    = HTTPRequest?.EventTrackingId   ?? EventTracking_Id.New;
 }
Example #6
0
 /// <summary>
 /// Create a new HTTP response.
 /// </summary>
 /// <param name="HTTPRequest">The HTTP request for this response.</param>
 /// <param name="HTTPStatusCode">A HTTP status code</param>
 public HTTPResponseBuilder(HTTPRequest HTTPRequest,
                            HTTPStatusCode HTTPStatusCode = null)
 {
     this._HTTPRequest      = HTTPRequest;
     this._HTTPStatusCode   = HTTPStatusCode;
     this._Timestamp        = DateTime.Now;
     this.ProtocolName      = "HTTP";
     this.ProtocolVersion   = new HTTPVersion(1, 1);
     this.CancellationToken = HTTPRequest?.CancellationToken ?? new CancellationTokenSource().Token;
     base.EventTrackingId   = HTTPRequest?.EventTrackingId ?? EventTracking_Id.New;
 }
Example #7
0
        // Error Handling

        #region (internal) GetErrorHandler(Host, URL, HTTPMethod = null, HTTPContentType = null, HTTPStatusCode = null)

        /// <summary>
        /// Return the best matching error handler for the given parameters.
        /// </summary>
        internal Tuple <MethodInfo, IEnumerable <Object> > GetErrorHandler(String Host,
                                                                           String URL,
                                                                           HTTPMethod HTTPMethod           = null,
                                                                           HTTPContentType HTTPContentType = null,
                                                                           HTTPStatusCode HTTPStatusCode   = null)

        {
            lock (Lock)
            {
                return(null);
            }
        }
Example #8
0
        public HTTPErrorAttribute(HTTPMethods HTTPMethod, String UriTemplate, HTTPStatusCode myHTTPStatusCode)
        {
            this.HTTPMethod = org.GraphDefined.Vanaheimr.Hermod.HTTP.HTTPMethod.ParseEnum(HTTPMethod);

            if (this.HTTPMethod == null)
            {
                throw new ArgumentNullException("Invalid HTTPMethod!");
            }

            this.UriTemplate    = UriTemplate;
            this.HTTPStatusCode = myHTTPStatusCode;
        }
Example #9
0
        private void NotifyErrors(HTTPRequest HTTPRequest,
                                  TCPConnection TCPConnection,
                                  DateTime Timestamp,
                                  HTTPStatusCode HTTPStatusCode,
                                  HTTPRequest Request     = null,
                                  HTTPResponse Response   = null,
                                  String Error            = null,
                                  Exception LastException = null,
                                  Boolean CloseConnection = true)
        {
            #region Call OnError delegates

            var ErrorLogLocal = ErrorLog;
            if (ErrorLogLocal != null)
            {
                ErrorLogLocal(this, Timestamp, Request, Response, Error, LastException);
            }

            #endregion

            #region Send error page to HTTP client

            var Content = String.Empty;

            if (Error != null)
            {
                Content += Error + Environment.NewLine;
            }

            if (LastException != null)
            {
                Content += LastException.Message + Environment.NewLine;
            }

            var _HTTPResponse = new HTTPResponseBuilder(HTTPRequest)
            {
                HTTPStatusCode = HTTPStatusCode,
                Date           = Timestamp,
                Content        = Content.ToUTF8Bytes()
            };

            TCPConnection.WriteLineToResponseStream(_HTTPResponse.ToString());

            if (CloseConnection)
            {
                TCPConnection.Close();
            }

            #endregion
        }
        public override string ToString()
        {
            string errorText = base.ToString() + Environment.NewLine +
                               "ErrorTitle: " + ErrorTitle ?? "<none>" + Environment.NewLine +
                               "ErrorDescription: " + ErrorDescription ?? "<none>" + Environment.NewLine +
                               "HTTPStatusCode: " + (HTTPStatusCode?.ToString() ?? "<none>") + Environment.NewLine +
                               "RequestUrl: " + RequestUrl ?? "<none>";

            if (ExtraData != null)
            {
                foreach (var kv in ExtraData)
                {
                    errorText += Environment.NewLine + kv.Key + ": " + kv.Value;
                }
            }

            return(errorText);
        }
Example #11
0
 /// <summary>
 /// Return the best matching error handler for the given parameters.
 /// </summary>
 public Tuple<MethodInfo, IEnumerable<Object>> GetErrorHandler(String myHost, String myURL, HTTPMethod myHTTPMethod = null, HTTPContentType myHTTPContentType = null, HTTPStatusCode myHTTPStatusCode = null)
 {
     return null;
 }
Example #12
0
 /// <summary>
 /// Set the HTTP status code.
 /// </summary>
 /// <param name="HTTPStatusCode">A HTTP status code.</param>
 public HTTPRequestBuilder SetHTTPStatusCode(HTTPStatusCode HTTPStatusCode)
 {
     this.HTTPStatusCode = HTTPStatusCode;
     return this;
 }
Example #13
0
        /// <summary>
        /// Return a HTTP error response using the best-matching content type.
        /// </summary>
        /// <param name="HTTPRequest">The HTTP request.</param>
        /// <param name="StatusCode">A HTTP status code.</param>
        /// <param name="Reasons">Optional application side reasons for this error.</param>
        public static HTTPResponse HTTPErrorResponse_old(HTTPRequest HTTPRequest, HTTPStatusCode StatusCode, String Reasons = null)
        {
            #region Initial checks

            if (StatusCode == null)
            {
                return(HTTPErrorResponse_old(HTTPRequest, HTTPStatusCode.InternalServerError, "Calling the HTTPError lead to an error!"));
            }

            var Content     = String.Empty;
            var ContentType = HTTPRequest.Accept.BestMatchingContentType(HTTPContentType.JSON_UTF8,
                                                                         HTTPContentType.HTML_UTF8,
                                                                         HTTPContentType.TEXT_UTF8,
                                                                         HTTPContentType.XML_UTF8);

            #endregion

            #region JSON_UTF8

            // {
            //     "error": {
            //         "code"    : 400
            //         "message" : "Bad Request"
            //         "reason"  : "The first paramter is not a valid number!"
            //     }
            // }
            if (ContentType == HTTPContentType.JSON_UTF8)
            {
                Content = (Reasons == null) ? "{ \"error\": { \"code\" : " + StatusCode.Code + ", \"message\" : \"" + StatusCode.Name + "\" } }" :
                          "{ \"error\": { \"code\" : " + StatusCode.Code + ", \"message\" : \"" + StatusCode.Name + "\", \"reasons\" : \"" + Reasons + "\" } }";
            }

            #endregion

            #region HTML_UTF8

            //<!doctype html>
            //<html>
            //  <head>
            //    <meta charset="UTF-8">
            //    <title>Error 400 - Bad Request</title>
            //  </head>
            //  <body>
            //    <h1>Error 400 - Bad Request</h1>
            //    The first paramter is not a valid number!
            //  </body>
            //</html>
            else if (ContentType == HTTPContentType.HTML_UTF8)
            {
                Content = (Reasons == null) ? "<!doctype html><html><head><meta charset=\"UTF-8\"><title>Error " + StatusCode.Code + " - " + StatusCode.Name + "</title></head><body><h1>Error " + StatusCode.Code + " - " + StatusCode.Name + "</h1></body></html>" :
                          "<!doctype html><html><head><meta charset=\"UTF-8\"><title>Error " + StatusCode.Code + " - " + StatusCode.Name + "</title></head><body><h1>Error " + StatusCode.Code + " - " + StatusCode.Name + "</h1>" + Reasons + "</body></html>";
            }

            #endregion

            #region TEXT_UTF8

            // Error 400 - Bad Request
            // The first paramter is not a valid number!
            else if (ContentType == HTTPContentType.TEXT_UTF8 || ContentType == HTTPContentType.ALL)
            {
                Content = (Reasons == null) ? "Error " + StatusCode.Code + " - " + StatusCode.Name :
                          "Error " + StatusCode.Code + " - " + StatusCode.Name + Environment.NewLine + Reasons;
            }

            #endregion

            #region XML_UTF8

            // <?xml version="1.0" encoding="UTF-8"?>
            // <error>
            //     <code>400</code>
            //     <message>Bad Request</message>
            //     <reason>The first paramter is not a valid number!</message>
            // </error>
            else if (ContentType == HTTPContentType.XML_UTF8)
            {
                Content = (Reasons == null) ? "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error><code>" + StatusCode.Code + "</code><message>" + StatusCode.Name + "</message></error></xml>" :
                          "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error><code>" + StatusCode.Code + "</code><message>" + StatusCode.Name + "</message><reasons>" + Reasons + "</reasons></error></xml>";
            }

            #endregion

            #region GEXF+XML

            // <?xml version="1.0" encoding="UTF-8"?>
            // <gexf xmlns=\"http://www.gexf.net/1.2draft\" version="1.2">
            //   <meta lastmodifieddate="2009-03-20">
            //     <creator>Vanaheimr Walkyr</creator>
            //     <description>HTTP Error</description>
            //   </meta>
            //   <graph mode="static" defaultedgetype="directed">
            //     <attributes class="edge">
            //       <attribute id="0" title="Reasons" type="string"/>
            //     </attributes>
            //     <nodes>
            //       <node id="Request" label="Request" />
            //       <node id="Error"   label="Error 400 - Bad Request" />
            //     </nodes>
            //     <edges>
            //       <edge id="0" source="Request" target="Error">
            //         <attvalues>
            //           <attvalue for="0" value="The first paramter is not a valid number!"/>
            //         </attvalues>
            //       <edge>
            //     </edges>
            //   </graph>
            // </gexf>
            //     <reason></message>
            else if (ContentType == HTTPContentType.GEXF_UTF8)
            {
                Content = (Reasons == null) ?
                          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                          "<gexf xmlns=\"http://www.gexf.net/1.2draft\" version=\"1.2\">" +
                          "<meta lastmodifieddate=\"2009-03-20\"><creator>Vanaheimr Walkyr</creator><description>HTTP Error</description></meta>" +
                          "<graph mode=\"static\" defaultedgetype=\"directed\">" +
                          "<nodes>" +
                          "<node id=\"Request\" label=\"Request\" />" +
                          "<node id=\"Error\"   label=\"Error " + StatusCode.Code + " - " + StatusCode.Name + "\" />" +
                          "</nodes><edges>" +
                          "<edge id=\"0\" source=\"Request\" target=\"Error\" />" +
                          "</edges></graph></gexf>" :

                          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                          "<gexf xmlns=\"http://www.gexf.net/1.2draft\" version=\"1.2\">" +
                          "<meta lastmodifieddate=\"2009-03-20\"><creator>Vanaheimr Walkyr</creator><description>HTTP Error</description></meta>" +
                          "<graph mode=\"static\" defaultedgetype=\"directed\">" +
                          "<attributes class=\"edge\">" +
                          "  <attribute id=\"0\" title=\"Reasons\" type=\"string\" />" +
                          "</attributes> " +
                          "<nodes>" +
                          "<node id=\"Request\" label=\"Request\" />" +
                          "<node id=\"Error\"   label=\"Error " + StatusCode.Code + " - " + StatusCode.Name + "\" />" +
                          "</nodes><edges>" +
                          "<edge id=\"0\" source=\"Request\" target=\"Error\">" +
                          "<attvalues>" +
                          "<attvalue for=\"0\" value=\"" + Reasons + "\" />" +
                          "</attvalues>" +
                          "<edge>" +
                          "</edges></graph></gexf>";
            }

            #endregion

            var x = new HTTPResponseBuilder(HTTPRequest)
            {
                HTTPStatusCode = StatusCode,
                CacheControl   = "no-cache",
                Connection     = "close",
                Content        = Content.ToUTF8Bytes()
            };

            return(x);
        }
Example #14
0
 /// <summary>
 /// Set the HTTP status code.
 /// </summary>
 /// <param name="HTTPStatusCode">A HTTP status code.</param>
 public HTTPResponseBuilder SetHTTPStatusCode(HTTPStatusCode HTTPStatusCode)
 {
     this.HTTPStatusCode = HTTPStatusCode;
     return this;
 }
Example #15
0
 /// <summary>
 /// Sends a nicely-formatted error with specified HTTP Error Code and Error Message
 /// to the client. This response is not buffered and thus calling this method will 
 /// immediately send the response to the client.
 /// </summary>
 /// <param name="errorCode">The HTTP Status Code to send</param>
 /// <param name="errorMessage">The Error Message to send. For example "Not Found" or "Not Supported"</param>
 public void WriteError(HTTPStatusCode errorCode, string errorMessage)
 {
     SendError((int)errorCode, errorMessage);
 }
Example #16
0
 public DataResponse(byte[] data, HTTPStatusCode status) : this(data, (Int32)status)
 {
 }
Example #17
0
 /// <summary>
 /// Sends a custom-formatted error with specified HTTP Error Code, Error Message
 /// Response Content and mime type
 /// to the client. This response is not buffered and thus calling this method will 
 /// immediately send the response to the client.
 /// </summary>
 /// <param name="errorCode">The HTTP Status Code to send</param>
 /// <param name="errorMessage">The Error Message to send. For example "Not Found" or "Not Supported"</param>
 public void WriteError(HTTPStatusCode errorCode, string errorMessage, string errorContent, string mimeType)
 {
     SendError((int)errorCode, errorMessage, errorContent, mimeType);
 }
Example #18
0
 public RedirectResponse(string route, HTTPStatusCode status = HTTPStatusCode.SeeOther) : this(route, (Int32)status)
 {
 }
Example #19
0
 public StringResponse(string response, HTTPStatusCode status = HTTPStatusCode.OK) : this(response, (Int32)status)
 {
 }
Example #20
0
 public HTTPErrorAttribute(HTTPMethod HTTPMethod, String UriTemplate, HTTPStatusCode myHTTPStatusCode)
 {
     this.HTTPMethod     = HTTPMethod;
     this.UriTemplate    = UriTemplate;
     this.HTTPStatusCode = myHTTPStatusCode;
 }
Example #21
0
        public HTTPResponse HTTPErrorResponse(HTTPRequest HTTPRequest, HTTPStatusCode StatusCode, String Reason = null, String ETag = null)
        {
            #region Initial checks

            if (StatusCode == null)
            {
                return(HTTPErrorResponse(HTTPRequest, HTTPStatusCode.InternalServerError, "Calling the HTTPError lead to an error!"));
            }

            var Content     = String.Empty;
            var ContentType = HTTPRequest.Accept.BestMatchingContentType(HTTPContentType.JSON_UTF8,
                                                                         HTTPContentType.HTML_UTF8,
                                                                         HTTPContentType.TEXT_UTF8,
                                                                         HTTPContentType.XML_UTF8);

            #endregion

            #region JSON_UTF8

            // {
            //     "error": {
            //         "code"    : 400
            //         "message" : "Bad Request"
            //         "reason"  : "The first paramter is not a valid number!"
            //     }
            // }
            if (ContentType == HTTPContentType.JSON_UTF8)
            {
                Content = (Reason == null) ? "{\r\n  \"error\":\r\n  {\r\n    \"code\": " + StatusCode.Code + ",\r\n    \"message\": \"" + StatusCode.Name + "\"\r\n  }\r\n}" :
                          "{\r\n  \"error\":\r\n  {\r\n    \"code\": " + StatusCode.Code + ",\r\n    \"message\": \"" + StatusCode.Name + "\",\r\n    \"reason\": \"" + Reason + "\"\r\n  }\r\n}";
            }

            #endregion

            #region HTML_UTF8

            //<!doctype html>
            //<html>
            //  <head>
            //    <meta charset="UTF-8">
            //    <title>Error 400 - Bad Request</title>
            //  </head>
            //  <body>
            //    <h1>Error 400 - Bad Request</h1>
            //    The first paramter is not a valid number!
            //  </body>
            //</html>
            else if (ContentType == HTTPContentType.HTML_UTF8)
            {
                Content = (Reason == null) ? "<!doctype html><html><head><meta charset=\"UTF-8\"><title>Error " + StatusCode.Code + " - " + StatusCode.Name + "</title></head><body><h1>Error " + StatusCode.Code + " - " + StatusCode.Name + "</h1></body></html>" :
                          "<!doctype html><html><head><meta charset=\"UTF-8\"><title>Error " + StatusCode.Code + " - " + StatusCode.Name + "</title></head><body><h1>Error " + StatusCode.Code + " - " + StatusCode.Name + "</h1>" + Reason + "</body></html>";
            }

            #endregion

            #region TEXT_UTF8

            // Error 400 - Bad Request
            // The first paramter is not a valid number!
            else if (ContentType == HTTPContentType.TEXT_UTF8 || ContentType == HTTPContentType.ALL)
            {
                Content = (Reason == null) ? "Error " + StatusCode.Code + " - " + StatusCode.Name :
                          "Error " + StatusCode.Code + " - " + StatusCode.Name + Environment.NewLine + Reason;
            }

            #endregion

            #region XML_UTF8

            // <?xml version="1.0" encoding="UTF-8"?>
            // <error>
            //     <code>400</code>
            //     <message>Bad Request</message>
            //     <reason>The first paramter is not a valid number!</message>
            // </error>
            else if (ContentType == HTTPContentType.XML_UTF8)
            {
                Content = (Reason == null) ? "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error><code>" + StatusCode.Code + "</code><message>" + StatusCode.Name + "</message></error></xml>" :
                          "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error><code>" + StatusCode.Code + "</code><message>" + StatusCode.Name + "</message><reasons>" + Reason + "</reasons></error></xml>";
            }

            #endregion

            var response = new HTTPResponseBuilder(HTTPRequest)
            {
                Date           = DateTime.Now,
                HTTPStatusCode = StatusCode,
                CacheControl   = "no-cache",
                Connection     = "close",
                ContentType    = ContentType,
                Content        = Content.ToUTF8Bytes()
            };

            if (ETag != null)
            {
                response.ETag = ETag;
            }

            return(response);
        }
Example #22
0
 public HandlingException(HTTPStatusCode status, string errorMessage)
 {
     Status       = status;
     ErrorMessage = errorMessage;
 }
Example #23
0
 /// <summary>
 /// Set the HTTP status code.
 /// </summary>
 /// <param name="HTTPStatusCode">A HTTP status code.</param>
 public HTTPRequestBuilder SetHTTPStatusCode(HTTPStatusCode HTTPStatusCode)
 {
     this.HTTPStatusCode = HTTPStatusCode;
     return(this);
 }
Example #24
0
        // Error Handling

        #region (internal) GetErrorHandler(Host, URL, HTTPMethod = null, HTTPContentType = null, HTTPStatusCode = null)

        /// <summary>
        /// Return the best matching error handler for the given parameters.
        /// </summary>
        internal Tuple<MethodInfo, IEnumerable<Object>> GetErrorHandler(String           Host,
                                                                        String           URL,
                                                                        HTTPMethod       HTTPMethod       = null,
                                                                        HTTPContentType  HTTPContentType  = null,
                                                                        HTTPStatusCode   HTTPStatusCode   = null)

        {

            lock (Lock)
            {
                return null;
            }

        }
Example #25
0
 /// <summary>
 /// Sends a nicely-formatted error with specified HTTP Error Code and Error Message
 /// to the client. This response is not buffered and thus calling this method will
 /// immediately send the response to the client.
 /// </summary>
 /// <param name="errorCode">The HTTP Status Code to send</param>
 /// <param name="errorMessage">The Error Message to send. For example "Not Found" or "Not Supported"</param>
 public void WriteError(HTTPStatusCode errorCode, string errorMessage)
 {
     SendError((int)errorCode, errorMessage);
 }
Example #26
0
 /// <summary>
 /// Sends a custom-formatted error with specified HTTP Error Code, Error Message
 /// Response Content and mime type
 /// to the client. This response is not buffered and thus calling this method will
 /// immediately send the response to the client.
 /// </summary>
 /// <param name="errorCode">The HTTP Status Code to send</param>
 /// <param name="errorMessage">The Error Message to send. For example "Not Found" or "Not Supported"</param>
 public void WriteError(HTTPStatusCode errorCode, string errorMessage, string errorContent, string mimeType)
 {
     SendError((int)errorCode, errorMessage, errorContent, mimeType);
 }
Example #27
0
 public HandlingException(HTTPStatusCode status, string errorMessage)
 {
     Status = status;
     ErrorMessage = errorMessage;
 }
Example #28
0
 public HTTPErrorAttribute(HTTPMethod HTTPMethod, String UriTemplate, HTTPStatusCode myHTTPStatusCode)
 {
     this.HTTPMethod     = HTTPMethod;
     this.UriTemplate    = UriTemplate;
     this.HTTPStatusCode = myHTTPStatusCode;
 }
Example #29
0
        /// <summary>
        /// Return a HTTP error response using the best-matching content type.
        /// </summary>
        /// <param name="HTTPRequest">The HTTP request.</param>
        /// <param name="StatusCode">A HTTP status code.</param>
        /// <param name="Reasons">Optional application side reasons for this error.</param>
        public static HTTPResponse HTTPErrorResponse_old(HTTPRequest HTTPRequest, HTTPStatusCode StatusCode, String Reasons = null)
        {
            #region Initial checks

            if (StatusCode == null)
                return HTTPErrorResponse_old(HTTPRequest, HTTPStatusCode.InternalServerError, "Calling the HTTPError lead to an error!");

            var Content     = String.Empty;
            var ContentType = HTTPRequest.Accept.BestMatchingContentType(HTTPContentType.JSON_UTF8,
                                                                         HTTPContentType.HTML_UTF8,
                                                                         HTTPContentType.TEXT_UTF8,
                                                                         HTTPContentType.XML_UTF8);

            #endregion

            #region JSON_UTF8

            // {
            //     "error": {
            //         "code"    : 400
            //         "message" : "Bad Request"
            //         "reason"  : "The first paramter is not a valid number!"
            //     }
            // }
            if (ContentType == HTTPContentType.JSON_UTF8)
                Content = (Reasons == null) ? "{ \"error\": { \"code\" : " + StatusCode.Code + ", \"message\" : \"" + StatusCode.Name + "\" } }" :
                                              "{ \"error\": { \"code\" : " + StatusCode.Code + ", \"message\" : \"" + StatusCode.Name + "\", \"reasons\" : \"" + Reasons + "\" } }";

            #endregion

            #region HTML_UTF8

            //<!doctype html>
            //<html>
            //  <head>
            //    <meta charset="UTF-8">
            //    <title>Error 400 - Bad Request</title>
            //  </head>
            //  <body>
            //    <h1>Error 400 - Bad Request</h1>
            //    The first paramter is not a valid number!
            //  </body>
            //</html>
            else if (ContentType == HTTPContentType.HTML_UTF8)
                Content = (Reasons == null) ? "<!doctype html><html><head><meta charset=\"UTF-8\"><title>Error " + StatusCode.Code + " - " + StatusCode.Name + "</title></head><body><h1>Error " + StatusCode.Code + " - " + StatusCode.Name + "</h1></body></html>" :
                                              "<!doctype html><html><head><meta charset=\"UTF-8\"><title>Error " + StatusCode.Code + " - " + StatusCode.Name + "</title></head><body><h1>Error " + StatusCode.Code + " - " + StatusCode.Name + "</h1>" + Reasons + "</body></html>";

            #endregion

            #region TEXT_UTF8

            // Error 400 - Bad Request
            // The first paramter is not a valid number!
            else if (ContentType == HTTPContentType.TEXT_UTF8 || ContentType == HTTPContentType.ALL)
                Content = (Reasons == null) ? "Error " + StatusCode.Code + " - " + StatusCode.Name :
                                              "Error " + StatusCode.Code + " - " + StatusCode.Name + Environment.NewLine + Reasons;

            #endregion

            #region XML_UTF8

            // <?xml version="1.0" encoding="UTF-8"?>
            // <error>
            //     <code>400</code>
            //     <message>Bad Request</message>
            //     <reason>The first paramter is not a valid number!</message>
            // </error>
            else if (ContentType == HTTPContentType.XML_UTF8)
                Content = (Reasons == null) ? "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error><code>" + StatusCode.Code + "</code><message>" + StatusCode.Name + "</message></error></xml>" :
                                              "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error><code>" + StatusCode.Code + "</code><message>" + StatusCode.Name + "</message><reasons>" + Reasons + "</reasons></error></xml>";

            #endregion

            #region GEXF+XML

            // <?xml version="1.0" encoding="UTF-8"?>
            // <gexf xmlns=\"http://www.gexf.net/1.2draft\" version="1.2">
            //   <meta lastmodifieddate="2009-03-20">
            //     <creator>Vanaheimr Walkyr</creator>
            //     <description>HTTP Error</description>
            //   </meta>
            //   <graph mode="static" defaultedgetype="directed">
            //     <attributes class="edge">
            //       <attribute id="0" title="Reasons" type="string"/>
            //     </attributes>
            //     <nodes>
            //       <node id="Request" label="Request" />
            //       <node id="Error"   label="Error 400 - Bad Request" />
            //     </nodes>
            //     <edges>
            //       <edge id="0" source="Request" target="Error">
            //         <attvalues>
            //           <attvalue for="0" value="The first paramter is not a valid number!"/>
            //         </attvalues>
            //       <edge>
            //     </edges>
            //   </graph>
            // </gexf>
            //     <reason></message>
            else if (ContentType == HTTPContentType.GEXF_UTF8)
                Content = (Reasons == null) ?
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                    "<gexf xmlns=\"http://www.gexf.net/1.2draft\" version=\"1.2\">" +
                    "<meta lastmodifieddate=\"2009-03-20\"><creator>Vanaheimr Walkyr</creator><description>HTTP Error</description></meta>" +
                    "<graph mode=\"static\" defaultedgetype=\"directed\">" +
                    "<nodes>" +
                      "<node id=\"Request\" label=\"Request\" />" +
                      "<node id=\"Error\"   label=\"Error " + StatusCode.Code + " - " + StatusCode.Name + "\" />" +
                    "</nodes><edges>" +
                      "<edge id=\"0\" source=\"Request\" target=\"Error\" />" +
                    "</edges></graph></gexf>" :

                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                    "<gexf xmlns=\"http://www.gexf.net/1.2draft\" version=\"1.2\">" +
                    "<meta lastmodifieddate=\"2009-03-20\"><creator>Vanaheimr Walkyr</creator><description>HTTP Error</description></meta>" +
                    "<graph mode=\"static\" defaultedgetype=\"directed\">" +
                    "<attributes class=\"edge\">" +
                    "  <attribute id=\"0\" title=\"Reasons\" type=\"string\" />" +
                    "</attributes> " +
                    "<nodes>" +
                      "<node id=\"Request\" label=\"Request\" />" +
                      "<node id=\"Error\"   label=\"Error " + StatusCode.Code + " - " + StatusCode.Name + "\" />" +
                    "</nodes><edges>" +
                      "<edge id=\"0\" source=\"Request\" target=\"Error\">" +
                        "<attvalues>" +
                          "<attvalue for=\"0\" value=\"" + Reasons + "\" />" +
                        "</attvalues>" +
                      "<edge>" +
                    "</edges></graph></gexf>";

            #endregion

            var x = new HTTPResponseBuilder(HTTPRequest) {
                HTTPStatusCode = StatusCode,
                CacheControl   = "no-cache",
                Connection     = "close",
                Content        = Content.ToUTF8Bytes()
            };

            return x;
        }
Example #30
0
 public HTTPErrorAttribute(String UriTemplate)
 {
     this.HTTPMethod     = HTTPMethod.GET;
     this.UriTemplate    = UriTemplate;
     this.HTTPStatusCode = null;
 }