From 983fd8d35f33983214b0227c672e89c3c69974ce Mon Sep 17 00:00:00 2001 From: ruri Date: Mon, 10 Jun 2019 18:55:32 +0200 Subject: [PATCH] Added the content-type field for file multipart contents. --- OpenBullet/LSDoc.xml | 4 +- .../Pages/StackerBlocks/PageBlockRequest.xaml | 2 +- OpenBullet/SyntaxHelper.xml | 4 +- RuriLib/Blocks/BlockRequest.cs | 38 +++++++++++-------- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/OpenBullet/LSDoc.xml b/OpenBullet/LSDoc.xml index 9cd5244..4f9fb2e 100644 --- a/OpenBullet/LSDoc.xml +++ b/OpenBullet/LSDoc.xml @@ -275,8 +275,8 @@ REQUEST METHOD "URL" [AcceptEncoding?] [AutoRedirect?] [ReadResponseSource?] [Pa [STANDARD / MULTIPART / BASICAUTH] [CONTENT "postdata"] [CONTENTTYPE "ctype"] - [STRINGCONTENT "name: value"]* - [FILECONTENT "name: path"]* + [STRINGCONTENT "name:value"]* + [FILECONTENT "name:path:content-type"]* [BOUNDARY "abcd"] [COOKIE "abc: def"]* [HEADER "abc: def"]* diff --git a/OpenBullet/Pages/StackerBlocks/PageBlockRequest.xaml b/OpenBullet/Pages/StackerBlocks/PageBlockRequest.xaml index dd5d375..e35dfe1 100644 --- a/OpenBullet/Pages/StackerBlocks/PageBlockRequest.xaml +++ b/OpenBullet/Pages/StackerBlocks/PageBlockRequest.xaml @@ -99,7 +99,7 @@ - Syntax:STRING:fieldname:fieldvalueFILE:fieldname:filepath + Syntax:STRING:fieldname:fieldvalueFILE:fieldname:filepath:content-type diff --git a/OpenBullet/SyntaxHelper.xml b/OpenBullet/SyntaxHelper.xml index e3e50d4..0926d3e 100644 --- a/OpenBullet/SyntaxHelper.xml +++ b/OpenBullet/SyntaxHelper.xml @@ -14,8 +14,8 @@ REQUEST METHOD "URL" [AcceptEncoding?] [AutoRedirect?] [ReadResponseSource?] [Pa [STANDARD / MULTIPART / BASICAUTH] [CONTENT "postdata"] [CONTENTTYPE "ctype"] - [STRINGCONTENT "name: value"]* - [FILECONTENT "name: path"]* + [STRINGCONTENT "name:value"]* + [FILECONTENT "name:path:content-type"]* [BOUNDARY "abcd"] [COOKIE "abc: def"]* [HEADER "abc: def"]* diff --git a/RuriLib/Blocks/BlockRequest.cs b/RuriLib/Blocks/BlockRequest.cs index b3f265d..18931ac 100644 --- a/RuriLib/Blocks/BlockRequest.cs +++ b/RuriLib/Blocks/BlockRequest.cs @@ -180,23 +180,23 @@ namespace RuriLib break; case "STRINGCONTENT": - var stringContentPair = ParsePair(LineParser.ParseLiteral(ref input, "STRING CONTENT")); - MultipartContents.Add(new MultipartContent() { Type = MultipartContentType.String, Name = stringContentPair.Key, Value = stringContentPair.Value }); + var stringContentPair = ParseString(LineParser.ParseLiteral(ref input, "STRING CONTENT"), ':', 2); + MultipartContents.Add(new MultipartContent() { Type = MultipartContentType.String, Name = stringContentPair[0], Value = stringContentPair[1] }); break; case "FILECONTENT": - var fileContentPair = ParsePair(LineParser.ParseLiteral(ref input, "FILE CONTENT")); - MultipartContents.Add(new MultipartContent() { Type = MultipartContentType.File, Name = fileContentPair.Key, Value = fileContentPair.Value }); + var fileContentTriplet = ParseString(LineParser.ParseLiteral(ref input, "FILE CONTENT"), ':', 3); + MultipartContents.Add(new MultipartContent() { Type = MultipartContentType.File, Name = fileContentTriplet[0], Value = fileContentTriplet[1], ContentType = fileContentTriplet[2] }); break; case "COOKIE": - var cookiePair = ParsePair(LineParser.ParseLiteral(ref input, "COOKIE VALUE")); - CustomCookies[cookiePair.Key] = cookiePair.Value; + var cookiePair = ParseString(LineParser.ParseLiteral(ref input, "COOKIE VALUE"), ':', 2); + CustomCookies[cookiePair[0]] = cookiePair[1]; break; case "HEADER": - var headerPair = ParsePair(LineParser.ParseLiteral(ref input, "HEADER VALUE")); - CustomHeaders[headerPair.Key] = headerPair.Value; + var headerPair = ParseString(LineParser.ParseLiteral(ref input, "HEADER VALUE"), ':', 2); + CustomHeaders[headerPair[0]] = headerPair[1]; break; case "CONTENTTYPE": @@ -236,14 +236,15 @@ namespace RuriLib } /// - /// Parses a pair of values separated by a colon. + /// Parses values from a string. /// - /// The string containing colon-separated values - /// The pair of values. - public static KeyValuePair ParsePair(string pair) + /// The string to parse + /// The character that separates the elements + /// The number of elements to return + /// The array of the parsed elements. + public static string[] ParseString(string input, char separator, int count) { - var split = pair.Split(new[] { ':' }, 2); - return new KeyValuePair(split[0].Trim(), split[1].Trim()); + return input.Split(new[] { separator }, count).Select(s => s.Trim()).ToArray(); } /// @@ -421,6 +422,8 @@ namespace RuriLib { var rValue = ReplaceValues(c.Value, data); var rName = ReplaceValues(c.Name, data); + var rContentType = ReplaceValues(c.ContentType, data); + if (c.Type == MultipartContentType.String) { mContent.Add(new StringContent(rValue), rName); @@ -428,8 +431,8 @@ namespace RuriLib } else if (c.Type == MultipartContentType.File) { - mContent.Add(new FileContent(rValue), rName); - data.Log(new LogEntry($"Content-Disposition: form-data; name=\"{rName}\"{Environment.NewLine}{Environment.NewLine}[FILE CONTENT OMITTED]", Colors.MediumTurquoise)); + mContent.Add(new FileContent(rValue), rName, rValue, rContentType); + data.Log(new LogEntry($"Content-Disposition: form-data; name=\"{rName}\"; filename=\"{rValue}\"{Environment.NewLine}Content-Type: {rContentType}{Environment.NewLine}{Environment.NewLine}[FILE CONTENT OMITTED]", Colors.MediumTurquoise)); } data.Log(new LogEntry(bdry, Colors.MediumTurquoise)); } @@ -725,5 +728,8 @@ namespace RuriLib /// The value of the multipart content (a string value or a file path). public string Value { get; set; } = ""; + + /// The Content-Type of the file content. + public string ContentType { get; set; } = ""; } }