Example #1
0
        private static void ProcessFile(SnippetsGenerator generator, string language, string file)
        {
            // convert http request into a type that works with SnippetGenerator.ProcessPayloadRequest()
            // we are not altering the types as it should continue serving the HTTP endpoint as well
            using var streamContent = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(file))));
            streamContent.Headers.Add("Content-Type", "application/http;msgtype=request");

            string snippet;

            try
            {
                // This is a very fast operation, it is fine to make is synchronuous.
                // With the parallel foreach in the main method, processing all snippets for C# in both Beta and V1 takes about 7 seconds.
                // As of this writing, the code was processing 2650 snippets
                // Using async-await is costlier as this operation is all in-memory and task creation and scheduling overhead is high for that.
                // With async-await, the same operation takes 1 minute 7 seconds.
                using var message = streamContent.ReadAsHttpRequestMessageAsync().Result;
                snippet           = generator.ProcessPayloadRequest(message, language);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"Exception while processing {file}.{Environment.NewLine}{e.Message}{Environment.NewLine}{e.StackTrace}");
                return;
            }

            File.WriteAllText(file.Replace("-httpSnippet", $"---{language}"), snippet);
        }
Example #2
0
        private async Task <HttpRequestMessage> SimulateRequestOverTheWireAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Contract.Requires(request != null);
            Contract.Requires(Contract.Result <Task <HttpRequestMessage> >() != null);

            var         stream  = new MemoryStream();
            HttpContent content = new HttpMessageContent(request);

            await content.CopyToAsync(stream);

            await stream.FlushAsync(cancellationToken);

            stream.Position = 0L;
            content         = new StreamContent(stream);
            SetMediaType(content, "request");

            return(await content.ReadAsHttpRequestMessageAsync(cancellationToken));
        }
        public async Task <IActionResult> PostAsync(string lang = "c#")
        {
            Request.EnableBuffering();
            using var streamContent = new StreamContent(Request.Body);
            streamContent.Headers.Add("Content-Type", "application/http;msgtype=request");

            try
            {
                using HttpRequestMessage requestPayload = await streamContent.ReadAsHttpRequestMessageAsync().ConfigureAwait(false);

                var response = _snippetGenerator.ProcessPayloadRequest(requestPayload, lang);
                return(new StringResult(response));
            }
            catch (Exception e)
            {
                return(new BadRequestObjectResult(e.Message));
            }
        }
Example #4
0
        public static HttpMessage Parse(string data)
        {
            var httpMessage = new HttpMessage();

            HttpContent content;

            using (var sr = new StringReader(data))
            {
                // First line of data is (request|response) followed by a GUID to link request to response
                // Rest of data is in message/http format

                var firstLine = sr.ReadLine().Split(':');
                if (firstLine.Length < 2)
                {
                    throw new ArgumentException("Invalid formatted event :" + data);
                }
                httpMessage.IsRequest = firstLine[0] == "request";
                httpMessage.MessageId = Guid.Parse(firstLine[1]);

                var stream = new MemoryStream(Encoding.UTF8.GetBytes(sr.ReadToEnd()));
                stream.Position = 0;
                content         = new StreamContent(stream);
            }

            var contentType = new MediaTypeHeaderValue("application/http");

            content.Headers.ContentType = contentType;

            if (httpMessage.IsRequest)
            {
                contentType.Parameters.Add(new NameValueHeaderValue("msgtype", "request"));

                // Using .Result isn't too evil because content is a locally buffered memory stream
                // Although if this were hosted in a System.Web based ASP.NET host it might block
                httpMessage.HttpRequestMessage = content.ReadAsHttpRequestMessageAsync().Result;
            }
            else
            {
                contentType.Parameters.Add(new NameValueHeaderValue("msgtype", "response"));
                httpMessage.HttpResponseMessage = content.ReadAsHttpResponseMessageAsync().Result;
            }
            return(httpMessage);
        }
Example #5
0
        public void Post([FromBody] string value)
        {
            StreamContent             content            = new StreamContent(ControllerContext.HttpContext.Request.Body);
            Task <HttpRequestMessage> httpRequestMessage = content.ReadAsHttpRequestMessageAsync();
            HttpRequestMessage        result             = httpRequestMessage.Result;



            bool IsBeta = (bool)result.Properties["IsBeta"];

            string serviceRoot;

            if (IsBeta)
            {
                serviceRoot = GraphVersion.graphBeta;
            }
            else
            {
                serviceRoot = GraphVersion.graphV1;
            }

            SnippetsGenerator snippetGenerator = new SnippetsGenerator(result, serviceRoot);
        }