summaryrefslogtreecommitdiffstats
path: root/cgi/cgicookie.c
blob: 704b7fcc50c0068be7cee10c37ff97b1abd9d4b6 (plain)
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
/*
** Copyright 2007 Double Precision, Inc.
** See COPYING for distribution information.
*/

/*
*/

#include	"cgi.h"
#include	<stdio.h>
#include	<string.h>
#include	<stdlib.h>
#include	<ctype.h>

extern void error(const char *);

static void enomem()
{
	error("Out of memory.");
}

char	*cgi_cookie(const char *p)
{
size_t	pl=strlen(p);
const char *c=getenv("HTTP_COOKIE");
char	*buf;

	while (c && *c)
	{
	size_t	i;

		for (i=0; c[i] && c[i] != '='; i++)
			;
		if (i == pl && strncmp(p, c, i) == 0)
		{
			c += i;
			if (*c)	++c;	/* skip over = */
			for (i=0; c[i] && c[i] != ';'; i++)
				;

			buf=malloc(i+1);
			if (!buf)	enomem();
			memcpy(buf, c, i);
			buf[i]=0;
			cgiurldecode(buf);
			return (buf);
		}
		c=strchr(c, ';');
		if (c)
			do
			{
				++c;
			} while (isspace((int)(unsigned char)*c));
	}
	buf=malloc(1);
	if (!buf)	enomem();
	*buf=0;
	return (buf);
}

void cgi_setcookie(const char *name, const char *value)
{
char	*p;
const	char *sn;

	p=cgiurlencode(value);
	sn=getenv("SCRIPT_NAME");
	if (!sn || !*sn)
		sn="/";
	printf("Set-Cookie: %s=%s; path=%s\n", name, value, sn);
	free(p);
}