 Third Cicle [5]
![Third Cicle [5] Third Cicle [5]](images/magic/35.gif)
°¡ÀÔÀÏ: 2004³â 01¿ù 15ÀÏ °Ô½Ã¹°: 47
|
´ÙÀ½ Äڵ带 WebServer.cs·Î Scripts Æú´õ¿¡ ÀúÀåÇϽŠÈÄ
À¥»ó¿¡¼ http://localhost:8080/status.html ¶ó°í ÁÖ¼Ò¸¦ ÀÔ·ÂÇϽøé À¥À¸·Î ¼¹ö »óŸ¦ È®ÀÎ ÇÏ½Ç ¼ö ÀÖ½À´Ï´Ù.
µÚ¿¡ Æ÷Æ®(À§¿¡¼ 8080)ÀÌ ¸¾¿¡ µéÁö ¾ÊÀ¸½Ã¸é
´ÙÀ½ ºÎºÐÀ» ã¾Æ ¿øÇÏ´Â Æ÷Æ®·Î º¯°æÇØÁÖ¼¼¿ä.
ÄÚµå : | private const int port = 8080; |
Âü°í·Î Html ű׳ª ¼Ò½º¸¦ Á¶±Ý¸¸ º¸½Ç ¼ö ÀÖ´Ù¸é
AdminÀ̳ª GMÀ» À¥»óÀ¸·Î ¾Èº¸ÀÌ°Ô ÇÒ ¼öµµ ÀÖ°í
°¢Á¾ ÆÄ¶ó¹ÌÅ͸¦ º¸ÀÌ°Ô ¶Ç´Â ¾È º¸ÀÌ°Ô ÇÏ½Ç ¼ö ÀÖÀ¸´Ï Âü°í ÇϽñ⠹ٶø´Ï´Ù.
ÄÚµå : | /*
* WebServer.cs - Version 1.0
* Last Modified On 20.10.2003 at 17:18:42
* This script is a simple web server for RunUO Project.
* (c) Angels 2003
*/
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading ;
using System.Collections;
using System.Xml;
using Server;
using Server.Network;
using Server.Misc;
namespace WebServer
{
class WebServer
{
private TcpListener m_Listener;
private const int port = 8080;
private Thread m_Thread;
private Socket m_Socket;
private NetworkStream m_Stream;
public static WebServer m_Server;
public string webaction;
public string webdata;
public byte[] bytes;
public static bool DirectoryListingEnabled = true;
private static Hashtable MIMELookupTable = new Hashtable();
public WebServer()
{
m_Thread = new Thread(new ThreadStart(MainLoop));
}
public static void Initialize()
{
m_Server = new WebServer();
m_Server.Start();
}
public void Start()
{
m_Thread.Start();
Console.WriteLine ( "WebServer: WebServer is listening on port {0}", port );
}
private static void InitMimeTable()
{
MIMELookupTable.Add("htm","text/html");
MIMELookupTable.Add(".html","text/html");
MIMELookupTable.Add(".mdl","text/html");
MIMELookupTable.Add(".txt","text/plain");
MIMELookupTable.Add(".gif","image/gif");
MIMELookupTable.Add(".gz","application/x-gzip");
MIMELookupTable.Add(".z","application/x-compress");
MIMELookupTable.Add(".pac","application/x-ns-proxy-autoconfig");
MIMELookupTable.Add(".jf","application/x-javascript");
MIMELookupTable.Add(".ls","application/x-javascript");
MIMELookupTable.Add(".mocha","application/x-javascript");
MIMELookupTable.Add(".tcl","application/x-tcl");
MIMELookupTable.Add(".csh","application/x-csh");
MIMELookupTable.Add(".ai","application/postscript");
MIMELookupTable.Add(".eps","application/postscript");
MIMELookupTable.Add(".PS","application/postscript");
MIMELookupTable.Add(".exe","application/octet-stream");
MIMELookupTable.Add(".bin","application/octet-stream");
MIMELookupTable.Add(".cpio","application/x-cpio");
MIMELookupTable.Add(".gtar","application/x-gtar");
MIMELookupTable.Add(".tar","application/x-tar");
MIMELookupTable.Add(".shar","appliation/x-shar");
MIMELookupTable.Add(".zip","application/x-zip-compressed");
MIMELookupTable.Add(".sit","application/x-stuffit");
MIMELookupTable.Add(".hqx","application/mac-binhex40");
MIMELookupTable.Add(".avi","video/x-msvideo");
MIMELookupTable.Add(".qt","video/quicktime");
MIMELookupTable.Add(".mov","video/quicktime");
MIMELookupTable.Add(".mpeg","video/mpeg");
MIMELookupTable.Add(".mpg","video/mpeg");
MIMELookupTable.Add(".mpe","video/mpeg");
MIMELookupTable.Add(".wav","audio/x-wav");
MIMELookupTable.Add(".aif","audio/x-aiff");
MIMELookupTable.Add(".aiff","audio/x-aiff");
MIMELookupTable.Add(".aifc","audio/x-aiff");
MIMELookupTable.Add(".au","audio/basic");
MIMELookupTable.Add(".snd","audio/basic");
MIMELookupTable.Add(".fif","application/fractals");
MIMELookupTable.Add(".ief","image/ief");
MIMELookupTable.Add(".bmp","image/x-MS-bmp");
MIMELookupTable.Add(".rgb","image/x-rgb");
MIMELookupTable.Add(".ppm","image/x-portable-pixmap");
MIMELookupTable.Add(".pbm","image/x-portable-bitmap");
MIMELookupTable.Add(".pnm","image/x-portable-anymap");
MIMELookupTable.Add(".xwd","image/xwindowdump");
MIMELookupTable.Add(".xpm","image/x-pixmap");
MIMELookupTable.Add(".xbm","image/x-bitmap");
MIMELookupTable.Add(".ras","image/x-cmu-raster");
MIMELookupTable.Add(".tiff","image/tiff");
MIMELookupTable.Add(".tif","image/tiff");
MIMELookupTable.Add(".texi","application/x-texinfo");
MIMELookupTable.Add(".texinfo","application/x-texinfo");
MIMELookupTable.Add(".dvi","application/x-dvi");
MIMELookupTable.Add(".latex","application/x-latex");
MIMELookupTable.Add(".tex","application/x-tex");
MIMELookupTable.Add(".rtf","application/rtf");
}
private static string GetFileExtension(string path)
{
return path.Substring(path.IndexOf("."));
}
private static string LookUpMIME(string extension)
{
try
{
if(MIMELookupTable.ContainsKey((object)extension))
return MIMELookupTable[extension] as string;
else
return "text/html";
}
catch(Exception e)
{
Console.WriteLine("In LookUpMIME while processing "+extension+":"+e.ToString());
return "text/html";
}
}
public static byte[] WebHeader(byte[] content, string StatusCode, int iTotBytes, string MIMEtype)
{
string sBuffer = "HTTP/1.1 " + StatusCode + "\r\n" +
"Server: RunUO.WebServer\r\n" +
"Content-Type: "+MIMEtype+"\r\n" +
"Accept-Ranges: bytes\r\n" +
"Content-Length: " + iTotBytes + "\r\n\r\n";
sBuffer = sBuffer + "<HTML>\r\n<BODY>"+"\r\n<H2>"+Encoding.ASCII.GetString(content)+"\r\n</H2>\r\n</BODY>\r\n</HTML>";
byte[] bSendData = Encoding.ASCII.GetBytes(sBuffer);
return bSendData;
}
public void MainLoop()
{
char delimiter;
m_Listener = new TcpListener(port) ;
m_Listener.Start();
while ( true )
{
m_Socket = m_Listener.AcceptSocket();
if ( m_Socket.Connected )
{
byte[] data = new Byte[1024];
try
{
m_Socket.Receive ( data,data.Length,0 );
}
catch(Exception e)
{
Console.WriteLine("WebServer: Warning in WebServer.MainLoop while receiving from socket");
Console.WriteLine("WebServer: Warning description: "+e.ToString());
}
string cmd = Encoding.ASCII.GetString(data);
delimiter = '\n';
string[] responce = cmd.Split(delimiter);
delimiter = ' ';
string[] action = responce[0].Split(delimiter);
if ( action[0].ToUpper() == "GET" )
{
webaction = action[1];
bytes = ProcessRequest(webaction);
SendData(ref m_Socket, bytes);
}
else
{
bytes = InvalidMethod();
SendData(ref m_Socket,bytes);
}
}
m_Socket.Close();
}
m_Listener.Stop();
}
public static void SendData(ref Socket socket, byte[] data)
{
try
{
socket.Send(data, data.Length, 0);
}
catch(Exception e)
{
Console.WriteLine("WebServer: Warning in WebServer.SendData while sending {0}",data);
Console.WriteLine("WebServer: Warning description: "+e.ToString());
}
}
public static byte[] InvalidMethod()
{
byte[] bytes;
string sInvalidMethod = "405 Method Not Allowed. Only GET method supported!";
bytes = WebHeader(Encoding.ASCII.GetBytes(sInvalidMethod),"405 Method Not Allowed",sInvalidMethod.Length,"text/html");
return bytes;
}
public static byte[] ReadRequestedFile(string RelativePath)
{
byte[] bytes;
if (RelativePath.IndexOf("../") > 0 )
{
string Forbidden = "403 Forbidden. You have no access to "+RelativePath;
bytes = WebHeader(Encoding.ASCII.GetBytes(Forbidden), "403 Forbidden", Forbidden.Length,"text/html");
}
else
{
string FileName = Directory.GetCurrentDirectory() + "\\web\\" + RelativePath;
try
{
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader reader = new BinaryReader(fs);
bytes = new byte[fs.Length];
reader.Read(bytes, 0, bytes.Length);
reader.Close();
fs.Close();
WebHeader (bytes, "200 OK", bytes.Length,LookUpMIME(GetFileExtension(FileName)));
}
catch(Exception e)
{
string sErrorMessage = "404 Not Found. Requested file not found";
bytes = WebHeader (Encoding.ASCII.GetBytes(sErrorMessage), "404 Not Found", sErrorMessage.Length,"text/html");
}
}
return bytes;
}
public static byte[] ProcessRequest(string RelativePath)
{
byte[] bytes;
string FileName = Directory.GetCurrentDirectory() + "\\web\\" + RelativePath;
if(File.Exists(FileName))
{
bytes = ReadRequestedFile(RelativePath);
return bytes;
}
if(Directory.Exists(FileName) && DirectoryListingEnabled)
{
bytes = ReadDirectoryListing(RelativePath);
return bytes;
}
string sErrorMessage = "404 Not Found. Requested file not found";
return WebHeader (Encoding.ASCII.GetBytes(sErrorMessage), "404 Not Found", sErrorMessage.Length,"text/html");
}
public static byte[] ReadDirectoryListing(string RelativePath)
{
if (RelativePath.IndexOf("..") > 0 )
{
string Forbidden = "403 Forbidden. You have no access to "+RelativePath;
return WebHeader(Encoding.ASCII.GetBytes(Forbidden), "403 Forbidden", Forbidden.Length,"text/html");
}
else
{
string wwwroot = Directory.GetCurrentDirectory() + "\\web\\";
byte[] bytes;
string output = "";
try
{
// Obtain the file system entries in the directory path.
string[] directoryEntries =
System.IO.Directory.GetFileSystemEntries(wwwroot+RelativePath);
output = "\r\n<HTML>\r\n<HEAD>\r\n<TITLE>Index of "+RelativePath+"\r\n</TITLE>\r\n"+
"</HEAD>\r\n<BODY>\r\n<H1>Index of "+RelativePath+"\r\n</H1>\r\n";
output=output+"<PRE>Name\r\n<HR>\r\n";
if (RelativePath != "/")
output=output+"<IMG alt=[DIR] src=\"images/folder.gif\"> <A HREF=\""+"/"+"\"><font color=9054>Server Root</font></A>\r\n";
foreach (string str in directoryEntries)
{
if (RelativePath == "/")
if(Directory.Exists(str))
output=output+"<IMG alt=[DIR] src=\"images/folder.gif\"> <A HREF=\""+RelativePath+Path.GetFileName(str)+"\">"+Path.GetFileName(str)+"</A>\r\n";
else
output=output+"<IMG alt=[DIR] src=\"images/file.gif\"> <A HREF=\""+RelativePath+Path.GetFileName(str)+"\">"+Path.GetFileName(str)+"</A>\r\n";
else
if(Directory.Exists(str))
output=output+"<IMG alt=[DIR] src=\"images/folder.gif\"> <A HREF=\""+RelativePath+"/"+Path.GetFileName(str)+"\">"+Path.GetFileName(str)+"</A>\r\n";
else
output=output+"<IMG alt=[DIR] src=\"images/file.gif\"> <A HREF=\""+RelativePath+"/"+Path.GetFileName(str)+"\">"+Path.GetFileName(str)+"</A>\r\n";
}
}
catch (ArgumentNullException)
{
string sErrorMessage = "404 Not Found. Requested file not found";
return WebHeader (Encoding.ASCII.GetBytes(sErrorMessage), "404 Not Found", sErrorMessage.Length,"text/html");
}
catch (System.Security.SecurityException)
{
string Forbidden = "403 Forbidden. You have no access to "+RelativePath;
return WebHeader(Encoding.ASCII.GetBytes(Forbidden), "403 Forbidden", Forbidden.Length,"text/html");
}
catch (ArgumentException)
{
string sErrorMessage = "404 Not Found. Requested file not found";
return WebHeader (Encoding.ASCII.GetBytes(sErrorMessage), "404 Not Found", sErrorMessage.Length,"text/html");
}
catch (System.IO.DirectoryNotFoundException)
{
string sErrorMessage = "404 Not Found. Requested file not found";
return WebHeader (Encoding.ASCII.GetBytes(sErrorMessage), "404 Not Found", sErrorMessage.Length,"text/html");
}
output=output+"</PRE><HR><ADDRESS>RunUO Web Server / "+ServerList.ServerName+" / Port "+
port.ToString()+"</ADDRESS>"+"</BODY></HTML>";
bytes = WebHeader (Encoding.ASCII.GetBytes(output), "200 OK", output.Length,"text/html");
return bytes;
}
}
}
} |
|
|