コード例 #1
0
ファイル: PowerLineServer.cs プロジェクト: yanivka/PowerLine
        private async Task HandleContextAsync(HttpListenerContext context)
        {
            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;
            }
            result.Context.response.StatusCode = result.Context.responseCode;
            if (result.Context.responseText != null)
            {
                result.Context.response.StatusDescription = result.Context.responseText;
            }
            result.Context.response.Headers.Clear();
            if (result.Context.ResponseHeader != null)
            {
                foreach (KeyValuePair <string, string> header in result.Context.ResponseHeader)
                {
                    result.Context.response.Headers.Add(header.Key, header.Value);
                }
            }
            if (result.Context.ResponsePayloadLength != -1)
            {
                result.Context.response.ContentLength64 = result.Context.ResponsePayloadLength;
            }
            if (result.Context.ResponsePayload != null)
            {
                result.Context.ResponsePayload.CopyTo(result.Context.response.OutputStream);
                result.Context.ResponsePayload.Close();
            }

            result.Context.response.Close(); // Send the response to the remote endpoint
        }
コード例 #2
0
ファイル: PowerLineServer.cs プロジェクト: yanivka/PowerLine
        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);
        }