/// <summary> /// RequestAPI implementation /// </summary> /// <param name="uri">full uri</param> /// <param name="method">using method for HTTP negotiation</param> /// <param name="param">additional parameters</param> /// <param name="request">used request</param> /// <returns>XML documents</returns> public sealed override XDocument RequestAPI(string uri, RequestMethod method, IEnumerable <KeyValuePair <string, string> > param, out HttpWebRequest request) { // validate arguments if (String.IsNullOrEmpty(uri)) { throw new ArgumentNullException(uri); } else if (uri.Length < 5) { throw new ArgumentException("uri is too short."); } string target = uri; // detection return type of api var docType = DocumentTypes.Invalid; if (target.EndsWith("xml")) { docType = DocumentTypes.Xml; } else if (target.EndsWith("json")) { docType = DocumentTypes.Json; } else { throw new ArgumentException("format can't identify. uriPartial is must ends with .xml or .json."); } // pre-validation authentication if (String.IsNullOrEmpty(Token) || String.IsNullOrEmpty(Secret)) { throw new WebException("OAuth Not Validated", WebExceptionStatus.ProtocolError); } var reg = GetHeader(target, method, param); try { var ps = JoinParamAsUrl(param); byte[] body = null; if (!String.IsNullOrWhiteSpace(ps)) { if (method == RequestMethod.GET) { target += "?" + ps; } if (method == RequestMethod.POST) { body = Encoding.ASCII.GetBytes(ps); } } request = Http.CreateRequest(new Uri(target), method.ToString(), contentType: "application/x-www-form-urlencoded"); request.Headers.Add("Authorization", "OAuth " + reg); var ret = Http.WebConnect <XDocument>(request, responseconv: res => { switch (docType) { case DocumentTypes.Xml: return(this.XDocumentGenerator(res)); case DocumentTypes.Json: return(this.XDocumentGenerator(res, (s) => JsonReaderWriterFactory.CreateJsonReader(s, XmlDictionaryReaderQuotas.Max))); default: throw new NotSupportedException("Invalid format."); } }, senddata: body); if (ret.Succeeded) { return(ret.Data); } else { if (ret.ThrownException != null) { throw ret.ThrownException; } else { throw new WebException(); } } } catch (WebException) { throw; } catch (XmlException) { throw; } catch (IOException) { throw; } }
private string GetReferrer(string URL, string Username, string Password, string Link, string Field) { string error = ""; if (Link == null) { return(""); } try { using (var client = new HttpClient()) { client.BaseAddress = new Uri(URL); var byteArray = Encoding.ASCII.GetBytes(Username + ":" + Password); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); var message = new HttpRequestMessage { //Content = new StringContent(json, Encoding.UTF8, "application/json") }; message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage post = client.GetAsync(Link.Substring(Link.IndexOf("/api/now"))).Result; var content = post.Content.ReadAsStringAsync(); if (post.IsSuccessStatusCode) { XDocument xml = XDocument.Load(JsonReaderWriterFactory.CreateJsonReader(Encoding.ASCII.GetBytes(content.Result), new XmlDictionaryReaderQuotas())); foreach (XElement xe in xml.Descendants("result")) { if (xe.Element(Field) != null) { if (xe.Element(Field).HasElements) { error = xe.Element(Field).Element("link").Value; } else { error = xe.Element(Field).Value; } } } } else if (String.IsNullOrWhiteSpace(content.Result) == false) { error = "!" + content.Result; } else { error = "!" + post.ReasonPhrase + " (" + (int)post.StatusCode + ")"; } } } catch (Exception ex) { if (error == "") { error = "!"; } error += ex.Message; while (ex.InnerException != null) { ex = ex.InnerException; error += " ~ " + ex.Message; } } return(error); }
/// <summary> /// Send a procedure call over the wire. /// </summary> /// <param name="receiver"> /// Identifier of remote receiving object. /// </param> /// <param name="name"> /// Name of procedure /// </param> /// <param name="args"> /// Array of arguments to the call. /// </param> /// <param name="wait"> /// True to wait for a result, false to return /// done immediately. /// </param> public GraceObject SendCall(int receiver, string name, object[] args, bool wait) { if (hardStop) { Environment.Exit(0); } if (stop) { return(GraceObject.Done); } int theKey = callKey++; var stream = new MemoryStream(10240); var jsonWriter = JsonReaderWriterFactory.CreateJsonWriter( stream ); var xmlDoc = new XmlDocument(); var root = xmlDoc.CreateElement("root"); root.SetAttribute("type", "object"); xmlDoc.AppendChild(root); var mode = xmlDoc.CreateElement("mode"); var tn = xmlDoc.CreateTextNode("call"); mode.AppendChild(tn); root.AppendChild(mode); var reply = xmlDoc.CreateElement("noreply"); reply.SetAttribute("type", "boolean"); tn = xmlDoc.CreateTextNode(wait ? "false" : "true"); reply.AppendChild(tn); root.AppendChild(reply); var rec = xmlDoc.CreateElement("receiver"); rec.SetAttribute("type", "number"); tn = xmlDoc.CreateTextNode("" + receiver); rec.AppendChild(tn); root.AppendChild(rec); var key = xmlDoc.CreateElement("key"); key.SetAttribute("type", "number"); tn = xmlDoc.CreateTextNode("" + theKey); key.AppendChild(tn); root.AppendChild(key); var nameEl = xmlDoc.CreateElement("name"); tn = xmlDoc.CreateTextNode("" + name); nameEl.AppendChild(tn); root.AppendChild(nameEl); var arguments = xmlDoc.CreateElement("args"); arguments.SetAttribute("type", "array"); foreach (var a in args) { var item = xmlDoc.CreateElement("item"); arguments.AppendChild(item); if (a is string) { tn = xmlDoc.CreateTextNode((string)a); item.AppendChild(tn); } else if (a is GraceNumber) { tn = xmlDoc.CreateTextNode(((GraceNumber)a).Double .ToString()); item.AppendChild(tn); item.SetAttribute("type", "number"); } else if (a is GraceBlock) { var b = (GraceBlock)a; int id = getBlockId(b); item.SetAttribute("type", "object"); var obj = xmlDoc.CreateElement("callback"); tn = xmlDoc.CreateTextNode("" + id); obj.AppendChild(tn); item.AppendChild(obj); } else if (a is int[]) { var id = ((int[])a)[0]; item.SetAttribute("type", "object"); var obj = xmlDoc.CreateElement("key"); tn = xmlDoc.CreateTextNode("" + id); obj.AppendChild(tn); item.AppendChild(obj); } } root.AppendChild(arguments); #if DEBUG_WS Console.WriteLine(xmlDoc.OuterXml); #endif xmlDoc.WriteTo(jsonWriter); jsonWriter.Flush(); #if DEBUG_WS Console.WriteLine( "Capacity = {0}, Length = {1}, Position = {2}\n", stream.Capacity.ToString(), stream.Length.ToString(), stream.Position.ToString()); #endif stream.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(stream); EventWaitHandle handle; if (wait) { handle = new EventWaitHandle(false, EventResetMode.ManualReset); WebSocketEndpoint.AddEventHandle(theKey, handle); wss.Send(reader.ReadToEnd()); handle.WaitOne(); return(WebSocketEndpoint.GetEventResult(theKey)); } wss.Send(reader.ReadToEnd()); return(GraceObject.Done); }
// Internal for tests public void WriteBootJson(Stream output, string entryAssemblyName) { var icuDataMode = ICUDataMode.Sharded; if (string.Equals(InvariantGlobalization, "true", StringComparison.OrdinalIgnoreCase)) { icuDataMode = ICUDataMode.Invariant; } else if (LoadAllICUData) { icuDataMode = ICUDataMode.All; } var result = new BootJsonData { entryAssembly = entryAssemblyName, cacheBootResources = CacheBootResources, debugBuild = DebugBuild, linkerEnabled = LinkerEnabled, resources = new ResourcesData(), config = new List<string>(), icuDataMode = icuDataMode, }; // Build a two-level dictionary of the form: // - assembly: // - UriPath (e.g., "System.Text.Json.dll") // - ContentHash (e.g., "4548fa2e9cf52986") // - runtime: // - UriPath (e.g., "dotnet.js") // - ContentHash (e.g., "3448f339acf512448") if (Resources != null) { var resourceData = result.resources; foreach (var resource in Resources) { ResourceHashesByNameDictionary resourceList; var fileName = resource.GetMetadata("FileName"); var extension = resource.GetMetadata("Extension"); var resourceCulture = resource.GetMetadata("Culture"); var assetType = resource.GetMetadata("AssetType"); var resourceName = $"{fileName}{extension}"; if (IsLazyLoadedAssembly(resourceName)) { resourceData.lazyAssembly ??= new ResourceHashesByNameDictionary(); resourceList = resourceData.lazyAssembly; } else if (!string.IsNullOrEmpty(resourceCulture)) { resourceData.satelliteResources ??= new Dictionary<string, ResourceHashesByNameDictionary>(StringComparer.OrdinalIgnoreCase); resourceName = resourceCulture + "/" + resourceName; if (!resourceData.satelliteResources.TryGetValue(resourceCulture, out resourceList)) { resourceList = new ResourceHashesByNameDictionary(); resourceData.satelliteResources.Add(resourceCulture, resourceList); } } else if (string.Equals(extension, ".pdb", StringComparison.OrdinalIgnoreCase)) { resourceData.pdb ??= new ResourceHashesByNameDictionary(); if (IsLazyLoadedAssembly($"{fileName}.dll")) { resourceList = resourceData.lazyAssembly; } else { resourceList = resourceData.pdb; } } else if (string.Equals(extension, ".dll", StringComparison.OrdinalIgnoreCase)) { resourceList = resourceData.assembly; } else if (string.Equals(assetType, "native", StringComparison.OrdinalIgnoreCase)) { resourceList = resourceData.runtime; } else { // This should include items such as XML doc files, which do not need to be recorded in the manifest. continue; } if (!resourceList.ContainsKey(resourceName)) { resourceList.Add(resourceName, $"sha256-{resource.GetMetadata("FileHash")}"); } } } if (ConfigurationFiles != null) { foreach (var configFile in ConfigurationFiles) { result.config.Add(Path.GetFileName(configFile.ItemSpec)); } } var serializer = new DataContractJsonSerializer(typeof(BootJsonData), new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true }); using var writer = JsonReaderWriterFactory.CreateJsonWriter(output, Encoding.UTF8, ownsStream: false, indent: true); serializer.WriteObject(writer, result); }
/// <summary> /// Reads the httpRequest in the operation context and casts the respose to JSON /// if the Accept header is set to application/json /// </summary> /// <param name="reply"></param> /// <param name="correlationState"></param> public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState) { var ctx = OperationContext.Current.Extensions.Find <RequestContext>(); HttpResponseMessageProperty prop = new HttpResponseMessageProperty(); reply.Properties.Add(HttpResponseMessageProperty.Name, prop); var httpStatus = 200; var httpStatusDescription = string.Empty; // Check status code var statusProperty = reply.Properties.FirstOrDefault <KeyValuePair <string, object> >(p => p.Key == MessageHelper.HttpStatusCode); if (statusProperty.Value != null) { httpStatus = int.Parse(statusProperty.Value.ToString()); prop.StatusCode = (HttpStatusCode)httpStatus; } // Check status description var statusDescriptionProperty = reply.Properties.FirstOrDefault <KeyValuePair <string, object> >(p => p.Key == MessageHelper.HttpStatusDescription); if (statusDescriptionProperty.Value != null) { httpStatusDescription = statusDescriptionProperty.Value.ToString(); prop.StatusDescription = httpStatusDescription; } if (ctx != null && ctx.RequestHeader.Headers["Accept"] != null && ctx.RequestHeader.Headers["Accept"].ToString().ToLower().Contains("application/json")) { XmlDictionaryReader dicReader = reply.GetReaderAtBodyContents(); // Remove namespaces from json XmlDocument doc = new XmlDocument(); doc.Load(dicReader); XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(doc.OuterXml)); string xml = xmlDocumentWithoutNs.ToString(); doc.LoadXml(xml); string jsonString = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.None, true); byte[] jsonReplyBytes = Encoding.UTF8.GetBytes(jsonString); XmlDictionaryReader newReplyBodyReader = JsonReaderWriterFactory.CreateJsonReader(jsonReplyBytes, XmlDictionaryReaderQuotas.Max); Message newReply = Message.CreateMessage(MessageVersion.None, null, newReplyBodyReader); newReply.Properties.Add("Content-Type", "application/json;charset=utf-8"); newReply.Properties.Add("Accept", "application/json;charset=utf-8"); // Set the outgoing Content-Type to application/json newReply.Properties.Add(HttpResponseMessageProperty.Name, prop); prop.Headers.Add("Content-Type", "application/json;charset=utf-8"); prop.StatusCode = (HttpStatusCode)httpStatus; prop.StatusDescription = httpStatusDescription; WebBodyFormatMessageProperty bodyFormat = new WebBodyFormatMessageProperty(WebContentFormat.Json); newReply.Properties.Add(WebBodyFormatMessageProperty.Name, bodyFormat); WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json; reply = newReply; } OperationContext.Current.Extensions.Remove(ctx); }
public void ConstructorNullReaderQuotas() { JsonReaderWriterFactory.CreateJsonReader(GetInput("{}"), null); }
/// <summary> /// 将 Json 转换成动态类型 /// </summary> /// <param name="json"></param> /// <param name="encoding"></param> /// <returns></returns> public static dynamic Parse(string json, Encoding encoding) { using var reader = JsonReaderWriterFactory.CreateJsonReader(encoding.GetBytes(json), XmlDictionaryReaderQuotas.Max); return(ToValue(XElement.Load(reader))); }
internal void UpdateTask() { Global.UpdateModel = null; if (!UserSettings.All.CheckForUpdates) { return; } #if !UWP try { var request = (HttpWebRequest)WebRequest.Create("https://api.github.com/repos/NickeManarin/ScreenToGif/releases/latest"); request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"; request.Proxy = WebHelper.GetProxy(); var response = (HttpWebResponse)request.GetResponse(); using (var resultStream = response.GetResponseStream()) { if (resultStream == null) { return; } using (var reader = new StreamReader(resultStream)) { var result = reader.ReadToEnd(); var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(result), new System.Xml.XmlDictionaryReaderQuotas()); var release = XElement.Load(jsonReader); var version = Version.Parse(release.XPathSelectElement("tag_name")?.Value ?? "0.1"); if (version.Major == 0 || version <= Assembly.GetExecutingAssembly().GetName().Version) { return; } Global.UpdateModel = new UpdateModel { Version = version, Description = release.XPathSelectElement("body")?.Value ?? "", InstallerDownloadUrl = release.XPathSelectElement("assets/item[2]/browser_download_url")?.Value ?? "", InstallerSize = Convert.ToInt64(release.XPathSelectElement("assets/item[2]/size")?.Value ?? "0"), PortableDownloadUrl = release.XPathSelectElement("assets/item[1]/browser_download_url")?.Value ?? "", PortableSize = Convert.ToInt64(release.XPathSelectElement("assets/item[1]/size")?.Value ?? "0") }; Application.Current.Dispatcher.Invoke(() => NotificationManager.AddNotification(string.Format(LocalizationHelper.Get("Update.NewRelease.Verbose"), Global.UpdateModel.Version), StatusType.Update, "update", UpdateAction)); } } } catch (Exception ex) { LogWriter.Log(ex, "Check for update task"); } #endif GC.Collect(); }
private async Task <bool> CheckOnGithub() { try { //If the app was installed by Chocolatey, avoid this. if (AppDomain.CurrentDomain.BaseDirectory.EndsWith(@"Chocolatey\lib\screentogif\content\")) { return(true); } #region GraphQL equivalent //query { // repository(owner: "NickeManarin", name: "ScreenToGif") { // releases(first: 1, orderBy: { field: CREATED_AT, direction: DESC}) { // nodes { // name // tagName // createdAt // url // isPrerelease // description // releaseAssets(last: 2) { // nodes { // name // downloadCount // downloadUrl // size // } // } // } // } // } //} #endregion var request = (HttpWebRequest)WebRequest.Create("https://api.github.com/repos/NickeManarin/ScreenToGif/releases/latest"); request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"; request.Proxy = WebHelper.GetProxy(); var response = (HttpWebResponse)await request.GetResponseAsync(); using (var resultStream = response.GetResponseStream()) { if (resultStream == null) { return(false); } using (var reader = new StreamReader(resultStream)) { var result = await reader.ReadToEndAsync(); var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(result), new System.Xml.XmlDictionaryReaderQuotas()); var release = XElement.Load(jsonReader); var version = Version.Parse(release.XPathSelectElement("tag_name")?.Value ?? "0.1"); if (version.Major == 0 || version <= Assembly.GetExecutingAssembly().GetName().Version) { return(true); } Global.UpdateAvailable = new UpdateAvailable { Version = version, Description = release.XPathSelectElement("body")?.Value ?? "", PortableDownloadUrl = release.XPathSelectElement("assets/item[1]/browser_download_url")?.Value ?? "", PortableSize = Convert.ToInt64(release.XPathSelectElement("assets/item[1]/size")?.Value ?? "0"), PortableName = release.XPathSelectElement("assets/item[1]/name")?.Value ?? "ScreenToGif.zip", InstallerDownloadUrl = release.XPathSelectElement("assets/item[2]/browser_download_url")?.Value ?? "", InstallerSize = Convert.ToInt64(release.XPathSelectElement("assets/item[2]/size")?.Value ?? "0"), InstallerName = release.XPathSelectElement("assets/item[2]/name")?.Value ?? "ScreenToGif.Setup.msi", }; Application.Current.Dispatcher?.BeginInvoke(new Action(() => NotificationManager.AddNotification(string.Format(LocalizationHelper.Get("S.Updater.NewRelease.Info"), Global.UpdateAvailable.Version), StatusType.Update, "update", PromptUpdate))); //Download update to be installed when the app closes. if (UserSettings.All.InstallUpdates && !string.IsNullOrEmpty(Global.UpdateAvailable.InstallerDownloadUrl)) { await DownloadUpdate(); } } } return(true); } catch (Exception ex) { LogWriter.Log(ex, "Impossible to check for updates on Github"); return(false); } finally { GC.Collect(); } }
public override ArraySegment <byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset) { ArraySegment <byte> byteArray = new ArraySegment <byte>(); MemoryStream stream = new MemoryStream(); StreamWriter sw = new StreamWriter(stream); XmlWriter writer = null; byte[] messageBytes = null; int messageLength = 0; int totalLength = 0; byte[] totalBytes = null; string methodName = null; string headerType = null; if (message.Properties.ContainsKey(JSONPMessageProperty.Name)) { methodName = ((JSONPMessageProperty)(message.Properties[JSONPMessageProperty.Name])).MethodName; headerType = ((JSONPMessageProperty)(message.Properties[JSONPMessageProperty.Name])).HeaderType; } if (headerType == null) { writer = XmlWriter.Create(sw); message.WriteMessage(writer); writer.Close(); messageBytes = stream.GetBuffer(); messageLength = (int)stream.Position; stream.Close(); totalLength = messageLength + messageOffset; totalBytes = bufferManager.TakeBuffer(totalLength); Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength); byteArray = new ArraySegment <byte>(totalBytes, messageOffset, messageLength); } else { if (methodName == null) { if (headerType.Contains("text/xml") || headerType.Contains("application/xml")) { writer = XmlWriter.Create(sw); message.WriteMessage(writer); writer.Close(); messageBytes = stream.GetBuffer(); messageLength = (int)stream.Position; stream.Close(); totalLength = messageLength + messageOffset; totalBytes = bufferManager.TakeBuffer(totalLength); Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength); byteArray = new ArraySegment <byte>(totalBytes, messageOffset, messageLength); } else if (headerType.Contains("application/json")) { writer = JsonReaderWriterFactory.CreateJsonWriter(stream); message.WriteMessage(writer); writer.Flush(); messageBytes = stream.GetBuffer(); messageLength = (int)stream.Position; stream.Close(); writer.Close(); totalLength = messageLength + messageOffset; totalBytes = bufferManager.TakeBuffer(totalLength); Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength); byteArray = new ArraySegment <byte>(totalBytes, messageOffset, messageLength); } } else //if there is a JSONP request - check header { sw.Write(methodName + "( "); sw.Flush(); writer = JsonReaderWriterFactory.CreateJsonWriter(stream); message.WriteMessage(writer); writer.Flush(); sw.Write(" );"); sw.Flush(); messageBytes = stream.GetBuffer(); messageLength = (int)stream.Position; totalLength = messageLength + messageOffset; totalBytes = bufferManager.TakeBuffer(totalLength); Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength); byteArray = new ArraySegment <byte>(totalBytes, messageOffset, messageLength); writer.Close(); } } return(byteArray); }
static IEnumerable <string> GetModifiedFilesRemotely(Harness harness, int pull_request) { var path = Path.Combine(harness.LogDirectory, "pr" + pull_request + "-remote-files.log"); if (!File.Exists(path)) { Directory.CreateDirectory(harness.LogDirectory); using (var client = CreateClient()) { var rv = new List <string> (); var url = $"https://api.github.com/repos/xamarin/xamarin-macios/pulls/{pull_request}/files?per_page=100"; // 100 items per page is max do { byte [] data; try { data = client.DownloadData(url); } catch (WebException we) { harness.Log("Could not load pull request files: {0}\n{1}", we, new StreamReader(we.Response.GetResponseStream()).ReadToEnd()); File.WriteAllText(path, string.Empty); return(new string [] { }); } var reader = JsonReaderWriterFactory.CreateJsonReader(data, new XmlDictionaryReaderQuotas()); var doc = new XmlDocument(); doc.Load(reader); foreach (XmlNode node in doc.SelectNodes("/root/item/filename")) { rv.Add(node.InnerText); } url = null; var link = client.ResponseHeaders ["Link"]; try { if (link != null) { var ltIdx = link.IndexOf('<'); var gtIdx = link.IndexOf('>', ltIdx + 1); while (ltIdx >= 0 && gtIdx > ltIdx) { var linkUrl = link.Substring(ltIdx + 1, gtIdx - ltIdx - 1); if (link [gtIdx + 1] != ';') { break; } var commaIdx = link.IndexOf(',', gtIdx + 1); string rel; if (commaIdx != -1) { rel = link.Substring(gtIdx + 3, commaIdx - gtIdx - 3); } else { rel = link.Substring(gtIdx + 3); } if (rel == "rel=\"next\"") { url = linkUrl; break; } if (commaIdx == -1) { break; } ltIdx = link.IndexOf('<', commaIdx); gtIdx = link.IndexOf('>', ltIdx + 1); } } } catch (Exception e) { harness.Log("Could not paginate github response: {0}: {1}", link, e.Message); } } while (url != null); File.WriteAllLines(path, rv.ToArray()); return(rv); } } return(File.ReadAllLines(path)); }
public async Task UpdatePackageInfoAsync(PipPackageView package, CancellationToken cancel) { string description = null; List <string> versions = null; using (var client = new WebClient()) { Stream data; try { data = await client.OpenReadTaskAsync(new Uri(_index ?? DefaultIndex, package.Name + "/json")); } catch (WebException) { // No net access return; } try { using (var reader = JsonReaderWriterFactory.CreateJsonReader(data, new XmlDictionaryReaderQuotas())) { var doc = XDocument.Load(reader); // TODO: Get package URL //url = (string)doc.Document // .Elements("root") // .Elements("info") // .Elements("package_url") // .FirstOrDefault(); description = (string)doc.Document .Elements("root") .Elements("info") .Elements("description") .FirstOrDefault(); versions = doc.Document .Elements("root") .Elements("releases") .Elements() .Attributes("item") .Select(a => a.Value) .ToList(); } } catch (InvalidOperationException) { } } cancel.ThrowIfCancellationRequested(); bool changed = false; await _cacheLock.WaitAsync(); try { PipPackageView inCache; if (!_cache.TryGetValue(package.Name, out inCache)) { inCache = _cache[package.Name] = new PipPackageView(this, package.Name, null, null); } if (!string.IsNullOrEmpty(description)) { var lines = description.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); var firstLine = string.Join( " ", lines.TakeWhile(s => !IsSeparatorLine(s)).Select(s => s.Trim()) ); if (firstLine.Length > 500) { firstLine = firstLine.Substring(0, 497) + "..."; } if (firstLine == "UNKNOWN") { firstLine = string.Empty; } inCache.Description = firstLine; package.Description = firstLine; changed = true; } if (versions != null) { var updateVersion = Pep440Version.TryParseAll(versions) .Where(v => v.IsFinalRelease) .OrderByDescending(v => v) .FirstOrDefault(); inCache.UpgradeVersion = updateVersion; package.UpgradeVersion = updateVersion; changed = true; } } finally { _cacheLock.Release(); } if (changed) { TriggerWriteCacheToDisk(); } }
public static WeatherForecast GetForecast(string area) { WeatherForecast f = new WeatherForecast(); try { string json = GetJson(area, "forecast"); if (!json.Equals(string.Empty) && !json.Contains("error data")) { XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(json), XmlDictionaryReaderQuotas.Max); XmlDocument xdoc = new XmlDocument(); xdoc.Load(reader); Log.WriteDebug("GetForecast>xdoc>>>>>" + xdoc.InnerText); #region 输出完整的XML,分析格式 //StringWriter stringWriter = new StringWriter(); //XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter); //xmlTextWriter.Formatting = Formatting.Indented; //foreach (XmlNode xmlNode in xdoc) //{ // xmlNode.WriteTo(xmlTextWriter); //} //Log.WriteLog(stringWriter.ToString()); #endregion #region 用xml解析json,并为相关的成员变量赋值 ///获取到的属性信息 XmlNode info = xdoc.FirstChild.SelectSingleNode("c"); f.AreaID = info.SelectSingleNode("c1").InnerText; f.ENName = info.SelectSingleNode("c2").InnerText; f.CNName = info.SelectSingleNode("c3").InnerText; f.ENCityName = info.SelectSingleNode("c4").InnerText; f.CNCityName = info.SelectSingleNode("c5").InnerText; f.ENProvName = info.SelectSingleNode("c6").InnerText; f.CNProvName = info.SelectSingleNode("c7").InnerText; f.ENCountryName = info.SelectSingleNode("c8").InnerText; f.CNCountryName = info.SelectSingleNode("c9").InnerText; f.CityLevel = uint.Parse(info.SelectSingleNode("c10").InnerText); f.CityAreaCode = info.SelectSingleNode("c11").InnerText; f.PostCode = uint.Parse(info.SelectSingleNode("c12").InnerText); f.Longitude = float.Parse(info.SelectSingleNode("c13").InnerText); f.Latitude = float.Parse(info.SelectSingleNode("c14").InnerText); f.Altitude = int.Parse(info.SelectSingleNode("c15").InnerText); f.RadarStationNo = info.SelectSingleNode("c16").InnerText; ///f0 数据发布时间 IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true); f.PublishTime = System.DateTime.ParseExact(xdoc.FirstChild.SelectSingleNode("f").SelectSingleNode("f0").FirstChild.InnerText, "yyyyMMddHHmm", culture); ///第一天 XmlNode DayOne = xdoc.FirstChild.SelectSingleNode("f").SelectSingleNode("f1").FirstChild; f.DayWeatherCodeOne = DayOne.SelectSingleNode("fa").InnerText; f.NightWeatherCodeOne = DayOne.SelectSingleNode("fb").InnerText; f.DayTemperatureOne = DayOne.SelectSingleNode("fc").InnerText; f.NightTemperatureOne = DayOne.SelectSingleNode("fd").InnerText; f.DayWindDirectionOne = DayOne.SelectSingleNode("fe").InnerText; f.NightWindDirectionOne = DayOne.SelectSingleNode("ff").InnerText; f.DayWindForceOne = DayOne.SelectSingleNode("fg").InnerText; f.NightWindForceOne = DayOne.SelectSingleNode("fh").InnerText; f.SunriseTimeOne = DayOne.SelectSingleNode("fi").InnerText.Split('|')[0]; f.SunsetTimeOne = DayOne.SelectSingleNode("fi").InnerText.Split('|')[1]; ///第二天 XmlNode DayTwo = DayOne.NextSibling; f.DayWeatherCodeTwo = DayTwo.SelectSingleNode("fa").InnerText; f.NightWeatherCodeTwo = DayTwo.SelectSingleNode("fb").InnerText; f.DayTemperatureTwo = DayTwo.SelectSingleNode("fc").InnerText; f.NightTemperatureTwo = DayTwo.SelectSingleNode("fd").InnerText; f.DayWindDirectionTwo = DayTwo.SelectSingleNode("fe").InnerText; f.NightWindDirectionTwo = DayTwo.SelectSingleNode("ff").InnerText; f.DayWindForceTwo = DayTwo.SelectSingleNode("fg").InnerText; f.NightWindForceTwo = DayTwo.SelectSingleNode("fh").InnerText; f.SunriseTimeTwo = DayTwo.SelectSingleNode("fi").InnerText.Split('|')[0]; f.SunsetTimeTwo = DayTwo.SelectSingleNode("fi").InnerText.Split('|')[1]; ///第三天 XmlNode DayThree = DayTwo.NextSibling; f.DayWeatherCodeThree = DayThree.SelectSingleNode("fa").InnerText; f.NightWeatherCodeThree = DayThree.SelectSingleNode("fb").InnerText; f.DayTemperatureThree = DayThree.SelectSingleNode("fc").InnerText; f.NightTemperatureThree = DayThree.SelectSingleNode("fd").InnerText; f.DayWindDirectionThree = DayThree.SelectSingleNode("fe").InnerText; f.NightWindDirectionThree = DayThree.SelectSingleNode("ff").InnerText; f.DayWindForceThree = DayThree.SelectSingleNode("fg").InnerText; f.NightWindForceThree = DayThree.SelectSingleNode("fh").InnerText; f.SunriseTimeThree = DayThree.SelectSingleNode("fi").InnerText.Split('|')[0]; f.SunsetTimeThree = DayThree.SelectSingleNode("fi").InnerText.Split('|')[1]; #endregion } } catch (Exception ex) { Log.WriteError("GetForecast错误>>>>>" + ex.Message); } return(f); }
public static WeatherIndex GetIndex(string area) { WeatherIndex i = new WeatherIndex(); try { string json = GetJson(area, "index"); if (!json.Equals(string.Empty) && !json.Contains("error data")) { XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(json), XmlDictionaryReaderQuotas.Max); XmlDocument xdoc = new XmlDocument(); xdoc.Load(reader); Log.WriteDebug(xdoc.InnerText); #region 输出完整的XML,分析格式 //StringWriter stringWriter = new StringWriter(); //XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter); //xmlTextWriter.Formatting = Formatting.Indented; //foreach (XmlNode xmlNode in xdoc) //{ // xmlNode.WriteTo(xmlTextWriter); //} //Log.WriteLog(stringWriter.ToString()); #endregion #region 用xml解析json,并为相关的成员变量赋值 try { ///获取到的属性信息 XmlNode clIndex = xdoc.FirstChild.SelectSingleNode("i").FirstChild; i.cl = clIndex.SelectSingleNode("i1").InnerText; i.clCN = clIndex.SelectSingleNode("i2").InnerText; i.clCNAlias = clIndex.SelectSingleNode("i3").InnerText; i.clLevel = clIndex.SelectSingleNode("i4").InnerText; i.clDetails = clIndex.SelectSingleNode("i5").InnerText; XmlNode coIndex = clIndex.NextSibling; i.co = coIndex.SelectSingleNode("i1").InnerText; i.coCN = coIndex.SelectSingleNode("i2").InnerText; i.coCNAlias = coIndex.SelectSingleNode("i3").InnerText; i.coLevel = coIndex.SelectSingleNode("i4").InnerText; i.coDetails = coIndex.SelectSingleNode("i5").InnerText; XmlNode ctIndex = coIndex.NextSibling; i.ct = ctIndex.SelectSingleNode("i1").InnerText; i.ctCN = ctIndex.SelectSingleNode("i2").InnerText; i.ctCNAlias = ctIndex.SelectSingleNode("i3").InnerText; i.ctLevel = ctIndex.SelectSingleNode("i4").InnerText; i.ctDetails = ctIndex.SelectSingleNode("i5").InnerText; } catch (Exception ex) { Log.WriteError("GetIndex>>>>>" + ex.Message); } #endregion } } catch (Exception ex) { Log.WriteError("GetIndex错误>>>>>" + ex.Message); } return(i); }
public void ConstructorNullBytes() { JsonReaderWriterFactory.CreateJsonReader((byte [])null, new XmlDictionaryReaderQuotas()); }
protected override void DownloadHTMLPage() { List <string> thumbs = new List <string>(); string htmlPage = ""; string str = ""; string baseURL = "//i.4cdn.org/" + URL.Split('/')[3] + "/"; string JURL = "http://a.4cdn.org/" + URL.Split('/')[3] + "/thread/" + URL.Split('/')[5] + ".json"; try { //Add a UserAgent to prevent 403 using (var web = new WebClient()) { web.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"; htmlPage = web.DownloadString(URL); //Prevent the html from being destroyed by the anti adblock script htmlPage = htmlPage.Replace("f=\"to\"", "f=\"penis\""); string json = web.DownloadString(JURL); byte[] bytes = Encoding.ASCII.GetBytes(json); using (var stream = new MemoryStream(bytes)) { var quotas = new XmlDictionaryReaderQuotas(); var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, quotas); var xml = XDocument.Load(jsonReader); str = xml.ToString(); } } XmlDocument doc = new XmlDocument(); doc.LoadXml(str); XmlNodeList xmlTim = doc.DocumentElement.SelectNodes("/root/posts/item/tim"); XmlNodeList xmlExt = doc.DocumentElement.SelectNodes("/root/posts/item/ext"); XmlNodeList xmlFilenames = doc.DocumentElement.SelectNodes("/root/posts/item/filename"); for (int i = 0; i < xmlExt.Count; i++) { string old = baseURL + xmlTim[i].InnerText + xmlExt[i].InnerText; string rep = xmlTim[i].InnerText + xmlExt[i].InnerText; htmlPage = htmlPage.Replace(old, rep); //get the actual filename saved string filename = Path.GetFileNameWithoutExtension(new ImageLink(baseURL + xmlTim[i].InnerText + xmlExt[i].InnerText, xmlFilenames[i].InnerText).GenerateNewFilename((ImageFileNameFormat)Properties.Settings.Default.imageFilenameFormat)); //Save thumbs for files that need it if (rep.Split('.')[1] == "webm" /*|| rep.Split('.')[1] == ""*/) { old = "//t.4cdn.org/" + URL.Split('/')[3] + "/" + xmlTim[i].InnerText + "s.jpg"; thumbs.Add("http:" + old); htmlPage = htmlPage.Replace(xmlTim[i].InnerText, filename); htmlPage = htmlPage.Replace("//i.4cdn.org/" + URL.Split('/')[3] + "/" + filename, "thumb/" + xmlTim[i].InnerText); } else { string thumbName = rep.Split('.')[0] + "s"; htmlPage = htmlPage.Replace(thumbName + ".jpg", rep.Split('.')[0] + "." + rep.Split('.')[1]); htmlPage = htmlPage.Replace("/" + thumbName, thumbName); htmlPage = htmlPage.Replace("//i.4cdn.org/" + URL.Split('/')[3] + "/" + xmlTim[i].InnerText, xmlTim[i].InnerText); htmlPage = htmlPage.Replace(xmlTim[i].InnerText, filename); //easy fix for images } htmlPage = htmlPage.Replace("//is2.4chan.org/" + URL.Split('/')[3] + "/" + xmlTim[i].InnerText, xmlTim[i].InnerText); //bandaid fix for is2 urls htmlPage = htmlPage.Replace("/" + rep, rep); } htmlPage = htmlPage.Replace("=\"//", "=\"http://"); //Save thumbs for files that need it for (int i = 0; i < thumbs.Count; i++) { Utils.DownloadToDir(thumbs[i], SaveTo + "\\thumb"); } if (!string.IsNullOrWhiteSpace(htmlPage)) { File.WriteAllText(SaveTo + "\\Thread.html", htmlPage); } } catch (Exception ex) { Program.Log(ex); } }
public void ConstructorNullStream() { JsonReaderWriterFactory.CreateJsonReader((Stream)null, new XmlDictionaryReaderQuotas()); }
public string GetDotNetExecutable(string directory) { if (directory == null) { throw new ArgumentNullException(nameof(directory)); } lock (dotnet_executables) { if (dotnet_executables.TryGetValue(directory, out var value)) { return(value); } } // Find the first global.json up the directory hierarchy (stopping at the root directory) string global_json = null; var dir = directory; while (dir.Length > 2) { global_json = Path.Combine(dir, "global.json"); if (File.Exists(global_json)) { break; } dir = Path.GetDirectoryName(dir); } if (!File.Exists(global_json)) { throw new Exception($"Could not find any global.json file in {directory} or above"); } // Parse the global.json we found, and figure out if it tells us to use .NET 3.1.100 / 5.X.XXX or not. var contents = File.ReadAllBytes(global_json); using var reader = JsonReaderWriterFactory.CreateJsonReader(contents, new XmlDictionaryReaderQuotas()); var doc = new XmlDocument(); doc.Load(reader); var version = doc.SelectSingleNode("/root/sdk").InnerText; string executable; switch (version [0]) { case '3': case '5': executable = dotnetPath; break; default: executable = dotnet6Path; break; } getLog()?.WriteLine($"Mapped .NET SDK version {version} to {executable} for {directory}"); lock (dotnet_executables) { dotnet_executables [directory] = executable; } return(executable); }
public void ConstructorNullEncodingAndReaderClose() { JsonReaderWriterFactory.CreateJsonReader(GetInput("{}"), null, new XmlDictionaryReaderQuotas(), null); }
public PowerBISettings(string settingsFilePath = null) { if (string.IsNullOrEmpty(settingsFilePath)) { var executingDirectory = DirectoryUtility.GetExecutingDirectory(); settingsFilePath = Path.Combine(executingDirectory, FileName); } if (!File.Exists(settingsFilePath)) { throw new FileNotFoundException("Unable to find setting configuration", settingsFilePath); } PowerBIConfiguration configuration = null; using (var fileReader = File.OpenRead(settingsFilePath)) { var serializer = new DataContractJsonSerializer(typeof(PowerBIConfiguration)); using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(fileReader, Encoding.UTF8, XmlDictionaryReaderQuotas.Max, null)) { configuration = serializer.ReadObject(jsonReader) as PowerBIConfiguration; } } this.Settings = configuration.Settings; var cloudEnvironments = GetGlobalServiceConfig().Result; // Ignore non-valid environments var environments = configuration.Environments .Where(e => Enum.TryParse(e.Name, out PowerBIEnvironmentType result)) .Select(e => { if (!string.IsNullOrEmpty(e.CloudName)) { string cloudName = e.CloudName; var cloudEnvironment = cloudEnvironments.Environments.FirstOrDefault(c => c.CloudName.Equals(cloudName, StringComparison.OrdinalIgnoreCase)); if (cloudEnvironment == null) { throw new NotSupportedException($"Unable to find cloud name: {cloudName}"); } var backendService = cloudEnvironment.Services.First(s => s.Name.Equals("powerbi-backend", StringComparison.OrdinalIgnoreCase)); return(new PowerBIEnvironment() { Name = (PowerBIEnvironmentType)Enum.Parse(typeof(PowerBIEnvironmentType), e.Name), AzureADAuthority = cloudEnvironment.Services.First(s => s.Name.Equals("aad", StringComparison.OrdinalIgnoreCase)).Endpoint, AzureADClientId = e.ClientId, AzureADRedirectAddress = e.Redirect, AzureADResource = backendService.ResourceId, GlobalServiceEndpoint = backendService.Endpoint }); } else { return(new PowerBIEnvironment() { Name = (PowerBIEnvironmentType)Enum.Parse(typeof(PowerBIEnvironmentType), e.Name), AzureADAuthority = e.Authority, AzureADClientId = e.ClientId, AzureADRedirectAddress = e.Redirect, AzureADResource = e.Resource, GlobalServiceEndpoint = e.GlobalService }); } }) .Cast <IPowerBIEnvironment>().ToDictionary(e => e.Name, e => e); this.Environments = environments; }
/// <summary> /// 将 Steam 转换成动态类型 /// </summary> /// <param name="stream"></param> /// <param name="encoding"></param> /// <returns></returns> public static dynamic Parse(Stream stream, Encoding encoding) { using var reader = JsonReaderWriterFactory.CreateJsonReader(stream, encoding, XmlDictionaryReaderQuotas.Max, _ => { }); return(ToValue(XElement.Load(reader))); }
/// <summary> /// Called during deserialization to read an object of the specified <paramref name="type"/> /// from the specified <paramref name="readStream"/>. /// </summary> /// <param name="type">The type of object to read.</param> /// <param name="readStream">The <see cref="Stream"/> from which to read.</param> /// <param name="content">The <see cref="HttpContent"/> for the content being written.</param> /// <param name="formatterLogger">The <see cref="IFormatterLogger"/> to log events to.</param> /// <returns>A <see cref="Task"/> whose result will be the object instance that has been read.</returns> public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { if (type == null) { throw Error.ArgumentNull("type"); } if (readStream == null) { throw Error.ArgumentNull("readStream"); } return(TaskHelpers.RunSynchronously <object>(() => { HttpContentHeaders contentHeaders = content == null ? null : content.Headers; // If content length is 0 then return default value for this type if (contentHeaders != null && contentHeaders.ContentLength == 0) { return GetDefaultValueForType(type); } // Get the character encoding for the content Encoding effectiveEncoding = SelectCharacterEncoding(contentHeaders); try { #if !NETFX_CORE if (UseDataContractJsonSerializer) { DataContractJsonSerializer dataContractSerializer = GetDataContractSerializer(type); using (XmlReader reader = JsonReaderWriterFactory.CreateJsonReader(new NonClosingDelegatingStream(readStream), effectiveEncoding, _readerQuotas, null)) { return dataContractSerializer.ReadObject(reader); } } else #endif { using (JsonTextReader jsonTextReader = new JsonTextReader(new StreamReader(readStream, effectiveEncoding)) { CloseInput = false, MaxDepth = _maxDepth }) { JsonSerializer jsonSerializer = JsonSerializer.Create(_jsonSerializerSettings); if (formatterLogger != null) { // Error must always be marked as handled // Failure to do so can cause the exception to be rethrown at every recursive level and overflow the stack for x64 CLR processes jsonSerializer.Error += (sender, e) => { Exception exception = e.ErrorContext.Error; formatterLogger.LogError(e.ErrorContext.Path, exception); e.ErrorContext.Handled = true; }; } return jsonSerializer.Deserialize(jsonTextReader, type); } } } catch (Exception e) { if (formatterLogger == null) { throw; } formatterLogger.LogError(String.Empty, e); return GetDefaultValueForType(type); } })); }
public async Task <PackageSpec> GetPackageInfoAsync(PackageSpec entry, CancellationToken cancel) { string description = null; List <string> versions = null; bool useCache = IsInLiveCache(entry.Name); if (useCache) { using (await _cacheLock.LockAsync(cancel)) { PackageSpec result; if (_cache.TryGetValue(entry.Name, out result)) { return(result.Clone()); } else { return(new PackageSpec()); } } } TaskCompletionSource <PackageSpec> tcs = null; Task <PackageSpec> t = null; lock (_activeRequests) { if (_activeRequests.TryGetValue(entry.Name, out tcs)) { t = tcs.Task; } else { _activeRequests[entry.Name] = tcs = new TaskCompletionSource <PackageSpec>(); } } if (t != null) { return((await t).Clone()); } try { using (var client = new WebClient()) { Stream data; client.Headers[HttpRequestHeader.UserAgent] = UserAgent; try { data = await client.OpenReadTaskAsync(new Uri(Index, entry.Name + "/json")) .ConfigureAwait(false); } catch (WebException) { // No net access or no such package AddToLiveCache(entry.Name); return(new PackageSpec()); } try { using (var reader = JsonReaderWriterFactory.CreateJsonReader(data, new XmlDictionaryReaderQuotas())) { var doc = XDocument.Load(reader); // TODO: Get package URL //url = (string)doc.Document // .Elements("root") // .Elements("info") // .Elements("package_url") // .FirstOrDefault(); description = (string)doc.Document .Elements("root") .Elements("info") .Elements("description") .FirstOrDefault(); versions = doc.Document .Elements("root") .Elements("releases") .Elements() .Attributes("item") .Select(a => a.Value) .ToList(); } } catch (InvalidOperationException) { } } bool changed = false; PackageSpec result; using (await _cacheLock.LockAsync(cancel)) { if (!_cache.TryGetValue(entry.Name, out result)) { result = _cache[entry.Name] = new PackageSpec(entry.Name); } if (!string.IsNullOrEmpty(description)) { var lines = description.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); var firstLine = string.Join( " ", lines.TakeWhile(s => !IsSeparatorLine(s)).Select(s => s.Trim()) ); if (firstLine.Length > 500) { firstLine = firstLine.Substring(0, 497) + "..."; } if (firstLine == "UNKNOWN") { firstLine = string.Empty; } result.Description = firstLine; changed = true; } if (versions != null) { var updateVersion = PackageVersion.TryParseAll(versions) .Where(v => v.IsFinalRelease) .OrderByDescending(v => v) .FirstOrDefault(); result.ExactVersion = updateVersion; changed = true; } } if (changed) { TriggerWriteCacheToDisk(); AddToLiveCache(entry.Name); } var r = result.Clone(); // Inform other waiters that we have completed tcs.TrySetResult(r); return(r); } catch (Exception ex) { tcs.TrySetException(ex); throw; } finally { lock (_activeRequests) { _activeRequests.Remove(entry.Name); } } }
public IEnumerable <string> GetModifiedFiles(int pullRequest) { var path = Path.Combine(harness.LogDirectory, "pr" + pullRequest + "-files.log"); if (!File.Exists(path)) { var rv = GetModifiedFilesLocally(pullRequest); if (rv == null || rv.Count() == 0) { rv = GetModifiedFilesRemotely(pullRequest); if (rv == null) { rv = new string [] { } } ; } File.WriteAllLines(path, rv.ToArray()); return(rv); } return(File.ReadAllLines(path)); } IEnumerable <string> GetModifiedFilesRemotely(int pullRequest) { var path = Path.Combine(harness.LogDirectory, "pr" + pullRequest + "-remote-files.log"); if (!File.Exists(path)) { Directory.CreateDirectory(harness.LogDirectory); using (var client = CreateClient()) { var rv = new List <string> (); var url = $"https://api.github.com/repos/xamarin/xamarin-macios/pulls/{pullRequest}/files?per_page=100"; // 100 items per page is max do { byte [] data; try { data = client.DownloadData(url); } catch (WebException we) { harness.Log("Could not load pull request files: {0}\n{1}", we, new StreamReader(we.Response.GetResponseStream()).ReadToEnd()); File.WriteAllText(path, string.Empty); return(new string [] { }); } var reader = JsonReaderWriterFactory.CreateJsonReader(data, new XmlDictionaryReaderQuotas()); var doc = new XmlDocument(); doc.Load(reader); foreach (XmlNode node in doc.SelectNodes("/root/item/filename")) { rv.Add(node.InnerText); } url = null; var link = client.ResponseHeaders ["Link"]; try { if (link != null) { var ltIdx = link.IndexOf('<'); var gtIdx = link.IndexOf('>', ltIdx + 1); while (ltIdx >= 0 && gtIdx > ltIdx) { var linkUrl = link.Substring(ltIdx + 1, gtIdx - ltIdx - 1); if (link [gtIdx + 1] != ';') { break; } var commaIdx = link.IndexOf(',', gtIdx + 1); string rel; if (commaIdx != -1) { rel = link.Substring(gtIdx + 3, commaIdx - gtIdx - 3); } else { rel = link.Substring(gtIdx + 3); } if (rel == "rel=\"next\"") { url = linkUrl; break; } if (commaIdx == -1) { break; } ltIdx = link.IndexOf('<', commaIdx); gtIdx = link.IndexOf('>', ltIdx + 1); } } } catch (Exception e) { harness.Log("Could not paginate github response: {0}: {1}", link, e.Message); } } while (url != null); File.WriteAllLines(path, rv.ToArray()); return(rv); } } return(File.ReadAllLines(path)); } IEnumerable <string> GetModifiedFilesLocally(int pullRequest) { var base_commit = $"origin/pr/{pullRequest}/merge^"; var head_commit = $"origin/pr/{pullRequest}/merge"; harness.Log("Fetching modified files for commit range {0}..{1}", base_commit, head_commit); if (string.IsNullOrEmpty(head_commit) || string.IsNullOrEmpty(base_commit)) { return(null); } using (var git = new Process()) { git.StartInfo.FileName = "git"; git.StartInfo.Arguments = $"diff-tree --no-commit-id --name-only -r {base_commit}..{head_commit}"; var output = new MemoryLog() { Timestamp = false // ensure we do not add the timestap or the logic for the file check will be hard and having it adds no value }; var rv = processManager.RunAsync(git, harness.HarnessLog, stdoutLog: output, stderrLog: output).Result; if (rv.Succeeded) { return(output.ToString().Split(new char [] { '\n' }, StringSplitOptions.RemoveEmptyEntries)); } harness.Log("Could not fetch commit range:"); harness.Log(output.ToString()); return(null); } } byte[] DownloadPullRequestInfo(int pullRequest) { var path = Path.Combine(harness.LogDirectory, "pr" + pullRequest + ".log"); if (!File.Exists(path)) { Directory.CreateDirectory(harness.LogDirectory); using (var client = CreateClient()) { byte [] data; try { data = client.DownloadData($"https://api.github.com/repos/xamarin/xamarin-macios/pulls/{pullRequest}"); File.WriteAllBytes(path, data); return(data); } catch (WebException we) { harness.Log("Could not load pull request info: {0}\n{1}", we, new StreamReader(we.Response.GetResponseStream()).ReadToEnd()); File.WriteAllText(path, string.Empty); return(new byte [0]); } } } return(File.ReadAllBytes(path)); } }
private XmlDictionaryWriter CreateJsonWriter() { // this assembly shouldn't have any external dependencies, so using this legacy JSON writer return(JsonReaderWriterFactory.CreateJsonWriter(_stream, Encoding.UTF8, ownsStream: false)); }
private void Serverthread() { //wait on port 4242 with a 200ms timeout for data receives IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 4242); UdpClient udpClient = new UdpClient(ipep); udpClient.Client.ReceiveTimeout = 200; IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); while (started) { byte[] data = null; try { data = udpClient.Receive(ref sender); } catch (System.Net.Sockets.SocketException) { } //just try receive again if (data == null) { continue; } //parse JSON that was given to us and read data we want to use XmlDictionaryReader jsonReader = JsonReaderWriterFactory.CreateJsonReader(data, new XmlDictionaryReaderQuotas()); XElement wiiUGamePad = XElement.Load(jsonReader).Element("wiiUGamePad"); int hold = Int32.Parse(wiiUGamePad.Element("hold").Value); int tpTouch = Int32.Parse(wiiUGamePad.Element("tpTouch").Value); int tpX = Int32.Parse(wiiUGamePad.Element("tpX").Value); int tpY = Int32.Parse(wiiUGamePad.Element("tpY").Value); Double lStickY = Double.Parse(wiiUGamePad.Element("lStickY").Value, CultureInfo.InvariantCulture); Double rStickY = Double.Parse(wiiUGamePad.Element("rStickY").Value, CultureInfo.InvariantCulture); //get our basic mouse data ready Input[] input = new Input[1]; input[0].Type = InputMouse; input[0].MouseInput.Flags = 0; input[0].MouseInput.Time = 0; input[0].MouseInput.ExtraInfo = IntPtr.Zero; bool left = false, right = false, middle = false; //first, translate touchscreen info to mouse position if (tpTouch != 0) { input[0].MouseInput.Flags |= MouseEventAbsolute | MouseEventMove; double inputXscale = (65535.0 / (double)SystemInformation.VirtualScreen.Width); double inputYscale = (65535.0 / (double)SystemInformation.VirtualScreen.Height); double touchXbase = ((double)tpX * multi) + xOffset; double touchYbase = ((double)tpY * multi) + yOffset; input[0].MouseInput.X = (int)(touchXbase * inputXscale); input[0].MouseInput.Y = (int)(touchYbase * inputYscale); //also allow left clicks with touchscreen if requested if (touchleft) { left = true; } } //process buttons for left/right/middle clicks if ((hold & (VPAD_BUTTON_A | VPAD_BUTTON_ZL | VPAD_BUTTON_ZR)) != 0) { left = true; } if ((hold & (VPAD_BUTTON_B | VPAD_BUTTON_L | VPAD_BUTTON_R)) != 0) { right = true; } if ((hold & (VPAD_BUTTON_STICK_L | VPAD_BUTTON_STICK_R)) != 0) { middle = true; } //set mouse left/right/middle click status if (!prevleft && left) { input[0].MouseInput.Flags |= MouseEventLeftDown; } else if (prevleft && !left) { input[0].MouseInput.Flags |= MouseEventLeftUp; } if (!prevright && right) { input[0].MouseInput.Flags |= MouseEventRightDown; } else if (prevright && !right) { input[0].MouseInput.Flags |= MouseEventRightUp; } if (!prevmiddle && middle) { input[0].MouseInput.Flags |= MouseEventMiddleDown; } else if (prevmiddle && !middle) { input[0].MouseInput.Flags |= MouseEventMiddleUp; } prevleft = left; prevright = right; prevmiddle = middle; //set up mouse scroll wheel using analog sticks if (lStickY > 0.15 || rStickY > 0.15) { double inY = Math.Max(lStickY, rStickY); if (checkWheelScroll(inY) || !wasScrolling) { input[0].MouseInput.Flags |= MouseEventWheel; input[0].MouseInput.MouseData = 120; wasScrolling = true; } } else if (lStickY < -0.15 || rStickY < -0.15) { double inY = Math.Max(-lStickY, -rStickY); if (checkWheelScroll(inY) || !wasScrolling) { input[0].MouseInput.Flags |= MouseEventWheel; input[0].MouseInput.MouseData = -120; wasScrolling = true; } } else if (wasScrolling) //reset to slow interval { wasScrolling = false; ourTimer.Enabled = false; curTicks = 0; } //finally send our mouse data to OS SendInput(1, input, Marshal.SizeOf(input[0])); } udpClient.Close(); }
/// <summary> /// Send a syntax or other static error over the RPC /// channel. /// </summary> /// <param name="code">Error code</param> /// <param name="module">Module name</param> /// <param name="line">Line number of the error</param> /// <param name="message">Error message</param> public void SendStaticError(string code, string module, int line, string message) { if (stop) { return; } var stream = new MemoryStream(10240); var jsonWriter = JsonReaderWriterFactory.CreateJsonWriter( stream ); var xmlDoc = new XmlDocument(); var root = xmlDoc.CreateElement("root"); // msg = message var msg = xmlDoc.CreateElement("message"); var tn = xmlDoc.CreateTextNode(message); msg.AppendChild(tn); root.AppendChild(msg); // mode = "static-error" var mode = xmlDoc.CreateElement("mode"); tn = xmlDoc.CreateTextNode("static-error"); mode.AppendChild(tn); root.AppendChild(mode); // line = line var lineEl = xmlDoc.CreateElement("line"); lineEl.SetAttribute("type", "number"); tn = xmlDoc.CreateTextNode("" + line); lineEl.AppendChild(tn); root.AppendChild(lineEl); // module = module var moduleEl = xmlDoc.CreateElement("module"); tn = xmlDoc.CreateTextNode(module); moduleEl.AppendChild(tn); root.AppendChild(moduleEl); // code = code var codeEl = xmlDoc.CreateElement("code"); tn = xmlDoc.CreateTextNode(code); codeEl.AppendChild(tn); root.AppendChild(codeEl); root.SetAttribute("type", "object"); xmlDoc.AppendChild(root); #if DEBUG_WS Console.WriteLine(xmlDoc.OuterXml); #endif xmlDoc.WriteTo(jsonWriter); jsonWriter.Flush(); #if DEBUG_WS Console.WriteLine( "Capacity = {0}, Length = {1}, Position = {2}\n", stream.Capacity.ToString(), stream.Length.ToString(), stream.Position.ToString()); #endif stream.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(stream); wss.Send(reader.ReadToEnd()); }
XmlDictionaryReader CreateReader(string s) { return(JsonReaderWriterFactory.CreateJsonReader(GetInput(s), new XmlDictionaryReaderQuotas())); }
/// <summary> /// Callback for the Upload HttpWebRequest.beginGetRequestStream /// </summary> /// <param name="asyncResult">IAsyncResult object</param> void OnUploadGetRequestStreamCompleted(IAsyncResult asyncResult) { AsyncArgsWrapper wrapper = asyncResult.AsyncState as AsyncArgsWrapper; try { Stream requestStream = wrapper.WebRequest.EndGetRequestStream(asyncResult); // Create a SyncWriter to write the contents this._syncWriter = (base.SerializationFormat == SerializationFormat.ODataAtom) ? (SyncWriter) new ODataAtomWriter(base.BaseUri) : (SyncWriter) new ODataJsonWriter(base.BaseUri); this._syncWriter.StartFeed(wrapper.CacheRequest.IsLastBatch, wrapper.CacheRequest.KnowledgeBlob ?? new byte[0]); foreach (IOfflineEntity entity in wrapper.CacheRequest.Changes) { // Skip tombstones that dont have a ID element. if (entity.ServiceMetadata.IsTombstone && string.IsNullOrEmpty(entity.ServiceMetadata.Id)) { continue; } string tempId = null; // Check to see if this is an insert. i.e ServiceMetadata.Id is null or empty if (string.IsNullOrEmpty(entity.ServiceMetadata.Id)) { if (wrapper.TempIdToEntityMapping == null) { wrapper.TempIdToEntityMapping = new Dictionary <string, IOfflineEntity>(); } tempId = Guid.NewGuid().ToString(); wrapper.TempIdToEntityMapping.Add(tempId, entity); } this._syncWriter.AddItem(entity, tempId); } if (base.SerializationFormat == SerializationFormat.ODataAtom) { XmlWriterSettings settings = new XmlWriterSettings(); settings.CheckCharacters = false; XmlWriter writer = XmlWriter.Create(requestStream, settings); this._syncWriter.WriteFeed(writer); } else { this._syncWriter.WriteFeed(JsonReaderWriterFactory.CreateJsonWriter(requestStream)); } requestStream.Flush(); requestStream.Close(); if (this._beforeRequestHandler != null) { // Invoke user code and wait for them to call back us when they are done with the input request this._workerManager.PostProgress(wrapper.WorkerRequest, this.FirePreRequestHandler, wrapper); } else { this.GetWebResponse(wrapper); } } catch (Exception e) { if (ExceptionUtility.IsFatal(e)) { throw; } wrapper.Error = e; this._workerManager.CompleteWorkRequest(wrapper.WorkerRequest, wrapper); } }
override protected string getLinks() { string exed = ""; string JSONUrl = "http://a.4cdn.org/" + getURL().Split('/')[3] + "/thread/" + getURL().Split('/')[5] + ".json"; string baseURL = "http://i.4cdn.org/" + getURL().Split('/')[3] + "/"; string str = ""; XmlNodeList xmlTim; XmlNodeList xmlExt; XmlNodeList xmlFilename; try { string Content; using (WebClient wc = new WebClient()){ wc.Headers.Add("User-Agent: " + Adv.Default.UserAgent); Content = wc.DownloadString(JSONUrl); } byte[] bytes = Encoding.ASCII.GetBytes(Content); using (var stream = new MemoryStream(bytes)) { var quotas = new XmlDictionaryReaderQuotas(); var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, quotas); var xml = XDocument.Load(jsonReader); str = xml.ToString(); stream.Flush(); stream.Close(); } XmlDocument doc = new XmlDocument(); doc.LoadXml(str); if (getURL().Split('/')[3] == "f") { xmlTim = doc.DocumentElement.SelectNodes("/root/posts/item/filename"); } else { xmlTim = doc.DocumentElement.SelectNodes("/root/posts/item/tim"); } xmlFilename = doc.DocumentElement.SelectNodes("/root/posts/item/filename"); xmlExt = doc.DocumentElement.SelectNodes("/root/posts/item/ext"); for (int i = 0; i < xmlExt.Count; i++) { exed = exed + baseURL + xmlTim[i].InnerText + xmlExt[i].InnerText + "\n"; // MessageBox.Show(exed); } return(exed); } catch (WebException webEx) { if (((int)webEx.Status) == 7) { this.Gone = true; } else { ErrorLog.reportWebError(webEx); } throw webEx; } catch (Exception ex) { ErrorLog.reportError(ex.ToString()); throw ex; } }