2019년 2월 28일 목요일

C# - SimpleHttpServer



C# SimpleHttpServer


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SimpleHttpServer;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace TestHttpServer2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Shown(object sender, EventArgs e)
{
textBox1.Text = GetLocalIP();
}

private void button1_Click(object sender, EventArgs e)
{
string port = textBox2.Text;
if (!string.IsNullOrWhiteSpace(port))
{
int _port = 8282;

try
{
_port = Convert.ToInt16(port);
}
catch
{
_port = 8282;
}

HttpServer httpServer = new HttpServer(_port, Routes.GET);

Thread thread = new Thread(new ThreadStart(httpServer.Listen));
thread.Start();

MessageBox.Show("Server start !");
}
}

private string GetLocalIP()
{
string localIP = string.Empty;
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}

return localIP;
}

}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleHttpServer.Models;

namespace TestHttpServer2
{
class Routes
{
public static List<Route> GET
{
get
{
return new List<Route>()
{
new Route()
{
Callable = HomeIndex,
UrlRegex = "^\\/$",
Method = "GET"
}
, new Route()
{
Callable = testIndex,
UrlRegex = "^\\/get_data",
Method = "GET"
}
, new Route()
{
Callable = test2Index,
UrlRegex = "^\\/test2",
Method = "GET"
}
};
}
}

private static HttpResponse test2Index(HttpRequest request)
{
//HttpBuilder

return new HttpResponse()
{
ContentAsUTF8 = "TEST2",
ReasonPhrase = "OK",
StatusCode = "200"
};
}

private static HttpResponse testIndex(HttpRequest request)
{
return new HttpResponse()
{
ContentAsUTF8 = "TEST",
ReasonPhrase = "OK",
StatusCode = "200"
};
}

private static HttpResponse HomeIndex(HttpRequest request)
{
return new HttpResponse()
{
ContentAsUTF8 = "Hello",
ReasonPhrase = "OK",
StatusCode = "200"
};
}
}
}

using SimpleHttpServer.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleHttpServer
{
class HttpBuilder
{
public const string ResErrorsPath = "/Resources/Errors/";
public const string ResPagesPath = "/Resources/Pages/";

public static HttpResponse InternalServerError()
{
//string currentDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string content = File.ReadAllText(@"../.." + ResErrorsPath + "500.html");

return new HttpResponse()
{
ReasonPhrase = "InternalServerError",
StatusCode = "500",
ContentAsUTF8 = content
};
}

public static HttpResponse NotFound()
{
//string currentDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string content = File.ReadAllText(@"../.." + ResErrorsPath + "404.html");

return new HttpResponse()
{
ReasonPhrase = "NotFound",
StatusCode = "404",
ContentAsUTF8 = content
};
}

public static HttpResponse GetHttpDocument(string fileName)
{
string content = File.ReadAllText(@"../.." + ResPagesPath + fileName);

return new HttpResponse()
{
ReasonPhrase = "OK",
StatusCode = "200",
ContentAsUTF8 = content
};
}
}
}






javascript - SQL 예약어 제거

  <script language="javascript"> //특수문자, 특정문자열(sql예약어) 제거 function checkSearchedWord(obj){ obj.value = obj.value+&quo...