From 5c3cc791bd2a1f6abe489cc1238de68bc9615c4f Mon Sep 17 00:00:00 2001 From: aktheknight Date: Wed, 4 Apr 2018 14:35:13 +0100 Subject: [PATCH] Add arg for max number of redirect requests --- HttpPing/Program.cs | 49 ++++++++++++++++++++++++++------------------- README.md | 17 ++++++++-------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/HttpPing/Program.cs b/HttpPing/Program.cs index dd5280c..86c6b8d 100644 --- a/HttpPing/Program.cs +++ b/HttpPing/Program.cs @@ -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 = ""; @@ -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": @@ -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(); @@ -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 @@ -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")) @@ -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; @@ -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); + } } } diff --git a/README.md b/README.md index 24e0d89..7143b53 100644 --- a/README.md +++ b/README.md @@ -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