public SerializableOperation(IRemoraOperation operation)
        {
            if (operation == null) throw new ArgumentNullException("operation");
            Contract.EndContractBlock();

            OperationId = operation.OperationId;
            IncomingUri = operation.IncomingUri != null ? operation.IncomingUri.ToString() : null;
            Kind = operation.Kind;
            OnError = operation.OnError;
            if (operation.Exception != null)
            {
                ExceptionType = operation.Exception.GetType().AssemblyQualifiedName;
                ExceptionMessage = operation.Exception.ToString();
            }

            if (operation.Request != null)
                Request = new SerializableRequest(operation.Request);

            if (operation.Response != null)
                Response = new SerializableResponse(operation.Response);

            CreatedAtUtc = operation.CreatedAtUtc;

            if (operation.ExecutingPipeline != null)
            {
                PipelineName = operation.ExecutingPipeline.Id;
                if (operation.ExecutingPipeline.Definition != null)
                    PipelineComponents = string.Join(",",
                                                     operation.ExecutingPipeline.Definition.ComponentDefinitions.Select(
                                                         x => x.RefId));
            }
        }
示例#2
0
        private SerializableResponse Handler(SerializableRequest arg)
        {
            var result = new List <ServiceModel>();

            foreach (var item in _availableRepositoriesRepository.GetAll())
            {
                result.Add(new ServiceModel
                {
                    Id       = item.Id.ToString(),
                    Official = item.Official,
                    Prefix   = item.Prefix,
                    Type     = item.Type.ToLowerInvariant(),
                    Address  = _properties.Host + "/" + item.Prefix + "/" + item.Address.TrimStart('/')
                });
            }

            return(JsonResponse(result));
        }
示例#3
0
        public SerializableResponse HandleRequest(SerializableRequest request)
        {
            var res         = new Dictionary <string, string>();
            var splittedUrl = request.Url.TrimStart('/').Split('/');

            if (splittedUrl.Length != _realPath.Length)
            {
                throw new Exception();
            }
            for (int i = 0; i < splittedUrl.Length; i++)
            {
                var spl = splittedUrl[i];
                var mtc = _realPath[i];
                if (string.Compare(spl, mtc, true) == 0)
                {
                    continue;
                }
                var start = mtc.IndexOf("{");
                var end   = mtc.IndexOf("}");
                var pre   = start > 0 ? mtc.Substring(0, start) : "";
                var post  = mtc.Substring(end + 1);
                if (spl.StartsWith(pre) && spl.EndsWith(post))
                {
                    if (pre.Length > 0)
                    {
                        spl = spl.Substring(pre.Length);
                        mtc = mtc.Substring(pre.Length);
                    }
                    if (post.Length > 0)
                    {
                        spl = spl.Substring(0, spl.Length - post.Length);
                        mtc = mtc.Substring(0, mtc.Length - post.Length);
                    }
                    res.Add(mtc.Trim('{', '}'), spl);
                    continue;
                }

                throw new Exception();
            }
            request.PathParams = res;
            return(_handler(request));
        }
示例#4
0
        protected SerializableResponse RemoteRequest(String realUrl, SerializableRequest sr)
        {
            if (sr.QueryParams.ContainsKey("runlocal"))
            {
                throw new Exception();
            }
            //throw new Exception();
            var res    = new SerializableResponse();
            var method = HttpMethod.Get;

            if (sr.Method == "GET")
            {
                method = HttpMethod.Get;
            }
            if (sr.Method == "POST")
            {
                method = HttpMethod.Post;
            }
            if (sr.Method == "PUT")
            {
                method = HttpMethod.Put;
            }
            if (sr.Method == "DELETE")
            {
                method = HttpMethod.Delete;
            }
            var qp = "";

            if (sr.QueryParams.Any())
            {
                qp = "?" + string.Join("&", sr.QueryParams.Select(kvp => kvp.Key + "=" + kvp.Value));
            }

            var requestMessage = new HttpRequestMessage(method, realUrl + qp);

            foreach (var reqh in sr.Headers)
            {
                if (reqh.Key == "Host")
                {
                    var uri = new Uri(realUrl);
                    requestMessage.Headers.Add(reqh.Key, uri.Host);
                }
                else
                {
                    requestMessage.Headers.Add(reqh.Key, reqh.Value);
                }
            }
            if (sr.Method == "POST" || sr.Method == "PUT")
            {
                requestMessage.Content = new ByteArrayContent(sr.Content);
            }
            HttpClientHandler handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };
            var client = new HttpClient(handler);
            var result = client.SendAsync(requestMessage);

            result.Wait();
            var resh = result.Result;

            res.ContentType = resh.Content.Headers.ContentType.MediaType;

            res.Headers["Date"]          = DateTime.Now.ToString("r");
            res.Headers["Last-Modified"] = DateTime.Now.ToString("r");
            var rb = resh.Content.ReadAsByteArrayAsync();

            rb.Wait();
            res.Content = rb.Result;
            return(res);
        }