private void ServeClient(Socket s) { string request = GetRequest(s); if (request.Length <= 0) { RespondError(s); return; } string url = GetURL(request); if (url.StartsWith("OPTIONS")) { //Empty response RespondText(s, "", "text/html"); } else if (url.StartsWith("GET /storage/")) { string[] parts = url.Substring(5).Split('/'); string addonid = parts[1]; string key = parts[2]; RespondText(s, storage.GetValue(addonid, key) ?? "null", "text/json"); } else if (url.StartsWith("PUT /storage/")) { string[] parts = url.Substring(5).Split('/'); string addonid = parts[1]; string key = parts[2]; string value = GetBody(request); storage.SetValue(addonid, key, value); //Empty response RespondText(s, "", "text/html"); } else if (url.StartsWith("GET /addons")) { if (settings.serveAddons) { TryServeAddons(s, url); } else { RespondError(s, 500, "Addon serving is disabled in config/addons.json"); } } else { RespondText(s, JsonConvert.SerializeObject(jsResp), "text/json"); } s.Close(); }
private void ServeClient(Socket s) { byte[] resp; string content = ""; string request = GetRequest(s); if (request.Length <= 0) { resp = Encoding.UTF8.GetBytes($"HTTP/1.1 500 Internal Server Error\r\nContent-Length: {Encoding.UTF8.GetByteCount(content)}\r\nContent-Type: text/json\r\nAccess-Control-Allow-Methods: *\r\nAccess-Control-Allow-Origin: *\r\n\r\n{ content }\r\n"); using (var context = new SocketAsyncEventArgs()) { context.SetBuffer(resp, 0, resp.Length); s.SendAsync(context); } return; } string url = GetURL(request); if (url.StartsWith("OPTIONS")) { content = ""; } else if (url.StartsWith("GET /storage/")) { string[] parts = url.Substring(5).Split('/'); string addonid = parts[1]; string key = parts[2]; content = storage.GetValue(addonid, key) ?? "null"; } else if (url.StartsWith("PUT /storage/")) { string[] parts = url.Substring(5).Split('/'); string addonid = parts[1]; string key = parts[2]; string value = GetBody(request); storage.SetValue(addonid, key, value); content = ""; } else { content = JsonConvert.SerializeObject(jsResp); } resp = Encoding.UTF8.GetBytes($"HTTP/1.1 200 OK\r\nContent-Length: {Encoding.UTF8.GetByteCount(content)}\r\nContent-Type: text/json\r\nAccess-Control-Allow-Methods: *\r\nAccess-Control-Allow-Origin: *\r\n\r\n{ content }\r\n"); using (var context = new SocketAsyncEventArgs()) { context.SetBuffer(resp, 0, resp.Length); s.SendAsync(context); } }