public static void EndSend(WebStatsClient client, byte[] buffer) { if (OnSend != null) { VitaNexCore.TryCatch(() => OnSend(client, buffer)); } }
public static void Connected(WebStatsClient client) { lock (Clients) { if (!Clients.Contains(client)) { Clients.Add(client); } } CMOptions.ToConsole("[{0}] Client connected: {1}", Clients.Count, client.Client.Client.RemoteEndPoint); if (OnConnected != null) { VitaNexCore.TryCatch( () => OnConnected(client), e => { lock (Clients) { Clients.Remove(client); } CMOptions.ToConsole(e); }); } }
public static void EndReceive(WebStatsClient client, string data, byte[] buffer) { if (OnReceive != null) { VitaNexCore.TryCatch(() => OnReceive(client, data, buffer)); } }
public static void Disconnected(WebStatsClient client) { lock (Clients) { Clients.Remove(client); } CMOptions.ToConsole("[{0}] Client disconnected: {1}", Clients.Count, client.Client.Client.RemoteEndPoint); if (OnDisconnected != null) { VitaNexCore.TryCatch(() => OnDisconnected(client), CMOptions.ToConsole); } }
private static void HandleHttpRequest(WebStatsClient client, Dictionary <string, string> headers) { var queries = new Dictionary <string, string>(); bool preFlight = false; string[] args; if (headers.ContainsKey("GET")) { args = headers["GET"].Split(' '); } else if (headers.ContainsKey("OPTIONS")) { preFlight = true; args = headers["OPTIONS"].Split(' '); } else { args = new string[0]; } if (args.Length > 0) { string rawQuery = !String.IsNullOrWhiteSpace(args[0]) ? args[0] : String.Empty; if (rawQuery.StartsWith("/?")) { foreach (string q in rawQuery.Substring(2).Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries)) { var query = q.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries); string qk = query.Length > 0 ? query[0] : String.Empty; if (String.IsNullOrWhiteSpace(qk)) { return; } string qv = query.Length > 1 ? query[1] : String.Empty; if (!queries.ContainsKey(qk)) { queries.Add(qk, qv); } else { queries[qk] = qv; } } } } bool origin = headers.ContainsKey("Origin"); bool jsonp = false; string response = preFlight ? String.Empty : GetJSON(false); if (queries.ContainsKey("callback")) { string func = queries["callback"]; if (String.IsNullOrWhiteSpace(func)) { func = "webstatsCallback"; } response = String.Format("{0}({1});", func, response); jsonp = true; } var sendHeaders = new List <string> { "HTTP/1.1 200 OK", // "Date: " + DateTime.UtcNow.ToSimpleString("D, d M y t@h:m:s@") + " GMT", // "Last-Modified: " + _LastUpdate.ToSimpleString("D, d M y t@h:m:s@"), // "Server: " + ServerList.ServerName }; if (preFlight) { if (origin) { sendHeaders.AddRange( new[] { "Access-Control-Allow-Methods: POST, GET, OPTIONS", // "Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept", // "Access-Control-Allow-Origin: " + headers["Origin"] }); } sendHeaders.AddRange( new[] { "Vary: Accept-Encoding", // "Content-Encoding: deflate", // "Content-Length: 0", // "Content-Type: text/plain; charset=utf-8", // "Keep-Alive: timeout=2, max=120", // "Connection: keep-alive" }); client.Send(String.Join("\r\n", sendHeaders) + "\r\n\r\n", false, false, EndSend); } else { byte[] buffer; int length; client.Encode(response, out buffer, out length); client.Compress(ref buffer, ref length); if (origin) { sendHeaders.Add("Access-Control-Allow-Origin: " + headers["Origin"]); } sendHeaders.AddRange( new[] { "Vary: Accept-Encoding", // "Content-Encoding: deflate", // "Content-Length: " + length, // (jsonp ? "Content-Type: text/javascript;" : "Content-Type: application/json;") + " charset=utf-8", // "Connection: close" }); client.Send(String.Join("\r\n", sendHeaders) + "\r\n\r\n", false, false, EndSend); client.Send(buffer, length, false, EndSend); } if (!CMOptions.ModuleDebug) { return; } CMOptions.ToConsole("HEADERS>>>\n"); CMOptions.ToConsole(sendHeaders.ToArray()); }
public static void HandleConnection(WebStatsClient client) { VitaNexCore.TryCatch( () => { var headers = new Dictionary <string, string>(); client.Receive( false, true, (c, d, b) => { EndReceive(c, d, b); if (d.Length == 0) { return; } var lines = d.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); lines = lines.Take(lines.Length - 1).ToArray(); if (CMOptions.ModuleDebug) { CMOptions.ToConsole(lines.Not(String.IsNullOrWhiteSpace).ToArray()); } lines.ForEach( line => { line = line.Trim(); var header = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (header.Length == 0) { return; } string hk = header[0].Replace(":", String.Empty); if (String.IsNullOrWhiteSpace(hk)) { return; } string hv = header.Length > 1 ? String.Join(" ", header.Skip(1)) : String.Empty; if (!headers.ContainsKey(hk)) { headers.Add(hk, hv); } else { headers[hk] = hv; } }); }); if (headers.Count > 0) { HandleHttpRequest(client, headers); return; } client.Send(GetJSON(false), true, false, EndSend); }, CMOptions.ToConsole); if (client != null) { client.Close(); } }
private static void HandleHttpRequest(WebStatsClient client, Dictionary<string, string> headers) { var queries = new Dictionary<string, string>(); bool preFlight = false; string[] args; if (headers.ContainsKey("GET")) { args = headers["GET"].Split(' '); } else if (headers.ContainsKey("OPTIONS")) { preFlight = true; args = headers["OPTIONS"].Split(' '); } else { args = new string[0]; } if (args.Length > 0) { string rawQuery = !String.IsNullOrWhiteSpace(args[0]) ? args[0] : String.Empty; if (rawQuery.StartsWith("/?")) { foreach (string q in rawQuery.Substring(2).Split(new[] {'&'}, StringSplitOptions.RemoveEmptyEntries)) { var query = q.Split(new[] {'='}, StringSplitOptions.RemoveEmptyEntries); string qk = query.Length > 0 ? query[0] : String.Empty; if (String.IsNullOrWhiteSpace(qk)) { return; } string qv = query.Length > 1 ? query[1] : String.Empty; if (!queries.ContainsKey(qk)) { queries.Add(qk, qv); } else { queries[qk] = qv; } } } } bool origin = headers.ContainsKey("Origin"); bool jsonp = false; string response = preFlight ? String.Empty : GetJSON(false); if (queries.ContainsKey("callback")) { string func = queries["callback"]; if (String.IsNullOrWhiteSpace(func)) { func = "webstatsCallback"; } response = String.Format("{0}({1});", func, response); jsonp = true; } var sendHeaders = new List<string> { "HTTP/1.1 200 OK", // "Date: " + DateTime.UtcNow.ToSimpleString("D, d M y t@h:m:s@") + " GMT", // "Last-Modified: " + _LastUpdate.ToSimpleString("D, d M y t@h:m:s@"), // "Server: " + ServerList.ServerName }; if (preFlight) { if (origin) { sendHeaders.AddRange( new[] { "Access-Control-Allow-Methods: POST, GET, OPTIONS", // "Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept", // "Access-Control-Allow-Origin: " + headers["Origin"] }); } sendHeaders.AddRange( new[] { "Vary: Accept-Encoding", // "Content-Encoding: deflate", // "Content-Length: 0", // "Content-Type: text/plain; charset=utf-8", // "Keep-Alive: timeout=2, max=120", // "Connection: keep-alive" }); client.Send(String.Join("\r\n", sendHeaders) + "\r\n\r\n", false, false, EndSend); } else { byte[] buffer; int length; client.Encode(response, out buffer, out length); client.Compress(ref buffer, ref length); if (origin) { sendHeaders.Add("Access-Control-Allow-Origin: " + headers["Origin"]); } sendHeaders.AddRange( new[] { "Vary: Accept-Encoding", // "Content-Encoding: deflate", // "Content-Length: " + length, // (jsonp ? "Content-Type: text/javascript;" : "Content-Type: application/json;") + " charset=utf-8", // "Connection: close" }); client.Send(String.Join("\r\n", sendHeaders) + "\r\n\r\n", false, false, EndSend); client.Send(buffer, length, false, EndSend); } if (!CMOptions.ModuleDebug) { return; } CMOptions.ToConsole("HEADERS>>>\n"); CMOptions.ToConsole(sendHeaders.ToArray()); }
public static void HandleConnection(WebStatsClient client) { VitaNexCore.TryCatch( () => { var headers = new Dictionary<string, string>(); client.Receive( false, true, (c, d, b) => { EndReceive(c, d, b); if (d.Length == 0) { return; } var lines = d.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries); lines = lines.Take(lines.Length - 1).ToArray(); if (CMOptions.ModuleDebug) { CMOptions.ToConsole(lines.Not(String.IsNullOrWhiteSpace).ToArray()); } lines.ForEach( line => { line = line.Trim(); var header = line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries); if (header.Length == 0) { return; } string hk = header[0].Replace(":", String.Empty); if (String.IsNullOrWhiteSpace(hk)) { return; } string hv = header.Length > 1 ? String.Join(" ", header.Skip(1)) : String.Empty; if (!headers.ContainsKey(hk)) { headers.Add(hk, hv); } else { headers[hk] = hv; } }); }); if (headers.Count > 0) { HandleHttpRequest(client, headers); return; } client.Send(GetJSON(false), true, false, EndSend); }, CMOptions.ToConsole); if (client != null) { client.Close(); } }
public static void EndSend(WebStatsClient client, byte[] buffer) { if (OnSend != null) { VitaNexCore.TryCatch(() => OnSend(client, buffer)); } }
public static void EndReceive(WebStatsClient client, string data, byte[] buffer) { if (OnReceive != null) { VitaNexCore.TryCatch(() => OnReceive(client, data, buffer)); } }
public static void Disconnected(WebStatsClient client) { lock (Clients) { Clients.Remove(client); } CMOptions.ToConsole("[{0}] Client disconnected: {1}", Clients.Count, client.Client.Client.RemoteEndPoint); if (OnDisconnected != null) { VitaNexCore.TryCatch(() => OnDisconnected(client), CMOptions.ToConsole); } }
public static void Connected(WebStatsClient client) { lock (Clients) { if (!Clients.Contains(client)) { Clients.Add(client); } } CMOptions.ToConsole("[{0}] Client connected: {1}", Clients.Count, client.Client.Client.RemoteEndPoint); if (OnConnected != null) { VitaNexCore.TryCatch( () => OnConnected(client), e => { lock (Clients) { Clients.Remove(client); } CMOptions.ToConsole(e); }); } }