private string SerializeContent(HttpContentSettingsGroup content) { return(content switch { StringHttpContentSettingsGroup x => $"new StringHttpContent({CSharpWriter.FromSetting(x.Name)}, {CSharpWriter.FromSetting(x.Data)}, {CSharpWriter.FromSetting(x.ContentType)})", RawHttpContentSettingsGroup x => $"new RawHttpContent({CSharpWriter.FromSetting(x.Name)}, {CSharpWriter.FromSetting(x.Data)}, {CSharpWriter.FromSetting(x.ContentType)})", FileHttpContentSettingsGroup x => $"new FileHttpContent({CSharpWriter.FromSetting(x.Name)}, {CSharpWriter.FromSetting(x.FileName)}, {CSharpWriter.FromSetting(x.ContentType)})", _ => throw new NotImplementedException() });
public override void FromLC(ref string script, ref int lineNumber) { /* * TYPE:STANDARD * "name=hello&value=hi" * "application/x-www-form-urlencoded" * * TYPE:RAW * BASE64_DATA * "application/octet-stream" * * TYPE:BASICAUTH * "myUser" * "myPass" * * TYPE:MULTIPART * "myBoundary" * CONTENT:STRING "name" "content" "content-type" * CONTENT:RAW "name" BASE64_DATA "content-type" * CONTENT:FILE "name" "fileName" "content-type" * */ // First parse the options that are common to every BlockInstance base.FromLC(ref script, ref lineNumber); using var reader = new StringReader(script); string line, lineCopy; while ((line = reader.ReadLine()) != null) { line = line.Trim(); lineCopy = line; lineNumber++; if (string.IsNullOrWhiteSpace(line)) { continue; } if (line.StartsWith("TYPE:")) { try { var reqParams = Regex.Match(line, "TYPE:([A-Z]+)").Groups[1].Value; switch (reqParams) { case "STANDARD": var standardReqParams = new StandardRequestParams(); // Read one line to parse the content line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; LoliCodeParser.ParseSettingValue(ref line, standardReqParams.Content, new StringParameter()); // Read another line to parse the content-type line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; LoliCodeParser.ParseSettingValue(ref line, standardReqParams.ContentType, new StringParameter()); RequestParams = standardReqParams; break; case "RAW": var rawReqParams = new RawRequestParams(); // Read one line to parse the content line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; LoliCodeParser.ParseSettingValue(ref line, rawReqParams.Content, new ByteArrayParameter()); // Read another line to parse the content-type line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; LoliCodeParser.ParseSettingValue(ref line, rawReqParams.ContentType, new StringParameter()); RequestParams = rawReqParams; break; case "BASICAUTH": var basicAuthReqParams = new BasicAuthRequestParams(); // Read one line to parse the username line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; LoliCodeParser.ParseSettingValue(ref line, basicAuthReqParams.Username, new StringParameter()); // Read another line to parse the password line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; LoliCodeParser.ParseSettingValue(ref line, basicAuthReqParams.Password, new StringParameter()); RequestParams = basicAuthReqParams; break; case "MULTIPART": var multipartReqParams = new MultipartRequestParams(); // Read one line to parse the boundary line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; LoliCodeParser.ParseSettingValue(ref line, multipartReqParams.Boundary, new StringParameter()); RequestParams = multipartReqParams; break; default: throw new LoliCodeParsingException(lineNumber, $"Invalid type: {reqParams}"); } } catch (NullReferenceException) { throw new LoliCodeParsingException(lineNumber, "Missing options for the selected content"); } catch { throw new LoliCodeParsingException(lineNumber, $"Could not parse the setting: {lineCopy.TruncatePretty(50)}"); } } else if (line.StartsWith("CONTENT:")) { try { var multipart = (MultipartRequestParams)RequestParams; var token = LineParser.ParseToken(ref line); var tokenType = Regex.Match(token, "CONTENT:([A-Z]+)").Groups[1].Value; switch (tokenType) { case "STRING": var stringContent = new StringHttpContentSettingsGroup(); LoliCodeParser.ParseSettingValue(ref line, stringContent.Name, new StringParameter()); LoliCodeParser.ParseSettingValue(ref line, stringContent.Data, new StringParameter()); LoliCodeParser.ParseSettingValue(ref line, stringContent.ContentType, new StringParameter()); multipart.Contents.Add(stringContent); break; case "RAW": var rawContent = new RawHttpContentSettingsGroup(); LoliCodeParser.ParseSettingValue(ref line, rawContent.Name, new StringParameter()); LoliCodeParser.ParseSettingValue(ref line, rawContent.Data, new ByteArrayParameter()); LoliCodeParser.ParseSettingValue(ref line, rawContent.ContentType, new StringParameter()); multipart.Contents.Add(rawContent); break; case "FILE": var fileContent = new FileHttpContentSettingsGroup(); LoliCodeParser.ParseSettingValue(ref line, fileContent.Name, new StringParameter()); LoliCodeParser.ParseSettingValue(ref line, fileContent.FileName, new StringParameter()); LoliCodeParser.ParseSettingValue(ref line, fileContent.ContentType, new StringParameter()); multipart.Contents.Add(fileContent); break; } } catch { throw new LoliCodeParsingException(lineNumber, $"Could not parse the multipart content: {lineCopy.TruncatePretty(50)}"); } } else { try { LoliCodeParser.ParseSetting(ref line, Settings, Descriptor); } catch { throw new LoliCodeParsingException(lineNumber, $"Could not parse the setting: {lineCopy.TruncatePretty(50)}"); } } } }