Skip to content

Commit dd6893e

Browse files
author
Koen Deforche
committed
reset timeout watchdog when in a recursive eventloop
1 parent c680a57 commit dd6893e

File tree

11 files changed

+115
-20
lines changed

11 files changed

+115
-20
lines changed

examples/hangman/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,19 @@ WT_ADD_EXAMPLE(hangman.wt
1313
)
1414
TARGET_LINK_LIBRARIES(hangman.wt wtdbo wtdbosqlite3)
1515

16+
# Test whether crypt(3) is provided by libc. If it's not, check if
17+
# libcrypt exists and if it provides crypt(3).
18+
CHECK_FUNCTION_EXISTS(crypt CRYPT_EXISTS)
19+
IF(NOT CRYPT_EXISTS)
20+
CHECK_LIBRARY_EXISTS(crypt crypt "" CRYPT_LIB_EXISTS)
21+
IF(CRYPT_LIB_EXISTS)
22+
SET(CMAKE_REQUIRED_LIBRARIES "crypt")
23+
CHECK_FUNCTION_EXISTS(crypt CRYPT_EXISTS)
24+
ENDIF(CRYPT_LIB_EXISTS)
25+
ENDIF(NOT CRYPT_EXISTS)
26+
27+
IF(CRYPT_LIB_EXISTS)
28+
TARGET_LINK_LIBRARIES(hangman.wt crypt)
29+
ENDIF(CRYPT_LIB_EXISTS)
30+
1631
INCLUDE_DIRECTORIES(${WT_SOURCE_DIR}/src)

examples/hangman/HighScoresWidget.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void HighScoresWidget::update()
2727
{
2828
clear();
2929

30-
WText *title = new WText("<h2>Hall of fame</h2>", this);
30+
new WText("<h2>Hall of fame</h2>", this);
3131

3232
int ranking = session_->findRanking(session_->user());
3333

examples/hangman/ImagesWidget.C

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const int ImagesWidget::maxGuesses_ = 9;
1414

1515
ImagesWidget::ImagesWidget(WContainerWidget *parent)
1616
{
17-
for (unsigned int i = 0; i <= maxGuesses_; ++i) {
17+
for (int i = 0; i <= maxGuesses_; ++i) {
1818
std::string fname = "icons/hangman";
1919
fname += boost::lexical_cast<std::string>(i) + ".png";
2020
WImage *theImage = new WImage(fname, this);
@@ -40,7 +40,7 @@ void ImagesWidget::init()
4040

4141
void ImagesWidget::badGuess()
4242
{
43-
if (badGuesses_ < hangmanImages_.size() - 1) {
43+
if (badGuesses_ < (int)hangmanImages_.size() - 1) {
4444
hangmanImages_[badGuesses_]->hide();
4545
hangmanImages_[++badGuesses_]->show();
4646
}

examples/hangman/Session.C

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
using namespace Wt;
1313

1414
Session::Session()
15-
: sqlite3_(WApplication::instance()->appRoot() + "hangman.db")
15+
: sqlite3_(WApplication::instance()->appRoot() + "hangman.db")
1616
{
1717
session_.setConnection(sqlite3_);
1818
sqlite3_.setProperty("show-queries", "true");
@@ -38,7 +38,7 @@ bool Session::login(std::string name, std::string password)
3838
if (!user_)
3939
user_ = session_.add(new User(name, password));
4040

41-
bool ok = user_->password == password;
41+
bool ok = user_->authenticate(password);
4242

4343
transaction.commit();
4444

examples/hangman/User.C

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,74 @@
1+
/*
2+
* Copyright (C) 2011 Emweb bvba, Kessel-Lo, Belgium.
3+
*
4+
* See the LICENSE file for terms of use.
5+
*/
6+
7+
#if !defined(WIN32) && !defined(__QNXNTO__)
8+
#define _XOPEN_SOURCE
9+
#include <unistd.h>
10+
#endif
11+
112
#include "User.h"
213

14+
#if !defined(WIN32) && !defined(__CYGWIN__) && !defined(ANDROID)
15+
#define HAVE_CRYPT
16+
#endif
17+
18+
#ifdef HAVE_CRYPT
19+
namespace {
20+
std::string generateSalt() {
21+
/* Salt generation from glibc manual */
22+
unsigned long seed[2];
23+
char salt[] = "$1$........";
24+
const char *const seedchars =
25+
"./0123456789ABCDEFGHIJKLMNOPQRST"
26+
"UVWXYZabcdefghijklmnopqrstuvwxyz";
27+
28+
/* Generate a (not very) random seed. */
29+
seed[0] = time(NULL);
30+
seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
31+
32+
/* Turn it into printable characters from `seedchars'. */
33+
for (int i = 0; i < 8; i++)
34+
salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
35+
36+
return salt;
37+
}
38+
39+
std::string md5(const std::string& s, const std::string& salt) {
40+
return crypt(s.c_str(), salt.c_str());
41+
}
42+
}
43+
#endif
44+
345
using namespace Wt;
446
using namespace Wt::Dbo;
547

6-
User::User(const std::string &name, const std::string &password) :
7-
name(name),
8-
password(password),
9-
gamesPlayed(0),
10-
score(0)
48+
User::User(const std::string &name, const std::string &password)
49+
: name(name),
50+
gamesPlayed(0),
51+
score(0)
52+
{
53+
setPassword(password);
54+
}
55+
56+
57+
void User::setPassword(const std::string& password)
1158
{
59+
#ifdef HAVE_CRYPT
60+
password_ = md5(password, generateSalt());
61+
#else
62+
// This should not be used in production...
63+
password_ = password;
64+
#endif
65+
}
1266

67+
bool User::authenticate(const std::string& password) const
68+
{
69+
#ifdef HAVE_CRYPT
70+
return md5(password, password_) == password_;
71+
#else
72+
return password_ == password;
73+
#endif
1374
}

examples/hangman/User.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,30 @@
1616

1717
class User : public Wt::Dbo::ptr<User> {
1818
public:
19-
std::string name;
20-
std::string password;
21-
int gamesPlayed;
22-
long long score;
23-
Wt::WDateTime lastLogin;
19+
std::string name;
20+
int gamesPlayed;
21+
long long score;
22+
Wt::WDateTime lastLogin;
2423

2524
template<class Action>
2625
void persist(Action& a)
2726
{
2827
Wt::Dbo::field(a, name, "name");
29-
Wt::Dbo::field(a, password, "password");
28+
Wt::Dbo::field(a, password_, "password");
3029
Wt::Dbo::field(a, gamesPlayed, "gamesPlayed");
3130
Wt::Dbo::field(a, score, "score");
3231
Wt::Dbo::field(a, lastLogin, "lastLogin");
3332
}
3433

3534
User() {}
3635

36+
void setPassword(const std::string& password);
37+
bool authenticate(const std::string& password) const;
38+
3739
User(const std::string &name, const std::string &password);
40+
41+
private:
42+
std::string password_;
3843
};
3944

4045
typedef Wt::Dbo::collection< Wt::Dbo::ptr<User> > Users;

examples/hangman/WordWidget.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class WordWidget : public Wt::WContainerWidget
2626
std::vector<Wt::WText *> wordLetters_;
2727
std::wstring word_;
2828

29-
int displayedLetters_;
29+
unsigned displayedLetters_;
3030
};
3131

3232
#endif //WORD_WIDGET_H_

examples/hangman/hangman.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Wt::WApplication *createApplication(const Wt::WEnvironment& env)
1919

2020
app->useStyleSheet("style/hangman.css");
2121

22-
HangmanGame *game = new HangmanGame(app->root());
22+
new HangmanGame(app->root());
2323

2424
return app;
2525
}

examples/widgetgallery/MvcWidgets.C

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,19 @@ void MvcWidgets::changeRegexp()
8181
{
8282
WString regexp = regexpFilter->text();
8383

84-
if (WRegExp(regexp).isValid()) {
84+
bool valid;
85+
#ifndef WT_TARGET_JAVA
86+
valid = WRegExp(regexp).isValid();
87+
#else
88+
try {
89+
WRegExp r(regexp.toUTF8());
90+
valid = true;
91+
} catch (std::exception& e) {
92+
valid = false;
93+
}
94+
#endif
95+
96+
if (valid) {
8597
filteredCocktails->setFilterRegExp(regexp);
8698
filteredSortedCocktails->setFilterRegExp(regexp);
8799
regexpFilter->removeStyleClass("Wt-invalid");

examples/wt-homepage/wt-home.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
</div>
5252

5353
<div id="chat"></div>
54-
<script src="http://www.webtoolkit.eu/wt/examples/simplechat/chat.js?div=chat" type="text/javascript"></script>
54+
<script src="http://www.webtoolkit.eu/wt/examples/simplechat/chat.js?div=chat" type="text/javascript"></script>
5555

5656
<div class="clearall"></div>
5757
</div>

0 commit comments

Comments
 (0)