morry2341

Big Databank Server

Nov 30th, 2022
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.IO;
  6. using System.Net.Sockets;
  7.  
  8.  
  9. namespace EinfacherWebserver
  10. {
  11. class Program
  12. {
  13. private Int32 Port = 3306;
  14. string path = "/usr/local/mysql/data/sys/sys_config.ibd";
  15. private SqlDataReader rdr;
  16. private SqlConnection sqlConn;
  17. private static TcpListener listener;
  18.  
  19.  
  20.  
  21.  
  22. private class Requests
  23. {
  24. Queue<TcpClient> q = new Queue<TcpClient>();
  25. public void Add(TcpClient client)
  26. {
  27. q.Enqueue(client);
  28. }
  29.  
  30. public void Process(string path)
  31. {
  32. Console.WriteLine("Queue: " + q.Count);
  33.  
  34. while (q.Count > 0)
  35. {
  36. try
  37. {
  38. var req = new Request(q.Dequeue(), path);
  39.  
  40. Console.WriteLine(" " + req.LocalUrl);
  41. }
  42. catch (Exception e)
  43. {
  44. Console.WriteLine("Error: " + e.Message);
  45.  
  46. }
  47. }
  48. }
  49.  
  50. public static void Main(string[] args)
  51. {
  52. Int32 Port = 3306;
  53. listener = new TcpListener(IPAddress.Loopback, Port);
  54. listener.Start();
  55.  
  56. Console.WriteLine("Server started ");
  57.  
  58.  
  59. var requests = new Requests();
  60. //Process process = new Process();
  61.  
  62. while (true)
  63. {
  64. var client = listener.AcceptTcpClient();
  65. requests.Add(client);
  66.  
  67. if (!listener.Pending())
  68. {
  69. Uri uriAddress = new Uri("http://127.0.0.1:3306");
  70. //string LocalPath ="http:///localhost:8080";
  71. requests.Process(uriAddress.LocalPath);
  72. }
  73.  
  74.  
  75. }
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. }
  84.  
  85.  
  86. }
  87. public class Request
  88. {
  89. private TcpClient c;
  90. private string localPath;
  91. private SqlDataReader rdr;
  92. private SqlConnection sqlConn;
  93. private TcpListener listener;
  94.  
  95. public string LocalUrl { get; private set; }
  96. public string Verb { get; private set; }
  97. public string HttpVersion { get; private set; }
  98. public List<string> Header { get; private set; }
  99.  
  100. public Request(TcpClient c, string localPath)
  101. {
  102. this.c = c;
  103. this.localPath = localPath;
  104.  
  105. Header = new List<string>();
  106.  
  107. Process();
  108. }
  109.  
  110. private void Process()
  111. {
  112. var r = new StreamReader(c.GetStream());
  113. var w = new StreamWriter(c.GetStream());
  114.  
  115. try
  116. {
  117. HandleRequest(r, w);
  118. }
  119. catch (Exception e)
  120. {
  121. Failed(w);
  122.  
  123. }
  124. finally
  125. {
  126. w.Close();
  127. r.Close();
  128. c.Close();
  129. }
  130. }
  131.  
  132. private void HandleRequest(StreamReader r, StreamWriter w)
  133. {
  134. string request = r.ReadLine();
  135.  
  136. string connectionString =
  137. @"server=127.0.0.1;port=3306;password=mypass;userid=root;database=mydatabase;";
  138.  
  139. using (SqlConnection conn = new SqlConnection(connectionString))
  140. {
  141. SqlCommand cmd = new SqlCommand("SELECT * FROM sys.new_table;");
  142. cmd.CommandType = CommandType.Text;
  143. cmd.Connection = conn;
  144.  
  145. try
  146. {
  147. if (request != null)
  148. {
  149. string[] tokens = request.Split(' ');
  150. Verb = tokens[0].ToUpper();
  151. LocalUrl = tokens[1];
  152. HttpVersion = tokens[2];
  153.  
  154. ReadHeader(r);
  155.  
  156. string file = Path.Combine(
  157. Path.GetFullPath(localPath),
  158. LocalUrl.TrimStart('/'));
  159.  
  160. string text = File.ReadAllText(file);
  161.  
  162. Succeeded(w, Path.GetExtension(LocalUrl).TrimStart('.'));
  163.  
  164. w.Write(text);
  165.  
  166. conn.Open();
  167. }
  168. }
  169. catch (IndexOutOfRangeException e)
  170. {
  171. Console.WriteLine($"Error Here! {e}");
  172. }
  173.  
  174. }
  175.  
  176. }
  177.  
  178. private void Succeeded(StreamWriter stream, string extention)
  179. {
  180. stream.WriteLine("HTTP/1.0 200 OK");
  181. stream.WriteLine("Content-Type: text/" + extention);
  182. stream.WriteLine("Connection: close");
  183. stream.WriteLine("");
  184. }
  185.  
  186. private void Failed(StreamWriter stream)
  187. {
  188. stream.WriteLine("HTTP/1.0 404 not found");
  189. stream.WriteLine("Connection: close");
  190. stream.WriteLine("");
  191.  
  192. }
  193.  
  194. private void ReadHeader(StreamReader r)
  195. {
  196. //DO SOMETHING
  197. }
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment