public static string SendYcmdRequestRaw(string path, object requestData, YcmdProcess p) { var request = (HttpWebRequest)WebRequest.Create(GenerateUri(path, p)); string requestJson = JsonConvert.SerializeObject(requestData); var data = Encoding.UTF8.GetBytes(requestJson); request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = data.Length; //Add x-ycm-hmac header. string hmac = GenerateHmac("POST", path, requestJson, p); request.Headers.Add("X-Ycm-Hmac", hmac); Console.WriteLine(hmac); using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } var r = (HttpWebResponse)request.GetResponse(); var jsonString = new StreamReader(r.GetResponseStream()).ReadToEnd(); return(jsonString); }
public static YcmdProcess StartServer(YcmdProcesses name, string extra_config) { YcmdProcess pp = new YcmdProcess(); pp.name = name; pp.port = (int)name; pp.extra_config_path = Program.config.media_dir + "WebSockets/ycmd/YcmdConfig/" + extra_config; Console.WriteLine("Starting YCMD server '" + name.ToString() + "'..."); //Generate the secret key. byte[] secretKey = new byte[16]; LibRpws.LibRpwsCore.rand.NextBytes(secretKey); pp.secret_key = secretKey; //Create the config file. string conf = File.ReadAllText(Program.config.media_dir + "WebSockets/ycmd/DefaultYcmdSettings.json"); conf = conf.Replace("%KEY%", Convert.ToBase64String(secretKey)); conf = conf.Replace("%CONF%", pp.extra_config_path); Console.WriteLine(pp.extra_config_path); //Save to a temporary directory. string tempFile = Program.config.temp_files + "ycmd_temp_config_" + name.ToString() + ".json"; File.WriteAllText(tempFile, conf); //Run Python and execute this file. ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/usr/bin/python3", Arguments = Program.config.ycmd_binary + " --options_file " + tempFile + " --port " + pp.port, }; pp.p = new Process() { StartInfo = startInfo, }; pp.p.Start(); Console.WriteLine("YCMD server '" + name.ToString() + "' started."); //Add process process_dict.Add(name, pp); return(GetServer(name)); }
public static string GenerateHmac(string method, string path, string body, YcmdProcess p) { //Convert all to text byte[] b_method = Encoding.UTF8.GetBytes(method); byte[] b_path = Encoding.UTF8.GetBytes(path); byte[] b_body = Encoding.UTF8.GetBytes(body); byte[] joined; using (MemoryStream ms = new MemoryStream()) { WriteHmacToMs(b_method, ms, p); WriteHmacToMs(b_path, ms, p); WriteHmacToMs(b_body, ms, p); ms.Position = 0; joined = new byte[ms.Length]; ms.Read(joined, 0, joined.Length); } return(Convert.ToBase64String(CalculateHmac(joined, p))); }
private static byte[] CalculateHmac(byte[] data, YcmdProcess p) { HMAC h = new HMACSHA256(p.secret_key); return(h.ComputeHash(data)); }
private static void WriteHmacToMs(byte[] data, Stream s, YcmdProcess p) { byte[] buf = CalculateHmac(data, p); s.Write(buf, 0, buf.Length); }
public static T SendYcmdRequest <T>(string path, object requestData, YcmdProcess p) { string jsonString = SendYcmdRequestRaw(path, requestData, p); return(JsonConvert.DeserializeObject <T>(jsonString)); }
public static string GenerateUri(string pathname, YcmdProcess p) { return("http://" + YCMD_HOSTNAME + ":" + p.port.ToString() + pathname); }
public static CompletionResponse GetCodeComplete(string filename, int col, int line, string data, YcmdProcesses p, out string commands) { //Create the request data to send. SimpleRequest req = new SimpleRequest(); req.column_num = col; req.line_num = line; req.filepath = filename; req.force_semantic = true; req.file_data = new Dictionary <string, FileData>(); req.file_data.Add(filename, GenerateFileData(data)); //Send this data to the server and get a reply. CompletionResponse reply = YcmdController.SendYcmdRequest <CompletionResponse>("/completions", req, YcmdProcess.GetServer(p)); commands = YcmdController.SendYcmdRequestRaw("/defined_subcommands", req, YcmdProcess.GetServer(p)); return(reply); }