Add get_terminal_codepage().
[elinks.git] / src / cookies / parser.c
blob16a6b96188f308e34cc4577a29a008b327340777
1 /* Cookies name-value pairs parser */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdlib.h>
9 #include "elinks.h"
11 #include "cookies/parser.h"
12 #include "util/string.h"
15 /* In order to be able to compile parsetst, you should try to use minimum
16 * of foreign stuff here. */
18 #if 0
19 static inline void
20 debug_cookie_parser(struct cookie_str *cstr, unsigned char *pos, int ws, int eq)
22 int namelen = int_max(cstr->nam_end - cstr->str, 0);
23 int valuelen = int_max(cstr->val_end - cstr->val_start, 0);
25 printf("[%.*s] :: (%.*s) :: %d,%d [%s] %d\n",
26 namelen, cstr->str,
27 valuelen, cstr->val_start,
28 ws, eq, pos, cstr->nam_end - cstr->str);
30 #else
31 #define debug_cookie_parser(cstr, pos, ws, eq)
32 #endif
35 /* This function parses the starting name/value pair from the cookie string.
36 * The syntax is simply: <name token> [ '=' <value token> ] with possible
37 * spaces between tokens and '='. However spaces in the value token is also
38 * allowed. See bug 174 for a description why. */
39 /* Defined in RFC 2965. */
40 /* Return cstr on success, NULL on failure. */
41 struct cookie_str *
42 parse_cookie_str(struct cookie_str *cstr, unsigned char *str)
44 memset(cstr, 0, sizeof(*cstr));
45 cstr->str = str;
47 /* Parse name token */
48 while (*str != ';' && *str != '=' && !isspace(*str) && *str)
49 str++;
51 /* Bail out if name token is empty */
52 if (str == cstr->str) return NULL;
54 cstr->nam_end = str;
56 skip_space(str);
58 switch (*str) {
59 case '\0':
60 case ';':
61 /* No value token, so just set to empty value */
62 cstr->val_start = str;
63 cstr->val_end = str;
64 return cstr;
66 case '=':
67 /* Map 'a===b' to 'a=b' */
68 do str++; while (*str == '=');
69 break;
71 default:
72 /* No spaces in the name token is allowed */
73 return NULL;
76 skip_space(str);
78 /* Parse value token */
80 /* Start with empty value, so even 'a=' will work */
81 cstr->val_start = str;
82 cstr->val_end = str;
84 for (; *str != ';' && *str; str++) {
85 /* Allow spaces in the value but leave out ending spaces */
86 if (!isspace(*str))
87 cstr->val_end = str + 1;
90 return cstr;