aboutsummaryrefslogtreecommitdiff
path: root/src/Cookies.c
blob: c153608469667689df602ee1f2ed56d62c97bd74 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
 /*@@
   @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 "util_String.h"

#include "httpRequest.h"
#include "Cookies.h"
#include "SString_Namespace.h"
static const char *rcsid = "$Header$";

CCTK_FILEVERSION(CactusConnect_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)
{
  String *message = String_New();

  SetToCString(message, "Set-Cookie: ");
  ConcatCString(message, name);
  ConcatCString(message, "=");
  ConcatCString(message, value);

  if(path)
  {
    ConcatCString(message, "; path=");
    ConcatCString(message, path);
  }

  if(domain)
  {
    ConcatCString(message, "; domain=");
    ConcatCString(message, domain);
  }

  if(expires)
  {
    ConcatCString(message, "; expires=");
    ConcatCString(message, expires);
  }

  if(secure)
  {
    ConcatCString(message, "; secure");
  }    

  ConcatCString(message, "\r\n");

  HTTP_SendString(request, message);

  String_Delete( 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)
{
  String *message = String_New();

  /* Clear the value */
  SetToCString(message, "Set-Cookie: ");
  ConcatCString(message, name);
  ConcatCString(message, "=");

  if(path)
  {
    ConcatCString(message, "; path=");
    ConcatCString(message, path);
  }

  /* Pick a date in the past */
  ConcatCString(message, "; expires Sun Sep 17 21:57:45 CEST 2000");
  ConcatCString(message, "\r\n");

  HTTP_SendString(request, message);

  String_Delete( 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 *cookie_name)
{
  char *retval = NULL;
  const String *COOKIE = String_Make("cookie");
  String *header = String_New();
  /* Get the cookie header */
  if( HTTP_GetHeaderValueString( request, COOKIE, header ) )
  {
    String *name = String_Make( cookie_name );
    String *value = String_New();
    size_t index = 0;
    
    /* Search for name=value */
    while( SetNextToken( header, value, ";", index ) )
    {
      size_t position = 0;
      if( FindStringFrom( value, name, &position )
      &&  position == 0 )
      {
        if( FindCharFrom( value, '=', &position )
	&&  position == Length( name ) )
        {
          TrimLeading( value, position + 1 );
          retval = Util_Strdup( GetBuffer( value ) );
        }
	break;
      }
    }
    String_Delete(name);
    String_Delete(value);
  }
  String_Delete(header);

  return retval;
}

/********************************************************************
 *********************     Local Routines   *************************
 ********************************************************************/