public override Task Invoke(IOwinContext context) { //context.Response.Headers.Add("Access-Control-Allow-Origin", "*".Split(',')); //context.Response.Headers.Add("Access-Control-Allow-Credentials", "true".Split(',')); //// Added "Accept-Encoding" to this list //context.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Accept-Encoding, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name".Split(',')); //context.Response.Headers.Add("Access-Control-Allow-Methods", "POST,GET,PUT,PATCH,DELETE,OPTIONS".Split(',')); //// New Code Starts here //if (context.Request.Method == "OPTIONS") //{ // context.Response.StatusCode = (int)HttpStatusCode.OK; // return Next.Invoke(context); //} string serviceName = context.Request.Uri.PathAndQuery.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); bool isWebSocketd = context.Request.Headers.ContainsKey("Sec-WebSocket-Key"); if (!BaseProvider.ExistService(serviceName, CurrentServerBase) && !isWebSocketd && !context.Request.Headers.ContainsKey("signalgo") && !context.Request.Headers.ContainsKey("signalgo-servicedetail") && context.Request.Headers["content-type"] != "SignalGo Service Reference") { return(Next.Invoke(context)); } OwinClientInfo owinClientInfo = new OwinClientInfo(CurrentServerBase); owinClientInfo.ConnectedDateTime = DateTime.Now; owinClientInfo.IPAddressBytes = IPAddress.Parse(context.Request.RemoteIpAddress).GetAddressBytes(); owinClientInfo.ClientId = Guid.NewGuid().ToString(); CurrentServerBase.Clients.TryAdd(owinClientInfo.ClientId, owinClientInfo); owinClientInfo.OwinContext = context; owinClientInfo.RequestHeaders = context.Request.Headers; owinClientInfo.ResponseHeaders = context.Response.Headers; if (isWebSocketd) { owinClientInfo.StreamHelper = SignalGoStreamBase.CurrentBase; Action <IDictionary <string, object>, Func <IDictionary <string, object>, Task> > accept = context.Get <Action <IDictionary <string, object>, Func <IDictionary <string, object>, Task> > >("websocket.Accept"); if (accept == null) { // Bad Request context.Response.StatusCode = 400; context.Response.Write("Not a valid websocket request"); return(Task.FromResult <object>(null)); } WebsocketClient websocketClient = new WebsocketClient() { ClientInfo = owinClientInfo, CurrentServerBase = CurrentServerBase }; accept(null, websocketClient.RunWebSocket); return(Task.FromResult <object>(null)); } else { owinClientInfo.StreamHelper = SignalGoStreamBase.CurrentBase; owinClientInfo.ClientStream = new PipeNetworkStream(new DuplexStream(context.Request.Body, context.Response.Body)); return(HttpProvider.AddHttpClient(owinClientInfo, CurrentServerBase, context.Request.Uri.PathAndQuery, context.Request.Method, null, null)); } }
public async Task Invoke(HttpContext context) { //context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); //context.Response.Headers.Add("Access-Control-Allow-Credentials", "true"); //// Added "Accept-Encoding" to this list //context.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Accept-Encoding, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name"); //context.Response.Headers.Add("Access-Control-Allow-Methods", "POST,GET,PUT,PATCH,DELETE,OPTIONS"); //// New Code Starts here //if (context.Request.Method == "OPTIONS") //{ // context.Response.StatusCode = (int)HttpStatusCode.OK; // return _next.Invoke(context); //} string uri = context.Request.Path.Value + context.Request.QueryString.ToString(); string serviceName = uri.Contains('/') ? uri.Substring(0, uri.LastIndexOf('/')).Trim('/') : ""; bool isWebSocketd = context.Request.Headers.ContainsKey("Sec-WebSocket-Key"); if (!BaseProvider.ExistService(serviceName, CurrentServerBase) && !isWebSocketd && !context.Request.Headers.ContainsKey("signalgo") && !context.Request.Headers.ContainsKey("signalgo-servicedetail") && context.Request.Headers["content-type"] != "SignalGo Service Reference") { await _next.Invoke(context); return; } OwinClientInfo owinClientInfo = new OwinClientInfo(CurrentServerBase); owinClientInfo.ChangeStatusAction = (code) => { context.Response.StatusCode = code; }; owinClientInfo.ConnectedDateTime = DateTime.Now; owinClientInfo.IPAddressBytes = context.Connection.RemoteIpAddress.GetAddressBytes(); owinClientInfo.ClientId = Guid.NewGuid().ToString(); CurrentServerBase.Clients.TryAdd(owinClientInfo.ClientId, owinClientInfo); //owinClientInfo.OwinContext = context; owinClientInfo.RequestHeaders = new HttpHeaderCollection(context.Request.Headers); owinClientInfo.ResponseHeaders = new HttpHeaderCollection(context.Response.Headers); if (context.Request.Headers.ContainsKey("signalgo") && context.Request.Headers["signalgo"] == "SignalGoHttpDuplex") { context.Response.Headers["Content-Length"] = "170"; context.Request.EnableRewind(); context.Request.EnableBuffering(); owinClientInfo.StreamHelper = SignalGoStreamBase.CurrentBase; owinClientInfo.ClientStream = new PipeNetworkStream(new DuplexStream(context.Request.Body, context.Response.Body)); owinClientInfo.ProtocolType = ClientProtocolType.HttpDuplex; if (context.Request.Headers["SignalgoDuplexWebSocket"] == "true") { owinClientInfo.StreamHelper = SignalGoStreamWebSocketLlight.CurrentWebSocket; await HttpProvider.AddSignalGoWebSocketHttpClient(owinClientInfo, CurrentServerBase); } else { await HttpProvider.AddSignalGoWebSocketHttpClient(owinClientInfo, CurrentServerBase); } await Task.FromResult <object>(null); } else if (isWebSocketd) { owinClientInfo.StreamHelper = SignalGoStreamBase.CurrentBase; bool web = context.WebSockets.IsWebSocketRequest; WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); owinClientInfo.ClientStream = new PipeNetworkStream(new WebsocketStream(webSocket)); if (context.Request.Headers["SignalgoDuplexWebSocket"] == "true") { owinClientInfo.StreamHelper = SignalGoStreamWebSocketLlight.CurrentWebSocket; await HttpProvider.AddSignalGoWebSocketHttpClient(owinClientInfo, CurrentServerBase); } else { await HttpProvider.AddWebSocketHttpClient(owinClientInfo, CurrentServerBase); } await Task.FromResult <object>(null); } else { owinClientInfo.StreamHelper = SignalGoStreamBase.CurrentBase; owinClientInfo.ClientStream = new PipeNetworkStream(new DuplexStream(context.Request.Body, context.Response.Body)); await HttpProvider.AddHttpClient(owinClientInfo, CurrentServerBase, uri, context.Request.Method, null, null); } }