Skip to content

Commit

Permalink
Merge pull request #3 from AKTheKnight/master
Browse files Browse the repository at this point in the history
Add arg for max number of redirect requests
  • Loading branch information
Killeroo authored Apr 5, 2018
2 parents d1565b0 + 5c3cc79 commit 852d23e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
49 changes: 28 additions & 21 deletions HttpPing/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ class Program
private static bool UseCommonLogFormat { get; set; } = false;
private static int Interval { get; set; } = 30000;
private static int Requests { get; set; } = 5;
private static int Redirects { get; set; } = 4;

// Console Properties
private static ConsoleColor DefaultBackgroundColor { get; set; }
private static ConsoleColor DefaultForegroundColor { get; set; }

// Constants
private const string Usage = "USAGE: Requester web_address [-d] [-t] [-ts] [-n count] [-i interval] [-l] [-nc]";
private const string Usage = "USAGE: Requester web_address [-d] [-t] [-ts] [-n count] [-i interval] [-l] [-nc] [-r redirectCount]";

private static string resolvedAddress = "";

Expand Down Expand Up @@ -50,8 +51,7 @@ private static void Main(string[] args)
case "-?":
case "--?":
case "/?":
Console.WriteLine(Usage);
Environment.Exit(0);
Exit();
break;
case "-i":
case "--i":
Expand Down Expand Up @@ -88,6 +88,15 @@ private static void Main(string[] args)
case "/ts":
Timestamp = true;
break;
case "-r":
case "--r":
case "/r":
Redirects = Convert.ToInt32(args[count + 1]);
if (Redirects <= 0)
{
Exit("Number of redirects must be higher than 0");
}
break;
default:
if (arg.Contains("-"))
throw new ArgumentException();
Expand All @@ -97,33 +106,23 @@ private static void Main(string[] args)
}
catch (IndexOutOfRangeException)
{
Error("Missing argument parameter");
Console.WriteLine(Usage);
Environment.Exit(1);
Exit("Missing argument parameter");
}
catch (ArgumentException)
{
Error("Incorrect argument found");
Console.WriteLine(Usage);
Environment.Exit(1);
Exit("Incorrect argument found");
}
catch (FormatException)
{
Error("Could not convert argument");
Console.WriteLine(Usage);
Environment.Exit(1);
Exit("Could not convert argument");
}
catch (OverflowException)
{
Error("Could not convert argument");
Console.WriteLine(Usage);
Environment.Exit(1);
Exit("Could not convert argument");
}
catch (Exception e)
{
Error(e.GetType().ToString());
Console.WriteLine(Usage);
Environment.Exit(1);
Exit(e.GetType().ToString());
}

// Find address
Expand All @@ -134,8 +133,7 @@ private static void Main(string[] args)
query = args.Last();
else
{
Error("Could not find URL/Web address");
Environment.Exit(1);
Exit("Could not find URL/Web address");
}
// Add http part if not already there
if (!query.Contains("http"))
Expand All @@ -156,7 +154,7 @@ private static void HttpRequestLoop(string query)
{
// Construct request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(query);
request.MaximumAutomaticRedirections = 4;
request.MaximumAutomaticRedirections = Redirects;
request.AutomaticDecompression = DecompressionMethods.GZip;
request.Credentials = CredentialCache.DefaultCredentials;

Expand Down Expand Up @@ -304,5 +302,14 @@ private static void ResetConsoleColors()
Console.BackgroundColor = DefaultBackgroundColor;
Console.ForegroundColor = DefaultForegroundColor;
}

private static void Exit(string message = null)
{
if (!string.IsNullOrEmpty(message))
Error(message);

Console.WriteLine(Usage);
Environment.Exit(1);
}
}
}
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ Download it here: [[Stable Releases]](https://github.com/Killeroo/HttpPing/relea
- Common Log Format (NCSA log format)

## Usage:
HttpPing.exe address [-d] [-t] [-ts] [-n count] [-i interval]
HttpPing.exe address [-d] [-t] [-ts] [-n count] [-i interval] [-r redirectCount]
## Arguments:
[-d] Detailed mode: shows server and cache info
[-t] Infinite mode: Keep sending requests until stopped (Ctrl-C)
[-n count] Send a specific number of requests
[-ts] Include timestamp of when each request was sent
[-i interval] Interval between each request in milliseconds (default 30000)
[-l] Use Common Log Format (https://en.wikipedia.org/wiki/Common_Log_Format)
[-nc] No color
[-d] Detailed mode: shows server and cache info
[-t] Infinite mode: Keep sending requests until stopped (Ctrl-C)
[-n count] Send a specific number of requests
[-ts] Include timestamp of when each request was sent
[-i interval] Interval between each request in milliseconds (default 30000)
[-l] Use Common Log Format (https://en.wikipedia.org/wiki/Common_Log_Format)
[-nc] No color
[-r redirectCount] Follow redirect requests a maximum number of times (default 4)
## Example
HttpPing.exe google.com -t -l
Expand Down

0 comments on commit 852d23e

Please # to comment.