Added the content-type field for file multipart contents.

This commit is contained in:
ruri
2019-06-10 18:55:32 +02:00
parent d845af13e1
commit 983fd8d35f
4 changed files with 27 additions and 21 deletions
+2 -2
View File
@@ -275,8 +275,8 @@ REQUEST METHOD "URL" [AcceptEncoding?] [AutoRedirect?] [ReadResponseSource?] [Pa
[STANDARD / MULTIPART / BASICAUTH] [STANDARD / MULTIPART / BASICAUTH]
[CONTENT "postdata"] [CONTENT "postdata"]
[CONTENTTYPE "ctype"] [CONTENTTYPE "ctype"]
[STRINGCONTENT "name: value"]* [STRINGCONTENT "name:value"]*
[FILECONTENT "name: path"]* [FILECONTENT "name:path:content-type"]*
[BOUNDARY "abcd"] [BOUNDARY "abcd"]
[COOKIE "abc: def"]* [COOKIE "abc: def"]*
[HEADER "abc: def"]* [HEADER "abc: def"]*
@@ -99,7 +99,7 @@
</Style> </Style>
</RichTextBox.Resources> </RichTextBox.Resources>
<RichTextBox.ToolTip> <RichTextBox.ToolTip>
<TextBlock>Syntax:<LineBreak/>STRING:fieldname:fieldvalue<LineBreak/>FILE:fieldname:filepath</TextBlock> <TextBlock>Syntax:<LineBreak/>STRING:fieldname:fieldvalue<LineBreak/>FILE:fieldname:filepath:content-type</TextBlock>
</RichTextBox.ToolTip> </RichTextBox.ToolTip>
</RichTextBox> </RichTextBox>
</StackPanel> </StackPanel>
+2 -2
View File
@@ -14,8 +14,8 @@ REQUEST METHOD "URL" [AcceptEncoding?] [AutoRedirect?] [ReadResponseSource?] [Pa
[STANDARD / MULTIPART / BASICAUTH] [STANDARD / MULTIPART / BASICAUTH]
[CONTENT "postdata"] [CONTENT "postdata"]
[CONTENTTYPE "ctype"] [CONTENTTYPE "ctype"]
[STRINGCONTENT "name: value"]* [STRINGCONTENT "name:value"]*
[FILECONTENT "name: path"]* [FILECONTENT "name:path:content-type"]*
[BOUNDARY "abcd"] [BOUNDARY "abcd"]
[COOKIE "abc: def"]* [COOKIE "abc: def"]*
[HEADER "abc: def"]* [HEADER "abc: def"]*
+22 -16
View File
@@ -180,23 +180,23 @@ namespace RuriLib
break; break;
case "STRINGCONTENT": case "STRINGCONTENT":
var stringContentPair = ParsePair(LineParser.ParseLiteral(ref input, "STRING CONTENT")); var stringContentPair = ParseString(LineParser.ParseLiteral(ref input, "STRING CONTENT"), ':', 2);
MultipartContents.Add(new MultipartContent() { Type = MultipartContentType.String, Name = stringContentPair.Key, Value = stringContentPair.Value }); MultipartContents.Add(new MultipartContent() { Type = MultipartContentType.String, Name = stringContentPair[0], Value = stringContentPair[1] });
break; break;
case "FILECONTENT": case "FILECONTENT":
var fileContentPair = ParsePair(LineParser.ParseLiteral(ref input, "FILE CONTENT")); var fileContentTriplet = ParseString(LineParser.ParseLiteral(ref input, "FILE CONTENT"), ':', 3);
MultipartContents.Add(new MultipartContent() { Type = MultipartContentType.File, Name = fileContentPair.Key, Value = fileContentPair.Value }); MultipartContents.Add(new MultipartContent() { Type = MultipartContentType.File, Name = fileContentTriplet[0], Value = fileContentTriplet[1], ContentType = fileContentTriplet[2] });
break; break;
case "COOKIE": case "COOKIE":
var cookiePair = ParsePair(LineParser.ParseLiteral(ref input, "COOKIE VALUE")); var cookiePair = ParseString(LineParser.ParseLiteral(ref input, "COOKIE VALUE"), ':', 2);
CustomCookies[cookiePair.Key] = cookiePair.Value; CustomCookies[cookiePair[0]] = cookiePair[1];
break; break;
case "HEADER": case "HEADER":
var headerPair = ParsePair(LineParser.ParseLiteral(ref input, "HEADER VALUE")); var headerPair = ParseString(LineParser.ParseLiteral(ref input, "HEADER VALUE"), ':', 2);
CustomHeaders[headerPair.Key] = headerPair.Value; CustomHeaders[headerPair[0]] = headerPair[1];
break; break;
case "CONTENTTYPE": case "CONTENTTYPE":
@@ -236,14 +236,15 @@ namespace RuriLib
} }
/// <summary> /// <summary>
/// Parses a pair of values separated by a colon. /// Parses values from a string.
/// </summary> /// </summary>
/// <param name="pair">The string containing colon-separated values</param> /// <param name="input">The string to parse</param>
/// <returns>The pair of values.</returns> /// <param name="separator">The character that separates the elements</param>
public static KeyValuePair<string, string> ParsePair(string pair) /// <param name="count">The number of elements to return</param>
/// <returns>The array of the parsed elements.</returns>
public static string[] ParseString(string input, char separator, int count)
{ {
var split = pair.Split(new[] { ':' }, 2); return input.Split(new[] { separator }, count).Select(s => s.Trim()).ToArray();
return new KeyValuePair<string, string>(split[0].Trim(), split[1].Trim());
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -421,6 +422,8 @@ namespace RuriLib
{ {
var rValue = ReplaceValues(c.Value, data); var rValue = ReplaceValues(c.Value, data);
var rName = ReplaceValues(c.Name, data); var rName = ReplaceValues(c.Name, data);
var rContentType = ReplaceValues(c.ContentType, data);
if (c.Type == MultipartContentType.String) if (c.Type == MultipartContentType.String)
{ {
mContent.Add(new StringContent(rValue), rName); mContent.Add(new StringContent(rValue), rName);
@@ -428,8 +431,8 @@ namespace RuriLib
} }
else if (c.Type == MultipartContentType.File) else if (c.Type == MultipartContentType.File)
{ {
mContent.Add(new FileContent(rValue), rName); mContent.Add(new FileContent(rValue), rName, rValue, rContentType);
data.Log(new LogEntry($"Content-Disposition: form-data; name=\"{rName}\"{Environment.NewLine}{Environment.NewLine}[FILE CONTENT OMITTED]", Colors.MediumTurquoise)); 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)); data.Log(new LogEntry(bdry, Colors.MediumTurquoise));
} }
@@ -725,5 +728,8 @@ namespace RuriLib
/// <summary>The value of the multipart content (a string value or a file path).</summary> /// <summary>The value of the multipart content (a string value or a file path).</summary>
public string Value { get; set; } = ""; public string Value { get; set; } = "";
/// <summary>The Content-Type of the file content.</summary>
public string ContentType { get; set; } = "";
} }
} }