private void Test1Handler(HttpEntityManager http, UriTemplateMatch match) { if (http.User != null) http.Reply("OK", 200, "OK", "text/plain"); else http.Reply("Please authenticate yourself", 401, "Unauthorized", "text/plain"); }
private void TestAnonymousHandler(HttpEntityManager http, UriTemplateMatch match) { if (http.User != null) http.Reply("ERROR", 500, "ERROR", "text/plain"); else http.Reply("OK", 200, "OK", "text/plain"); }
private void TestEncodingHandler(HttpEntityManager http, UriTemplateMatch match) { var a = match.BoundVariables["a"]; var b = match.BoundVariables["b"]; http.Reply(new { a = a, b = b, rawSegment = http.RequestedUrl.Segments[2] }.ToJson(), 200, "OK", "application/json"); }
public static void ReplyContent(this HttpEntityManager self, byte[] response, int code, string description, string type, IEnumerable <KeyValuePair <string, string> > headers, Action <Exception> onError) { self.Reply(response, code, description, type, null, headers, onError); }
public static void ReplyTextContent(this HttpEntityManager self, string response, int code, string description, string type, IEnumerable <KeyValuePair <string, string> > headers, Action <Exception> onError) { //TODO: add encoding header??? self.Reply(Helper.UTF8NoBom.GetBytes(response ?? string.Empty), code, description, type, Helper.UTF8NoBom, headers, onError); }
private void TestEncodingHandler(HttpEntityManager http, UriTemplateMatch match, string a) { var b = match.BoundVariables["b"]; http.Reply( new { a = a, b = b, rawSegment = http.RequestedUrl.Segments[1], requestUri = match.RequestUri, rawUrl = http.HttpEntity.Request.RawUrl }.ToJson(), 200, "OK", "application/json"); }
private void OnGetHistogram(HttpEntityManager entity, UriTemplateMatch match) { var name = match.BoundVariables["name"]; var histogram = Histograms.HistogramService.GetHistogram(name); if (histogram == null) { entity.ReplyStatus(HttpStatusCode.NotFound, "Not found", _ => { }); return; } var writer = new StringWriter(); lock (histogram) { histogram.outputPercentileDistribution(writer, outputValueUnitScalingRatio: 1000.0*1000.0); } var response = Encoding.ASCII.GetBytes(writer.ToString()); entity.Reply(response, HttpStatusCode.OK, "OK", ContentType.PlainText, Encoding.ASCII, null, _ => { }); }
private void ReplyWithContent(HttpEntityManager http, string contentLocalPath) { //NOTE: this is fix for Mono incompatibility in UriTemplate behavior for /a/b{*C} if (("/" + contentLocalPath).StartsWith(_localWebRootPath)) contentLocalPath = contentLocalPath.Substring(_localWebRootPath.Length); //_logger.Trace("{0} requested from MiniWeb", contentLocalPath); try { var extensionToContentType = new Dictionary<string, string> { { ".png", "image/png"} , { ".jpg", "image/jpeg"} , { ".jpeg", "image/jpeg"} , { ".css", "text/css"} , { ".htm", "text/html"} , { ".html", "text/html"} , { ".js", "application/javascript"} , { ".json", "application/json"} , { ".ico", "image/vnd.microsoft.icon"} }; var extension = Path.GetExtension(contentLocalPath); var fullPath = Path.Combine(_fileSystemRoot, contentLocalPath); string contentType; if (string.IsNullOrEmpty(extension) || !extensionToContentType.TryGetValue(extension.ToLower(), out contentType) || !File.Exists(fullPath)) { _logger.Info("Replying 404 for {0} ==> {1}", contentLocalPath, fullPath); http.ReplyTextContent( "Not Found", 404, "Not Found", "text/plain", null, ex => _logger.InfoException(ex, "Error while replying from MiniWeb")); } else { var config = GetWebPageConfig(contentType); var content = File.ReadAllBytes(fullPath); http.Reply(content, config.Code, config.Description, config.ContentType, config.Encoding, config.Headers, ex => _logger.InfoException(ex, "Error while replying from MiniWeb")); } } catch (Exception ex) { http.ReplyTextContent(ex.ToString(), 500, "Internal Server Error", "text/plain", null, Console.WriteLine); } }
public static void ReplyStatus(this HttpEntityManager self, int code, string description, Action <Exception> onError) { self.Reply(null, code, description, null, null, null, onError); }
public static void ReplyStatus(this HttpEntityManager self, int code, string description, Action <Exception> onError, IEnumerable <KeyValuePair <string, string> > headers = null) { self.Reply(null, code, description, null, null, null, onError); }
private void TestTimeoutHandler(HttpEntityManager http, UriTemplateMatch match) { var sleepFor = int.Parse(match.BoundVariables["sleepfor"]); System.Threading.Thread.Sleep(sleepFor); http.Reply("OK", 200, "OK", "text/plain"); }