Home  /  RSS  /  RSS Comments  /  Enter

Session Sharing with Non-CppCMS technologies

11/16/15, by artyom ; Posted in: Progress; one comment

One of the problems in integrating different technologies on same web site is sharing the data between them, in particular sharing session data.

For example you have a huge web platform written in PHP or Java and you want to improve performance of certain subsystems poring them to CppCMS. On of the first issues you'd encounter is how to share the session between them - so every side would know who is the user, what permissions he has, etc.

So I made cppcms::session_pool and cppcms::session_interface accessible outside the usual request/response scope and wrapped it with pure C API - such that no C++ exceptions are thrown and every function is resolvable via dlopen/GetProcAddress- to make it more accessible for integration with different languages.

Several modules for different programming languages were implemented allowing smooth integration with their web frameworks and APIs:

Actually there is no particular limits regarding technology - just a question of implementing loadable module for a specific language/platform.

In general it consists of a SessionPool object that is created from a configuration file and exists globally. It generates a special Session objects that is loaded from Http Request cookies, updated and saved to the Http Response object.

It looks like this:

PHP:

// pool initialization
$pool=CppCMS_SessionPool::from_config('cppcms-config.js');
// per request session access
$session=$pool->session();
$session->load();
$x=0;
if($session->is_set('x')) {
        $x=$session['x'];
}

$x=intval($x)+1;
$session['x']=$x;
$session->save();
...

Java/Servlet:

static SessionPool pool;

public void init() throws ServletException
{
    pool = SessionPool.openFromConfig("/path/to/cppcms-config.js");
}

public void doGet(HttpServletRequest request,
                  HttpServletResponse response) 
                    throws ServletException, IOException
{
    Session session = pool.getSession();
    session.load(request);
    String x="0";
    if(session.isSet("x"))
        x=session.get("x");
    x=Integer.toString(Integer.parseInt(x)+1);
    session.set("x",x);
    session.save(response);
    session.close();
    ...
}

Python with Django:

# Create global pool
pool=cppcms.SessionPool('/path/to/cppcms-config.js')

# Actual view
def home(request):
    s=pool.session()
    s.load(django_request=request)
    v='0'
    if 'x' in s:
            v= s['x']
    s['x']=str(int(v)+1)
    response = HttpResponse()
    s.save(django_response=response)
    ...

C#/ASP.Net:

static SessionPool pool;
static Example() {
    pool = SessionPool.FromConfig("cppcms-config.js");
}
protected void Page_Load(object sender,EventArgs e)
{
    using(Session s = pool.Session()) {
        s.Load(Request);
        string v="0";
        if(s.IsSet("x"))
            v=s["x"];
        v = (int.Parse(v) + 1).ToString();
        s["x"]=v;
        s.Save(Response);
    }
    ...
}

So basically you have a full access to CppCMS session from 3rd party most popular technologies.

Still thinking of implementing a module for Ruby on Rails but I have never written a line of code in Ruby so it is quite challenging for me. I'll probably wait till somebody contributes one.

Comments

alexey, at 3/18/16, 1:36 AM

add some twit/retwit/google+ buttons, or start an account there. there are many c++ coders

Add Comment:

 
 the email would not displayed
 

You can write your messages using Markdown syntax.

You must enable JavaScript in order to post comments.

Pages

Categories