1 | #include "cookie.h"
|
---|
2 | #include <string.h>
|
---|
3 |
|
---|
4 | struct cookie_write_control g_cookie_write_control;
|
---|
5 | #define CWC(x) g_cookie_write_control.x
|
---|
6 |
|
---|
7 | void cookie_write_open(char *buffer, unsigned int buflen)
|
---|
8 | {
|
---|
9 | // memset(buffer, 0, buflen);
|
---|
10 | CWC(buffer)= buffer;
|
---|
11 | CWC(buflen)= buflen;
|
---|
12 | CWC(stackpos)=0;
|
---|
13 | }
|
---|
14 |
|
---|
15 | void cookie_write_putch(char c)
|
---|
16 | {
|
---|
17 | *CWC(buffer)++ = c;
|
---|
18 | CWC(stackpos)++;
|
---|
19 | }
|
---|
20 |
|
---|
21 | void cookie_write_putstr(char *str)
|
---|
22 | {
|
---|
23 | while(*str != '\0') cookie_write_putch(*str++);
|
---|
24 | }
|
---|
25 |
|
---|
26 | void cookie_set(char *key, char* value)
|
---|
27 | {
|
---|
28 | cookie_write_putstr("Set-Cookie: ");
|
---|
29 | cookie_write_putstr(key);
|
---|
30 | cookie_write_putch('=');
|
---|
31 | cookie_write_putstr(value);
|
---|
32 | cookie_write_putstr("; path=/; SameSite=Strict\r\n");
|
---|
33 | }
|
---|
34 |
|
---|
35 | void cookie_close(void)
|
---|
36 | {
|
---|
37 | cookie_write_putch('\0');
|
---|
38 | }
|
---|
39 |
|
---|
40 | void cookie_parse_tokens(cookie_token_data_typDef *cookie)
|
---|
41 | {
|
---|
42 | if(cookie == NULL) return;
|
---|
43 | uint16_t count_token = 0, pos_char = 0;
|
---|
44 | for(; pos_char < cookie->len; pos_char++){
|
---|
45 | switch(cookie->data[pos_char]){
|
---|
46 | case '=':
|
---|
47 | cookie->tokens[count_token].end = pos_char - 1;
|
---|
48 | count_token++;
|
---|
49 | cookie->tokens[count_token].start = pos_char + 1;
|
---|
50 | break;
|
---|
51 |
|
---|
52 | case ';':
|
---|
53 | cookie->tokens[count_token].end = pos_char - 1;
|
---|
54 | count_token++;
|
---|
55 | break;
|
---|
56 |
|
---|
57 | case ' ':
|
---|
58 | cookie->tokens[count_token].start = pos_char + 1;
|
---|
59 | break;
|
---|
60 | default:
|
---|
61 | if(pos_char == 0) cookie->tokens[count_token].start = pos_char;
|
---|
62 | cookie->tokens[count_token].size++;
|
---|
63 | break;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | cookie->tokens[count_token].end = pos_char - 1;
|
---|
67 | cookie->tokenCount = count_token + 1;
|
---|
68 | }
|
---|
69 |
|
---|
70 | void cookie_get_option(cookie_token_data_typDef *cookie, char *option, char *value)
|
---|
71 | {
|
---|
72 | if((cookie == NULL) || (option == NULL)) *value = NULL;
|
---|
73 | for(uint16_t i = 0; i < cookie->tokenCount; i++){
|
---|
74 | if(memcmp(option, &cookie->data[cookie->tokens[i].start], cookie->tokens[i].size) == 0){ // ñîøëîñü, çíà÷èò ñëåäóþùèé òîêåí òî ÷òî íóæíî ïîëó÷èòü
|
---|
75 | if(cookie->tokens[i + 1].size > 0) {
|
---|
76 | memset(value, 0x00, strlen(value));
|
---|
77 | memcpy(value, &cookie->data[cookie->tokens[i + 1].start], cookie->tokens[i + 1].size);
|
---|
78 | cookie->data[cookie->tokens[i + 1].size] = 0;
|
---|
79 | }
|
---|
80 | else *value = NULL;// íåò çíà÷åíèÿ
|
---|
81 | return;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | *value = NULL;
|
---|
85 | }
|
---|