public static DbConfigurationDatabaseType GetDbConfigurationTypeByConnectionStringName(string connectionStringName, ILogging logger = null) { var dbConfigurationDatabaseType = DbConfigurationDatabaseType.Unknown; var connectionStringSettings = ConfigurationManager.ConnectionStrings[connectionStringName]; // Attempt to find connection string in configuration file. if (connectionStringSettings == null) { if (logger != null) { logger.WarnFormat("No connection string with name='{0}' could be found.", connectionStringName); } return(dbConfigurationDatabaseType); } var configConnectionStringProviderName = connectionStringSettings.ProviderName; // Attempt to find providerName attribute value from matching connection string. if (string.IsNullOrEmpty(configConnectionStringProviderName)) { if (logger != null) { logger.WarnFormat("No providerName attribute value was found for connection string with name='{0}'.", connectionStringName); } return(dbConfigurationDatabaseType); } // Try to match 'providerName' value found to enum 'DbConfigurationDatabaseType'. dbConfigurationDatabaseType = GetDbConfigurationTypeByProviderName(configConnectionStringProviderName); return(dbConfigurationDatabaseType); }
private void ReadCallback(IAsyncResult result) { if (state == HttpStates.CLOSED) { return; } State = HttpStates.READING; var read = 0; try { read = stream.EndRead(result); if (read < 0) { throw new HttpException("Client did not send anything"); } _logger.DebugFormat("{0} - Read {1} bytes", this, read); readStream.Write(buffer, 0, read); lastActivity = DateTime.Now; } catch (Exception) { if (!IsATimeout) { _logger.WarnFormat("{0} - Failed to read data", this); Close(); } return; } try { if (!hasHeaders) { readStream.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(readStream); for (var line = reader.ReadLine(); line != null; line = reader.ReadLine()) { line = line.Trim(); if (string.IsNullOrEmpty(line)) { hasHeaders = true; readStream = new MemoryStream(); if (headers.ContainsKey("content-length") && uint.TryParse(headers["content-length"], out bodyBytes)) { if (bodyBytes > (1 << 20)) { throw new IOException("Body too long"); } var bytes = Encoding.ASCII.GetBytes(reader.ReadToEnd()); readStream.Write(bytes, 0, bytes.Length); _logger.DebugFormat("Must read body bytes {0}", bodyBytes); } else { readStream = new MemoryStream(); } break; } if (method == null) { var parts = line.Split(new char[] { ' ' }, 3); method = parts[0].Trim().ToUpperInvariant(); path = parts[1].Trim(); _logger.DebugFormat("{0} - {1} request for {2}", this, method, path); } else { var parts = line.Split(new char[] { ':' }, 2); headers[parts[0]] = Uri.UnescapeDataString(parts[1]).Trim(); } } } if (bodyBytes != 0 && bodyBytes > readStream.Length) { _logger.DebugFormat( "{0} - Bytes to go {1}", this, bodyBytes - readStream.Length); Read(); return; } using (readStream) { body = Encoding.UTF8.GetString(readStream.ToArray()); _logger.Debug(body); _logger.Debug(headers); } SetupResponse(); } catch (Exception ex) { _logger.Warn(String.Format("{0} - Failed to process request", this), ex); response = Error500.HandleRequest(this); SendResponse(); } }
private IResponse ProcessSoapRequest(IRequest request) { var soap = new XmlDocument(); soap.LoadXml(request.Body); var sparams = new RawHeaders(); var body = soap.SelectSingleNode("//soap:Body", namespaceMgr); var method = body.FirstChild; foreach (var p in method.ChildNodes) { var e = p as XmlElement; if (e == null) { continue; } sparams.Add(e.LocalName, e.InnerText.Trim()); } var env = new XmlDocument(); env.AppendChild(env.CreateXmlDeclaration("1.0", "utf-8", "yes")); var envelope = env.CreateElement("SOAP-ENV", "Envelope", NS_SOAPENV); env.AppendChild(envelope); envelope.SetAttribute( "encodingStyle", NS_SOAPENV, "http://schemas.xmlsoap.org/soap/encoding/"); var rbody = env.CreateElement("SOAP-ENV:Body", NS_SOAPENV); env.DocumentElement.AppendChild(rbody); var code = HttpCode.Ok; try { IEnumerable <KeyValuePair <string, string> > result; switch (method.LocalName) { case "GetSearchCapabilities": result = HandleGetSearchCapabilities(); break; case "GetSortCapabilities": result = HandleGetSortCapabilities(); break; case "GetSystemUpdateID": result = HandleGetSystemUpdateID(); break; case "Browse": result = HandleBrowse(request, sparams); break; case "X_GetFeatureList": result = HandleXGetFeatureList(); break; case "X_SetBookmark": result = HandleXSetBookmark(sparams); break; case "GetCurrentConnectionIDs": result = HandleGetCurrentConnectionIDs(); break; case "GetCurrentConnectionInfo": result = HandleGetCurrentConnectionInfo(); break; case "GetProtocolInfo": result = HandleGetProtocolInfo(); break; case "IsAuthorized": result = HandleIsAuthorized(); break; case "IsValidated": result = HandleIsValidated(); break; case "RegisterDevice": result = HandleRegisterDevice(); break; default: throw new HttpStatusException(HttpCode.NotFound); } var response = env.CreateElement(String.Format( "u:{0}Response", method.LocalName), method.NamespaceURI); rbody.AppendChild(response); foreach (var i in result) { var ri = env.CreateElement(i.Key); ri.InnerText = i.Value; response.AppendChild(ri); } } catch (Exception ex) { code = HttpCode.InternalError; var fault = env.CreateElement("SOAP-ENV", "Fault", NS_SOAPENV); var faultCode = env.CreateElement("faultcode"); faultCode.InnerText = "500"; fault.AppendChild(faultCode); var faultString = env.CreateElement("faultstring"); faultString.InnerText = ex.ToString(); fault.AppendChild(faultString); var detail = env.CreateDocumentFragment(); detail.InnerXml = "<detail><UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\"><errorCode>401</errorCode><errorDescription>Invalid Action</errorDescription></UPnPError></detail>"; fault.AppendChild(detail); rbody.AppendChild(fault); _logger.WarnFormat( "Invalid call: Action: {0}, Params: {1}, Problem {2}", method.LocalName, sparams, ex.Message); } var rv = new StringResponse(code, "text/xml", env.OuterXml); rv.Headers.Add("EXT", string.Empty); return(rv); }