int main(void){ pci_init(); #ifdef ENABLE_FASTCGI while(FCGI_Accept() >= 0) { #endif qentry_t *req = qcgireq_parse(NULL, 0); char *name = req->getstr(req, "username", true); if(name == NULL){ qcgires_redirect(req, BAD_REGISTER); goto end; } char *admin = req->getstr(req, "adminpassword", true); if(admin == NULL){ qcgires_redirect(req, BAD_REGISTER); free(name); goto end; } if(strncmp(admin, ADMIN_SECRET, strlen(ADMIN_SECRET)) != 0){ fprintf(stderr, "%s%s\n", "Invalid Registration Attempt: ", admin); qcgires_redirect(req, BAD_REGISTER); free(name); free(admin); goto end; } if( create_user(name) == 1 ){ /* Log the User in */ qentry_t *sess = NULL; sess = qcgisess_init(req, NULL); qcgisess_settimeout(sess, SESSION_TIME); if(sess){ sess->putstr(sess, "username", name, true); qcgisess_save(sess); sess->free(sess); } qcgires_redirect(req, APPLICATION); }else{ fprintf(stderr, "%s%s\n", "Could not create user: "******"text/html"); // De-allocate memories req->free(req); #ifdef ENABLE_FASTCGI } #endif return 0; }
int main(void) { #ifdef ENABLE_FASTCGI while(FCGI_Accept() >= 0) { #endif qentry_t *req = qcgireq_parse(NULL, 0); // fetch queries time_t expire = (time_t)req->getint(req, "expire"); char *mode = req->getstr(req, "mode", false); char *name = req->getstr(req, "name", false); char *value = req->getstr(req, "value", false); // start session. qentry_t *sess = qcgisess_init(req, NULL); // Mose case, you don't need to set timeout. this is just example if (expire > 0) qcgisess_settimeout(sess, expire); if (mode) { switch (mode[0]) { case 's': // set req->putstr(sess, name, value, true); break; case 'r': // remove req->remove(sess, name); break; case 'd': // destroy qcgisess_destroy(sess); qcgires_setcontenttype(req, "text/plain"); printf("Session destroyed.\n"); #ifdef ENABLE_FASTCGI continue; #else return 0; #endif case 'v': // view default: break; } } // screen out qcgires_setcontenttype(req, "text/plain"); req->print(sess, stdout, true); // save session & free allocated memories qcgisess_save(sess); sess->free(sess); req->free(req); #ifdef ENABLE_FASTCGI } #endif return 0; }
int main(void){ pci_init(); #ifdef ENABLE_FASTCGI while(FCGI_Accept() >= 0) { #endif qentry_t *req = qcgireq_parse(NULL, 0); char * username = NULL; qentry_t *sess = NULL; sess = qcgisess_init(req, NULL); qcgisess_settimeout(sess, SESSION_TIME); if(sess == NULL){ qcgires_redirect(req, LOGIN); qcgisess_save(sess); goto end; } username = sess->getstr(sess, "username", true); sess->putstr(sess, "username", username, true); qcgisess_save(sess); if(username == NULL){ fprintf(stderr, "%s\n", "Session expired"); qcgires_redirect(req, LOGIN); qcgisess_save(sess); goto end; } char * message = NULL; char * targetUser = NULL; message = req->getstr(req, "message", true); targetUser = req->getstr(req, "user", true); if(message == NULL || targetUser == NULL){ free(username); if(message != NULL) free(message); if(targetUser != NULL) free(targetUser); goto end; } int success = updateConversation(username, targetUser, message); if(success == 0) qcgires_redirect(req, BAD_TALK); else qcgires_redirect(req, APPLICATION); free(username); free(message); free(targetUser); // De-allocate memories end: qcgires_setcontenttype(req, "text/html"); if(sess){ sess->free(sess); } req->free(req); #ifdef ENABLE_FASTCGI } #endif return 0; }
/** * Initialize session * * @param request a pointer of request structure returned by qcgireq_parse() * @param dirpath directory path where session data will be kept * * @return a pointer of malloced session data list (qentry_t type) * * @note * The returned qentry_t list must be de-allocated by calling qentry_t->free(). * And if you want to append or remove some user session data, use qentry_t->*() * functions then finally call qcgisess_save() to store updated session data. */ qentry_t *qcgisess_init(qentry_t *request, const char *dirpath) { // check content flag if (qcgires_getcontenttype(request) != NULL) { DEBUG("Should be called before qRequestSetContentType()."); return NULL; } qentry_t *session = qEntry(); if (session == NULL) return NULL; // check session status & get session id bool new_session; char *sessionkey; if (request->getstr(request, SESSION_ID, false) != NULL) { sessionkey = request->getstr(request, SESSION_ID, true); new_session = false; } else { // new session sessionkey = _genuniqid(); new_session = true; } // make storage path for session char session_repository_path[PATH_MAX]; char session_storage_path[PATH_MAX]; char session_timeout_path[PATH_MAX]; time_t session_timeout_interval = (time_t)SESSION_DEFAULT_TIMEOUT_INTERVAL; // seconds #pragma warning (disable:4996) if (dirpath != NULL) strncpy(session_repository_path, dirpath, sizeof(session_repository_path)); else strncpy(session_repository_path, SESSION_DEFAULT_REPOSITORY, sizeof(session_repository_path)); snprintf(session_storage_path, sizeof(session_storage_path), "%s/%s%s%s", session_repository_path, SESSION_PREFIX, sessionkey, SESSION_STORAGE_EXTENSION); snprintf(session_timeout_path, sizeof(session_timeout_path), "%s/%s%s%s", session_repository_path, SESSION_PREFIX, sessionkey, SESSION_TIMEOUT_EXTENSION); // validate exist session if (new_session == false) { int valid = _is_valid_session(session_timeout_path); if (valid <= 0) { // expired or not found if (valid < 0) { _q_unlink(session_storage_path); _q_unlink(session_timeout_path); } // remake storage path free(sessionkey); sessionkey = _genuniqid(); snprintf(session_storage_path, sizeof(session_storage_path), "%s/%s%s%s", session_repository_path, SESSION_PREFIX, sessionkey, SESSION_STORAGE_EXTENSION); snprintf(session_timeout_path, sizeof(session_timeout_path), "%s/%s%s%s", session_repository_path, SESSION_PREFIX, sessionkey, SESSION_TIMEOUT_EXTENSION); // set flag new_session = true; } } // if new session, set session id if (new_session == true) { qcgires_setcookie(request, SESSION_ID, sessionkey, 0, "/", NULL, NULL); // force to add session_in to query list request->putstr(request, SESSION_ID, sessionkey, true); // save session informations char created_sec[10+1]; snprintf(created_sec, sizeof(created_sec), "%ld", (long int)time(NULL)); session->putstr(session, INTER_SESSIONID, sessionkey, false); session->putstr(session, INTER_SESSION_REPO, session_repository_path, false); session->putstr(session, INTER_CREATED_SEC, created_sec, false); session->putint(session, INTER_CONNECTIONS, 1, false); // set timeout interval qcgisess_settimeout(session, session_timeout_interval); } else { // read session properties // read exist session informations session->load(session, session_storage_path); // update session informations int conns = session->getint(session, INTER_CONNECTIONS); session->putint(session, INTER_CONNECTIONS, ++conns, true); // set timeout interval qcgisess_settimeout(session, session->getint(session, INTER_INTERVAL_SEC)); } free(sessionkey); // set globals return session; }
int main(void){ pci_init(); #ifdef ENABLE_FASTCGI while(FCGI_Accept() >= 0) { #endif qentry_t *req = qcgireq_parse(NULL, 0); qentry_t *sess = NULL; sess = qcgisess_init(req, NULL); qcgisess_settimeout(sess, SESSION_TIME); qcgires_setcontenttype(req, "application/json"); if(!sess){ printf("%s", "{ text : \"Your Session has expired please login again\"}"); goto end; } char *name = sess->getstr(sess, "username", true); if(name == NULL){ printf("%s", "{ text : \"Your Session has expired and your username could not be determined, please login again\"}"); goto end; } if(sess){ sess->putstr(sess, "username", name, true); qcgisess_save(sess); } printf("{\"text\":"); char * target = req->getstr(req, "target", true); if(target == NULL){ printf("\"you must select a user to talk to\"}"); free(name); goto end; } char * filename = req->getstr(req, "f", true); if(filename == NULL){ printf("\"You must choose a file to read\"}"); free(name); free(target); goto end; } FILE * fp = getConversationByFileName(name, target, filename); if(fp == NULL){ free(name); printf("%s%s%s","\"Could not open conversation with user for reading: ", target,"\"}"); free(target); free(filename); goto end; } int cOrEOF; char c; printf("\""); while( (cOrEOF = fgetc(fp)) != EOF){ c = (char)cOrEOF; if(c == '"') printf("\\%c", c); else if(c == '\n') printf("<br/>"); else printf("%c", c); } fclose(fp); printf("\"}"); free(filename); free(target); free(name); end: if(sess){ sess->free(sess); } // De-allocate memories req->free(req); #ifdef ENABLE_FASTCGI } #endif return 0; }