Exemplo n.º 1
0
        public HttpClass(SupportedHttpMethods httpMethod, string uri)
        {
            _uri        = new Uri(uri);
            _httpMethod = new HttpMethod(httpMethod.ToString());

            switch (httpMethod)
            {
            case SupportedHttpMethods.GET:
                _action = get;
                break;

            case SupportedHttpMethods.POST:
                _action = post;
                break;

            case SupportedHttpMethods.PUT:
                _action = put;
                break;

            case SupportedHttpMethods.DELETE:
                _action = delete;
                break;

            default:
                throw new InvalidHttpMethodException();
            }
        }
        public virtual SillyProxyResponse Handle(SillyProxyRequest input, ILambdaContext lambdaContext)
        {
            try
            {
                if (input == null)
                {
                    throw new SillyException(SillyHttpStatusCode.ServerError, "Request aborted upon delivery.");
                }

                OriginalRequest = input;
                HttpMethod      = StringToHttpMethod(input.httpMethod);
                Path            = input.path;

                ISillyView sillyContent = Dispatch(this);

                if (sillyContent == null)
                {
                    throw new SillyException(SillyHttpStatusCode.NotFound, "The path " + input.path + " does not exist.");
                }

                SillyProxyResponse response = new SillyProxyResponse();
                response.body = sillyContent.Content;

                return(response);
            }
            catch (SillyException sillyEx)
            {
                return(buildErrorResponse(sillyEx));
            }
            catch (Exception Ex)
            {
                return(buildErrorResponse(SillyHttpStatusCode.ServerError, Ex.Message + "\n" + Ex.StackTrace));
            }
        }
Exemplo n.º 3
0
 public void HttpInvoke(SupportedHttpMethods httpMethod, string url)
 {
     using (var httpClass = new HttpClass(httpMethod, url))
     {
         httpClass.Invoke();
     }
 }
Exemplo n.º 4
0
 public void HttpInvoke(SupportedHttpMethods httpMethod, string url, string content)
 {
     using (var httpClass = new HttpClass(httpMethod, url, content))
        {
     httpClass.Invoke();
        }
 }
Exemplo n.º 5
0
        public HttpResponseMessage GetHttpResponseMessage(SupportedHttpMethods httpMethod, string url)
        {
            HttpResponseMessage _response;

            using (var httpClass = new HttpClass(httpMethod, url))
            {
                httpClass.Invoke();
                _response = httpClass.GetHttpResponseMessage();
            }

            return(_response);
        }
Exemplo n.º 6
0
        public HttpResponseMessage GetHttpResponseMessage(SupportedHttpMethods httpMethod, string url)
        {
            HttpResponseMessage _response;

               using (var httpClass = new HttpClass(httpMethod, url))
               {
            httpClass.Invoke();
            _response = httpClass.GetHttpResponseMessage();
               }

               return _response;
        }
Exemplo n.º 7
0
 public HttpClass(SupportedHttpMethods httpMethod, string uri, string content) : this(httpMethod, uri)
 {
     if (httpMethod == SupportedHttpMethods.POST || httpMethod == SupportedHttpMethods.PUT)
     {
         JObject.Parse(content);
         _content = new StringContent(content);
         _content.Headers.ContentType = new MediaTypeHeaderValue("text/json");
     }
     else
     {
         throw new InvalidHttpMethodContentCombinationException();
     }
 }
        private SillyRoute AddRoute(SupportedHttpMethods httpMethod, string key, string urlPattern, string controller, string method)
        {
            if (String.IsNullOrEmpty(key))
            {
                key = "default";
            }

            SillyRoute route = new SillyRoute(key, urlPattern, controller, method);

            Routes[httpMethod].Add(route);

            return(route);
        }
Exemplo n.º 9
0
        public string HttpInvoke(string url, SupportedHttpMethods httpMethod,
                                 string mediaType,
                                 string content = null,
                                 Dictionary <string, string> requestHeaders = null,
                                 Dictionary <string, string> contentHeaders = null)
        {
            using (var httpClass = new HttpClass(url, httpMethod, mediaType, content, requestHeaders, contentHeaders))
            {
                httpClass.Invoke();

                this.ProcessErrors(httpClass);

                return(httpClass.GetResponseContent());;
            }
        }
 public SillyProxyApplication()
 {
     Path            = string.Empty;
     OriginalRequest = null;
     HttpMethod      = SupportedHttpMethods.Unsupported;
 }
Exemplo n.º 11
0
 /// <summary>
 /// Gets the actions by path.
 /// </summary>
 /// <param name="requestSegments">Route segments of the request.</param>
 /// <param name="method">HTTP request method.</param>
 /// <returns>Actions matching to the provided path</returns>
 public IEnumerable <ActionContext> GetActionsBySegments(string[] requestSegments, SupportedHttpMethods method)
 {
     if (RequestSegmentsMatchController(requestSegments))
     {
         string[] requestSegmentsWithoutController = requestSegments.Skip(RouteSegments.Length).ToArray();
         var      actions = Actions.Where(x => x.RouteSegments.Length == requestSegmentsWithoutController.Length && x.HttpMethod == method).ToArray();
         if (actions.Length > 0)
         {
             foreach (var action in actions)
             {
                 if (IsActionMatchedToRequestSegments(action, requestSegmentsWithoutController))
                 {
                     yield return(action);
                 }
             }
         }
     }
 }
Exemplo n.º 12
0
 /// <summary>
 /// Determines whether this instance supports HTTP body.
 /// </summary>
 /// <param name="method">HTTP method to check.</param>
 /// <returns><c>true</c> if provided HTTP method supports HTTP body, otherwise <c>false</c></returns>
 public static bool HasBody(this SupportedHttpMethods method)
 => method == SupportedHttpMethods.Post || method == SupportedHttpMethods.Put;
Exemplo n.º 13
0
        public HttpClass(string uri,
                         SupportedHttpMethods httpMethod,
                         string mediaType = "text/json",
                         string content   = null,
                         Dictionary <string, string> requestHeaders = null,
                         Dictionary <string, string> contentHeaders = null)
        {
            if (content != null)
            {
                this.content = new StringContent(content);
                this.content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
            }



            this.rqstHeaders = requestHeaders;
            this.cntHeaders  = contentHeaders;

            if (this.rqstHeaders == null)
            {
                this.rqstHeaders = new Dictionary <string, string>();
            }

            if (this.cntHeaders == null)
            {
                this.cntHeaders = new Dictionary <string, string>();
            }

            this.rqstHeaders.Add("Authorization", this.BuildAuthHeader());
            this.rqstHeaders.Add("X-Auth-Token", this.BuildUserTokenHeader());

            this.httpClient = new HttpClient();
            this.uri        = new Uri(uri, UriKind.Absolute);
            this.httpMethod = new HttpMethod(httpMethod.ToString());

            if (ConfigurationManager.AppSettings["TimeOut"] != null)
            {
                this.httpClient.Timeout = new TimeSpan(0, Convert.ToInt16(ConfigurationManager.AppSettings["TimeOut"]), 0);
            }

            switch (httpMethod)
            {
            case SupportedHttpMethods.GET:
                this.action = this.Get;
                break;

            case SupportedHttpMethods.POST:
                this.action = this.Post;
                break;

            case SupportedHttpMethods.PUT:
                this.action = this.Put;
                break;

            case SupportedHttpMethods.DELETE:
                this.action = this.Delete;
                break;

            default:
                throw new InvalidHttpMethodException();
            }
        }