Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/CloudInit.ConfigDrive.Core/UserDataSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,32 @@ public class UserDataSerializer : IUserDataSerializer
public async Task<Stream> SerializeUserData(IEnumerable<UserData> userData, UserDataOptions options)
{
var sb = new StringBuilder();
sb.AppendLine("From nobody Fri Jan 11 07:00:00 1980");
sb.AppendLine("Content-Type: multipart/mixed; boundary=\"==BOUNDARY==\"");
sb.AppendLine("MIME-Version: 1.0");

sb.Append("From nobody Fri Jan 11 07:00:00 1980\n");
sb.Append("Content-Type: multipart/mixed; boundary=\"==BOUNDARY==\"\n");
sb.Append("MIME-Version: 1.0\n");

foreach (var data in userData)
{
sb.AppendLine("--==BOUNDARY==");
sb.AppendLine("MIME-Version: 1.0");
sb.Append("--==BOUNDARY==\n");
sb.Append("MIME-Version: 1.0\n");


sb.AppendLine($"Content-Type: {data.ContentType.Name}; charset=\"{data.Encoding.BodyName}\"");
sb.Append($"Content-Type: {data.ContentType.Name}; charset=\"{data.Encoding.BodyName}\"\n");

var contentString = data.Content;
if(!string.IsNullOrWhiteSpace(data.FileName))
sb.Append($"Content-Disposition: attachment; filename=\"{data.FileName}\"\n");

var contentString = data.Content.Replace("\r\n", "\n");
if (options.Base64Encode)
{
contentString = Convert.ToBase64String(data.Encoding.GetBytes(contentString), Base64FormattingOptions.InsertLineBreaks);
sb.AppendLine("Content-Transfer-Encoding: base64");
contentString = Convert
.ToBase64String(data.Encoding.GetBytes(contentString), Base64FormattingOptions.InsertLineBreaks)
.Replace("\r\n", "\n");
sb.Append("Content-Transfer-Encoding: base64\n");
}

sb.AppendLine(contentString);
sb.Append(contentString+ "\n");


}
Expand Down
16 changes: 13 additions & 3 deletions src/CloudInit.ConfigDrive.Primitives/UserData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using JetBrains.Annotations;

namespace Dbosoft.CloudInit.ConfigDrive
{
[PublicAPI]
public class UserData
{
public UserData(UserDataContentType contentType, string content, Encoding encoding)
Expand All @@ -14,9 +16,17 @@ public UserData(UserDataContentType contentType, string content, Encoding encodi
Encoding = encoding;
}

public UserDataContentType ContentType { get; set; }
public string Content { get; set; }
public Encoding Encoding { get; set; }
public UserData(UserDataContentType contentType, string content, string fileName, Encoding encoding)
{
ContentType = contentType;
Content = content;
FileName = fileName;
Encoding = encoding;
}

public UserDataContentType ContentType { get; set; }
public string Content { get; }
public Encoding Encoding { get; }
public string? FileName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task WritesFixedHeader()
Assert.Equal(@"From nobody Fri Jan 11 07:00:00 1980
Content-Type: multipart/mixed; boundary=""==BOUNDARY==""
MIME-Version: 1.0
", act);
".Replace("\r\n", "\n"), act);
}

[Fact]
Expand Down Expand Up @@ -59,7 +59,7 @@ await serializer.SerializeUserData(
MIME-Version: 1.0
Content-Type: text/cloud-config; charset=""us-ascii""
some config
", act);
".Replace("\r\n", "\n"), act);
}

[Fact]
Expand Down Expand Up @@ -90,7 +90,7 @@ await serializer.SerializeUserData(
Content-Type: text/cloud-config; charset=""us-ascii""
Content-Transfer-Encoding: base64
{Convert.ToBase64String(Encoding.ASCII.GetBytes("some config"), Base64FormattingOptions.InsertLineBreaks)}
", act);
".Replace("\r\n", "\n"), act);

}

Expand Down Expand Up @@ -118,7 +118,7 @@ public async Task GZipCompressed()
Assert.Equal(@"From nobody Fri Jan 11 07:00:00 1980
Content-Type: multipart/mixed; boundary=""==BOUNDARY==""
MIME-Version: 1.0
", act);
".Replace("\r\n", "\n"), act);

}

Expand Down