Esempio n. 1
0
        /// <summary>
        /// Send Response
        /// </summary>
        /// <param name="command"></param>
        /// <param name="writer"></param>
        /// <param name="encrypt"></param>
        /// <param name="statusCode"></param>
        /// <returns></returns>
        private async Task SendResponse(Command command, DataWriter writer, bool encrypt, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            var json = CommandParserHelper.SerializeCommand(command);

            string encryptHeader = string.Empty;

            if (encrypt)
            {
                json          = CommandParserHelper.EncryptMessage(json, SecretManager.EncryptionKey);
                encryptHeader = $"{EdisonEncryptHeader}: true\r\n";
            }

            var message = $"HTTP/1.1 {(int)statusCode} {statusCode}\r\nContent-Length: {json.Length}\r\ncontent-type: application/json\r\n{encryptHeader}Connection: close\r\n\r\n" + json;

            writer.WriteString(message);
            await writer.StoreAsync();

            DebugHelper.LogInformation($"Sent: {command.BaseCommand}, {command.Data}");
        }
Esempio n. 2
0
        /// <summary>
        /// Send Response
        /// </summary>
        /// <param name="command"></param>
        /// <param name="writer"></param>
        /// <param name="encrypt"></param>
        /// <param name="statusCode"></param>
        /// <returns></returns>
        private void SendResponse(Command command, HttpListenerResponse response, bool encrypt, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            var json = CommandParserHelper.SerializeCommand(command);

            //Headers
            response.StatusCode      = (int)statusCode;
            response.ContentType     = "application/json";
            response.ContentLength64 = json.Length;
            //Encrypt
            if (encrypt)
            {
                json = CommandParserHelper.EncryptMessage(json, SecretManager.EncryptionKey);
                response.AddHeader(EdisonEncryptHeader, "true");
            }
            response.AddHeader("Connection", "close");

            response.OutputStream.Write(Encoding.UTF8.GetBytes(json));
            DebugHelper.LogInformation($"Sent: {command.BaseCommand}, {command.Data}");
        }