Browse Source

use user class to load and get user informations.

release0.1.5
Georg Hopp 12 years ago
parent
commit
19c59fd0c6
  1. 17
      assets/js/session.js
  2. 2
      include/http/response.h
  3. 23
      src/application/adapter/http/update.c
  4. 2
      src/application/login.c
  5. 3
      src/http/Makefile.am
  6. 67
      src/http/response/user.c
  7. 5
      src/usertest.c

17
assets/js/session.js

@ -8,6 +8,9 @@ function Session(sId)
this.timeout = 0;
this.timeleft = 0;
this.username = "";
this.email = "";
this.firstname = "";
this.surname = "";
this.interval = null;
this.draw();
@ -18,12 +21,15 @@ Session.prototype.loadJSON = function(data)
this.stop();
this.id = ("0" == data.id)? "none" : data.id;
this.timeout = data.timeout * 10;
this.timeleft = data.timeleft * 10;
this.username = data.username;
//this.timeout = data.timeout * 10;
//this.timeleft = data.timeleft * 10;
//this.username = data.username;
this.email = data.email;
this.firstname = data.firstname;
this.surname = data.surname;
this.eSid.empty().append(this.id);
$("#main p:eq(1) span:eq(0)").empty().append(" " + this.username);
$("#main p:eq(1) span:eq(0)").empty().append(" " + this.firstname + " " + this.surname);
this.draw();
if (0 < this.timeleft)
@ -68,6 +74,9 @@ Session.prototype.stop = function()
this.timeout = 0;
this.timeleft = 0;
this.username = "";
this.email = "";
this.firstname = "";
this.surname = "";
this.eSid.empty().append(this.id);
$("#main p:eq(1) span:eq(0)").empty().append(" " + this.username);

2
include/http/response.h

@ -30,6 +30,7 @@
#include "class.h"
#include "http/message.h"
#include "session.h"
#include "user.h"
#include "asset.h"
@ -51,6 +52,7 @@ HttpResponse httpResponseMe();
HttpResponse httpResponseLoginForm();
HttpResponse httpResponseRandval(time_t, int);
HttpResponse httpResponseSession(Session);
HttpResponse httpResponseUser(User);
HttpResponse httpResponseAsset(const char *, size_t);
#endif // __HTTP_RESPONSE_H__

23
src/application/adapter/http/update.c

@ -151,6 +151,9 @@ loginAdapter(Application application, HttpWorker worker, Session session)
if (! applicationLogin(application, credential, session)) {
worker->current_response =
new(HttpResponse, "HTTP/1.1", 403, "Forbidden");
} else {
worker->current_response =
(HttpMessage)httpResponseUser(session->user);
}
delete(credential);
@ -190,8 +193,26 @@ applicationAdapterHttpUpdate(void * _this, void * subject)
}
if (0 == strcmp("GET", worker->current_request->method)) {
if (0 == strcmp("/user/get/", worker->current_request->path)) {
worker->current_response =
(HttpMessage)httpResponseUser(session->user);
return;
}
// if (0 == strcmp("/sess/", worker->current_request->path)) {
// if (NO_SESSION_SID == sid
// || NULL == applicationSessionGet(this->application, sid)) {
// sid = applicationSessionStart(this->application, NULL, 0);
// }
//
// worker->current_response =
// (HttpMessage)httpResponseSession(
// applicationSessionGet(this->application, sid));
// return;
// }
if (0 == strcmp("/randval/", worker->current_request->path)) {
if (NO_SESSION_SID != session->id) {
if (NULL != session->user) {
worker->current_response =
(HttpMessage)httpResponseRandval(
this->application->val->timestamp,

2
src/application/login.c

@ -42,7 +42,7 @@ applicationLogin(
size_t i;
for (i=0; i<this->nauth; i++) {
if (authenticate(this->auth, credential)) {
if (authenticate(this->auth[i], credential)) {
session->user = new(User, NULL);
switch (credential->type) {

3
src/http/Makefile.am

@ -17,7 +17,8 @@ RESP = response.c \
response/login_form.c \
response/asset.c \
response/randval.c \
response/session.c
response/session.c \
response/user.c
PARSER = parser.c \
parser/parse.c \
parser/new_message.c \

67
src/http/response/user.c

@ -0,0 +1,67 @@
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2012 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include "class.h"
#include "http/response.h"
#include "http/message.h"
#include "http/header.h"
#include "session.h"
#include "utils/memory.h"
#include "hash.h"
#define RESP_DATA "{\"email\":\"%s\",\"firstname\":\"%s\",\"surname\":\"%s\"}"
HttpResponse
httpResponseUser(User user)
{
char buffer[200];
HttpResponse response;
HttpMessage message;
size_t nbuf;
response = new(HttpResponse, "HTTP/1.1", 200, "OK");
message = (HttpMessage)response;
hashAdd(message->header,
new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json")));
nbuf = sprintf(buffer, RESP_DATA,
(NULL != user)? user->email : "",
(NULL != user)? user->firstname : "",
(NULL != user)? user->surname : "");
message->nbody = nbuf;
message->body = memMalloc(nbuf);
memcpy(message->body, buffer, nbuf);
return response;
}
// vim: set ts=4 sw=4:

5
src/usertest.c

@ -58,6 +58,11 @@ main(int argc, char * argv[])
return 1;
}
insertUser(
users,
CSTRA("georg"),
CSTRA("Georg"),
CSTRA("Hopp"));
insertUser(
users,
CSTRA("georg@steffers.org"),

Loading…
Cancel
Save