/***************************************************/ public IEnumerable <object> Pull(BatchRequest requests, HTTPConfig config) { if (config == null) { config = new HTTPConfig(); } string[] response = new string[requests.Requests.Count]; List <BHoMObject> result = new List <BHoMObject>(); using (HttpClient client = new HttpClient() { Timeout = config.Timeout }) { List <GetRequest> getRequests = requests.Requests.OfType <GetRequest>().ToList(); response = Task.WhenAll(getRequests.Select(x => Compute.MakeRequestAsync(x, client))).GetAwaiter().GetResult(); client.CancelPendingRequests(); client.Dispose(); } Parallel.ForEach(response, res => { if (res == null) { return; } BHoMObject obj = Engine.Serialiser.Convert.FromJson(res) as BHoMObject; if (obj == null) { Engine.Base.Compute.RecordNote($"{res.GetType()} failed to deserialise to a BHoMObject and is set to null." + $"Perform a request with Compute.GetRequest(string url) if you want the raw output"); return; // return is equivalent to `continue` in a Parallel.ForEach } result.Add(obj); }); return(result); }
public async Task <IActionResult> HandleHttpProxyConfigUpdate([FromBody] HTTPConfig config) { if (!ModelState.IsValid || config.listeners.IsNullOrEmpty()) { // ModelState takes care of checking if the field map succeeded. This means mode | allowed.d | banned.d do not need manual checking _response.Errors.Add(Errors.MISSING_BODY, ""); return(BadRequest(_response)); } var currentConfig = (HTTPConfig)_serviceConfigManager.Generate(Utility.GetServiceType("HTTPProxy")).First(); var localAvailableIPs = Utility.GetLocalIPs(); var availableIPs = localAvailableIPs as IPAddress[] ?? localAvailableIPs.ToArray(); var consumedListenerMap = new Dictionary <int, List <IPAddress> >(); // Check if all listeners are valid foreach (var listener in config.listeners) { if (IPAddress.TryParse(listener.Item1, out var holder)) { var ipChecked = AppConfig.BindToUnbound || availableIPs.Contains(holder) || holder.Equals(IPAddress.Any); var portChecked = listener.Item2 > 1023 && listener.Item2 < 65535; consumedListenerMap.TryGetValue(listener.Item2, out var existingListOfAddresses); // DAEM-58 compliance: prevent unicast.any listeners if port is not entirely free. if (existingListOfAddresses != null) { foreach (var ipAddress in existingListOfAddresses) { if (ipAddress.Equals(IPAddress.Any)) { _response.Errors.Add(Errors.PORT_CONFLICT_FOUND, "0.0.0.0"); break; } // Duplicate listener found if (ipAddress.Equals(holder)) { _response.Errors.Add(Errors.DUPLICATE_IP_AS_LISTENER_REQUEST, listener.Item1); break; } } } if (!ipChecked) { _response.Errors.Add(Errors.INVALID_IP_AS_LISTENER_REQUEST, listener.Item1); break; } if (!portChecked) { _response.Errors.Add(Errors.INVALID_PORT_AS_LISTENER_REQUEST, listener.Item2); break; } if (existingListOfAddresses == null) { existingListOfAddresses = new List <IPAddress>(); // If it was null, that means it wasn't in the dict either. consumedListenerMap.Add(listener.Item2, existingListOfAddresses); } // List is guaranteed not null at this stage, add the IP to it. existingListOfAddresses.Add(holder); } else { _response.Errors.Add(Errors.MALFORMED_IP_AS_LISTENER_REQUEST, listener.Item1); break; } } if (HasErrors()) { Logger.LogError("CCHH: Invalid listener request found."); return(BadRequest(_response)); } if (config.listeners != currentConfig.listeners || config.proxyMode != currentConfig.proxyMode || config.allowedDomains != currentConfig.allowedDomains || config.bannedDomains != currentConfig.bannedDomains) { // A difference was found between the running config and the candidate config // If listener config changed AND service is running, the service needs restarting as it can't be adjusted without the sockets being re-initialized. var service = _serviceManager.GetService(typeof(HTTPProxy)); var restartNeeded = !config.listeners.SequenceEqual(currentConfig.listeners) && service.GetState() == ServiceState.Running; //Update the running config, listener config will not apply until a full system restart is made. service.SetConfig(new List <IServiceConfig> { config }, restartNeeded); if (restartNeeded) { _response.Message = Messages.SERVICE_RESTART_NEEDED; } } // If we get to this point, it means the candidate config was valid and should be committed into the DB. var dbConfig = await Db.SingleAsync <Configuration>(x => x.Key == ConfigKeys.HttpConfig); dbConfig.Value = JsonConvert.SerializeObject(config); await Db.UpdateAsync(dbConfig); _response.Result = config; return(Ok(_response)); }
public CORSPostConfigurer(IOptions <HTTPConfig> config) { this.config = config.Value; }