private async Task <PowerLineEndPointExecutionResult> GetHandleResultAsync(HttpListenerContext context) { string[] UrlPath = context.Request.Url.AbsolutePath.Split('/'); PowerLineContext powerlineContext = new PowerLineContext(context, 0, UrlPath, context.Request.Url); return(await GetHandleResultAsync(powerlineContext)); }
private void AddResponsePayload(JObject obj, PowerLineContext context) { if (context.ResponsePayload != null) { obj.Add("contentType", context.ResponseContentType.ToString()); switch (context.ResponseContentType) { case PowerLineContextContentType.Json: JObject jsonPayload; using (StreamReader reader = new StreamReader(context.ResponsePayload)) { jsonPayload = JObject.Parse(reader.ReadToEnd()); } obj.Add("payload", jsonPayload); return; case PowerLineContextContentType.Text: string stringPayload; using (StreamReader reader = new StreamReader(context.ResponsePayload)) { stringPayload = reader.ReadToEnd(); } obj.Add("payload", stringPayload); return; case PowerLineContextContentType.Unknown: throw new Exception("Unkown content type can't send in websocket"); } } }
public PowerLineEndPointExecutionResult(PowerLinExecutionResultType resultType, PowerLineContext context, Exception exception, PowerLineEndPoint endpoint, PowerLineHandler handler) { this.ResultType = resultType; this.Context = context; this.Exception = exception; this.EndPoint = endpoint; this.Handler = handler; }
private async Task <PowerLineEndPointExecutionResult> GetHandleResultAsync(PowerLineContext context) { context.ResponseHeader["Server"] = "PowerLine powered by "; if (this.GetEndPoint(context.Path[1], out PowerLineEndPoint endpoint)) { return(await endpoint.OnRequestAsync(2, context.Path, context)); } else { PowerLineEndPoint dyanmicEndPoint = this.GetDynamicEndpoint(context.ReqeustUri); if (dyanmicEndPoint != null) { return(await dyanmicEndPoint.OnRequestAsync(2, context.Path, context)); } return(new PowerLineEndPointExecutionResult(context, PowerLinExecutionResultType.EndPointNotFound, null)); } }
public override async Task HandleRequest(PowerLineContext context) { if (!context.IsWebSocket) { context.SetResponse(400); context.SetResponseHttpString("Only websockets support this kind of endpoints"); return; } else { JObject obj = await context.ReadResponsePayloadAsJson(); string eventName = obj.GetValue <string>(new string[] { "payload", "name" }); lock (context.WebsocketClient.eventsLock) { if (context.WebsocketClient.events.ContainsKey(eventName)) { context.SetResponse((int)PowerLineEventHandlerResponseType.EventAlreadySubscribed); context.SetResponseHttpString("You are already subscribed to this event"); return; } } PowerLineEvent currentEvent = Server.GetEvent(eventName); if (currentEvent == null) { context.SetResponse((int)PowerLineEventHandlerResponseType.EventNotFound); context.SetResponseHttpString("Given event was not found"); return; } if (Server.innerSubscribeEvent(context.WebsocketClient, currentEvent)) { context.SetResponse((int)PowerLineEventHandlerResponseType.EventSubscribed); context.SetResponseHttpString("You are subscribed to the event"); return; } else { context.SetResponse((int)PowerLineEventHandlerResponseType.EventFaildSubscription); context.SetResponseHttpString("Something went wrong while susbscirbing to event"); return; } } }
internal async Task HandleWebsocketMessageAsync(PowerLineWebsocketClient client, Stream message) { JObject mainMessage; using (StreamReader reader = new StreamReader(message)) { mainMessage = JObject.Parse(reader.ReadToEnd()); } if (!mainMessage.TryGetValue("url", out string requestUrl)) { throw new Exception("Invaild request url"); } if (!mainMessage.TryGetValue("websocketId", out int websocketId)) { throw new Exception("Invaild request url"); } string[] UrlPath = requestUrl.Split('/'); PowerLineContext context = new PowerLineContext(mainMessage, message, 0, UrlPath, client); PowerLineEndPointExecutionResult result = await GetHandleResultAsync(context); switch (result.ResultType) { case PowerLinExecutionResultType.EndPointNotFound: result.Context.SetResponse(404); result.Context.SetResponseHttpString("Not Found"); break; case PowerLinExecutionResultType.HandlerException: result.Context.SetResponse(500); result.Context.SetResponseHttpString(result.Exception.Message); break; case PowerLinExecutionResultType.HttpMethodNotFound: result.Context.SetResponse(404); result.Context.SetResponseHttpString("Not Found [HttpMethod]"); break; } await client.SendResponseAsync(context, websocketId); }
public async Task SendResponseAsync(PowerLineContext context, int websocketid) { JObject obj = new JObject(); obj.Add("code", context.responseCode); obj.Add("websocketId", websocketid); if (context.responseText != null) { obj.Add("text", context.responseText); } if (context.ResponseHeader != null && context.ResponseHeader.Any()) { obj.Add("headers", context.ResponseHeader.GetJson()); } this.AddResponsePayload(obj, context); string rawJobject = obj.ToString(Newtonsoft.Json.Formatting.None); byte[] raw = System.Text.Encoding.UTF8.GetBytes(rawJobject); await this.SendRawData(raw); }
public PowerLineEndPointExecutionResult(PowerLineContext context, PowerLineEndPoint endPoint, PowerLineHandler handler, Exception exception) : this(PowerLinExecutionResultType.HandlerException, context, exception, endPoint, handler) { }
public override Task HandleRequest(PowerLineContext context) => this.HandlerFunction(context);
public PowerLineEndPointExecutionResult(PowerLineContext context, PowerLineEndPoint endPoint, PowerLineHandler handler) : this(PowerLinExecutionResultType.OK, context, null, endPoint, handler) { }
public override Task HandleRequest(PowerLineContext context) { this.HandlerFunction(context); return(Task.FromResult(0)); }
internal async Task <PowerLineEndPointExecutionResult> OnRequestAsync(int index, string[] requestPath, PowerLineContext context) { if (index >= requestPath.Length) { return(await this.OnSelfRequestAsync(index, requestPath, context)); } else { if (this.childEndPoints.TryGetValue(requestPath[index], out PowerLineEndPoint endpoint)) { return(await endpoint.OnRequestAsync(index + 1, requestPath, context)); } else if (this.Dynamic) { return(await this.OnSelfRequestAsync(index, requestPath, context)); } else { PowerLineEndPoint dynamicChildEndPoint = this.GetDyanmicEndpoint(); if (dynamicChildEndPoint != null) { return(await dynamicChildEndPoint.OnRequestAsync(index + 1, requestPath, context)); } return(new PowerLineEndPointExecutionResult(context, PowerLinExecutionResultType.EndPointNotFound, this)); } } }
public override Task HandleRequest(PowerLineContext context) => BaseHandler.HandleRequest(context);
public PowerLineEndPointExecutionResult(PowerLineContext context, PowerLinExecutionResultType resultType, PowerLineEndPoint endPoint) : this(resultType, context, null, endPoint, null) { }
public abstract Task HandleRequest(PowerLineContext context);
internal async Task <PowerLineEndPointExecutionResult> SafeHandleRequestAsync(PowerLineEndPoint endpoint, int index, string[] requestPath, PowerLineContext context) { try { context.PathIndex = index; await this.HandleRequest(context); return(new PowerLineEndPointExecutionResult(context, endpoint, this)); } catch (Exception ex) { return(new PowerLineEndPointExecutionResult(context, endpoint, this, ex)); } }
internal async Task <PowerLineEndPointExecutionResult> OnSelfRequestAsync(int index, string[] requestPath, PowerLineContext context) { if (this.handlers.TryGetValue(context.RequestMethod, out PowerLineHandler handler)) { return(await handler.SafeHandleRequestAsync(this, index, requestPath, context)); } else { return(new PowerLineEndPointExecutionResult(context, PowerLinExecutionResultType.HttpMethodNotFound, this)); } }