Ejemplo n.º 1
0
        public async Task Invoke(HttpContext httpContext)
        {
            if (!NetStitchMiddleware.Servers.TryGetValue(options.ServerID, out var server))
            {
                httpContext.Response.ContentType = "text/plain";
                httpContext.Response.StatusCode  = HttpStatus.NotFound;
                byte[] bytes = Encoding.UTF8.GetBytes($"ServerID Not Found : {options.ServerID}");
                httpContext.Response.Body.Write(bytes, 0, bytes.Length);
                return;
            }

            if (httpContext.Request.ContentType == "application/x-msgpack")
            {
                await next(httpContext).ConfigureAwait(false);
            }
            else if (httpContext.Request.ContentType == "application/x-www-form-urlencoded")
            {
                if (server.OperationMap.TryGetValue(httpContext.Request.Path, out var operation))
                {
                    using (var stream = new NonDisposableStream(new MemoryStream()))
                    {
                        var originalStream = httpContext.Response.Body;
                        httpContext.Response.Body = stream;

                        await CallOperationFromContentTypeOfFormUrlencoded(httpContext, server, operation).ConfigureAwait(false);

                        if (httpContext.Response.StatusCode == HttpStatus.OK)
                        {
                            httpContext.Response.ContentType = "application/json";
                            httpContext.Response.Headers.Remove("Content-Encording");
                            var returnType = operation.MethodInfo.ReturnType.GenericTypeArguments.FirstOrDefault();
                            var obj        = MessagePack.LZ4MessagePackSerializer.NonGeneric.Deserialize(returnType, stream);
                            using (var sw = new StreamWriter(originalStream, Encoding.UTF8))
                                using (var jw = new JsonTextWriter(sw))
                                {
                                    JsonSerializer.Create().Serialize(jw, obj);
                                }
                        }
                        else
                        {
                            stream.Position = 0;
                            stream.CopyTo(originalStream);
                        }
                    }
                }
            }
            else if (httpContext.Request.ContentType == "application/json")
            {
                if (server.OperationMap.TryGetValue(httpContext.Request.Path, out var operation))
                {
                    using (var stream = new NonDisposableStream(new MemoryStream()))
                    {
                        var originalStream = httpContext.Response.Body;
                        httpContext.Response.Body = stream;

                        await CallOperationFromContentTypeOfJson(httpContext, server, operation).ConfigureAwait(false);

                        if (httpContext.Response.StatusCode == HttpStatus.OK)
                        {
                            httpContext.Response.ContentType = "application/json";
                            httpContext.Response.Headers.Remove("Content-Encording");
                            var returnType = operation.MethodInfo.ReturnType.GenericTypeArguments.FirstOrDefault();
                            var obj        = MessagePack.LZ4MessagePackSerializer.NonGeneric.Deserialize(returnType, stream);
                            using (var sw = new StreamWriter(originalStream, Encoding.UTF8))
                                using (var jw = new JsonTextWriter(sw))
                                {
                                    JsonSerializer.Create().Serialize(jw, obj);
                                }
                        }
                        else
                        {
                            stream.Position = 0;
                            stream.CopyTo(originalStream);
                        }
                    }
                }
            }
            else
            {
                await CallSwagger(httpContext, server).ConfigureAwait(false);
            }
        }