Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace EasyDatabankServer
- {
- class Program
- {
- Int32 Port = 8080;
- string path = "/usr/local/mysql/data/sys/sys_config.ibd";
- private SqlDataReader rdr;
- private SqlConnection sqlConn;
- private TcpListener listener;
- static void Main(string[] args)
- {
- Int32 Port = 8080;
- string path = "/usr/local/mysql/data/sys/sys_config.ibd";
- }
- public void Start()
- {
- listener = new TcpListener(IPAddress.Loopback, Port);
- listener.Start();
- var requests = new Requests();
- while (true)
- {
- var client = listener.AcceptTcpClient();
- requests.Add(client);
- if (!listener.Pending())
- {
- Uri uriAddress = new Uri("http://localhost:8080");
- requests.Process(uriAddress.LocalPath);
- }
- }
- }
- private class Requests
- {
- Queue<TcpClient> q = new Queue<TcpClient>();
- public void Add(TcpClient client)
- {
- q.Enqueue(client);
- }
- public void Process(string path)
- {
- Console.WriteLine("Queue: " + q.Count);
- while (q.Count > 0)
- {
- try
- {
- var req = new Request(q.Dequeue(), path);
- Console.WriteLine(" " + req.LocalUrl);
- }
- catch (Exception e)
- {
- Console.WriteLine("Error: " + e.Message);
- }
- }
- }
- }
- public class Request
- {
- private TcpClient c;
- private string localPath;
- private SqlDataReader rdr;
- private SqlConnection sqlConn;
- private TcpListener listener;
- public string LocalUrl { get; private set; }
- public string Verb { get; private set; }
- public string HttpVersion { get; private set; }
- public List<string> Header { get; private set; }
- public Request(TcpClient c, string localPath)
- {
- this.c = c;
- this.localPath = localPath;
- Header = new List<string>();
- Process();
- }
- private void Process()
- {
- var r = new StreamReader(c.GetStream());
- var w = new StreamWriter(c.GetStream());
- try
- {
- HandleRequest(r, w);
- }
- catch (Exception e)
- {
- Failed(w);
- }
- finally
- {
- w.Close();
- r.Close();
- c.Close();
- }
- }
- private void HandleRequest(StreamReader r, StreamWriter w)
- {
- string request = r.ReadLine();
- string connectionString =
- "Data Source=sys;Initial Catalog=sys_config.ibd;Persist Security Info=True;User ID=root;Password=mypassword";
- using (SqlConnection conn = new SqlConnection(connectionString))
- {
- SqlCommand cmd = new SqlCommand("SELECT * FROM sys.sys_config");
- cmd.CommandType = CommandType.Text;
- cmd.Connection = conn;
- try
- {
- if (request != null)
- {
- string[] tokens = request.Split(' ');
- Verb = tokens[0].ToUpper();
- LocalUrl = tokens[1];
- HttpVersion = tokens[2];
- ReadHeader(r);
- string file = Path.Combine(
- Path.GetFullPath(localPath),
- LocalUrl.TrimStart('/'));
- string text = File.ReadAllText(file);
- Succeeded(w, Path.GetExtension(LocalUrl).TrimStart('.'));
- w.Write(text);
- conn.Open();
- }
- }
- catch (IndexOutOfRangeException e)
- {
- Console.WriteLine($"Error Here! {e}");
- }
- }
- }
- private void Succeeded(StreamWriter stream, string extention)
- {
- stream.WriteLine("HTTP/1.0 200 OK");
- stream.WriteLine("Content-Type: text/" + extention);
- stream.WriteLine("Connection: close");
- stream.WriteLine("");
- }
- private void Failed(StreamWriter stream)
- {
- stream.WriteLine("HTTP/1.0 404 not found");
- stream.WriteLine("Connection: close");
- stream.WriteLine("");
- }
- private void ReadHeader(StreamReader r)
- {
- //DO SOMETHING
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment