static HttpResponse Router(HttpRequest req) { #region Setup DateTime startTime = DateTime.Now; string ipPort = SourceIpPort(req); _Logging.Log(LoggingModule.Severity.Debug, "Router " + ipPort + " " + req.Method + " " + req.RawUrlWithoutQuery); HttpResponse resp = new HttpResponse(req, false, 500, null, null, Common.SerializeJson(new ErrorResponse("Internal server error", null), true), true); #endregion #region APIs try { #region Unauthenticated-Methods switch (req.Method.ToLower()) { case "get": #region get if (req.RawUrlWithoutQuery.Equals("/")) { resp = GetHelloWorld(req); return(resp); } if (req.RawUrlWithoutQuery.Equals("/favicon.ico") || req.RawUrlWithoutQuery.Equals("/robots.txt")) { resp = new HttpResponse(req, true, 200, null, null, null, true); return(resp); } break; #endregion case "put": #region put break; #endregion case "post": #region post break; #endregion case "delete": #region delete break; #endregion default: _Logging.Log(LoggingModule.Severity.Warn, "Router " + ipPort + " unknown method: " + req.Method); resp = new HttpResponse(req, false, 400, null, null, Common.SerializeJson(new ErrorResponse("Unknown method", null), true), true); return(resp); } #endregion #region Authenticate if (_Settings.Server.RequireAuthentication) { if (!_Auth.Authenticate(req)) { _Logging.Log(LoggingModule.Severity.Warn, "Router " + ipPort + " authentication failed"); resp = new HttpResponse(req, false, 401, null, null, Common.SerializeJson(new ErrorResponse("Unauthorized", null), true), true); return(resp); } } #endregion #region Authenticated-Methods switch (req.Method.ToLower()) { case "get": #region get if (req.RawUrlWithoutQuery.Equals("/_databaseclients")) { resp = GetDatabaseClients(req); return(resp); } if (req.RawUrlWithoutQuery.Equals("/_databases")) { resp = GetDatabases(req); return(resp); } if (req.RawUrlEntries.Count == 1) { resp = GetDatabase(req); return(resp); } if (req.RawUrlEntries.Count == 2 || req.RawUrlEntries.Count == 3) { resp = GetTable(req); return(resp); } break; #endregion case "put": #region put if (req.RawUrlEntries.Count == 2 || req.RawUrlEntries.Count == 3) { resp = PutTable(req); return(resp); } break; #endregion case "post": #region post if (req.RawUrlEntries.Count == 2) { resp = PostTable(req); return(resp); } break; #endregion case "delete": #region delete if (req.RawUrlEntries.Count == 2 || req.RawUrlEntries.Count == 3) { resp = DeleteTable(req); return(resp); } break; #endregion default: _Logging.Log(LoggingModule.Severity.Warn, "Router " + ipPort + " unknown method: " + req.Method); resp = new HttpResponse(req, false, 400, null, null, Common.SerializeJson(new ErrorResponse("Unknown method", null), true), true); return(resp); } #endregion resp = new HttpResponse(req, false, 404, null, null, Common.SerializeJson(new ErrorResponse("Unknown API", null), true), true); return(resp); } catch (Exception e) { _Logging.LogException("RestDbServer", "Router", e); resp = new HttpResponse(req, false, 500, null, null, Common.SerializeJson(new ErrorResponse("Internal server error", e.Message), true), true); return(resp); } finally { _Logging.Log(LoggingModule.Severity.Info, "Router " + ipPort + " " + req.Method + " " + req.RawUrlWithoutQuery + " " + Common.TotalMsFrom(startTime) + "ms " + resp.StatusCode); } #endregion }
static async Task DefaultRoute(HttpContext ctx) { DateTime startTime = DateTime.Now; string header = ctx.Request.SourceIp + ":" + ctx.Request.SourcePort + " "; _Logging.Debug(header + ctx.Request.Method + " " + ctx.Request.RawUrlWithoutQuery); #region APIs try { #region Unauthenticated-Methods switch (ctx.Request.Method) { case HttpMethod.GET: #region get if (ctx.Request.RawUrlWithoutQuery.Equals("/")) { ctx.Response.StatusCode = 200; ctx.Response.ContentType = "text/html; charset=utf8"; await ctx.Response.Send(RootHtml()); return; } if (ctx.Request.RawUrlWithoutQuery.Equals("/favicon.ico") || ctx.Request.RawUrlWithoutQuery.Equals("/robots.txt")) { ctx.Response.StatusCode = 200; await ctx.Response.Send(); return; } break; #endregion case HttpMethod.PUT: #region put break; #endregion case HttpMethod.POST: #region post break; #endregion case HttpMethod.DELETE: #region delete break; #endregion default: ctx.Response.StatusCode = 400; ctx.Response.ContentType = "application/json"; await ctx.Response.Send(Common.SerializeJson(new ErrorResponse("Unknown method", null), true)); return; } #endregion #region Authenticate if (_Settings.Server.RequireAuthentication) { if (!_Auth.Authenticate(ctx)) { _Logging.Warn(header + "authentication failed"); ctx.Response.StatusCode = 401; ctx.Response.ContentType = "application/json"; await ctx.Response.Send(Common.SerializeJson(new ErrorResponse("Unauthorized", null), true)); return; } } #endregion #region Authenticated-Methods switch (ctx.Request.Method) { case HttpMethod.GET: #region get if (ctx.Request.RawUrlWithoutQuery.Equals("/_databaseclients")) { await GetDatabaseClients(ctx); return; } if (ctx.Request.RawUrlWithoutQuery.Equals("/_databases")) { await GetDatabases(ctx); return; } if (ctx.Request.RawUrlEntries.Count == 1) { await GetDatabase(ctx); return; } if (ctx.Request.RawUrlEntries.Count == 2 || ctx.Request.RawUrlEntries.Count == 3) { await GetTable(ctx); return; } break; #endregion case HttpMethod.PUT: #region put if (ctx.Request.RawUrlEntries.Count == 2 || ctx.Request.RawUrlEntries.Count == 3) { await PutTable(ctx); return; } break; #endregion case HttpMethod.POST: #region post if (ctx.Request.RawUrlEntries.Count == 2) { await PostTable(ctx); return; } break; #endregion case HttpMethod.DELETE: #region delete if (ctx.Request.RawUrlEntries.Count == 2 || ctx.Request.RawUrlEntries.Count == 3) { await DeleteTable(ctx); return; } break; #endregion default: ctx.Response.StatusCode = 400; ctx.Response.ContentType = "application/json"; await ctx.Response.Send(Common.SerializeJson(new ErrorResponse("Unknown method", null), true)); return; } #endregion ctx.Response.StatusCode = 400; ctx.Response.ContentType = "application/json"; await ctx.Response.Send(Common.SerializeJson(new ErrorResponse("Unknown API", null), true)); } catch (Exception e) { _Logging.Exception("RestDb", "Router", e); ctx.Response.StatusCode = 500; ctx.Response.ContentType = "application/json"; await ctx.Response.Send(Common.SerializeJson(new ErrorResponse("Internal server error", e.Message), true)); } finally { _Logging.Debug(header + ctx.Request.Method + " " + ctx.Request.RawUrlWithoutQuery + " " + Common.TotalMsFrom(startTime) + "ms: " + ctx.Response.StatusCode); } #endregion }
static async Task DefaultRoute(HttpContext ctx) { DateTime startTime = DateTime.Now; string header = ctx.Request.Source.IpAddress + ":" + ctx.Request.Source.Port + " "; _Logging.Debug(header + ctx.Request.Method + " " + ctx.Request.Url.RawWithoutQuery); #region APIs try { #region Unauthenticated-Methods switch (ctx.Request.Method) { case HttpMethod.GET: #region get if (ctx.Request.Url.RawWithoutQuery.Equals("/")) { ctx.Response.StatusCode = 200; ctx.Response.ContentType = "text/html; charset=utf8"; await ctx.Response.Send(RootHtml()); return; } if (ctx.Request.Url.RawWithoutQuery.Equals("/favicon.ico") || ctx.Request.Url.RawWithoutQuery.Equals("/robots.txt")) { ctx.Response.StatusCode = 200; await ctx.Response.Send(); return; } break; #endregion case HttpMethod.PUT: #region put break; #endregion case HttpMethod.POST: #region post break; #endregion case HttpMethod.DELETE: #region delete break; #endregion default: ctx.Response.StatusCode = 400; ctx.Response.ContentType = "application/json"; await ctx.Response.Send(SerializationHelper.SerializeJson(new ErrorResponse(ErrorCodeEnum.InvalidRequest, "Unknown method", null), true)); return; } #endregion #region Build-Metadata RequestMetadata md = new RequestMetadata(ctx); #endregion #region Authenticate if (_Settings.Server.RequireAuthentication) { string apiKey = null; ApiKey key = null; if (!_Auth.Authenticate(ctx, out apiKey, out key)) { _Logging.Warn(header + "authentication failed"); ctx.Response.StatusCode = 401; ctx.Response.ContentType = "application/json"; await ctx.Response.Send(SerializationHelper.SerializeJson(new ErrorResponse(ErrorCodeEnum.NotAuthenticated, "You are not authorized to perform this operation", null), true)); return; } md.ApiKey = key; md.Params.ApiKey = apiKey; } if (md.Params.Metadata) { ctx.Response.StatusCode = 200; ctx.Response.ContentType = "application/json"; await ctx.Response.Send(SerializationHelper.SerializeJson(md, true)); return; } #endregion #region Authenticated-Methods switch (ctx.Request.Method) { case HttpMethod.GET: #region get if (ctx.Request.Url.RawWithoutQuery.Equals("/_databaseclients")) { await GetDatabaseClients(md); return; } if (ctx.Request.Url.RawWithoutQuery.Equals("/_databases")) { await GetDatabases(md); return; } if (ctx.Request.Url.Elements.Length == 1) { await GetDatabase(md); return; } if (ctx.Request.Url.Elements.Length == 2 || ctx.Request.Url.Elements.Length == 3) { await GetTableSelect(md); return; } break; #endregion case HttpMethod.PUT: #region put if (ctx.Request.Url.Elements.Length == 2 || ctx.Request.Url.Elements.Length == 3) { await PutTable(md); return; } break; #endregion case HttpMethod.POST: #region post if (ctx.Request.Url.Elements.Length == 1) { if (ctx.Request.Query.Elements.ContainsKey("raw")) { await PostRawQuery(md); return; } else { await PostTableCreate(md); return; } } if (ctx.Request.Url.Elements.Length == 2) { await PostTableInsert(md); return; } break; #endregion case HttpMethod.DELETE: #region delete if (ctx.Request.Url.Elements.Length == 2 || ctx.Request.Url.Elements.Length == 3) { await DeleteTable(md); return; } break; #endregion default: ctx.Response.StatusCode = 400; ctx.Response.ContentType = "application/json"; await ctx.Response.Send(SerializationHelper.SerializeJson(new ErrorResponse(ErrorCodeEnum.InvalidRequest, "Unknown method", null), true)); return; } #endregion ctx.Response.StatusCode = 400; ctx.Response.ContentType = "application/json"; await ctx.Response.Send(SerializationHelper.SerializeJson(new ErrorResponse(ErrorCodeEnum.InvalidRequest, "Unknown endpoint", null), true)); } catch (Exception e) { _Logging.Exception(e); ctx.Response.StatusCode = 500; ctx.Response.ContentType = "application/json"; await ctx.Response.Send(SerializationHelper.SerializeJson(new ErrorResponse(ErrorCodeEnum.InternalError, "Internal server error", e.Message, e), true)); } finally { _Logging.Debug(header + ctx.Request.Method + " " + ctx.Request.Url.RawWithoutQuery + " " + Common.TotalMsFrom(startTime) + "ms: " + ctx.Response.StatusCode); } #endregion }