public void Init() { _storeFile = new FileInfo(_parameters.Keys.Contains("file") ? Convert.ToString(_parameters.GetValuesForKey("file").First()):DefaultFileName); OpenConnection(_storeFile, out connection); SetupDatabase(); select = connection.CreateCommand(); select.CommandText = "SELECT data FROM store WHERE key = ? AND size = ? AND time = ?"; select.Parameters.Add(selectKey = select.CreateParameter()); selectKey.DbType = DbType.String; select.Parameters.Add(selectSize = select.CreateParameter()); selectSize.DbType = DbType.Int64; select.Parameters.Add(selectTime = select.CreateParameter()); selectTime.DbType = DbType.Int64; selectCover = connection.CreateCommand(); selectCover.CommandText = "SELECT cover FROM store WHERE key = ? AND size = ? AND time = ?"; selectCover.Parameters.Add(selectCoverKey = select.CreateParameter()); selectCoverKey.DbType = DbType.String; selectCover.Parameters.Add(selectCoverSize = select.CreateParameter()); selectCoverSize.DbType = DbType.Int64; selectCover.Parameters.Add(selectCoverTime = select.CreateParameter()); selectCoverTime.DbType = DbType.Int64; insert = connection.CreateCommand(); insert.CommandText = "INSERT OR REPLACE INTO store " + "VALUES(@key, @size, @time, @data, COALESCE(@cover, (SELECT cover FROM store WHERE key = @key)))"; insert.Parameters.Add(insertKey = select.CreateParameter()); insertKey.DbType = DbType.String; insertKey.ParameterName = "@key"; insert.Parameters.Add(insertSize = select.CreateParameter()); insertSize.DbType = DbType.Int64; insertSize.ParameterName = "@size"; insert.Parameters.Add(insertTime = select.CreateParameter()); insertTime.DbType = DbType.Int64; insertTime.ParameterName = "@time"; insert.Parameters.Add(insertData = select.CreateParameter()); insertData.DbType = DbType.Binary; insertData.ParameterName = "@data"; insert.Parameters.Add(insertCover = select.CreateParameter()); insertCover.DbType = DbType.Binary; insertCover.ParameterName = "@cover"; _logger.InfoFormat("FileStore at {0} is ready", StoreFile); vacuumer.Add(connection); }
private void SendSearchResponse(IPEndPoint endpoint, UpnpDevice dev) { var headers = new RawHeaders(); headers.Add("CACHE-CONTROL", "max-age = 600"); headers.Add("DATE", DateTime.Now.ToString("R")); headers.Add("EXT", string.Empty); headers.Add("LOCATION", dev.Descriptor.ToString()); headers.Add("SERVER", HttpServer.Signature); headers.Add("ST", dev.Type); headers.Add("USN", dev.USN); SendDatagram( endpoint, dev.Address, String.Format("HTTP/1.1 200 OK\r\n{0}\r\n", headers.HeaderBlock), false ); _logger.InfoFormat( "{2}, {1} - Responded to a {0} request", dev.Type, endpoint, dev.Address); }
private void SendResponse() { var statusCode = response.Status; var responseBody = ProcessRanges(response, ref statusCode); var responseStream = new ConcatenatedStream(); try { var headerBlock = new StringBuilder(); headerBlock.AppendFormat( "HTTP/1.1 {0} {1}\r\n", (uint)statusCode, HttpPhrases.Phrases[statusCode] ); headerBlock.Append(response.Headers.HeaderBlock); headerBlock.Append(CRLF); var headerStream = new MemoryStream( Encoding.ASCII.GetBytes(headerBlock.ToString())); responseStream.AddStream(headerStream); if (method != "HEAD" && responseBody != null) { responseStream.AddStream(responseBody); responseBody = null; } _logger.InfoFormat("{0} - {1} response for {2}", this, (uint)statusCode, path); state = HttpStates.WRITING; var sp = new StreamPump(responseStream, stream, BUFFER_SIZE); sp.Pump((pump, result) => { pump.Input.Close(); pump.Input.Dispose(); if (result == StreamPumpResult.Delivered) { _logger.DebugFormat("{0} - Done writing response", this); string conn; if (headers.TryGetValue("connection", out conn) && conn.ToUpperInvariant() == "KEEP-ALIVE") { ReadNext(); return; } } else { _logger.DebugFormat("{0} - Client aborted connection", this); } Close(); }); } catch (Exception) { responseStream.Dispose(); throw; } finally { if (responseBody != null) { responseBody.Dispose(); } } }