Skip to content

Commit

Permalink
implemented routing to a static file.
Browse files Browse the repository at this point in the history
  • Loading branch information
treefrogframework committed May 26, 2017
1 parent cb65b1b commit 6847156
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 67 deletions.
1 change: 1 addition & 0 deletions defaults/routes.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
# post /Book/new Book.create
# put /Book/:param Book.save
# delete /Book/:param Book.remove
# get / /index.html
3 changes: 3 additions & 0 deletions src/tactioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ void TActionContext::execute(THttpRequest &request, int sid)

} else {
accessLogger.setStatusCode( Tf::BadRequest ); // Set a default status code
if (rt.controller.startsWith("/")) {
path = rt.controller;
}

if (Q_LIKELY(method == Tf::Get)) { // GET Method
QString canonicalPath = QUrl(".").resolved(QUrl(path)).toString().mid(1);
Expand Down
135 changes: 68 additions & 67 deletions src/turlroute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,8 @@ class RouteDirectiveHash : public QMap<QString, int>
};
Q_GLOBAL_STATIC(RouteDirectiveHash, directiveHash)


static TUrlRoute *urlRoute = nullptr;

static void cleanup()
{
if (urlRoute) {
delete urlRoute;
urlRoute = nullptr;
}
}


/*!
* Initializes.
* Call this in main thread.
Expand All @@ -54,7 +44,13 @@ void TUrlRoute::instantiate()
if (!urlRoute) {
urlRoute = new TUrlRoute();
urlRoute->parseConfigFile();
qAddPostRoutine(::cleanup);

qAddPostRoutine([] {
if (urlRoute) {
delete urlRoute;
urlRoute = nullptr;
}
});
}
}

Expand Down Expand Up @@ -92,62 +88,67 @@ bool TUrlRoute::parseConfigFile()

bool TUrlRoute::addRouteFromString(const QString &line)
{
QStringList items = line.simplified().split(' ');
if (items.count() != 3) {
tError("Invalid directive, '%s'", qPrintable(line));
return false;
}

// Trimm quotes
items[1] = THttpUtility::trimmedQuotes(items[1]);
items[2] = THttpUtility::trimmedQuotes(items[2]);
QString &path = items[1];

if (path.contains(":params") && !path.endsWith(":params")) {
tError(":params must be specified as last directive.");
return false;
}

TRoute rt;

// Check method
rt.method = directiveHash()->value(items[0].toLower(), TRoute::Invalid);
if (rt.method == TRoute::Invalid) {
tError("Invalid directive, '%s'", qPrintable(items[0]));
return false;
}

// parse path
rt.componentList = splitPath(path);
rt.paramNum = rt.componentList.count(":param");
rt.hasVariableParams = rt.componentList.contains(":params");

for (int i = 0; i < rt.componentList.count(); ++i) {
const QString &c = rt.componentList[i];
if (c.startsWith(":")) {
if (c != ":param" && c != ":params") {
return false;
}
} else {
rt.keywordIndexes << i;
}
}

// parse controller and action
QStringList list = items[2].split(QRegExp("[#\\.]"));
if (list.count() == 2) {
rt.controller = list[0].toLower().toLatin1() + "controller";
rt.action = list[1].toLatin1();
} else {
tError("Invalid action, '%s'", qPrintable(items[2]));
return false;
}

routes << rt;
tSystemDebug("route: method:%d path:%s ctrl:%s action:%s params:%d",
rt.method, qPrintable(QLatin1String("/") + rt.componentList.join("/")), rt.controller.data(),
rt.action.data(), rt.hasVariableParams);
return true;
QStringList items = line.simplified().split(' ');
if (items.count() != 3) {
tError("Invalid directive, '%s'", qPrintable(line));
return false;
}

// Trimm quotes
items[1] = THttpUtility::trimmedQuotes(items[1]);
items[2] = THttpUtility::trimmedQuotes(items[2]);
const QString &path = items[1];

if (path.contains(":params") && !path.endsWith(":params")) {
tError(":params must be specified as last directive.");
return false;
}

TRoute rt;

// Check method
rt.method = directiveHash()->value(items[0].toLower(), TRoute::Invalid);
if (rt.method == TRoute::Invalid) {
tError("Invalid directive, '%s'", qPrintable(items[0]));
return false;
}

// parse path
rt.componentList = splitPath(path);
rt.paramNum = rt.componentList.count(":param");
rt.hasVariableParams = rt.componentList.contains(":params");

for (int i = 0; i < rt.componentList.count(); ++i) {
const QString &c = rt.componentList[i];
if (c.startsWith(":")) {
if (c != ":param" && c != ":params") {
return false;
}
} else {
rt.keywordIndexes << i;
}
}

if (items[2].startsWith("/")) {
// static file
rt.controller = items[2].toUtf8();
} else {
// parse controller and action
QStringList list = items[2].split(QRegExp("[#\\.]"));
if (list.count() == 2) {
rt.controller = list[0].toLower().toLatin1() + "controller";
rt.action = list[1].toLatin1();
} else {
tError("Invalid action, '%s'", qPrintable(items[2]));
return false;
}
}

routes << rt;
tSystemDebug("route: method:%d path:%s ctrl:%s action:%s params:%d",
rt.method, qPrintable(QLatin1String("/") + rt.componentList.join("/")), rt.controller.data(),
rt.action.data(), rt.hasVariableParams);
return true;
}


Expand Down

0 comments on commit 6847156

Please sign in to comment.