File: cookie.c

package info (click to toggle)
uwsgi 2.0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,564 kB
  • sloc: ansic: 87,066; python: 7,004; cpp: 1,133; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (81 lines) | stat: -rw-r--r-- 1,601 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "uwsgi.h"

/*

	cookie management functions (mainly used by the internal routing subsystem)

*/

static char *check_cookie(char *cookie, uint16_t cookie_len, char *key, uint16_t keylen, uint16_t *vallen) {
	uint16_t orig_cookie_len = cookie_len-1;
	// first lstrip white spaces
	char *ptr = cookie;
	uint16_t i;
	for(i=0;i<cookie_len;i++) {
		if (isspace((int)ptr[i])) {
			cookie++;
			cookie_len--;
		}
		else {
			break;
		}
	}

	// then rstrip (skipping the first char...)
	for(i=orig_cookie_len;i>0;i--) {
		if (isspace((int)ptr[i])) {
			cookie_len--;
		}
		else {
			break;
		}
	}

	// now search for the first equal sign
	char *equal = memchr(cookie, '=', cookie_len);
	if (!equal) return NULL;
	
	if (uwsgi_strncmp(key, keylen, cookie, equal-cookie)) {
		return NULL;
	}

	cookie_len -= (equal-cookie)+1;
	if (cookie_len == 0) return NULL;

	*vallen = cookie_len;
	return equal+1;
}

char *uwsgi_get_cookie(struct wsgi_request *wsgi_req, char *key, uint16_t keylen, uint16_t *vallen) {
	uint16_t i;

	char *cookie = wsgi_req->cookie;
	uint16_t cookie_len = 0;
	char *ptr = wsgi_req->cookie;
	//start splitting by ;
	for(i=0;i<wsgi_req->cookie_len;i++) {
		if (!cookie) {
			cookie = ptr + i;
		}
		if (ptr[i] == ';') {
			char *value = check_cookie(cookie, cookie_len, key, keylen, vallen);
			if (value) {
				return value;
			}
			cookie_len = 0;
			cookie = NULL;
		}
		else {
			cookie_len++;
		}
	}

	if (cookie_len > 0) {
		char *value = check_cookie(cookie, cookie_len, key, keylen, vallen);
		if (value) {
                	return value;
                }
	}

	return NULL;
}