private ResponseMessage FileGet(RequestMessage requestMessage)
        {
            string filename = GetFileNameFromRequestMessage(requestMessage);

            if (!_fileSystemHandler.FileExists(filename))
            {
                _logger.Info("The file '{0}' does not exist.", filename);
                return(ResponseMessageBuilder.Create("File is not found", 404));
            }

            byte[] bytes    = _fileSystemHandler.ReadFile(filename);
            var    response = new ResponseMessage
            {
                StatusCode = 200,
                BodyData   = new BodyData
                {
                    BodyAsBytes      = bytes,
                    DetectedBodyType = BodyType.Bytes,
                    DetectedBodyTypeFromContentType = BodyType.None
                }
            };

            if (BytesEncodingUtils.TryGetEncoding(bytes, out Encoding encoding) && FileBodyIsString.Select(x => x.Equals(encoding)).Any())
            {
                response.BodyData.DetectedBodyType = BodyType.String;
                response.BodyData.BodyAsString     = encoding.GetString(bytes);
            }

            return(response);
        }
Esempio n. 2
0
        public void TryGetEncoding_Invalid()
        {
            var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff }, out Encoding encoding);

            // Assert
            result.Should().BeFalse();
            encoding.Should().BeNull();
        }
Esempio n. 3
0
        public void TryGetEncoding_UTF32()
        {
            var result = BytesEncodingUtils.TryGetEncoding(new byte[] { 0xff, 0xfe, 0x00, 0x00 }, out Encoding encoding);

            // Assert
            result.Should().BeTrue();
            encoding.CodePage.Should().Be(Encoding.UTF32.CodePage);
        }