Ejemplo n.º 1
0
        public override Task ApplyArgument(HttpApiRequest request, string name, object argument)
        {
            if (request.Body != null)
                throw new Exception("Can only use StreamArgumentHandler for one argument.  If you need multiple arguments, you " +
                                    "should be using MultipartStreamArgumentHandler, which ought to have been selected " +
                                    "automatically under default conditions.");

            var streamBody = new StreamHttpBody((Stream)argument);
            request.Body = streamBody;

            return base.ApplyArgument(request, name, argument);
        }
Ejemplo n.º 2
0
        public override Task ApplyArgument(HttpApiRequest request, string name, object argument)
        {
            if (request.Body != null)
            {
                throw new Exception("Can only use StreamArgumentHandler for one argument.  If you need multiple arguments, you " +
                                    "should be using MultipartStreamArgumentHandler, which ought to have been selected " +
                                    "automatically under default conditions.");
            }

            var streamBody = new StreamHttpBody((Stream)argument);

            request.Body = streamBody;

            return(base.ApplyArgument(request, name, argument));
        }
Ejemplo n.º 3
0
        public Task <HttpHandlerResponse> Call(HttpApiRequest request)
        {
            var webRequest = WebRequest.CreateHttp(request.Url.ToString());

            webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            webRequest.Method = request.Method.ToString();
            if (request.Proxy != null)
            {
                webRequest.Proxy = request.Proxy;
            }
            foreach (var header in request.Headers)
            {
                switch (header.Name)
                {
                case "User-Agent":
                    webRequest.UserAgent = header.Values.Single();
                    break;

                case "Accept":
                    webRequest.Accept = header.Values.Single();
                    break;

                case "Content-Type":
                    webRequest.ContentType = header.Values.Single();
                    break;

                default:
                    webRequest.Headers.Add(header.Name, string.Join(",", header.Values));
                    break;
                }
            }

            var requestWriteTime = new Stopwatch();

            requestWriteTime.Start();
            if (request.Body != null)
            {
                var requestStream = webRequest.GetRequestStream();
                var stream        = request.Body.Accept(new ContentCreator());
                stream.CopyTo(requestStream);
                requestStream.Close();
            }
            else if (request.Method != HttpMethod.Get && request.Method != HttpMethod.Head)
            {
                var requestStream = webRequest.GetRequestStream();
                requestStream.Close();
            }

            HttpWebResponse response;

            try
            {
                response = (HttpWebResponse)webRequest.GetResponse();
            }
            catch (WebException e) when(e.Response != null)
            {
                response = (HttpWebResponse)e.Response;
            }

            requestWriteTime.Stop();

            var      responseHeaders = new List <HttpHeader>();
            HttpBody responseBody    = null;

            responseHeaders.AddRange(response.Headers.AllKeys.Select(x => new HttpHeader(x, response.Headers[x].Split(','))));

            var responseReadTime = new Stopwatch();

            responseReadTime.Start();

            var responseStream = response.GetResponseStream();

            switch (request.ResponseContentTypeOverride ?? response.ContentType.Split(';')[0])
            {
            case "application/json":
                var jsonString = Encoding.UTF8.GetString(responseStream.ReadToEnd());
                var json       = !string.IsNullOrEmpty(jsonString) ? JToken.Parse(jsonString) : null;      // Hack to workaround silly servers that send a content type and a 204
                if (json != null)
                {
                    responseBody = new JsonHttpBody(json);
                }
                break;

            case "application/x-www-form-urlencoded":
                throw new NotSupportedException();

//                    var stream = await message.Content.ReadAsStreamAsync();
//                    body = FormParser.ParseForm(stream);
//                    break;
            case "text/plain":
            case "text/html":
            case "":
                var text = Encoding.UTF8.GetString(responseStream.ReadToEnd());
                responseBody = new StringHttpBody(text);
                break;

            case "application/octet-stream":
                var stream = new MemoryStream();
                responseStream.CopyTo(stream);
                stream.Position = 0;
                responseBody    = new StreamHttpBody(stream);
                break;
            }

            responseStream.Close();
            responseReadTime.Stop();

            var result = new HttpApiResponse(response.StatusCode, responseBody, responseHeaders, response.ResponseUri.ToString());

            return(Task.FromResult(new HttpHandlerResponse(result, requestWriteTime.Elapsed, responseReadTime.Elapsed)));
        }
Ejemplo n.º 4
0
            public Stream VisitStreamBody(StreamHttpBody body)
            {
                var result = body.Stream;

                return(result);
            }
Ejemplo n.º 5
0
 public Task <byte[]> VisitStreamBodyAsync(StreamHttpBody body)
 {
     return(body.Stream.ReadToEndAsync());
 }
Ejemplo n.º 6
0
 public string VisitStreamBody(StreamHttpBody body)
 {
     return("application/octet-stream");
 }