Subversion Repositories RepoView

Rev

Rev 1 | Rev 5 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1 Rev 3
Line 1... Line 1...
1
/* $Id: cookie.c 1 2024-08-20 19:18:25Z nishi $ */
1
/* $Id: cookie.c 3 2024-08-20 21:05:24Z nishi $ */
-
 
2
 
-
 
3
#include "rv_auth.h"
-
 
4
 
-
 
5
#include "rv_util.h"
-
 
6
 
-
 
7
#include <stdio.h>
-
 
8
#include <stdlib.h>
-
 
9
 
-
 
10
extern char** environ;
-
 
11
 
-
 
12
struct cookie_entry {
-
 
13
	char* key;
-
 
14
	char* value;
-
 
15
};
-
 
16
 
-
 
17
struct cookie_entry** cookie_entries;
-
 
18
 
-
 
19
void parse_cookie(void) {
-
 
20
	cookie_entries = malloc(sizeof(*cookie_entries));
-
 
21
	cookie_entries[0] = NULL;
-
 
22
	char* cookie = getenv("HTTP_COOKIE");
-
 
23
	if(cookie != NULL) {
-
 
24
		cookie = rv_strdup(cookie);
-
 
25
		int i;
-
 
26
		int incr = 0;
-
 
27
		for(i = 0;; i++) {
-
 
28
			if(cookie[i] == 0 || cookie[i] == ';') {
-
 
29
				char oldc = cookie[i];
-
 
30
				cookie[i] = 0;
-
 
31
 
-
 
32
				char* key = cookie + incr;
-
 
33
				char* value = "";
-
 
34
 
-
 
35
				int j;
-
 
36
				for(j = 0; key[j] != 0; j++) {
-
 
37
					if(key[j] == '=') {
-
 
38
						key[j] = 0;
-
 
39
						value = key + j + 1;
-
 
40
						break;
-
 
41
					}
-
 
42
				}
-
 
43
				struct cookie_entry* entry = malloc(sizeof(*entry));
-
 
44
				entry->key = rv_strdup(key);
-
 
45
				entry->value = rv_strdup(value);
-
 
46
 
-
 
47
				struct cookie_entry** old_entries = cookie_entries;
-
 
48
				for(j = 0; old_entries[j] != NULL; j++)
-
 
49
					;
-
 
50
				cookie_entries = malloc(sizeof(*cookie_entries) * (j + 2));
-
 
51
				for(j = 0; old_entries[j] != NULL; j++) {
-
 
52
					cookie_entries[j] = old_entries[j];
-
 
53
				}
-
 
54
				cookie_entries[j] = entry;
-
 
55
				cookie_entries[j + 1] = NULL;
-
 
56
 
-
 
57
				int oldi = i;
-
 
58
				i++;
-
 
59
				for(; cookie[i] != 0 && (cookie[i] == ' ' || cookie[i] == '\t'); i++)
-
 
60
					;
-
 
61
				i--;
-
 
62
				incr = i + 1;
-
 
63
				if(oldc == 0) break;
-
 
64
			}
-
 
65
		}
-
 
66
		free(cookie);
-
 
67
	}
-
 
68
}
-
 
69
 
-
 
70
char* rv_logged_in(void) {
-
 
71
	parse_cookie();
-
 
72
	return NULL;
-
 
73
}
-
 
74
 
-
 
75
void rv_free_auth(void) {
-
 
76
	int i;
-
 
77
	for(i = 0; cookie_entries[i] != NULL; i++) {
-
 
78
		free(cookie_entries[i]->key);
-
 
79
		free(cookie_entries[i]->value);
-
 
80
		free(cookie_entries[i]);
-
 
81
	}
-
 
82
	free(cookie_entries);
-
 
83
}