Kannel: Open Source WAP and SMS gateway  svn-r5335
cookies.h File Reference

Go to the source code of this file.

Data Structures

struct  _cookie
 

Macros

#define MAX_HTTP_DATE_LENGTH   128
 

Typedefs

typedef struct _cookie Cookie
 

Functions

Cookiecookie_create (void)
 
void cookies_destroy (List *)
 
int get_cookies (List *, const WSPMachine *)
 
int set_cookies (List *, WSPMachine *)
 

Macro Definition Documentation

◆ MAX_HTTP_DATE_LENGTH

#define MAX_HTTP_DATE_LENGTH   128

Definition at line 106 of file cookies.h.

Referenced by parse_http_date().

Typedef Documentation

◆ Cookie

typedef struct _cookie Cookie

Function Documentation

◆ cookie_create()

Cookie* cookie_create ( void  )

Definition at line 88 of file cookies.c.

References emptyCookie.

Referenced by parse_cookie().

89 {
90  Cookie *p;
91 
92  p = gw_malloc(sizeof(Cookie)); /* Never returns NULL */
93 
94  *p = emptyCookie;
95  p -> max_age = -1;
96  time (&p -> birth);
97  return p;
98 }
static Cookie emptyCookie
Definition: cookies.c:85

◆ cookies_destroy()

void cookies_destroy ( List )

Definition at line 100 of file cookies.c.

References cookie_destroy(), gwlib_assert_init(), and gwlist_destroy().

101 {
103 
104  if (cookies == NULL)
105  return;
106 
107  gwlist_destroy(cookies, cookie_destroy);
108 }
static void cookie_destroy(void *)
Definition: cookies.c:455
void() gwlib_assert_init(void)
Definition: gwlib.c:72
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145

◆ get_cookies()

int get_cookies ( List ,
const WSPMachine  
)

Definition at line 111 of file cookies.c.

References add_cookie_to_cache(), cookie_destroy(), debug(), error(), get_header_value(), gwlist_get(), gwlist_len(), have_cookie(), info(), name, octstr_get_cstr, and parse_cookie().

Referenced by return_reply().

112 {
113  Octstr *header = NULL;
114  Octstr *value = NULL;
115  Cookie *cookie = NULL;
116  long pos = 0;
117 
118  /*
119  * This can happen if the user aborts while the HTTP request is pending from the server.
120  * In that case, the session machine is destroyed and is not available to this function
121  * for cookie caching.
122  */
123 
124  if (sm == NULL) {
125  info (0, "No session machine for cookie retrieval");
126  return 0;
127  }
128 
129  for (pos = 0; pos < gwlist_len(headers); pos++) {
130  header = gwlist_get(headers, pos);
131  /* debug ("wap.wsp.http", 0, "get_cookies: Examining header (%s)", octstr_get_cstr (header)); */
132  if (strncasecmp ("set-cookie", octstr_get_cstr (header),10) == 0) {
133  debug ("wap.wsp.http", 0, "Caching cookie (%s)", octstr_get_cstr (header));
134 
135  if ((value = get_header_value (header)) == NULL) {
136  error (0, "get_cookies: No value in (%s)", octstr_get_cstr(header));
137  continue;
138  }
139 
140  /* Parse the received cookie */
141  if ((cookie = parse_cookie(value)) != NULL) {
142 
143  /* Check to see if this cookie is already present */
144  if (have_cookie(sm->cookies, cookie) == 1) {
145  debug("wap.wsp.http", 0, "parse_cookie: Cookie present");
146  cookie_destroy(cookie);
147  continue;
148  } else {
149  add_cookie_to_cache(sm, cookie);
150  debug("wap.wsp.http", 0, "get_cookies: Added (%s)",
151  octstr_get_cstr(cookie -> name));
152  }
153  }
154  }
155  }
156 
157  debug("wap.wsp.http", 0, "get_cookies: End");
158  return 0;
159 }
void error(int err, const char *fmt,...)
Definition: log.c:648
void info(int err, const char *fmt,...)
Definition: log.c:672
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
static int have_cookie(List *, Cookie *)
Definition: cookies.c:361
static Octstr * get_header_value(Octstr *)
Definition: cookies.c:218
static void cookie_destroy(void *)
Definition: cookies.c:455
static Cookie * parse_cookie(Octstr *)
Definition: cookies.c:248
char * name
Definition: smsc_cimd2.c:212
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
static void add_cookie_to_cache(const WSPMachine *, Cookie *)
Definition: cookies.c:344

◆ set_cookies()

int set_cookies ( List ,
WSPMachine  
)

Definition at line 162 of file cookies.c.

References debug(), _cookie::domain, error(), expire_cookies(), gwlist_append(), gwlist_get(), gwlist_len(), _cookie::name, octstr_append(), octstr_append_char(), octstr_create, octstr_get_cstr, _cookie::path, _cookie::value, and _cookie::version.

Referenced by start_fetch().

163 {
164  Cookie *value = NULL;
165  Octstr *cookie = NULL;
166  long pos = 0;
167 
168  if (headers == NULL || sm == NULL) {
169  error (0, "set_cookies: Null argument(s) - no headers, WSPMachine or both");
170  return -1;
171  }
172 
173  /* Expire cookies that have timed out */
174  expire_cookies(sm->cookies);
175 
176  /* Walk through the cookie cache, adding the cookie to the request headers */
177  if (gwlist_len(sm->cookies) > 0) {
178  debug("wap.wsp.http", 0, "set_cookies: Cookies in cache");
179 
180  for (pos = 0; pos < gwlist_len(sm->cookies); pos++) {
181  value = gwlist_get(sm->cookies, pos);
182 
183  cookie = octstr_create("Cookie: ");
184  if (value->version)
185  octstr_append(cookie, value->version);
186  octstr_append(cookie, value->name);
187  octstr_append_char(cookie, '=');
188  octstr_append(cookie, value->value);
189 
190  if (value->path) {
191  octstr_append_char(cookie, ';');
192  octstr_append(cookie, value->path);
193  }
194  if (value->domain) {
195  octstr_append_char(cookie, ';');
196  octstr_append(cookie, value->domain);
197  }
198 
199  gwlist_append(headers, cookie);
200  debug("wap.wsp.http", 0, "set_cookies: Added (%s)", octstr_get_cstr (cookie));
201  }
202  } else
203  debug("wap.wsp.http", 0, "set_cookies: No cookies in cache");
204 
205  return 0;
206 }
void error(int err, const char *fmt,...)
Definition: log.c:648
void gwlist_append(List *list, void *item)
Definition: list.c:179
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1504
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_get(List *list, long pos)
Definition: list.c:292
void octstr_append_char(Octstr *ostr, int ch)
Definition: octstr.c:1517
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
static void expire_cookies(List *)
Definition: cookies.c:419
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.