Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish as Curl.Parser.Net and Curl.Converter.Net as NuGet packages #99

Merged
merged 7 commits into from
Oct 9, 2022
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,6 @@ tools/*

# cypress
**/cypress/videos

.vscode/
.DS_Store
65 changes: 64 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,74 @@ You can grab latest binaries [here](https://ci.appveyor.com/project/olsh/curl-to
1. Install cake

```bash
dotnet tool install -g Cake.Tool`
dotnet tool install -g Cake.Tool
```

2. Run build

```bash
dotnet cake build.cake
```

## NuGet Packages
### Curl.CommandLine.Parser
#### Key Features
- Parse cURL command into individual cURL options.
- Return parsing errors and warnings if the cURL input is invalid.

#### Installation
Install with NuGet
```cmd
dotnet add package Curl.CommandLine.Parser
```

#### Usage/Examples
```c#
var input = @"curl https://sentry.io/api/0/projects/1/groups/?status=unresolved -d '{""status"": ""resolved""}' -H 'Content-Type: application/json' -u 'username:password' -H 'Accept: application/json' -H 'User-Agent: curl/7.60.0'";

var output = new CurlParser(new ParsingOptions() { MaxUploadFiles = 10 }).Parse(input);

Console.WriteLine(output.Data.UploadData.First().Content);
// Output:
// {"status": "resolved"}
```

### Curl.HttpClient.Converter
#### Key Features
- Parse cURL command into C# code.
- Convert output from CurlParser into C# code.
- Return parsing errors and warnings if the cURL input is invalid.

#### Installation
Install with NuGet
```cmd
dotnet add package Curl.HttpClient.Converter
```

#### Usage/Examples
```c#
var input = @"curl https://sentry.io/api/0/projects/1/groups/?status=unresolved -d '{""status"": ""resolved""}' -H 'Content-Type: application/json' -u 'username:password' -H 'Accept: application/json' -H 'User-Agent: curl/7.60.0'";
var curlOption = new CurlParser(new ParsingOptions() { MaxUploadFiles = 10 }).Parse(input);
var output = new CurlConverter().ToCsharp(curlOption.Data);
// Output:
/*
// In production code, don't destroy the HttpClient through using, but better reuse an existing instance
// https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://sentry.io/api/0/projects/1/groups/?status=unresolved"))
{
request.Headers.TryAddWithoutValidation("Accept", "application/json");
request.Headers.TryAddWithoutValidation("User-Agent", "curl/7.60.0");

var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:password"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");

request.Content = new StringContent("{\"status\": \"resolved\"}");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

var response = await httpClient.SendAsync(request);
}
}
*/
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CurlToCSharp.Constants;
namespace Curl.CommandLine.Parser.Constants;

public static class Chars
{
Expand Down
294 changes: 294 additions & 0 deletions src/Curl.CommandLine.Parser/Constants/HeaderNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
namespace Curl.CommandLine.Parser.Constants;

public static class HeaderNames
{
// Use readonly statics rather than constants so ReferenceEquals works

/// <summary>Gets the <c>Accept</c> HTTP header name.</summary>
public static readonly string Accept = "Accept";

/// <summary>Gets the <c>Accept-Charset</c> HTTP header name.</summary>
public static readonly string AcceptCharset = "Accept-Charset";

/// <summary>Gets the <c>Accept-Encoding</c> HTTP header name.</summary>
public static readonly string AcceptEncoding = "Accept-Encoding";

/// <summary>Gets the <c>Accept-Language</c> HTTP header name.</summary>
public static readonly string AcceptLanguage = "Accept-Language";

/// <summary>Gets the <c>Accept-Ranges</c> HTTP header name.</summary>
public static readonly string AcceptRanges = "Accept-Ranges";

/// <summary>Gets the <c>Access-Control-Allow-Credentials</c> HTTP header name.</summary>
public static readonly string AccessControlAllowCredentials = "Access-Control-Allow-Credentials";

/// <summary>Gets the <c>Access-Control-Allow-Headers</c> HTTP header name.</summary>
public static readonly string AccessControlAllowHeaders = "Access-Control-Allow-Headers";

/// <summary>Gets the <c>Access-Control-Allow-Methods</c> HTTP header name.</summary>
public static readonly string AccessControlAllowMethods = "Access-Control-Allow-Methods";

/// <summary>Gets the <c>Access-Control-Allow-Origin</c> HTTP header name.</summary>
public static readonly string AccessControlAllowOrigin = "Access-Control-Allow-Origin";

/// <summary>Gets the <c>Access-Control-Expose-Headers</c> HTTP header name.</summary>
public static readonly string AccessControlExposeHeaders = "Access-Control-Expose-Headers";

/// <summary>Gets the <c>Access-Control-Max-Age</c> HTTP header name.</summary>
public static readonly string AccessControlMaxAge = "Access-Control-Max-Age";

/// <summary>Gets the <c>Access-Control-Request-Headers</c> HTTP header name.</summary>
public static readonly string AccessControlRequestHeaders = "Access-Control-Request-Headers";

/// <summary>Gets the <c>Access-Control-Request-Method</c> HTTP header name.</summary>
public static readonly string AccessControlRequestMethod = "Access-Control-Request-Method";

/// <summary>Gets the <c>Age</c> HTTP header name.</summary>
public static readonly string Age = "Age";

/// <summary>Gets the <c>Allow</c> HTTP header name.</summary>
public static readonly string Allow = "Allow";

/// <summary>Gets the <c>Alt-Svc</c> HTTP header name.</summary>
public static readonly string AltSvc = "Alt-Svc";

/// <summary>Gets the <c>:authority</c> HTTP header name.</summary>
public static readonly string Authority = ":authority";

/// <summary>Gets the <c>Authorization</c> HTTP header name.</summary>
public static readonly string Authorization = "Authorization";

/// <summary>Gets the <c>baggage</c> HTTP header name.</summary>
public static readonly string Baggage = "baggage";

/// <summary>Gets the <c>Cache-Control</c> HTTP header name.</summary>
public static readonly string CacheControl = "Cache-Control";

/// <summary>Gets the <c>Connection</c> HTTP header name.</summary>
public static readonly string Connection = "Connection";

/// <summary>Gets the <c>Content-Disposition</c> HTTP header name.</summary>
public static readonly string ContentDisposition = "Content-Disposition";

/// <summary>Gets the <c>Content-Encoding</c> HTTP header name.</summary>
public static readonly string ContentEncoding = "Content-Encoding";

/// <summary>Gets the <c>Content-Language</c> HTTP header name.</summary>
public static readonly string ContentLanguage = "Content-Language";

/// <summary>Gets the <c>Content-Length</c> HTTP header name.</summary>
public static readonly string ContentLength = "Content-Length";

/// <summary>Gets the <c>Content-Location</c> HTTP header name.</summary>
public static readonly string ContentLocation = "Content-Location";

/// <summary>Gets the <c>Content-MD5</c> HTTP header name.</summary>
public static readonly string ContentMD5 = "Content-MD5";

/// <summary>Gets the <c>Content-Range</c> HTTP header name.</summary>
public static readonly string ContentRange = "Content-Range";

/// <summary>Gets the <c>Content-Security-Policy</c> HTTP header name.</summary>
public static readonly string ContentSecurityPolicy = "Content-Security-Policy";

/// <summary>Gets the <c>Content-Security-Policy-Report-Only</c> HTTP header name.</summary>
public static readonly string ContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only";

/// <summary>Gets the <c>Content-Type</c> HTTP header name.</summary>
public static readonly string ContentType = "Content-Type";

/// <summary>Gets the <c>Correlation-Context</c> HTTP header name.</summary>
public static readonly string CorrelationContext = "Correlation-Context";

/// <summary>Gets the <c>Cookie</c> HTTP header name.</summary>
public static readonly string Cookie = "Cookie";

/// <summary>Gets the <c>Date</c> HTTP header name.</summary>
public static readonly string Date = "Date";

/// <summary>Gets the <c>DNT</c> HTTP header name.</summary>
public static readonly string DNT = "DNT";

/// <summary>Gets the <c>ETag</c> HTTP header name.</summary>
public static readonly string ETag = "ETag";

/// <summary>Gets the <c>Expires</c> HTTP header name.</summary>
public static readonly string Expires = "Expires";

/// <summary>Gets the <c>Expect</c> HTTP header name.</summary>
public static readonly string Expect = "Expect";

/// <summary>Gets the <c>From</c> HTTP header name.</summary>
public static readonly string From = "From";

/// <summary>Gets the <c>Grpc-Accept-Encoding</c> HTTP header name.</summary>
public static readonly string GrpcAcceptEncoding = "Grpc-Accept-Encoding";

/// <summary>Gets the <c>Grpc-Encoding</c> HTTP header name.</summary>
public static readonly string GrpcEncoding = "Grpc-Encoding";

/// <summary>Gets the <c>Grpc-Message</c> HTTP header name.</summary>
public static readonly string GrpcMessage = "Grpc-Message";

/// <summary>Gets the <c>Grpc-Status</c> HTTP header name.</summary>
public static readonly string GrpcStatus = "Grpc-Status";

/// <summary>Gets the <c>Grpc-Timeout</c> HTTP header name.</summary>
public static readonly string GrpcTimeout = "Grpc-Timeout";

/// <summary>Gets the <c>Host</c> HTTP header name.</summary>
public static readonly string Host = "Host";

/// <summary>Gets the <c>Keep-Alive</c> HTTP header name.</summary>
public static readonly string KeepAlive = "Keep-Alive";

/// <summary>Gets the <c>If-Match</c> HTTP header name.</summary>
public static readonly string IfMatch = "If-Match";

/// <summary>Gets the <c>If-Modified-Since</c> HTTP header name.</summary>
public static readonly string IfModifiedSince = "If-Modified-Since";

/// <summary>Gets the <c>If-None-Match</c> HTTP header name.</summary>
public static readonly string IfNoneMatch = "If-None-Match";

/// <summary>Gets the <c>If-Range</c> HTTP header name.</summary>
public static readonly string IfRange = "If-Range";

/// <summary>Gets the <c>If-Unmodified-Since</c> HTTP header name.</summary>
public static readonly string IfUnmodifiedSince = "If-Unmodified-Since";

/// <summary>Gets the <c>Last-Modified</c> HTTP header name.</summary>
public static readonly string LastModified = "Last-Modified";

/// <summary>Gets the <c>Link</c> HTTP header name.</summary>
public static readonly string Link = "Link";

/// <summary>Gets the <c>Location</c> HTTP header name.</summary>
public static readonly string Location = "Location";

/// <summary>Gets the <c>Max-Forwards</c> HTTP header name.</summary>
public static readonly string MaxForwards = "Max-Forwards";

/// <summary>Gets the <c>:method</c> HTTP header name.</summary>
public static readonly string Method = ":method";

/// <summary>Gets the <c>Origin</c> HTTP header name.</summary>
public static readonly string Origin = "Origin";

/// <summary>Gets the <c>:path</c> HTTP header name.</summary>
public static readonly string Path = ":path";

/// <summary>Gets the <c>Pragma</c> HTTP header name.</summary>
public static readonly string Pragma = "Pragma";

/// <summary>Gets the <c>Proxy-Authenticate</c> HTTP header name.</summary>
public static readonly string ProxyAuthenticate = "Proxy-Authenticate";

/// <summary>Gets the <c>Proxy-Authorization</c> HTTP header name.</summary>
public static readonly string ProxyAuthorization = "Proxy-Authorization";

/// <summary>Gets the <c>Proxy-Connection</c> HTTP header name.</summary>
public static readonly string ProxyConnection = "Proxy-Connection";

/// <summary>Gets the <c>Range</c> HTTP header name.</summary>
public static readonly string Range = "Range";

/// <summary>Gets the <c>Referer</c> HTTP header name.</summary>
public static readonly string Referer = "Referer";

/// <summary>Gets the <c>Retry-After</c> HTTP header name.</summary>
public static readonly string RetryAfter = "Retry-After";

/// <summary>Gets the <c>Request-Id</c> HTTP header name.</summary>
public static readonly string RequestId = "Request-Id";

/// <summary>Gets the <c>:scheme</c> HTTP header name.</summary>
public static readonly string Scheme = ":scheme";

/// <summary>Gets the <c>Sec-WebSocket-Accept</c> HTTP header name.</summary>
public static readonly string SecWebSocketAccept = "Sec-WebSocket-Accept";

/// <summary>Gets the <c>Sec-WebSocket-Key</c> HTTP header name.</summary>
public static readonly string SecWebSocketKey = "Sec-WebSocket-Key";

/// <summary>Gets the <c>Sec-WebSocket-Protocol</c> HTTP header name.</summary>
public static readonly string SecWebSocketProtocol = "Sec-WebSocket-Protocol";

/// <summary>Gets the <c>Sec-WebSocket-Version</c> HTTP header name.</summary>
public static readonly string SecWebSocketVersion = "Sec-WebSocket-Version";

/// <summary>Gets the <c>Sec-WebSocket-Extensions</c> HTTP header name.</summary>
public static readonly string SecWebSocketExtensions = "Sec-WebSocket-Extensions";

/// <summary>Gets the <c>Server</c> HTTP header name.</summary>
public static readonly string Server = "Server";

/// <summary>Gets the <c>Set-Cookie</c> HTTP header name.</summary>
public static readonly string SetCookie = "Set-Cookie";

/// <summary>Gets the <c>:status</c> HTTP header name.</summary>
public static readonly string Status = ":status";

/// <summary>Gets the <c>Strict-Transport-Security</c> HTTP header name.</summary>
public static readonly string StrictTransportSecurity = "Strict-Transport-Security";

/// <summary>Gets the <c>TE</c> HTTP header name.</summary>
public static readonly string TE = "TE";

/// <summary>Gets the <c>Trailer</c> HTTP header name.</summary>
public static readonly string Trailer = "Trailer";

/// <summary>Gets the <c>Transfer-Encoding</c> HTTP header name.</summary>
public static readonly string TransferEncoding = "Transfer-Encoding";

/// <summary>Gets the <c>Translate</c> HTTP header name.</summary>
public static readonly string Translate = "Translate";

/// <summary>Gets the <c>traceparent</c> HTTP header name.</summary>
public static readonly string TraceParent = "traceparent";

/// <summary>Gets the <c>tracestate</c> HTTP header name.</summary>
public static readonly string TraceState = "tracestate";

/// <summary>Gets the <c>Upgrade</c> HTTP header name.</summary>
public static readonly string Upgrade = "Upgrade";

/// <summary>Gets the <c>Upgrade-Insecure-Requests</c> HTTP header name.</summary>
public static readonly string UpgradeInsecureRequests = "Upgrade-Insecure-Requests";

/// <summary>Gets the <c>User-Agent</c> HTTP header name.</summary>
public static readonly string UserAgent = "User-Agent";

/// <summary>Gets the <c>Vary</c> HTTP header name.</summary>
public static readonly string Vary = "Vary";

/// <summary>Gets the <c>Via</c> HTTP header name.</summary>
public static readonly string Via = "Via";

/// <summary>Gets the <c>Warning</c> HTTP header name.</summary>
public static readonly string Warning = "Warning";

/// <summary>Gets the <c>Sec-WebSocket-Protocol</c> HTTP header name.</summary>
public static readonly string WebSocketSubProtocols = "Sec-WebSocket-Protocol";

/// <summary>Gets the <c>WWW-Authenticate</c> HTTP header name.</summary>
public static readonly string WWWAuthenticate = "WWW-Authenticate";

/// <summary>Gets the <c>X-Content-Type-Options</c> HTTP header name.</summary>
public static readonly string XContentTypeOptions = "X-Content-Type-Options";

/// <summary>Gets the <c>X-Frame-Options</c> HTTP header name.</summary>
public static readonly string XFrameOptions = "X-Frame-Options";

/// <summary>Gets the <c>X-Powered-By</c> HTTP header name.</summary>
public static readonly string XPoweredBy = "X-Powered-By";

/// <summary>Gets the <c>X-Requested-With</c> HTTP header name.</summary>
public static readonly string XRequestedWith = "X-Requested-With";

/// <summary>Gets the <c>X-UA-Compatible</c> HTTP header name.</summary>
public static readonly string XUACompatible = "X-UA-Compatible";

/// <summary>Gets the <c>X-XSS-Protection</c> HTTP header name.</summary>
public static readonly string XXSSProtection = "X-XSS-Protection";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CurlToCSharp.Constants;
namespace Curl.CommandLine.Parser.Constants;

public class HeaderValues
internal class HeaderValues
{
public const string ContentTypeWwwForm = "application/x-www-form-urlencoded";
}
Loading