Beispiel #1
0
        protected virtual HttpListenerResponse HandleRequest(HttpListenerContext context)
        {
            var requestBody = default(string);

            using (var reader = new StreamReader(context.Request.InputStream))
                requestBody = reader.ReadToEnd();

            if (DecryptRequest)
            {
                requestBody = WPDUtil.Transform(requestBody, "D");
            }

            var jsonObj = JObject.Parse(requestBody);

            var cmd = default(JToken);

            if (!jsonObj.TryGetValue("cmd", out cmd))
            {
                Logger.Get <WPDListener>().LogWarning("Invalid WPD-Request!");
                return(BadRequest(context.Response));
            }

            if (cmd.Type != JTokenType.String)
            {
                Logger.Get <WPDListener>().LogWarning("Illegal JSON-Type!");
                return(BadRequest(context.Response));
            }

            var sCmd = cmd.ToObject <string>();
            var type = default(Type);

            if (!Handlers.TryGetValue(sCmd, out type))
            {
                Logger.Get <WPDListener>().LogWarning($"Unhandled {sCmd}Request.");
                dynamic obj = JsonConvert.DeserializeObject(requestBody);
                Console.WriteLine(JsonConvert.SerializeObject(obj, Formatting.Indented));
                return(BadRequest(context.Response));
            }

            using (var request = JsonConvert.DeserializeObject(requestBody, type) as WPDRequest)
            {
                using (var writer = new StreamWriter(context.Response.OutputStream))
                    writer.Write(request.Handle().GetResponseBody());
            }

            return(context.Response);
        }
Beispiel #2
0
        public string GetResponseBody()
        {
            var data = JsonConvert.SerializeObject(this);

            return(EncryptResponse ? WPDUtil.Transform(data, "E") : data);
        }