Created
April 6, 2009 08:07
-
-
Save jamierumbelow/90669 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program.cs | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using HTTPXd; | |
namespace HTTPXd | |
{ | |
class Program | |
{ | |
public Server server; | |
static void Main(string[] args) | |
{ | |
Program prog = new Program(); | |
prog.server = new Server(); | |
if (args.Length == 0) | |
{ | |
prog.displayHelp(); | |
} | |
else | |
{ | |
switch (args[0]) | |
{ | |
case "start": prog.doStart(args); break; | |
case "stop": prog.doStop(args); break; | |
case "restart": prog.doStop(args); prog.doStart(args); break; | |
default: prog.displayHelp(); break; | |
} | |
} | |
} | |
public void doStart(string[] args) | |
{ | |
int port = 3003; | |
bool verbose = false; | |
bool quiet = true; | |
string config = ""; | |
string cArgs = ""; | |
cArgs = args.ToString(); | |
if (!(cArgs.IndexOf("-p") == -1)) { port = Convert.ToInt32(args[cArgs.IndexOf("-p") + 2]); } | |
server.start("127.0.0.1", port); | |
} | |
public void doStop(string[] args) | |
{ | |
} | |
public void displayHelp() | |
{ | |
Console.WriteLine("--- HTTPXd Web Server ---"); | |
Console.WriteLine(""); | |
Console.WriteLine("Usage:"); | |
Console.WriteLine("\t httpxd [command] [options]"); | |
Console.WriteLine(""); | |
Console.WriteLine("Commands:"); | |
Console.WriteLine("\t start - Starts the server"); | |
Console.WriteLine("\t stop - Stops the server"); | |
Console.WriteLine("\t restart - Restarts the server"); | |
Console.WriteLine(""); | |
Console.WriteLine("Options:"); | |
Console.WriteLine("\t -p [int] - specifies port number to use (default 3003)"); | |
Console.WriteLine("\t -v - switches on verbose mode"); | |
Console.WriteLine("\t -q - switches on quiet mode"); | |
Console.WriteLine("\t -c [path] - specifies config file"); | |
Console.WriteLine(""); | |
Console.WriteLine(""); | |
Console.WriteLine("Copyright (c) 2009 Jamie Rumbelow"); | |
} | |
} | |
} | |
server.cs | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Threading; | |
namespace HTTPXd | |
{ | |
class Server | |
{ | |
public TcpListener socket; | |
public IPAddress ip; | |
public void start(string ipPass, int port) | |
{ | |
Byte[] ipTo = Convert.ChangeType(ipPass,TypeCode.Byte); | |
this.ip = new IPAddress(ip); | |
this.socket = new TcpListener(this.ip, port); | |
this.socket.Start(); | |
Thread th = new Thread(new ThreadStart(listenForConnections)); | |
th.Start(); | |
} | |
public void listenForConnections() | |
{ | |
int StartPos = 0; | |
String Request; | |
String DirName; | |
String RequestedFile; | |
String ErrorMessage; | |
String LocalDir; | |
String MyWebServerRoot = "C:\\MyWebServerRoot\\"; | |
String PhysicalFilePath = ""; | |
String FormattedMessage = ""; | |
String Response = ""; | |
while(true) | |
{ | |
//Accept a new connection | |
Socket mySocket = this.socket.AcceptSocket(); | |
Console.WriteLine ("Socket Type " + mySocket.SocketType ); | |
if(mySocket.Connected) | |
{ | |
//make a byte array and receive data from the client | |
Byte[] Receive = new Byte[1024] ; | |
int i = mySocket.Receive(Receive,Receive.Length,0) ; | |
//Convert Byte to String | |
string Buffer = Encoding.ASCII.GetString(Receive); | |
//At present we will only deal with GET type | |
if (Buffer.Substring(0,3) != "GET" ) | |
{ | |
Console.WriteLine("Only Get Method is supported.."); | |
mySocket.Close(); | |
return; | |
} | |
// Look for HTTP request | |
StartPos = Buffer.IndexOf("HTTP",1); | |
// Get the HTTP text and version e.g. it will return "HTTP/1.1" | |
string HttpVersion = Buffer.Substring(StartPos,8); | |
// Extract the Requested Type and Requested file/directory | |
Request = Buffer.Substring(0,StartPos - 1); | |
//Replace backslash with Forward Slash, if Any | |
Request.Replace("\\","/"); | |
//If file name is not supplied add forward slash to indicate | |
//that it is a directory and then we will look for the | |
//default file name.. | |
if ((Request.IndexOf(".") <1) && (!Request.EndsWith("/"))) | |
{ | |
Request = Request + "/"; | |
} | |
//Extract the requested file name | |
StartPos = Request.LastIndexOf("/") + 1; | |
RequestedFile = Request.Substring(StartPos); | |
//Extract The directory Name | |
DirName = Request.Substring(Request.IndexOf("/"), | |
Request.LastIndexOf("/")-3); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment