summaryrefslogtreecommitdiffstats
path: root/cgi/cgicookie.c
diff options
context:
space:
mode:
authorSam Varshavchik2013-08-19 16:39:41 -0400
committerSam Varshavchik2013-08-25 14:43:51 -0400
commit9c45d9ad13fdf439d44d7443ae75da15ea0223ed (patch)
tree7a81a04cb51efb078ee350859a64be2ebc6b8813 /cgi/cgicookie.c
parenta9520698b770168d1f33d6301463bb70a19655ec (diff)
downloadcourier-libs-9c45d9ad13fdf439d44d7443ae75da15ea0223ed.tar.bz2
Initial checkin
Imported from subversion report, converted to git. Updated all paths in scripts and makefiles, reflecting the new directory hierarchy.
Diffstat (limited to 'cgi/cgicookie.c')
-rw-r--r--cgi/cgicookie.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/cgi/cgicookie.c b/cgi/cgicookie.c
new file mode 100644
index 0000000..704b7fc
--- /dev/null
+++ b/cgi/cgicookie.c
@@ -0,0 +1,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);
+}