/// <summary> /// Executes the SQL query from the "Query" tab and renders the results. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnQueryExecute_Click(object sender, EventArgs e) { #region Validation // Try to parse the base URL: UriBuilder reqUrib = getBaseURL(); if (reqUrib == null) { return; } if (String.IsNullOrEmpty(txtQuerySelect.Text)) { msgbox("SELECT is required."); return; } #endregion // Settings are good to save now: persistSettings(); // Construct the query string for the request: reqUrib.Query = UrlEncodeParameters(new Dictionary <string, string> { { "output", "json3" }, // JSON where each row is an array of column values { "no_query", "1" }, // Don't report the composed query back to us { "rowlimit", ((int)numQueryRowCount.Value).ToString() }, { "cs", txtConnectionString.Text.Trim() }, { "wi", txtQueryWithIdentifier.Text.Trim() }, { "we", txtQueryWithExpression.Text.Trim() }, { "select", txtQuerySelect.Text.Trim() }, { "from", txtQueryFrom.Text.Trim() }, { "where", txtQueryWhere.Text.Trim() }, { "groupBy", txtQueryGroupBy.Text.Trim() }, { "having", txtQueryHaving.Text.Trim() }, { "orderBy", txtQueryOrderBy.Text.Trim() } }); // Create the HTTP GET request to expect application/json: HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(reqUrib.Uri); req.Method = "GET"; req.Accept = "application/json"; // Disable reentrancy: pnlQuery.Enabled = false; btnQueryExecute.Enabled = false; var asyncst = new AsyncUpdateViewState() { req = req, enableQueryPanels = () => { // On UI thread pnlQuery.Enabled = true; btnQueryExecute.Enabled = true; }, dgResults = dgQueryResults, reportQueryTime = (int msec) => { // On UI thread lblQueryTime.Text = String.Format("Execution time: {0,6} msec", msec); }, handleError = (dict) => { // On UI thread string message = dict.GetValueOrDefaultAs("message", o => (string)o); msgbox(message); }, success = () => { // On UI thread } }; // Asynchronously fetch the response and display the results to the DataGridView when complete: req.BeginGetResponse(new AsyncCallback(responseCallbackUpdateGridResults), (object)asyncst); }
private void doMutate(bool commitMode) { #region Validation // Try to parse the base URL: UriBuilder reqUrib = getBaseURL(); if (reqUrib == null) { return; } if (String.IsNullOrEmpty(txtModifyPrivateKeyPath.Text)) { msgbox("An OpenSSL RSA private key file must be specified in order to use this feature."); return; } string privateKeyPath = txtModifyPrivateKeyPath.Text.Trim(); if (!File.Exists(privateKeyPath)) { msgbox("Could not find the OpenSSL RSA private key file specified."); return; } // Load the lines from the private key file: string[] openSSLprivateKey = File.ReadAllLines(privateKeyPath); // Decrypt the private key file, prompting for a passphrase if encrypted: string msgboxMessage = "Either failed to read private key or passphrase is incorrect!"; RSAParameters key; if (!privkeyFromOpenSSL( openSSLprivateKey, () => { if (String.IsNullOrEmpty(txtModifyPassphrase.Text)) { msgboxMessage = "This private key requires a passphrase. Please provide one."; return(null); } return(txtModifyPassphrase.Text); }, out key) ) { msgbox(msgboxMessage); return; } // Settings are good to save now: persistSettings(); string cmd = txtModifyCommand.Text.Trim(); string query = txtModifyQuery.Text.Trim(); if (String.IsNullOrEmpty(cmd) || String.IsNullOrEmpty(query)) { msgbox("Both a command and a verification query are required."); return; } #endregion // Disable reentrancy: pnlModifyCommand.Enabled = false; pnlModifyQuery.Enabled = false; if (commitMode) { btnModifyCommit.Enabled = false; } btnModifyTest.Enabled = false; byte[] data = Encoding.UTF8.GetBytes(cmd + "\n" + query); long timestampTicks = DateTime.UtcNow.Ticks; byte[] toSign = BitConverter.GetBytes(timestampTicks).Concat(data).ToArray(); byte[] signed = signDataWithKey(toSign, key); // POST to server: string pubkey64 = pubkeyToBase64(key); string signed64 = Convert.ToBase64String(signed); Debug.WriteLine(String.Format("public-key:\n{0}\n", pubkey64)); Debug.WriteLine(String.Format("signed-hash:\n{0}\n", signed64)); var postData = new Dictionary <string, string> { { "p", pubkey64 }, { "s", signed64 }, { "ts", timestampTicks.ToString() }, { "c", cmd }, { "q", query }, { "cs", txtConnectionString.Text.Trim() }, { "rowlimit", ((int)numQueryRowCount.Value).ToString() }, }; // Construct the query string for the request: reqUrib.Query = UrlEncodeParameters(new Dictionary <string, string> { { "mu", commitMode ? "1" : "0" } }); var asyncst = new AsyncUpdateViewState() { enableQueryPanels = () => { // On UI thread pnlModifyCommand.Enabled = true; pnlModifyQuery.Enabled = true; if (commitMode) { btnModifyCommit.Enabled = true; } btnModifyTest.Enabled = true; }, dgResults = dgModifyResults, reportQueryTime = (int msec) => { // On UI thread lblModifyTime.Text = String.Format("Execution time: {0,6} msec", msec); }, handleError = (dict) => { // On UI thread string message = dict.GetValueOrDefaultAs("message", o => (string)o); msgbox(message); }, success = () => { // On UI thread // Set isCommitEnabled to true and enable the Commit button: setCommitEnabled(true); } }; createPOSTRequestFormEncoded(reqUrib.Uri.AbsoluteUri, "application/json", postData, responseCallbackUpdateGridResults, asyncst); }
private static void createPOSTRequestFormEncoded(string url, string accept, Dictionary <string, string> postData, AsyncCallback updateGrid, AsyncUpdateViewState asyncst) { var req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "POST"; req.Accept = accept; req.ContentType = "application/x-www-form-urlencoded"; asyncst.req = req; asyncst.postData = postData; asyncst.updateGrid = updateGrid; req.BeginGetRequestStream(new AsyncCallback(sendPOSTdata), asyncst); }