diff options
-rw-r--r-- | doc/Cookies.txt | 80 | ||||
-rw-r--r-- | src/Cookies.c | 198 | ||||
-rw-r--r-- | src/http_Cookies.h | 40 | ||||
-rw-r--r-- | src/make.code.defn | 9 |
4 files changed, 326 insertions, 1 deletions
diff --git a/doc/Cookies.txt b/doc/Cookies.txt new file mode 100644 index 0000000..cd9a371 --- /dev/null +++ b/doc/Cookies.txt @@ -0,0 +1,80 @@ + /*@@ + @file Cookies.txt + @date Mon Sep 18 23:17:20 2000 + @author Tom Goodale + @desc + Description of the Cookie interface. + @enddesc + @version $Header$ + @@*/ + +There is an interface for setting and querying cookies, which is +accessed by including http_Cookies.h. This provides three functions + +int HTTP_CookieSend(httpRequest *request, + const char *name, + const char *value, + const char *path, + const char *domain, + const char *expires, + int secure); + +which will send a cookie with the appropriate name and value. All +other fields may be set to NULL, or 0 as appropriate. + +int HTTP_CookieCancel(httpRequest *request, + const char *name, + const char *path); + +which cancels a cookie by setting its value to "" and setting the +expiry date to the past. + +These two functions produce header lines, so should go between the +HTTP response (e.g. HTTP/1.0 200 OK), and the "\r\n\r\n" seperating +the header from the body. + +To get the value of a cookie, call + +char *HTTP_CookieGet(httpRequest *request, + const char *name); + +Note that the returned string must be freed afterwards. + +Currently no encoding is done on the value field; this will be added +later. As a result things could go seriously wrong if you include a +';' or 'name=', where 'name' is a cookie name, in the value field. + + +Example: +-------- + + /* Status message */ + strcpy(message,"HTTP/1.0 200 OK\r\n"); + + HTTP_Write(request, message, strlen(message)); + + /* Cookie */ + HTTP_CookieSend(request, "user", "niceuser", NULL,NULL,NULL,0); + + strcpy(message,"Content-Type: text/html\r\n\r\n"); + + HTTP_Write(request, message, strlen(message)); + + /* Start of body */ + strcpy(message, "<HTML><HEAD><TITLE>Cookie Test</TITLE></HEAD>\n"); + + strcat(message, "<BODY><center><h1>Cookie Test</h1></center>"); + + HTTP_Write(request, message, strlen(message)); + + strcpy(message, "<center>"); + HTTP_Write(request, message, strlen(message)); + + value2 = HTTP_CookieGet(request,"user"); + + sprintf(message, "<p>Cookie is '%s'</p>\n", value2); + + free(value2); + +... + diff --git a/src/Cookies.c b/src/Cookies.c new file mode 100644 index 0000000..7cf5199 --- /dev/null +++ b/src/Cookies.c @@ -0,0 +1,198 @@ + /*@@ + @file Cookies.c + @date Mon Sep 18 21:08:37 2000 + @author Tom Goodale + @desc + Cookie stuff. + @enddesc + @version $Header$ + @@*/ + +#include "cctk.h" + +#include <stdlib.h> +#include <string.h> + +#include "util_String.h" + +#include "http_Request.h" +#include "http_Cookies.h" + +static char *rcsid = "$Header$"; + +CCTK_FILEVERSION(DevThorns_httpd_Cookies_c) + +/******************************************************************** + ********************* Local Data Types *********************** + ********************************************************************/ + +/******************************************************************** + ********************* Local Routine Prototypes ********************* + ********************************************************************/ + +/******************************************************************** + ********************* Other Routine Prototypes ********************* + ********************************************************************/ + +/******************************************************************** + ********************* Local Data ***************************** + ********************************************************************/ + +/******************************************************************** + ********************* External Routines ********************** + ********************************************************************/ + + /*@@ + @routine HTTP_CookieSend + @date Mon Sep 18 22:42:35 2000 + @author Tom Goodale + @desc + Sends a cookie to a browser. + @enddesc + @calls + @calledby + @history + + @endhistory + +@@*/ +int HTTP_CookieSend(httpRequest *request, + const char *name, + const char *value, + const char *path, + const char *domain, + const char *expires, + int secure) +{ + char message[8000]; + + sprintf(message, "Set-Cookie: %s=%s", name, value); + + if(path) + { + sprintf(message, "%s; path=%s", message, path); + } + + if(domain) + { + sprintf(message,"%s; domain=%s", message, domain); + } + + if(expires) + { + sprintf(message,"%s; expires %s", message, expires); + } + + if(secure) + { + sprintf(message,"%s; secure=TRUE", message); + } + + strcat(message, "\r\n"); + + HTTP_Write(request, message, strlen(message)); + + return 0; +} + + /*@@ + @routine HTTP_CookieCancel + @date Mon Sep 18 22:43:04 2000 + @author Tom Goodale + @desc + Cancels a cookie. + @enddesc + @calls + @calledby + @history + + @endhistory + +@@*/ +int HTTP_CookieCancel(httpRequest *request, + const char *name, + const char *path) +{ + char message[8000]; + + /* Clear the value */ + sprintf(message, "%s=", name); + + if(path) + { + sprintf(message, "%s; path=%s", message, path); + } + + /* Pick a date in the past */ + sprintf(message,"%s; expires Sun Sep 17 21:57:45 CEST 2000", message); + + strcat(message, "\r\n"); + + HTTP_Write(request, message, sizeof(message)); + + return 0; +} + + /*@@ + @routine HTTP_CookieGet + @date Mon Sep 18 22:43:20 2000 + @author Tom Goodale + @desc + Gets the value of a cookie from a request. + @enddesc + @calls + @calledby + @history + + @endhistory + +@@*/ +char *HTTP_CookieGet(httpRequest *request, + const char *name) +{ + char *retval; + const char *header; + char *position; + char *copy; + char *start; + + /* Get the cookie header */ + header = HTTP_HeaderValue(request,"Cookie"); + + retval = NULL; + + if(header) + { + copy = Util_Strdup(header); + + position = copy; + + /* Search for name=value */ + while(position = strstr(position, name)) + { + position += strlen(name); + if(*position == '=') + { + start = position+1; + + position=strstr(start, ";"); + + if(position) + { + *position = 0; + } + retval = Util_Strdup(start); + break; + } + } + } + + free(copy); + + return retval; +} + +/******************************************************************** + ********************* Local Routines ************************* + ********************************************************************/ + diff --git a/src/http_Cookies.h b/src/http_Cookies.h new file mode 100644 index 0000000..5179227 --- /dev/null +++ b/src/http_Cookies.h @@ -0,0 +1,40 @@ + /*@@ + @header http_Cookies.h + @date Mon Sep 18 22:40:06 2000 + @author Tom Goodale + @desc + Functions to manipulate cookies. + @enddesc + @version $Header$ + @@*/ + +#ifndef __HTTP_COOKIES_H__ +#define __HTTP_COOKIES_H__ 1 + +#ifdef __cplusplus +extern "C" +{ +#endif + +int HTTP_CookieSend(httpRequest *request, + const char *name, + const char *value, + const char *path, + const char *domain, + const char *expires, + int secure); + +int HTTP_CookieCancel(httpRequest *request, + const char *name, + const char *path); + +char *HTTP_CookieGet(httpRequest *request, + const char *name); + + +#ifdef __cplusplus +} +#endif + +#endif /* __HTTP_COOKIES_H__ */ + diff --git a/src/make.code.defn b/src/make.code.defn index fcc4c5d..65d2b04 100644 --- a/src/make.code.defn +++ b/src/make.code.defn @@ -13,7 +13,14 @@ STEERING_SRC = Steer.c AUTHENTICATION_SRC = Authorisation.c base64.c -SRCS = $(MISC_SRC) $(SERVER_SRC) $(CONTENT_SRC) $(STEERING_SRC) $(AUTHENTICATION_SRC) +COOKIES_SRC = Cookies.c + +SRCS = $(MISC_SRC) \ + $(SERVER_SRC) \ + $(CONTENT_SRC) \ + $(STEERING_SRC) \ + $(AUTHENTICATION_SRC) \ + $(COOKIES_SRC) # Subdirectories containing source files SUBDIRS = |