private int OnStats(HttpReqResp req) { try { var date = GetVisualizationDateFromUrlParameters(req); date = VerifyDateForRetrospection(date); var visType = GetVisualizationTypesFromUrlParameters(req); // get data to display var title = GetRetrospectionTitle(visType, date); var dashboard = ((string)_resourceManager.GetObject("personalanalytics_html")); var visualizations = GetVisualizationsHtml(visType, date); visualizations.Wait(); // wait for the async task to complete // prepare html which is displayed in the browser control var html = dashboard.Replace("{title}", title).Replace("{visualizations}", visualizations.Result); req.Write(html); req.SetHeader("Content-Type", "text/html; charset=utf-8"); } catch (Exception e) { req.Write(e.ToString()); req.SetHeader("Content-Type", "text/html; charset=utf-8"); } return 200; }
private int OnStats(HttpReqResp req) { try { var date = GetVisualizationDateFromUrlParameters(req); date = VerifyDateForRetrospection(date); var visType = GetVisualizationTypesFromUrlParameters(req); // get data to display var title = GetRetrospectionTitle(visType, date); var dashboard = ((string)_resourceManager.GetObject("personalanalytics_html")); var visualizations = GetVisualizationsHtml(visType, date); visualizations.Wait(); // wait for the async task to complete // prepare html which is displayed in the browser control var html = dashboard.Replace("{title}", title).Replace("{visualizations}", visualizations.Result); req.Write(html); req.SetHeader("Content-Type", "text/html; charset=utf-8"); } catch (Exception e) { req.Write(e.ToString()); req.SetHeader("Content-Type", "text/html; charset=utf-8"); } return(200); }
private int OnResource(HttpReqResp req) { try { req.Write((byte[])_resourceManager.GetObject(req.Script.Replace('.', '_'))); req.SetHeader("Content-Type", req.Script.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ? "image/png" : "text/javascript"); } catch { } return(200); }
/// <summary> /// tries to parse the url /// </summary> /// <param name="req"></param> /// <returns></returns> public static VisType GetVisualizationTypesFromUrlParameters(HttpReqResp req) { try { if (req["type"] == null) { return(VisType.Day); // default } return((VisType)Enum.Parse(typeof(VisType), req["type"], true)); } catch { return(VisType.Day); // default } }
/// <summary> /// Parses the request parameter (for "date") as a DateTime /// or returns a fallback value if there is no or if it fails. /// </summary> /// <param name="req"></param> /// <returns></returns> public static DateTimeOffset GetVisualizationDateFromUrlParameters(HttpReqResp req) { var fallbackDate = DateTimeOffset.Now; try { var dateParam = req["date"]; if (dateParam != null) { return(DateTimeOffset.Parse(dateParam, CultureInfo.InvariantCulture)); } } catch { } return(fallbackDate); }
private void HandlerThread(object obj) { if (_clientCount > 9) { return; } _clientCount++; var client = (TcpClient)obj; Stream stream = client.GetStream(); try { var reader = new StreamReader(stream); var line = reader.ReadLine(); if (line == null) { line = string.Empty; } else { line = line.Split(' ')[1].Substring(1); } var token = (line.IndexOf('?') == -1 ? line : line.Substring(0, line.IndexOf('?'))); var query = (line.IndexOf('?') == -1 ? string.Empty : line.Substring(line.IndexOf('?') + 1)); var host = ""; var contentLength = 0; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("Host:", StringComparison.OrdinalIgnoreCase)) { host = line.Substring(5).Trim().Split(':')[0]; } if (line.StartsWith("Content-Length:", StringComparison.OrdinalIgnoreCase)) { contentLength = int.Parse(line.Substring(15).Trim(), CultureInfo.InvariantCulture); } else if (line == "") { if (contentLength != 0) { var buffer = new char[contentLength]; reader.Read(buffer, 0, contentLength); query += (query == string.Empty ? "" : "&") + new string(buffer); } break; } } var reqResp = new HttpReqResp(host, token, query); reqResp.SetHeader("Cache-Control", "no-cache"); var statusCode = (_handlers.ContainsKey(token) ? _handlers[token](reqResp) : 404); WriteString("HTTP/1.1 " + statusCode + " " + _statusCodes[statusCode] + "\r\n", stream); WriteString("Server: aHTTP 1.1\r\n", stream); reqResp.SetHeader("Content-Length", reqResp.Response.Length.ToString(CultureInfo.InvariantCulture)); for (var i = 0; i < reqResp.Headers.Count; i++) { WriteString(reqResp.Headers.GetKey(i) + ": " + reqResp.Headers.Get(i) + "\r\n", stream); } WriteString("\r\n", stream); try { stream.Write(reqResp.Response.GetBuffer(), 0, (int)reqResp.Response.Length); } catch { } } catch { } stream.Flush(); stream.Close(); client.Close(); _clientCount--; }
private void HandlerThread(object obj) { if (_clientCount > 9) return; _clientCount++; var client = (TcpClient)obj; Stream stream = client.GetStream(); try { var reader = new StreamReader(stream); var line = reader.ReadLine(); if (line == null) line = string.Empty; else line = line.Split(' ')[1].Substring(1); var token = (line.IndexOf('?') == -1 ? line : line.Substring(0, line.IndexOf('?'))); var query = (line.IndexOf('?') == -1 ? string.Empty : line.Substring(line.IndexOf('?') + 1)); var host = ""; var contentLength = 0; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("Host:", StringComparison.OrdinalIgnoreCase)) host = line.Substring(5).Trim().Split(':')[0]; if (line.StartsWith("Content-Length:", StringComparison.OrdinalIgnoreCase)) contentLength = int.Parse(line.Substring(15).Trim(), CultureInfo.InvariantCulture); else if (line == "") { if (contentLength != 0) { var buffer = new char[contentLength]; reader.Read(buffer, 0, contentLength); query += (query == string.Empty ? "" : "&") + new string(buffer); } break; } } var reqResp = new HttpReqResp(host, token, query); reqResp.SetHeader("Cache-Control", "no-cache"); var statusCode = (_handlers.ContainsKey(token) ? _handlers[token](reqResp) : 404); WriteString("HTTP/1.1 " + statusCode + " " + _statusCodes[statusCode] + "\r\n", stream); WriteString("Server: aHTTP 1.1\r\n", stream); reqResp.SetHeader("Content-Length", reqResp.Response.Length.ToString(CultureInfo.InvariantCulture)); for (var i = 0; i < reqResp.Headers.Count; i++) WriteString(reqResp.Headers.GetKey(i) + ": " + reqResp.Headers.Get(i) + "\r\n", stream); WriteString("\r\n", stream); try { stream.Write(reqResp.Response.GetBuffer(), 0, (int)reqResp.Response.Length); } catch { } } catch { } stream.Flush(); stream.Close(); client.Close(); _clientCount--; }
private int OnResource(HttpReqResp req) { try { req.Write((byte[])_resourceManager.GetObject(req.Script.Replace('.', '_'))); req.SetHeader("Content-Type", req.Script.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ? "image/png" : "text/javascript"); } catch { } return 200; }
/// <summary> /// tries to parse the url /// </summary> /// <param name="req"></param> /// <returns></returns> public static VisType GetVisualizationTypesFromUrlParameters(HttpReqResp req) { try { if (req["type"] == null) return VisType.Day; // default return (VisType)Enum.Parse(typeof(VisType), req["type"], true); } catch { return VisType.Day; // default } }
/// <summary> /// Parses the request parameter (for "date") as a DateTime /// or returns a fallback value if there is no or if it fails. /// </summary> /// <param name="req"></param> /// <returns></returns> public static DateTimeOffset GetVisualizationDateFromUrlParameters(HttpReqResp req) { var fallbackDate = DateTimeOffset.Now; try { var dateParam = req["date"]; if (dateParam != null) { return DateTimeOffset.Parse(dateParam, CultureInfo.InvariantCulture); } } catch { } return fallbackDate; }