Skip to content
Permalink
Browse files

SCHEMA CHANGE: adds date and IP to database

  • Loading branch information
remram44 committed Feb 15, 2015
1 parent 4b4b4c4 commit a0dbe92efb26dba9bdcaed8867cca639d9f06f29
Showing with 17 additions and 7 deletions.
  1. +4 −2 cgi.cpp
  2. +10 −3 database.cpp
  3. +2 −1 database.h
  4. +1 −1 test_database.cpp
@@ -229,7 +229,7 @@ int main()
std::string content = get_req(&request, req_in, req_err);

// Gets method and path from environment
std::string method, uri, host;
std::string method, uri, host, remote_addr;
{
const char *const *envp = request.envp;
for(; *envp; ++envp)
@@ -240,6 +240,8 @@ int main()
method = (*envp) + 15;
else if(startswith(*envp, "HTTP_HOST="))
host = (*envp) + 10;
else if(startswith(*envp, "REMOTE_ADDR="))
remote_addr = (*envp) + 12;
}
}

@@ -272,7 +274,7 @@ int main()
{
Key new_key = db.nextState();
std::string our_url = insults.generate(new_key);
db.storeURL(our_url, their_url);
db.storeURL(our_url, their_url, remote_addr);

req_out << "Status: 303 See Other\r\n"
"Server: insurlt\r\n"
@@ -61,6 +61,8 @@ Database::Database(const char *filepath, Generator &generator)
" id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
" our_url TEXT NOT NULL,"
" target_url TEXT NOT NULL,"
" author_ip TEXT NULL,"
" author_date TEXT NOT NULL,"
" views INTEGER NOT NULL"
" );",
"CREATE INDEX urls_our_url_idx ON urls(our_url);",
@@ -80,8 +82,9 @@ Database::Database(const char *filepath, Generator &generator)
-1, &m_stmtSetState, NULL));
check(sqlite3_prepare_v2(
m_DB,
"INSERT INTO urls(our_url, target_url, views) "
"VALUES(?, ?, 0);",
"INSERT INTO urls(our_url, target_url, "
"author_ip, author_date, views) "
"VALUES(?, ?, ?, datetime(), 0);",
-1, &m_stmtInsertURL, NULL));
check(sqlite3_prepare_v2(
m_DB,
@@ -129,14 +132,18 @@ Database::~Database()
}

void Database::storeURL(const std::string &our_url,
const std::string &their_url) throw(DatabaseError)
const std::string &their_url,
const std::string &their_address) throw(DatabaseError)
{
check(sqlite3_bind_text(m_stmtInsertURL, 1,
our_url.c_str(), our_url.size(),
SQLITE_TRANSIENT));
check(sqlite3_bind_text(m_stmtInsertURL, 2,
their_url.c_str(), their_url.size(),
SQLITE_TRANSIENT));
check(sqlite3_bind_text(m_stmtInsertURL, 3,
their_address.c_str(), their_address.size(),
SQLITE_TRANSIENT));

if(sqlite3_step(m_stmtInsertURL) != SQLITE_DONE)
goto sqlerror;
@@ -48,7 +48,8 @@ class Database {
~Database();

void storeURL(const std::string &our_url,
const std::string &their_url) throw(DatabaseError);
const std::string &their_url,
const std::string &their_address) throw(DatabaseError);
std::string resolveURL(const std::string &our_url,
bool increment_views) throw(DatabaseError);

@@ -40,7 +40,7 @@ class Database_test : public CppUnit::TestFixture {
db.resolveURL("http://bbb.clique-salope.ovh/", false) ==
"");
db.storeURL("http://aaa.clique-salope.ovh/",
"http://aaa.net/");
"http://aaa.net/", "10.0.0.1");
CPPUNIT_ASSERT(
db.resolveURL("http://aaa.clique-salope.ovh/", false) ==
"http://aaa.net/");

0 comments on commit a0dbe92

Please sign in to comment.
You can’t perform that action at this time.