Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using System.IO;
- using System.Threading;
- using System.Net.Sockets;
- namespace Location
- {
- public enum Style_Requests
- {
- whois,
- http0_9,
- http1_0,
- http1_1,
- }
- public class Location
- {
- public static int port = 43;
- public static string hostname = "whois.net.dcs.hull.ac.uk";
- public static Style_Requests Style = Style_Requests.whois;
- static void Main(string[] args)
- {
- try
- {
- List<string> input = new List<string>();
- foreach (string item in args)
- {
- input.Add(item);
- }
- //copy args to list
- input = Detect(input);
- int NoofArgs = input.Count;
- if (NoofArgs == 0)
- {
- throw new Exception("No Arguments Entered");
- }
- TcpClient clientUser = new TcpClient();
- clientUser.Connect(hostname, port);
- //Set send and recieve time outs to 1 second
- clientUser.SendTimeout = 1000;
- clientUser.ReceiveTimeout = 1000;
- StreamWriter sw = new StreamWriter(clientUser.GetStream());
- StreamReader sr = new StreamReader(clientUser.GetStream());
- switch (Style)
- {
- case Style_Requests.whois:
- Handle_W(input, NoofArgs, sw, sr);
- break;
- case Style_Requests.http0_9:
- Handle_9(input, NoofArgs, sw, sr);
- break;
- case Style_Requests.http1_0:
- Handle_10(input, NoofArgs, sw, sr);
- break;
- case Style_Requests.http1_1:
- Handle_11(input, NoofArgs, sw, sr);
- break;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- private static List<string> Detect(List<string> input)
- {
- for (int i = 0; i < input.Count; i++)
- {
- switch (input[i])
- {
- case "/h":
- input.RemoveAt(i);
- hostname = input[i];
- input.RemoveAt(i);
- i--;
- break;
- case "/p":
- input.RemoveAt(i);
- if (!int.TryParse(input[i], out port))
- {
- throw new Exception("Invalid Port Input");
- }
- input.RemoveAt(i);
- i--;
- break;
- case "/h9":
- Style = Style_Requests.http0_9;
- input.RemoveAt(i);
- i--;
- break;
- case "/h0":
- Style = Style_Requests.http1_0;
- input.RemoveAt(i);
- i--;
- break;
- case "/h1":
- Style = Style_Requests.http1_1;
- input.RemoveAt(i);
- i--;
- break;
- default:
- Console.WriteLine("Unrecognised Command");
- break;
- }
- }
- return input;
- }
- #region whois
- private static void Handle_W(List<string> input, int NoofArgs, StreamWriter sw, StreamReader sr)
- {
- switch (NoofArgs)
- {
- case 1:
- Request_W(input[0], sw, sr);
- break;
- case 2:
- Request_W(input[0], input[1], sw, sr);
- break;
- default:
- break;
- }
- }
- private static void Request_W(string name, StreamWriter sw, StreamReader sr)
- {
- sw.Write(name);
- sw.Flush();
- string serverMessage = sr.ReadToEnd().Trim();
- if (serverMessage == "ERROR: no entries found")
- {
- Console.WriteLine("ERROR: no entries found");
- }
- else
- {
- Console.WriteLine(name + " is " + serverMessage);
- }
- }
- private static void Request_W(string name, string location, StreamWriter sw, StreamReader sr)
- {
- sw.Write(name + location);
- sw.Flush();
- string serverMessage = sr.ReadToEnd().Trim();
- if (serverMessage == "OK")
- {
- Console.WriteLine("OK");
- }
- else
- {
- Console.WriteLine(serverMessage);
- }
- }
- #endregion
- #region h0.9
- private static void Handle_9(List<string> input, int NoofArgs, StreamWriter sw, StreamReader sr)
- {
- switch (NoofArgs)
- {
- case 1:
- Request_9(input[0], sw, sr);
- break;
- case 2:
- Request_9(input[0], input[1], sw, sr);
- break;
- default:
- break;
- }
- }
- private static void Request_9(string name, StreamWriter sw, StreamReader sr)
- {
- sw.WriteLine("Get /" + name);
- sw.Flush();
- string serverMessage = sr.ReadToEnd().Trim();
- if (serverMessage == "HTTP/0.9 404 Not Found \nContent-Type: test/plain")
- {
- Console.WriteLine(serverMessage);
- }
- else
- {
- Console.WriteLine("HTTP/0.9 200 OK" + Environment.NewLine +
- "Conent-Type: text/plain" + Environment.NewLine + Environment.NewLine + serverMessage);
- }
- }
- private static void Request_9(string name, string location, StreamWriter sw, StreamReader sr)
- {
- sw.WriteLine("Put /" + name + Environment.NewLine + Environment.NewLine + location);
- sw.Flush();
- string serverMessage = sr.ReadToEnd().Trim();
- if (serverMessage == "HTTP/0.9 200 OK")
- {
- Console.WriteLine("HTTP/0.9 200 OK" + Environment.NewLine +
- "Conent-Type: text/plain");
- }
- }
- #endregion
- #region h1.0
- private static void Handle_10(List<string> input, int NoofArgs, StreamWriter sw, StreamReader sr)
- {
- switch (NoofArgs)
- {
- case 1:
- Request_10(input[0], sw, sr);
- break;
- case 2:
- Request_10(input[0], input[1], sw, sr);
- break;
- default:
- break;
- }
- }
- private static void Request_10(string name, StreamWriter sw, StreamReader sr)
- {
- sw.WriteLine("GET /?" + name + " HTTP/1.0");
- sw.Flush();
- string serverMessage = sr.ReadToEnd().Trim();
- if (serverMessage == "HTTP/1.0 404 Not Found" + Environment.NewLine + "Content-Type: text/plain")
- {
- Console.WriteLine(serverMessage);
- }
- else
- {
- Console.Write(serverMessage);
- }
- }
- private static void Request_10(string name, string location, StreamWriter sw, StreamReader sr)
- {
- int locationlength = location.Length;
- sw.Write("Post / " + name + " HTTP/1.0" + Environment.NewLine + "Content-Length: " + locationlength
- + Environment.NewLine + location);
- sw.Flush();
- string serverMessage = sr.ReadToEnd().Trim();
- if (serverMessage == "HTTP/1.0 200 OK" + Environment.NewLine + "Content-Type: text/plain")
- {
- Console.Write(serverMessage);
- }
- }
- #endregion
- #region h1.1
- private static void Handle_11(List<string> input, int NoofArgs, StreamWriter sw, StreamReader sr)
- {
- switch (NoofArgs)
- {
- case 1:
- Request_11(input[0], sw, sr);
- break;
- case 2:
- Request_11(input[0], input[1], sw, sr);
- break;
- default:
- break;
- }
- }
- private static void Request_11(string name, StreamWriter sw, StreamReader sr)
- {
- sw.Write("GET /?name=" + name + " HTTP/1.1" + Environment.NewLine
- + "Host: " + hostname);
- sw.Flush();
- string serverMessage = sr.ReadToEnd().Trim();
- if (serverMessage == "HTTP/1.1 404 Not Found" + Environment.NewLine +
- "Conten-Type: text/plain")
- {
- Console.WriteLine(serverMessage);
- }
- else
- {
- Console.WriteLine(serverMessage);
- }
- }
- private static void Request_11(string name, string location, StreamWriter sw, StreamReader sr)
- {
- int TotalLength = location.Length + name.Length;
- sw.Write("GET / HTTP/1.1" + Environment.NewLine + "Host: " + hostname +
- Environment.NewLine + "Content-Length: " + TotalLength + Environment.NewLine +
- "name=" + name + "&location=" + location);
- string serverMessage = sr.ReadToEnd();
- if (serverMessage.Contains("OK"))
- {
- Console.Write(serverMessage);
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement