コード例 #1
0
ファイル: LoaderWorker.cs プロジェクト: aleDsz/AppServer
 /// <summary>
 /// Retrieve if endpoint is already defined
 /// </summary>
 /// <param name="path">Request path</param>
 /// <param name="method">HTTP method</param>
 /// <returns>True or false</returns>
 public bool ContainsEndPoint(string path, string method)
 {
     return(_restProcessor.ContainsEndPoint(path, method));
 }
コード例 #2
0
        RestResult ProcessHttpCalls(HttpListenerRequest request)
        {
            string[]    ePath = request.Url.AbsolutePath.Split(new char[] { '/' }, 2, StringSplitOptions.RemoveEmptyEntries);
            RestRequest req   = new RestRequest(request);

            if (ePath.Length == 0)
            {
                return(new RestResult(new ErrorObject {
                    ErrorCode = ErrorCodes.NotFound,
                    Message = "No application specified",
                    ErrorField = "url"
                }.ToJSON(), MimeTypes.JSON, HttpStatusCode.NotFound));
            }
            string path   = ePath.Length > 1 ? "/" + ePath[1] : "/";
            string method = request.HttpMethod;
            string app    = ePath[0];

            if (app != "remoteSigner")
            {
                /*return new RestResult(new ErrorObject {
                 *  ErrorCode = ErrorCodes.NotFound,
                 *  Message = $"Application {app} not found",
                 *  ErrorField = "url"
                 * }.ToJSON(), MimeTypes.JSON, HttpStatusCode.NotFound);*/
                // Bypass
                path = "/" + ePath[0] + path;
            }

            Logger.Debug("HTTP Server", $"Processing HTTP Call for App {app}: {method} {path}");

            if (restProcessor.ContainsEndPoint(path, method))
            {
                try {
                    return(restProcessor.CallEndPoint(path, method, req));
                } catch (Exception e) {
                    string exceptionName          = e.InnerException.GetType().Name;
                    string baseName               = e.InnerException.GetType().BaseType.Name;
                    IRestExceptionHandler handler = restProcessor.GetExceptionHandler(exceptionName) ?? restProcessor.GetExceptionHandler(baseName);
                    if (handler != null)
                    {
                        return(handler.HandleException(e.InnerException));
                    }
                    RestResult result = new RestResult();
                    string     exceptionMessage;
                    if (e.InnerException != null)   // In the rest exceptions the real exception will be at InnerException.
                    {
                        Logger.Debug("HTTP Server", $"Exception when calling application {app} in endpoint {method} {path} \r\n {e.InnerException}");
                        exceptionMessage = e.InnerException.ToString();
                    }
                    else     // But if we got a internal exception at AppServer, it will be in the root.
                    {
                        Logger.Debug("HTTP Server", $"Exception when calling application {app} in endpoint {method} {path} \r\n {e}");
                        exceptionMessage = e.ToString();
                    }
                    result.StatusCode  = HttpStatusCode.InternalServerError;
                    result.ContentType = "text/plain";
                    result.Result      = Encoding.UTF8.GetBytes(exceptionMessage);
                    return(result);
                }
            }
            return(new RestResult(new ErrorObject {
                ErrorCode = ErrorCodes.NotFound,
                Message = "Endpoint not found",
                ErrorField = "url"
            }.ToJSON(), MimeTypes.JSON, HttpStatusCode.NotFound));
        }