/// <summary> /// Process request and send output to client /// </summary> /// <param name="client">client to send output to</param> /// <param name="headers">dictionary of headers</param> /// <param name="request">the requested method</param> /// <param name="requestUrl">url to respond to</param> private void SendOutputToClient(IgbTcpClient client, Dictionary <string, string> headers, string request, string requestUrl) { using (MemoryStream ms = new MemoryStream()) using (StreamWriter sw = new StreamWriter(ms)) { ProcessRequest(request, requestUrl, headers, sw); sw.Flush(); ms.Seek(0, SeekOrigin.Begin); // We should support only the "GET" method client.Write(request.Equals("GET") ? "HTTP/1.1 200 OK\n" : "HTTP/1.1 501 Not Implemented\n"); client.Write("Server: EVEMon/1.0\n"); client.Write("Content-Type: text/html; charset=utf-8\n"); if (headers.ContainsKey("eve_trusted") && headers["eve_trusted"].ToLower(CultureConstants.DefaultCulture) == "no") { client.Write("eve.trustme: http://" + BuildHostAndPort(headers["host"]) + "/::EVEMon needs your pilot information.\n"); } client.Write("Connection: close\n"); client.Write("Content-Length: " + ms.Length.ToString() + "\n\n"); using (StreamReader sr = new StreamReader(ms)) { client.Write(sr.ReadToEnd()); } } }
/// <summary> /// Process request and send output to client. /// </summary> /// <param name="client">client to send output to</param> /// <param name="headers">dictionary of headers</param> /// <param name="request">the requested method</param> /// <param name="requestPath">url to respond to</param> private void SendOutputToClient(IgbTcpClient client, IDictionary <string, string> headers, string request, string requestPath) { MemoryStream stream = Util.GetMemoryStream(); using (StreamWriter sw = new StreamWriter(stream)) { ProcessRequest(request, requestPath, headers, sw); sw.Flush(); stream.Seek(0, SeekOrigin.Begin); // We should support only the "GET" method string responseStatusCode = request.Equals(HttpMethod.Get.Method) ? $"{(int)HttpStatusCode.OK} {HttpStatusCode.OK.ToString().ConvertUpperCamelCaseToString()}" : $"{(int)HttpStatusCode.NotImplemented} {HttpStatusCode.NotImplemented.ToString().ConvertUpperCamelCaseToString()}"; client.Write($"HTTP/1.1 {responseStatusCode}\n"); client.Write("Server: EVEMon/1.0\n"); client.Write("Content-Type: text/html; charset=utf-8\n"); if (headers.ContainsKey("eve_trusted") && headers["eve_trusted"].ToLower(CultureConstants.InvariantCulture) == "no") { client.Write($"eve.trustme: {BuildHostAndPort(headers["host"])}/::EVEMon needs your character information.\n"); } client.Write("Connection: close\n"); client.Write($"Content-Length: {stream.Length}\n\n"); StreamReader sr = new StreamReader(stream); client.Write(sr.ReadToEnd()); } }
/// <summary> /// Event triggered on client connection /// </summary> /// <param name="sender">Sending object</param> /// <param name="e">Argments</param> private void OnClientConnected(object sender, ClientConnectedEventArgs e) { IgbTcpClient cli = new IgbTcpClient(e.TcpClient); cli.DataRead += new EventHandler <IgbClientDataReadEventArgs>(OnDataRead); cli.Closed += new EventHandler <EventArgs>(OnClosed); lock (m_clients) { m_clients.Add(cli, new byte[0]); } cli.Start(); }
/// <summary> /// Process the buffer and respond to the client. /// </summary> /// <param name="client">client to respond to</param> /// <param name="buffer">buffer to use</param> /// <param name="length">length of tail</param> private void TryProcessBuffer(IgbTcpClient client, byte[] buffer, int length) { // make sure the request is well formed if (!TailHasTwoNewLine(buffer, length)) { return; } Dictionary <string, string> headers = new Dictionary <string, string>(); string requestUrl = ExtractHeaders(buffer, headers); SendOutputToClient(client, headers, requestUrl); client.Close(); }
/// <summary> /// Event triggered on data read /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnDataRead(object sender, IgbClientDataReadEventArgs e) { IgbTcpClient IgbSender = (IgbTcpClient)sender; byte[] newBuf; lock (m_clients) { byte[] existingBuf = m_clients[IgbSender]; newBuf = new byte[existingBuf.Length + e.Count]; Array.Copy(existingBuf, newBuf, existingBuf.Length); Array.Copy(e.Buffer, 0, newBuf, existingBuf.Length, e.Count); m_clients[IgbSender] = newBuf; } TryProcessBuffer(IgbSender, newBuf, Math.Min(e.Count + 1, newBuf.Length)); }
/// <summary> /// Process request and send output to client. /// </summary> /// <param name="client">client to send output to</param> /// <param name="headers">dictionary of headers</param> /// <param name="request">the requested method</param> /// <param name="requestPath">url to respond to</param> private void SendOutputToClient(IgbTcpClient client, IDictionary<string, string> headers, string request, string requestPath) { MemoryStream stream = Util.GetMemoryStream(); using (StreamWriter sw = new StreamWriter(stream)) { ProcessRequest(request, requestPath, headers, sw); sw.Flush(); stream.Seek(0, SeekOrigin.Begin); // We should support only the "GET" method string responseStatusCode = request.Equals(HttpMethod.Get.Method) ? $"{(int)HttpStatusCode.OK} {HttpStatusCode.OK.ToString().ConvertUpperCamelCaseToString()}" : $"{(int)HttpStatusCode.NotImplemented} {HttpStatusCode.NotImplemented.ToString().ConvertUpperCamelCaseToString()}"; client.Write($"HTTP/1.1 {responseStatusCode}\n"); client.Write("Server: EVEMon/1.0\n"); client.Write("Content-Type: text/html; charset=utf-8\n"); if (headers.ContainsKey("eve_trusted") && headers["eve_trusted"].ToLower(CultureConstants.InvariantCulture) == "no") { client.Write($"eve.trustme: {BuildHostAndPort(headers["host"])}/::EVEMon needs your character information.\n"); } client.Write("Connection: close\n"); client.Write($"Content-Length: {stream.Length}\n\n"); StreamReader sr = new StreamReader(stream); client.Write(sr.ReadToEnd()); } }
/// <summary> /// Process the buffer and respond to the client. /// </summary> /// <param name="client">client to respond to</param> /// <param name="buffer">buffer to use</param> /// <param name="length">length of tail</param> private void TryProcessBuffer(IgbTcpClient client, byte[] buffer, int length) { // make sure the request is well formed if (!TailHasTwoNewLine(buffer, length)) return; Dictionary<string, string> headers = new Dictionary<string, string>(); string requestType = String.Empty; string requestPath = ExtractHeaders(buffer, headers, ref requestType); SendOutputToClient(client, headers, requestType, requestPath); client.Close(); }
/// <summary> /// Event triggered on client connection. /// </summary> /// <param name="sender">Sending object</param> /// <param name="e">Argments</param> private void OnClientConnected(object sender, ClientConnectedEventArgs e) { IgbTcpClient cli = new IgbTcpClient(e.TcpClient); cli.DataRead += OnDataRead; cli.Closed += OnClosed; lock (m_clients) { m_clients.Add(cli, new byte[0]); } cli.Start(); }
/// <summary> /// Process request and send output to client /// </summary> /// <param name="client">client to send output to</param> /// <param name="headers">dictionary of headers</param> /// <param name="request">the requested method</param> /// <param name="requestUrl">url to respond to</param> private void SendOutputToClient(IgbTcpClient client, Dictionary<string, string> headers, string request, string requestUrl) { using (MemoryStream ms = new MemoryStream()) using (StreamWriter sw = new StreamWriter(ms)) { ProcessRequest(request, requestUrl, headers, sw); sw.Flush(); ms.Seek(0, SeekOrigin.Begin); // We should support only the "GET" method client.Write(request.Equals("GET") ? "HTTP/1.1 200 OK\n" : "HTTP/1.1 501 Not Implemented\n"); client.Write("Server: EVEMon/1.0\n"); client.Write("Content-Type: text/html; charset=utf-8\n"); if (headers.ContainsKey("eve_trusted") && headers["eve_trusted"].ToLower(CultureConstants.DefaultCulture) == "no") { client.Write("eve.trustme: http://" + BuildHostAndPort(headers["host"]) + "/::EVEMon needs your pilot information.\n"); } client.Write("Connection: close\n"); client.Write("Content-Length: " + ms.Length.ToString() + "\n\n"); using (StreamReader sr = new StreamReader(ms)) { client.Write(sr.ReadToEnd()); } } }