// common code for all requests Stream Common(string method, string catalog, string name, string id, Stream body = null) { string content = (body == null) ? null : ReadStream(body); var qparams = GetQueryParams(); Logger.WriteLine(2, "Server {0}: {1},{2},{3},{4} <{5}>", method, catalog, name, id, KvpToString(qparams), content); // could be extended to support multiple databases var gateway = HostProgram.GetGateway(catalog); var result = (gateway == null) ? Result.Failure("catalog not found: " + catalog) : gateway.JsonCall(method, name, id, qparams, content); if (result.Ok) { // string returned will actually be prefab json. WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK; WebOperationContext.Current.OutgoingResponse.ContentType = "application/json"; return(ToStream(result.Value as string)); } else { // Bad result should cause bad request, but then it sends html. Maybe later? WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK; WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"; return(ToStream(result.Message)); } //WebOperationContext.Current.OutgoingResponse.StatusCode = result.Ok ? HttpStatusCode.OK : HttpStatusCode.BadRequest; //WebOperationContext.Current.OutgoingResponse.ContentType = "application/json"; //return ToStream(result.Ok ? result.Value as string : result.Message); }
// handle repl execution // gateway responsible for decoding raw/json input and providing json output Stream Exec(string catalog, Stream body) { string content = ReadStream(body); Logger.WriteLine(2, "Server {0}: {1} <{2}>", "repl", catalog, content); var mode = (WebOperationContext.Current.IncomingRequest.ContentType == "application/json") ? ExecModes.JsonString : ExecModes.Raw; var gateway = HostProgram.GetGateway(catalog); var result = gateway.RunScript(content, mode); WebOperationContext.Current.OutgoingResponse.ContentType = (mode == ExecModes.Raw || !result.Ok) ? "text/plain" : "application/json"; WebOperationContext.Current.OutgoingResponse.StatusCode = result.Ok ? HttpStatusCode.OK : HttpStatusCode.BadRequest; return(ToStream(result.Ok ? result.Value as string : result.Message)); }