struct mandshurica_config* config;
-char* get_cookie(const char* cookie, const char* key){
+char* get_cookie(const char* cookie, const char* key) {
char* str = mandshurica_strdup(cookie);
int i;
int start = 0;
- for(i = 0; str[i] != 0; i++){
- if(str[i] == '='){
+ for(i = 0; str[i] != 0; i++) {
+ if(str[i] == '=') {
char* k = malloc(i - start + 1);
k[i] = 0;
memcpy(k, str + start, i - start);
- if(strcmp(key, k) == 0){
+ if(strcmp(key, k) == 0) {
int epos = i + 1;
- for(; str[i] != 0 && str[i] != ';'; i++);
+ for(; str[i] != 0 && str[i] != ';'; i++)
+ ;
char* v = malloc(i - epos + 1);
- v[i] = 0;
+ v[i - epos] = 0;
memcpy(v, str + epos, i - epos);
- for(; str[i] != 0 && str[i] != ' ' && str[i] != '\t'; i++);
+ for(; str[i] != 0 && str[i] != ' ' && str[i] != '\t'; i++)
+ ;
start = i + 1;
free(k);
free(str);
return v;
- }else{
- for(; str[i] != 0 && str[i] != ';'; i++);
- for(; str[i] != 0 && str[i] != ' ' && str[i] != '\t'; i++);
+ } else {
+ for(; str[i] != 0 && str[i] != ';'; i++)
+ ;
+ for(; str[i] != 0 && str[i] != ' ' && str[i] != '\t'; i++)
+ ;
start = i + 1;
}
free(k);
int i;
for(i = 0; i < len; i++) {
cbuf[0] = buf[i];
-force_back:;
+ force_back:;
if(conlen > 0 || nolen) {
conlen--;
postbuf[conincr] = cbuf[0];
send(sock, "Content-Length: 0\r\n", 19, 0);
send(sock, "\r\n", 2, 0);
} else if(form) {
- if(strcmp(path, "/cookie-logout") == 0){
+ if(strcmp(path, "/cookie-logout") == 0) {
bool did_logout = false;
- if(cookie != NULL){
+ if(cookie != NULL) {
char* token = get_cookie(cookie, "TOKEN");
- if(token != NULL){
+ if(token != NULL) {
struct ms_db* db = mandshurica_db_open(config->mandshurica_get_param("LoginDB"));
char* usr = mandshurica_db_get(db, token);
- if(usr != NULL){
+ if(usr != NULL) {
free(usr);
did_logout = true;
}
}
free(token);
}
- if(did_logout){
+ if(did_logout) {
param.login = false;
char* type;
uint64_t len;
char* data = NULL;
char* s;
-
+
struct stat st;
char* lastmod = NULL;
const char* pth = mandshurica_strcat(webroot, "/logout-success/");
free(lastmod);
}
send(sock, "Content-Length: ", 16, 0);
-
+
char* lenstr = malloc(513);
sprintf(lenstr, "%llu", len);
send(sock, lenstr, strlen(lenstr), 0);
free(lenstr);
-
- send(sock, "\r\n", 2, 0);
- send(sock, "Set-Cookie: TOKEN=; HttpOnly; MaxAge=0\r\n", 40, 0);
+
send(sock, "\r\n", 2, 0);
+ bool use_cookie = false;
+ for(j = 0; (*config->libs)[j] != NULL; j++) {
+ const char* type = (const char*)dlsym((*config->libs)[j]->lib, "mod_type");
+ if(strcmp(type, MS_MOD_AUTH) == 0) {
+ const char* authtype = (const char*)dlsym((*config->libs)[j]->lib, "mod_auth_type");
+ if(strcmp(authtype, "Cookie") == 0) {
+ use_cookie = true;
+ }
+ }
+ }
+ if(use_cookie) {
+ send(sock, "Set-Cookie: TOKEN=; HttpOnly; MaxAge=0\r\n", 40, 0);
+ send(sock, "\r\n", 2, 0);
+ }
send(sock, data, len, 0);
free(type);
} else {
send(sock, "HTTP/1.1 200 OK\r\n", 9 + 8, 0);
send(sock, "Connection: keep-alive\r\n", 24, 0);
send(sock, "Content-Length: 0\r\n", 19, 0);
- send(sock, "Set-Cookie: TOKEN=; HttpOnly; MaxAge=0\r\n", 40, 0);
- send(sock, "\r\n", 2, 0);
+ bool use_cookie = false;
+ for(j = 0; (*config->libs)[j] != NULL; j++) {
+ const char* type = (const char*)dlsym((*config->libs)[j]->lib, "mod_type");
+ if(strcmp(type, MS_MOD_AUTH) == 0) {
+ const char* authtype = (const char*)dlsym((*config->libs)[j]->lib, "mod_auth_type");
+ if(strcmp(authtype, "Cookie") == 0) {
+ use_cookie = true;
+ }
+ }
+ }
+ if(use_cookie) {
+ send(sock, "Set-Cookie: TOKEN=; HttpOnly; MaxAge=0\r\n", 40, 0);
+ send(sock, "\r\n", 2, 0);
+ }
send(sock, "\r\n", 2, 0);
}
- }else{
+ } else {
char* type;
uint64_t len;
char* data = NULL;
char* s;
-
+
struct stat st;
char* lastmod = NULL;
const char* pth = mandshurica_strcat(webroot, "/logout-fail/");
free(lastmod);
}
send(sock, "Content-Length: ", 16, 0);
-
+
char* lenstr = malloc(513);
sprintf(lenstr, "%llu", len);
send(sock, lenstr, strlen(lenstr), 0);
free(lenstr);
-
+
send(sock, "\r\n", 2, 0);
send(sock, "\r\n", 2, 0);
send(sock, data, len, 0);
send(sock, "\r\n", 2, 0);
}
}
- }else{
+ } else {
bool sent = false;
for(j = 0; (*config->libs)[j] != NULL; j++) {
const char* type = (const char*)dlsym((*config->libs)[j]->lib, "mod_type");
if(strcmp(type, MS_MOD_AUTH) == 0) {
const char* httppath = (const char*)dlsym((*config->libs)[j]->lib, "mod_http_path");
- run_again:;
+ run_again:;
if(httppath != NULL) {
if(strcmp(httppath, path) == 0) {
int (*mod_auth)(const char*, const char*, char**) = (int (*)(const char*, const char*, char**))dlsym((*config->libs)[j]->lib, "mod_auth");
uint64_t len;
char* data = NULL;
char* s;
-
+
struct stat st;
char* lastmod = NULL;
const char* pth = mandshurica_strcat(webroot, "/login-success/");
free(lastmod);
}
send(sock, "Content-Length: ", 16, 0);
-
+
char* lenstr = malloc(513);
sprintf(lenstr, "%llu", len);
send(sock, lenstr, strlen(lenstr), 0);
free(lenstr);
-
- send(sock, "\r\n", 2, 0);
- send(sock, "Set-Cookie: ", 11, 0);
- send(sock, "TOKEN=", 6, 0);
- send(sock, token, strlen(token), 0);
- send(sock, "; HttpOnly", 10, 0);
+
send(sock, "\r\n", 2, 0);
+ bool use_cookie = false;
+ for(j = 0; (*config->libs)[j] != NULL; j++) {
+ const char* type = (const char*)dlsym((*config->libs)[j]->lib, "mod_type");
+ if(strcmp(type, MS_MOD_AUTH) == 0) {
+ const char* authtype = (const char*)dlsym((*config->libs)[j]->lib, "mod_auth_type");
+ if(strcmp(authtype, "Cookie") == 0) {
+ use_cookie = true;
+ }
+ }
+ }
+ if(use_cookie) {
+ send(sock, "Set-Cookie: ", 11, 0);
+ send(sock, "TOKEN=", 6, 0);
+ send(sock, token, strlen(token), 0);
+ send(sock, "; HttpOnly", 10, 0);
+ send(sock, "\r\n", 2, 0);
+ }
send(sock, "\r\n", 2, 0);
send(sock, data, len, 0);
free(type);
send(sock, "HTTP/1.1 200 OK\r\n", 9 + 8, 0);
send(sock, "Connection: keep-alive\r\n", 24, 0);
send(sock, "Content-Length: 0\r\n", 19, 0);
- send(sock, "Set-Cookie: ", 11, 0);
- send(sock, "TOKEN=", 6, 0);
- send(sock, token, strlen(token), 0);
- send(sock, "; HttpOnly", 10, 0);
- send(sock, "\r\n", 2, 0);
+ bool use_cookie = false;
+ for(j = 0; (*config->libs)[j] != NULL; j++) {
+ const char* type = (const char*)dlsym((*config->libs)[j]->lib, "mod_type");
+ if(strcmp(type, MS_MOD_AUTH) == 0) {
+ const char* authtype = (const char*)dlsym((*config->libs)[j]->lib, "mod_auth_type");
+ if(strcmp(authtype, "Cookie") == 0) {
+ use_cookie = true;
+ }
+ }
+ }
+ if(use_cookie) {
+ send(sock, "Set-Cookie: ", 11, 0);
+ send(sock, "TOKEN=", 6, 0);
+ send(sock, token, strlen(token), 0);
+ send(sock, "; HttpOnly", 10, 0);
+ send(sock, "\r\n", 2, 0);
+ }
send(sock, "\r\n", 2, 0);
}
free(token);
uint64_t len;
char* data = NULL;
char* s;
-
+
struct stat st;
char* lastmod = NULL;
const char* pth = mandshurica_strcat(webroot, "/login-fail/");
free(lastmod);
}
send(sock, "Content-Length: ", 16, 0);
-
+
char* lenstr = malloc(513);
sprintf(lenstr, "%llu", len);
send(sock, lenstr, strlen(lenstr), 0);
free(lenstr);
-
+
send(sock, "\r\n", 2, 0);
send(sock, "\r\n", 2, 0);
send(sock, data, len, 0);
} else if(buf[i] == '\n') {
count++;
if(count == 2) {
- if(headers != NULL){
+ if(headers != NULL) {
int j;
+ bool use_cookie = false;
+ for(j = 0; (*config->libs)[j] != NULL; j++) {
+ const char* type = (const char*)dlsym((*config->libs)[j]->lib, "mod_type");
+ if(strcmp(type, MS_MOD_AUTH) == 0) {
+ const char* authtype = (const char*)dlsym((*config->libs)[j]->lib, "mod_auth_type");
+ if(strcmp(authtype, "Cookie") == 0) {
+ use_cookie = true;
+ }
+ }
+ }
for(j = 0; headers[j] != NULL; j += 2) {
- if(mandshurica_strcaseequ(headers[j], "Cookie")){
+ if(use_cookie && mandshurica_strcaseequ(headers[j], "Cookie")) {
cookie = mandshurica_strdup(headers[j + 1]);
char* v = get_cookie(headers[j + 1], "TOKEN");
- if(v != NULL){
+ if(v != NULL) {
struct ms_db* db = mandshurica_db_open(config->mandshurica_get_param("LoginDB"));
char* username = mandshurica_db_get(db, v);
- if(username != NULL){
+ if(username != NULL) {
param.login = true;
free(username);
}