Task HandleContextRoute(Route route, RestListenerContext context) { Action <RestListenerContext> handlerAction = route[context.Request.RawUrl]; Task handlerTask = null; if (handlerAction != null) { handlerTask = pool.Run(() => handlerAction(context)); } return(handlerTask); }
public async Task Start() { pool = new WorkerPool(4, true, false, 0); ServerContext.StartFunc(); while (true) { RestListenerContext httpContext = await ServerContext.GetContextAsync(); AddSession(httpContext); HandleContext(httpContext); } }
void HandleContext(RestListenerContext context) { switch (context.Request.HttpMethod) { case "GET": HandleContextRoute(Get, context); break; case "POST": HandleContextRoute(Post, context); break; case "PUT": HandleContextRoute(Put, context); break; case "UPDATE": HandleContextRoute(Update, context); break; case "DELETE": HandleContextRoute(Delete, context); break; default: ServerContext.DefaultHandler(context); break; } }
void AddSession(RestListenerContext context) { if (ServerContext.NeedsSession != null && ServerContext.GetSessionKey != null) { if (ServerContext.NeedsSession(context)) { Session session = null; if (!sessions.TryGetValue(ServerContext.GetSessionKey(context), out session)) { session = new Session(); if (ServerContext.GetValidUntil != null) { session.ValidUntil = ServerContext.GetValidUntil(session.CreationDate); } } context.Session = session; } } }
public static RestListenerContext ToRestListenerContext(this HttpListenerContext httpContext) { RestListenerRequest request = new RestListenerRequest(); HttpListenerRequest httpRequest = httpContext.Request; request.AcceptTypes = httpRequest.AcceptTypes; //request.ClientCertificateError = httpRequest.ClientCertificateError; request.ContentEncoding = httpRequest.ContentEncoding; request.ContentLength64 = httpRequest.ContentLength64; request.ContentType = httpRequest.ContentType; request.ContentLength64 = httpRequest.ContentLength64; request.Cookies = httpRequest.Cookies; // TODO: Form parameter request.HasEntityBody = httpRequest.HasEntityBody; request.Header = httpRequest.Headers.ToNameValueList(); request.HttpMethod = httpRequest.HttpMethod; request.InputStream = httpRequest.InputStream; request.IsAuthenticated = httpRequest.IsAuthenticated; request.IsLocal = httpRequest.IsLocal; request.IsSecureConnection = httpRequest.IsSecureConnection; request.IsWebSocketRequest = httpRequest.IsWebSocketRequest; request.KeepAlive = httpRequest.KeepAlive; request.LocalEndPoint = httpRequest.LocalEndPoint; request.ProtocolVersion = httpRequest.ProtocolVersion; request.QueryParameter = httpRequest.QueryString.ToNameValueList(); request.RawUrl = httpRequest.RawUrl; request.RemoteEndPoint = httpRequest.RemoteEndPoint; request.RequestTraceIdentifier = httpRequest.RequestTraceIdentifier; request.ServiceName = httpRequest.ServiceName; request.UrlReferrer = httpRequest.UrlReferrer; request.Url = httpRequest.Url; request.UserAgent = httpRequest.UserAgent; request.UserHostAddress = httpRequest.UserHostAddress; request.UserHostName = httpRequest.UserHostName; request.UserLanguages = httpRequest.UserLanguages; // TODO: Set form url parameter RestListenerResponse response = new RestListenerResponse(); HttpListenerResponse httpResponse = httpContext.Response; response.Abort = httpResponse.Abort; response.AppendCookie = httpResponse.AppendCookie; response.Close = httpResponse.Close; response.Redirect = httpResponse.Redirect; response.SetCookie = httpResponse.SetCookie; response.ContentLength64 = FProperty.Create(() => httpResponse.ContentLength64, cl => httpResponse.ContentLength64 = cl); response.ContentType = FProperty.Create(() => httpResponse.ContentType, ct => httpResponse.ContentType = ct); response.ContentEncoding = FProperty.Create(() => httpResponse.ContentEncoding, ce => httpResponse.ContentEncoding = ce); response.Cookies = FProperty.Create(() => httpResponse.Cookies, cookies => httpResponse.Cookies = cookies); response.Header = httpResponse.Headers.ToNameValueList(); response.KeepAlive = FProperty.Create(() => httpResponse.KeepAlive, ka => httpResponse.KeepAlive = ka); response.OutputStream = httpResponse.OutputStream; response.ProtocolVersion = FProperty.Create(() => httpResponse.ProtocolVersion, pv => httpResponse.ProtocolVersion = pv); response.RedirectLocation = FProperty.Create(() => httpResponse.RedirectLocation, rl => httpResponse.RedirectLocation = rl); response.StatusCode = FProperty.Create(() => httpResponse.StatusCode, sc => httpResponse.StatusCode = sc); response.SendChunked = FProperty.Create(() => httpResponse.SendChunked, sc => httpResponse.SendChunked = sc); response.StatusDescription = FProperty.Create(() => httpResponse.StatusDescription, sd => httpResponse.StatusDescription = sd); return(RestListenerContext.Create(request, response)); }