Browse Source

Now url_dispatcher is "official url class"

master
Artyom Beilis 15 years ago
parent
commit
a1293d493c
9 changed files with 24 additions and 181 deletions
  1. +2
    -2
      Makefile.am
  2. +1
    -1
      application.cpp
  3. +1
    -1
      application.h
  4. +12
    -6
      hello_world.cpp
  5. +0
    -92
      url.cpp
  6. +0
    -70
      url.h
  7. +6
    -5
      url_dispatcher.h
  8. +0
    -1
      worker_thread.cpp
  9. +2
    -3
      worker_thread.h

+ 2
- 2
Makefile.am View File

@@ -20,7 +20,7 @@ hello_world_view2.cpp: hello_world_skin2.tmpl hello_world_view1.tmpl
./cppcms_tmpl_cc hello_world_skin2.tmpl hello_world_view1.tmpl -o hello_world_view2.cpp -n view2 -d test

lib_LTLIBRARIES = libcppcms.la
libcppcms_la_SOURCES = global_config.cpp manager.cpp url.cpp worker_thread.cpp \
libcppcms_la_SOURCES = global_config.cpp manager.cpp worker_thread.cpp \
text_tool.cpp cache_interface.cpp base_cache.cpp thread_cache.cpp scgi.cpp \
base_view.cpp util.cpp form.cpp application.cpp session_interface.cpp \
session_cookies.cpp hmac_encryptor.cpp encryptor.cpp md5.c base64.cpp \
@@ -47,7 +47,7 @@ if EN_SQLITE_SESSIONS
libcppcms_la_SOURCES += session_sqlite_storage.cpp
endif

nobase_pkginclude_HEADERS = global_config.h text_tool.h url.h cppcms_error.h \
nobase_pkginclude_HEADERS = global_config.h text_tool.h cppcms_error.h \
manager.h worker_thread.h fcgi.h cache_interface.h archive.h \
base_cache.h thread_cache.h cgicc_connection.h scgi.h cgi_api.h \
process_cache.h shmem_allocator.h posix_mutex.h config.h cgi.h base_view.h \


+ 1
- 1
application.cpp View File

@@ -38,7 +38,7 @@ void application::on_404()
void application::main()
{
on_start();
if(url.parse()<0) {
if(url.dispatch(env->getPathInfo())<0) {
on_404();
}
on_end();


+ 1
- 1
application.h View File

@@ -9,7 +9,7 @@ struct application {

// Data
worker_thread &worker;
url_parser &url;
url_dispatcher &url;
manager const &app;
Cgicc *&cgi;
CgiEnvironment const *&env;


+ 12
- 6
hello_world.cpp View File

@@ -1,6 +1,7 @@
#include "application.h"
#include "manager.h"
#include "hello_world_view.h"
#include "regex.h"
using namespace cppcms;

class my_hello_world : public application {
@@ -8,12 +9,17 @@ public:
my_hello_world(worker_thread &w) :
application(w)
{
url.add("^/?$",
boost::bind(&my_hello_world::std,this));
url.add("^/test$",boost::bind(&my_hello_world::test,this));
url.add("^/test2$",boost::bind(&my_hello_world::test2,this));
url.add("^/cache$",boost::bind(&my_hello_world::cache_test,this));
url.add("^/png$",boost::bind(&my_hello_world::png,this));
using util::regex;
static const regex std("^/?$");
url.assign(std,&my_hello_world::std,this);
static const regex test("^/test$");
url.assign(test,&my_hello_world::test,this);
static const regex test2("^/test2$");
url.assign(test2,&my_hello_world::test2,this);
static const regex cache("^/cache$");
url.assign(cache,&my_hello_world::cache_test,this);
static const regex png("^/png$");
url.assign(png,&my_hello_world::png,this);
use_template("view2");
};
void test();


+ 0
- 92
url.cpp View File

@@ -1,92 +0,0 @@
#include "url.h"

#include "worker_thread.h"

using namespace std;
using namespace boost;
using namespace cppcms;
url_parser::~url_parser()
{
}

void url_parser::add(char const *exp,int id)
{
url_def url_def;
url_def.pattern=regex(exp);
url_def.type=url_def::ID;
url_def.id=id;
url_def.callback=NULL;
url_def.url=NULL;
patterns.push_back(url_def);
}

void url_parser::add(char const *exp,url_parser &url)
{
url_def url_def;
url_def.pattern=regex(exp);
url_def.type=url_def::URL;
url_def.id=0;
url_def.url=&url;
patterns.push_back(url_def);
}

void url_parser::add(char const *exp,callback_t callback)
{
url_def url_def;
url_def.pattern=regex(exp);
url_def.type=url_def::CALLBACK;
url_def.id=0;
url_def.callback=callback;
url_def.url=NULL;
patterns.push_back(url_def);

}

int url_parser::parse(string &query)
{
unsigned i;
for(i=0;i<patterns.size();i++) {
boost::regex &r=patterns[i].pattern;
string tmp;
if(boost::regex_match(query.c_str(),result,r)){
switch(patterns[i].type) {
case url_def::ID:
return patterns[i].id;
case url_def::URL:
tmp=result[1];
return patterns[i].url->parse(tmp);
case url_def::CALLBACK:
patterns[i].callback( result[1],result[2],
result[3],result[4],
result[5],result[6],
result[7],result[8],
result[0]);
return 0;
}
}
}
return -1;
}

int url_parser::parse()
{
string query;
if(worker){
query=worker->env->getPathInfo();
}
else {
return not_found;
}
return parse(query);
}

string url_parser::operator[](int i)
{
return result[i];
}

+ 0
- 70
url.h View File

@@ -1,70 +0,0 @@
#ifndef _URL_H
#define _URL_H

#include <map>
#include <memory>
#include <vector>
#include <boost/regex.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include "config.h"

// Some defines:

#if defined(HAVE_DOLLAR_SIGN) && !defined(NO_DEPRICATED)

#define $0 _9
#define $1 _1
#define $2 _2
#define $3 _3
#define $4 _4
#define $5 _5
#define $6 _6
#define $7 _7
#define $8 _8

#endif

namespace cppcms {

class worker_thread;

using std::string;


typedef boost::function<void(string,string,string,string,
string,string,string,string,
string)> callback_t;
class url_parser;

struct url_def {
boost::regex pattern;
enum { ID, CALLBACK , URL } type;
int id;
url_parser *url;
callback_t callback;
};

class url_parser {
std::vector<url_def>patterns;
worker_thread *worker;
boost::cmatch result;
void set_regex(char const *r);
public:
static const int not_found=-1;
static const int ok=0;
url_parser() {};
url_parser(worker_thread * w) { worker=w;};
~url_parser();
void add(char const *exp,int id);
void add(char const *exp,url_parser &url);
void add(char const *exp,callback_t callback);
void init(worker_thread *w) { worker=w; };
int parse();
int parse(string &s);
std::string operator[](int);
};

} // Namespace cppcms

#endif /* _URL_H */

+ 6
- 5
url_dispatcher.h View File

@@ -5,6 +5,7 @@
#include "defs.h"
#include "callback.h"
#include "hold_ptr.h"
#include "mem_bind.h"
#include <string>
#include <list>

@@ -106,7 +107,7 @@ namespace cppcms {
template<typename C>
void assign(util::regex const &match,void (C::*member)(),C *object)
{
assign(match,mem_bind(member,object));
assign(match,util::mem_bind(member,object));
}
///
/// This template function is a shortcut to assign(regex,callback,int). It allows
@@ -115,7 +116,7 @@ namespace cppcms {
template<typename C>
void assign(util::regex const &match,void (C::*member)(std::string),C *object,int e1)
{
assign(match,mem_bind(member,object),e1);
assign(match,util::mem_bind(member,object),e1);
}
///
/// This template function is a shortcut to assign(regex,callback,int,int). It allows
@@ -124,7 +125,7 @@ namespace cppcms {
template<typename C>
void assign(util::regex const &match,void (C::*member)(std::string,std::string),C *object,int e1,int e2)
{
assign(match,mem_bind(member,object),e1,e2);
assign(match,util::mem_bind(member,object),e1,e2);
}
template<typename C>
///
@@ -133,7 +134,7 @@ namespace cppcms {
///
void assign(util::regex const &match,void (C::*member)(std::string,std::string,std::string),C *object,int e1,int e2,int e3)
{
assign(match,mem_bind(member,object),e1,e2,e3);
assign(match,util::mem_bind(member,object),e1,e2,e3);
}
///
/// This template function is a shortcut to assign(regex,callback,int,int,int,int). It allows
@@ -142,7 +143,7 @@ namespace cppcms {
template<typename C>
void assign(util::regex const &match,void (C::*member)(std::string,std::string,std::string,std::string),C *object,int e1,int e2,int e3,int e4)
{
assign(match,mem_bind(member,object),e1,e2,e3,e4);
assign(match,util::mem_bind(member,object),e1,e2,e3,e4);
}




+ 0
- 1
worker_thread.cpp View File

@@ -16,7 +16,6 @@ using namespace cgicc;
namespace cppcms {

worker_thread::worker_thread(manager const &s) :
url(this),
app(s),
cache(this),
cout(&(this->out_buf)),


+ 2
- 3
worker_thread.h View File

@@ -14,7 +14,7 @@
#include <boost/signal.hpp>

#include "cppcms_error.h"
#include "url.h"
#include "url_dispatcher.h"
#include "cache_interface.h"
#include "base_cache.h"
#include "cgicc_connection.h"
@@ -35,7 +35,6 @@ class worker_thread: private boost::noncopyable {
int id;
pthread_t pid;

friend class url_parser;
friend class cache_iface;
friend class base_view;

@@ -54,7 +53,7 @@ class worker_thread: private boost::noncopyable {

public:

url_parser url;
url_dispatcher url;
manager const &app;
Cgicc *cgi;
CgiEnvironment const *env;


Loading…
Cancel
Save