Dillo v3.2.0-151-g90488cbf
Loading...
Searching...
No Matches
cookies.c
Go to the documentation of this file.
1/*
2 * Dillo cookies test
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18/*
19 * This has a big blob of the current src/IO/dpi.c in it.
20 * I hope there's a better way.
21 */
22
23#include <stdlib.h> /* malloc, etc. */
24#include <unistd.h> /* read, etc. */
25#include <stdio.h>
26#include <stdarg.h> /* va_list */
27#include <string.h> /* strchr */
28#include <errno.h>
29#include <time.h>
30/* net */
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <arpa/inet.h>
34#include <netinet/in.h>
35
36
37#define _MSG(...)
38
39#define MSG_INNARDS(prefix, ...) \
40 D_STMT_START { \
41 printf(prefix __VA_ARGS__); \
42 fflush (stdout); \
43 } D_STMT_END
44
45#define MSG(...) MSG_INNARDS("", __VA_ARGS__)
46#define MSG_WARN(...) MSG_INNARDS("** WARNING **: ", __VA_ARGS__)
47#define MSG_ERR(...) MSG_INNARDS("** ERROR **: ", __VA_ARGS__)
48
49
50#include "dlib/dlib.h" /* dIsxdigit */
51#include "dpip/dpip.h"
52
53static uint_t failed = 0;
54static uint_t passed = 0;
55
56static char SharedKey[32];
57
58/*
59 * Read all the available data from a filedescriptor.
60 * This is intended for short answers, i.e. when we know the server
61 * will write it all before being preempted. For answers that may come
62 * as an stream with delays, non-blocking is better.
63 * Return value: read data, or NULL on error and no data.
64 */
65static char *Dpi_blocking_read(int fd)
66{
67 int st;
68 const int buf_sz = 8*1024;
69 char buf[buf_sz], *msg = NULL;
70 Dstr *dstr = dStr_sized_new(buf_sz);
71
72 do {
73 st = read(fd, buf, buf_sz);
74 if (st < 0) {
75 if (errno == EINTR) {
76 continue;
77 } else {
78 MSG_ERR("[Dpi_blocking_read] %s\n", dStrerror(errno));
79 break;
80 }
81 } else if (st > 0) {
82 dStr_append_l(dstr, buf, st);
83 }
84 } while (st == buf_sz);
85
86 msg = (dstr->len > 0) ? dstr->str : NULL;
87 dStr_free(dstr, (dstr->len > 0) ? FALSE : TRUE);
88 return msg;
89}
90
91static void Dpi_close_fd(int fd)
92{
93 int st;
94
95 dReturn_if (fd < 0);
96 do
97 st = close(fd);
98 while (st < 0 && errno == EINTR);
99}
100
102{
103 int fd, ret = -1;
104
105 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
106 ret = fd;
107 }
108 return ret;
109}
110
111/*
112 * Read dpid's communication keys from its saved file.
113 * Return value: 1 on success, -1 on error.
114 */
115static int Dpi_read_comm_keys(int *port)
116{
117 FILE *In;
118 char *fname, *rcline = NULL, *tail;
119 int i, ret = -1;
120
121 fname = dStrconcat(dGethomedir(), "/.dillo/dpid_comm_keys", NULL);
122 if ((In = fopen(fname, "r")) == NULL) {
123 MSG_ERR("[Dpi_read_comm_keys] %s\n", dStrerror(errno));
124 } else if ((rcline = dGetline(In)) == NULL) {
125 MSG_ERR("[Dpi_read_comm_keys] empty file: %s\n", fname);
126 } else {
127 *port = strtol(rcline, &tail, 10);
128 for (i = 0; *tail && dIsxdigit(tail[i+1]); ++i)
129 SharedKey[i] = tail[i+1];
130 SharedKey[i] = 0;
131 ret = 1;
132 }
133 if (In)
134 fclose(In);
135 dFree(rcline);
136 dFree(fname);
137
138 return ret;
139}
140
142{
143 struct sockaddr_in sin;
144 const socklen_t sin_sz = sizeof(sin);
145 int sock_fd, dpid_port, ret = -1;
146
147 /* socket connection test */
148 memset(&sin, 0, sizeof(sin));
149 sin.sin_family = AF_INET;
150 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
151
152 if (Dpi_read_comm_keys(&dpid_port) != -1) {
153 sin.sin_port = htons(dpid_port);
154 if ((sock_fd = Dpi_make_socket_fd()) == -1) {
155 MSG("Dpi_check_dpid_ids: sock_fd=%d %s\n", sock_fd, dStrerror(errno));
156 } else if (connect(sock_fd, (struct sockaddr *)&sin, sin_sz) == -1) {
157 MSG("Dpi_check_dpid_ids: %s\n", dStrerror(errno));
158 } else {
159 Dpi_close_fd(sock_fd);
160 ret = 1;
161 }
162 }
163 return ret;
164}
165
166static int Dpi_blocking_write(int fd, const char *msg, int msg_len)
167{
168 int st, sent = 0;
169
170 while (sent < msg_len) {
171 st = write(fd, msg + sent, msg_len - sent);
172 if (st < 0) {
173 if (errno == EINTR) {
174 continue;
175 } else {
176 MSG_ERR("[Dpi_blocking_write] %s\n", dStrerror(errno));
177 break;
178 }
179 }
180 sent += st;
181 }
182
183 return (sent == msg_len) ? 1 : -1;
184}
185
186/*
187 * Start dpid.
188 * Return: 0 starting now, 1 Error.
189 */
190static int Dpi_start_dpid(void)
191{
192 pid_t pid;
193 int st_pipe[2], ret = 1;
194 char *answer;
195
196 /* create a pipe to track our child's status */
197 if (pipe(st_pipe))
198 return 1;
199
200 pid = fork();
201 if (pid == 0) {
202 /* This is the child process. Execute the command. */
203 char *path1 = dStrconcat(dGethomedir(), "/.dillo/dpid", NULL);
204 Dpi_close_fd(st_pipe[0]);
205 if (execl(path1, "dpid", (char*)NULL) == -1) {
206 dFree(path1);
207 if (execlp("dpid", "dpid", (char*)NULL) == -1) {
208 MSG("Dpi_start_dpid (child): %s\n", dStrerror(errno));
209 if (Dpi_blocking_write(st_pipe[1], "ERROR", 5) == -1) {
210 MSG("Dpi_start_dpid (child): can't write to pipe.\n");
211 }
212 Dpi_close_fd(st_pipe[1]);
213 _exit (EXIT_FAILURE);
214 }
215 }
216 } else if (pid < 0) {
217 /* The fork failed. Report failure. */
218 MSG("Dpi_start_dpid: %s\n", dStrerror(errno));
219 /* close the unused pipe */
220 Dpi_close_fd(st_pipe[0]);
221 Dpi_close_fd(st_pipe[1]);
222
223 } else {
224 /* This is the parent process, check our child status... */
225 Dpi_close_fd(st_pipe[1]);
226 if ((answer = Dpi_blocking_read(st_pipe[0])) != NULL) {
227 MSG("Dpi_start_dpid: can't start dpid\n");
228 dFree(answer);
229 } else {
230 ret = 0;
231 }
232 Dpi_close_fd(st_pipe[0]);
233 }
234
235 return ret;
236}
237
238/*
239 * Confirm that the dpid is running. If not, start it.
240 * Return: 0 running OK, 1 starting (EAGAIN), 2 Error.
241 */
242static int Dpi_check_dpid(int num_tries)
243{
244 static int starting = 0;
245 int check_st = 1, ret = 2;
246
247 check_st = Dpi_check_dpid_ids();
248 _MSG("Dpi_check_dpid: check_st=%d\n", check_st);
249
250 if (check_st == 1) {
251 /* connection test with dpi server passed */
252 starting = 0;
253 ret = 0;
254 } else {
255 if (!starting) {
256 /* start dpid */
257 if (Dpi_start_dpid() == 0) {
258 starting = 1;
259 ret = 1;
260 }
261 } else if (++starting < num_tries) {
262 /* starting */
263 ret = 1;
264 } else {
265 /* we waited too much, report an error... */
266 starting = 0;
267 }
268 }
269
270 _MSG("Dpi_check_dpid:: %s\n",
271 (ret == 0) ? "OK" : (ret == 1 ? "EAGAIN" : "ERROR"));
272 return ret;
273}
274
275
277{
278 int cst, try = 0,
279 n_tries = 12; /* 3 seconds */
280
281 /* test the dpid, and wait a bit for it to start if necessary */
282 while ((cst = Dpi_check_dpid(n_tries)) == 1) {
283 MSG("Dpi_blocking_start_dpid: try %d\n", ++try);
284 dUsleep(250000U); /* 1/4 sec */
285 }
286 return cst;
287}
288
289
290/*
291 * Return the dpi server's port number, or -1 on error.
292 * (A query is sent to dpid and then its answer parsed)
293 * note: as the available servers and/or the dpi socket directory can
294 * change at any time, we'll ask each time. If someday we find
295 * that connecting each time significantly degrades performance,
296 * an optimized approach can be tried.
297 */
298static int Dpi_get_server_port(const char *server_name)
299{
300 int sock_fd = -1, dpi_port = -1;
301 int dpid_port, ok = 0;
302 struct sockaddr_in sin;
303 char *cmd, *request, *rply = NULL, *port_str;
304 socklen_t sin_sz;
305
306 dReturn_val_if_fail (server_name != NULL, dpi_port);
307 _MSG("Dpi_get_server_port:: server_name = [%s]\n", server_name);
308
309 /* Read dpid's port from saved file */
310 if (Dpi_read_comm_keys(&dpid_port) != -1) {
311 ok = 1;
312 }
313 if (ok) {
314 /* Connect a socket with dpid */
315 ok = 0;
316 sin_sz = sizeof(sin);
317 memset(&sin, 0, sizeof(sin));
318 sin.sin_family = AF_INET;
319 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
320 sin.sin_port = htons(dpid_port);
321 if ((sock_fd = Dpi_make_socket_fd()) == -1 ||
322 connect(sock_fd, (struct sockaddr *)&sin, sin_sz) == -1) {
323 MSG("Dpi_get_server_port: %s\n", dStrerror(errno));
324 } else {
325 ok = 1;
326 }
327 }
328 if (ok) {
329 /* ask dpid to check the dpi and send its port number back */
330 ok = 0;
331 request = a_Dpip_build_cmd("cmd=%s msg=%s", "check_server", server_name);
332 _MSG("[%s]\n", request);
333
334 if (Dpi_blocking_write(sock_fd, request, strlen(request)) == -1) {
335 MSG("Dpi_get_server_port: %s\n", dStrerror(errno));
336 } else {
337 ok = 1;
338 }
339 dFree(request);
340 }
341 if (ok) {
342 /* Get the reply */
343 ok = 0;
344 if ((rply = Dpi_blocking_read(sock_fd)) == NULL) {
345 MSG("Dpi_get_server_port: can't read server port from dpid.\n");
346 } else {
347 ok = 1;
348 }
349 }
350 if (ok) {
351 /* Parse reply */
352 ok = 0;
353 cmd = a_Dpip_get_attr(rply, "cmd");
354 if (strcmp(cmd, "send_data") == 0) {
355 port_str = a_Dpip_get_attr(rply, "msg");
356 _MSG("Dpi_get_server_port: rply=%s\n", rply);
357 _MSG("Dpi_get_server_port: port_str=%s\n", port_str);
358 dpi_port = strtol(port_str, NULL, 10);
359 dFree(port_str);
360 ok = 1;
361 }
362 dFree(cmd);
363 }
364 dFree(rply);
365 Dpi_close_fd(sock_fd);
366
367 return ok ? dpi_port : -1;
368}
369
370
371static int Dpi_connect_socket(const char *server_name)
372{
373 struct sockaddr_in sin;
374 int sock_fd, dpi_port, ret = -1;
375 char *cmd = NULL;
376
377 /* Query dpid for the port number for this server */
378 if ((dpi_port = Dpi_get_server_port(server_name)) == -1) {
379 _MSG("Dpi_connect_socket:: can't get port number for %s\n", server_name);
380 return -1;
381 }
382 _MSG("Dpi_connect_socket: server=%s port=%d\n", server_name, dpi_port);
383
384 /* connect with this server's socket */
385 memset(&sin, 0, sizeof(sin));
386 sin.sin_family = AF_INET;
387 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
388 sin.sin_port = htons(dpi_port);
389
390 if ((sock_fd = Dpi_make_socket_fd()) == -1) {
391 perror("[dpi::socket]");
392 } else if (connect(sock_fd, (void*)&sin, sizeof(sin)) == -1) {
393 MSG("[dpi::connect] errno:%d %s\n", errno, dStrerror(errno));
394
395 /* send authentication Key (the server closes sock_fd on auth error) */
396 } else if (!(cmd = a_Dpip_build_cmd("cmd=%s msg=%s", "auth", SharedKey))) {
397 MSG_ERR("[Dpi_connect_socket] Can't make auth message.\n");
398 } else if (Dpi_blocking_write(sock_fd, cmd, strlen(cmd)) == -1) {
399 MSG_ERR("[Dpi_connect_socket] Can't send auth message.\n");
400 } else {
401 ret = sock_fd;
402 }
403 dFree(cmd);
404 if (sock_fd != -1 && ret == -1) /* can't send cmd? */
405 Dpi_close_fd(sock_fd);
406
407 return ret;
408}
409
410
411char *a_Dpi_send_blocking_cmd(const char *server_name, const char *cmd)
412{
413 int cst, sock_fd;
414 char *ret = NULL;
415
416 /* test the dpid, and wait a bit for it to start if necessary */
417 if ((cst = Dpi_blocking_start_dpid()) != 0) {
418 return ret;
419 }
420
421 if ((sock_fd = Dpi_connect_socket(server_name)) == -1) {
422 MSG_ERR("[a_Dpi_send_blocking_cmd] Can't connect to server.\n");
423 } else if (Dpi_blocking_write(sock_fd, cmd, strlen(cmd)) == -1) {
424 MSG_ERR("[a_Dpi_send_blocking_cmd] Can't send message.\n");
425 } else if ((ret = Dpi_blocking_read(sock_fd)) == NULL) {
426 MSG_ERR("[a_Dpi_send_blocking_cmd] Can't read message.\n");
427 }
428 Dpi_close_fd(sock_fd);
429
430 return ret;
431}
432
433
434
435void a_Cookies_set(const char *cookie, const char *host, const char *path,
436 const char *date)
437{
438 char *cmd, *dpip_tag;
439
440 if (date)
441 cmd = a_Dpip_build_cmd("cmd=%s cookie=%s host=%s path=%s date=%s",
442 "set_cookie", cookie,
443 host, path, date);
444 else
445 cmd = a_Dpip_build_cmd("cmd=%s cookie=%s host=%s path=%s",
446 "set_cookie", cookie,
447 host, path);
448
449 dpip_tag = a_Dpi_send_blocking_cmd("cookies", cmd);
450 _MSG("a_Cookies_set: dpip_tag = {%s}\n", dpip_tag);
451 dFree(dpip_tag);
452 dFree(cmd);
453}
454
455
456char *a_Cookies_get_query(const char *scheme, const char *host,
457 const char *path)
458{
459 char *cmd, *dpip_tag, *query;
460
461 cmd = a_Dpip_build_cmd("cmd=%s scheme=%s host=%s path=%s",
462 "get_cookie", scheme,
463 host, path);
464
465 /* Get the answer from cookies.dpi */
466 _MSG("cookies.c: a_Dpi_send_blocking_cmd cmd = {%s}\n", cmd);
467 dpip_tag = a_Dpi_send_blocking_cmd("cookies", cmd);
468 _MSG("cookies.c: after a_Dpi_send_blocking_cmd resp={%s}\n", dpip_tag);
469 dFree(cmd);
470
471 if (dpip_tag != NULL) {
472 query = a_Dpip_get_attr(dpip_tag, "cookie");
473 dFree(dpip_tag);
474 } else {
475 query = dStrdup("");
476 }
477
478 return query;
479}
480
481static void expect(int lineno, const char *exp_reply,
482 const char *scheme, const char *host, const char *path)
483{
484 char *reply = a_Cookies_get_query(scheme, host, path);
485
486 if (strcmp(reply, exp_reply)) {
487 MSG("line %d: EXPECTED: %s GOT: %s\n", lineno, exp_reply, reply);
488 failed++;
489 } else {
490 passed++;
491 }
492}
493
494static void toomany()
495{
496 a_Cookies_set("1=1", "toomany.com", "/", NULL);
497 a_Cookies_set("2=1", "toomany.com", "/", NULL);
498 a_Cookies_set("3=1", "toomany.com", "/", NULL);
499 a_Cookies_set("4=1", "toomany.com", "/", NULL);
500 a_Cookies_set("5=1", "toomany.com", "/", NULL);
501 a_Cookies_set("6=1", "toomany.com", "/", NULL);
502 a_Cookies_set("7=1", "toomany.com", "/path/", NULL);
503 a_Cookies_set("8=1", "toomany.com", "/", NULL);
504 a_Cookies_set("9=1", "toomany.com", "/", NULL);
505 a_Cookies_set("10=1", "toomany.com", "/", NULL);
506 a_Cookies_set("11=1", "toomany.com", "/", NULL);
507 a_Cookies_set("12=1", "toomany.com", "/", NULL);
508 a_Cookies_set("13=1", "toomany.com", "/", NULL);
509 a_Cookies_set("14=1", "toomany.com", "/", NULL);
510 a_Cookies_set("15=1", "toomany.com", "/", NULL);
511 a_Cookies_set("16=1", "toomany.com", "/", NULL);
512 a_Cookies_set("17=1", "toomany.com", "/", NULL);
513 a_Cookies_set("18=1", "toomany.com", "/", NULL);
514 a_Cookies_set("19=1", "toomany.com", "/", NULL);
515 a_Cookies_set("20=1", "toomany.com", "/", NULL);
516 a_Cookies_set("21=1", "toomany.com", "/", NULL);
517 /* 1 was oldest and discarded */
518 expect(__LINE__, "Cookie: 7=1; 2=1; 3=1; 4=1; 5=1; 6=1; 8=1; 9=1; 10=1; "
519 "11=1; 12=1; 13=1; 14=1; 15=1; 16=1; 17=1; 18=1; 19=1; "
520 "20=1; 21=1\r\n", "http", "toomany.com", "/path/");
521 sleep(1);
522 /* touch all of them except #7 (path matching) */
523 expect(__LINE__, "Cookie: 2=1; 3=1; 4=1; 5=1; 6=1; 8=1; 9=1; 10=1; "
524 "11=1; 12=1; 13=1; 14=1; 15=1; 16=1; 17=1; 18=1; 19=1; "
525 "20=1; 21=1\r\n", "http", "toomany.com", "/");
526 a_Cookies_set("22=1", "toomany.com", "/", NULL);
527 /* 7 was oldest and discarded */
528 expect(__LINE__, "Cookie: 2=1; 3=1; 4=1; 5=1; 6=1; 8=1; 9=1; 10=1; "
529 "11=1; 12=1; 13=1; 14=1; 15=1; 16=1; 17=1; 18=1; 19=1; "
530 "20=1; 21=1; 22=1\r\n", "http", "toomany.com", "/path/");
531}
532
533static void maxage()
534{
535 time_t t = time(NULL)+1000;
536 char *server_date = dStrdup(ctime(&t));
537
538 a_Cookies_set("name=val; max-age=0", "maxage0.com", "/", NULL);
539 expect(__LINE__, "", "http", "maxage0.com", "/");
540
541 a_Cookies_set("name=val; max-age=-0", "maxage-0.com", "/", NULL);
542 expect(__LINE__, "", "http", "maxage-0.com", "/");
543
544 a_Cookies_set("name=val; max-age=100", "maxage100.com", "/", NULL);
545 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxage100.com", "/");
546
547 a_Cookies_set("name=val; max-age=-100", "maxage-100.com", "/", NULL);
548 expect(__LINE__, "", "http", "maxage-100.com", "/");
549
550 a_Cookies_set("name=val; max-age=2000000000", "maxage2bil.com", "/", NULL);
551 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxage2bil.com", "/");
552
553 a_Cookies_set("name=val; max-age=3000000000", "maxage3bil.com", "/", NULL);
554 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxage3bil.com", "/");
555
556 a_Cookies_set("name=val; max-age=7000000000", "maxage7bil.com", "/", NULL);
557 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxage7bil.com", "/");
558
559 a_Cookies_set("name=val; max-age=-2000000000", "maxage-2bil.com", "/",NULL);
560 expect(__LINE__, "", "http", "maxage-2bil.com", "/");
561
562 a_Cookies_set("name=val; max-age=-3000000000", "maxage-3bil.com", "/",NULL);
563 expect(__LINE__, "", "http", "maxage-3bil.com", "/");
564
565 a_Cookies_set("name=val; max-age=-7000000000", "maxage-7bil.com", "/",NULL);
566 expect(__LINE__, "", "http", "maxage-7bil.com", "/");
567
568 /* just having a server date shouldn't matter */
569
570 a_Cookies_set("name=val; max-age=0", "maxage0s.com", "/", server_date);
571 expect(__LINE__, "", "http", "maxage0s.com", "/");
572
573 a_Cookies_set("name=val; max-age=100", "maxage100s.com", "/", server_date);
574 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxage100s.com", "/");
575
576 a_Cookies_set("name=val; max-age=-100", "maxage-100s.com", "/",server_date);
577 expect(__LINE__, "", "http", "maxage-100s.com", "/");
578
579 /* MAX-AGE and EXPIRES */
580 a_Cookies_set("name=val; max-age=90; expires=Wed Jan 20 01:26:32 2010",
581 "maxagelater.com", "/", NULL);
582 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxagelater.com", "/");
583
584 a_Cookies_set("name=val; max-age=90; expires=Wed Jan 20 01:26:32 2010",
585 "maxagelaters.com", "/", server_date);
586 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxagelaters.com", "/");
587
588 dFree(server_date);
589}
590
592{
593 char *string;
594 time_t t = time(NULL)+1000;
595 char *server_date = dStrdup(ctime(&t));
596 time_t expt = t + 1000;
597 char *exp_date = dStrdup(ctime(&expt));
598
599 string = dStrconcat("name=val; expires=", exp_date, NULL);
600 a_Cookies_set(string, "e2000s1000.com", "/", NULL);
601 expect(__LINE__, "Cookie: name=val\r\n", "http", "e2000s1000.com", "/");
602
603 a_Cookies_set(string, "e2000s1000s.com", "/", server_date);
604 expect(__LINE__, "Cookie: name=val\r\n", "http", "e2000s1000s.com", "/");
605
606 expt = t - 500; /* past for the server, future for us */
607 dFree(exp_date);
608 exp_date = dStrdup(ctime(&expt));
609
610 string = dStrconcat("name=val; expires=", exp_date, NULL);
611 a_Cookies_set(string, "e500s1000.com", "/", NULL);
612 expect(__LINE__, "Cookie: name=val\r\n", "http", "e500s1000.com", "/");
613
614 a_Cookies_set(string, "e500s1000s.com", "/", server_date);
615 expect(__LINE__, "", "http", "e500s1000s.com", "/");
616
617 expt = t; /* expire at future-for-us server date */
618 dFree(exp_date);
619 exp_date = dStrdup(ctime(&expt));
620
621 string = dStrconcat("name=val; expires=", exp_date, NULL);
622 a_Cookies_set(string, "e1000s1000.com", "/", NULL);
623 expect(__LINE__, "Cookie: name=val\r\n", "http", "e1000s1000.com", "/");
624
625 a_Cookies_set(string, "e1000s1000s.com", "/", server_date);
626 expect(__LINE__, "", "http", "e1000s1000s.com", "/");
627
628 expt = time(NULL); /* now */
629 dFree(exp_date);
630 exp_date = dStrdup(ctime(&expt));
631
632 string = dStrconcat("name=val; expires=", exp_date, NULL);
633 a_Cookies_set(string, "e0s1000.com", "/", NULL);
634 expect(__LINE__, "", "http", "e0s1000.com", "/");
635
636 a_Cookies_set(string, "e0s1000s.com", "/", server_date);
637 expect(__LINE__, "", "http", "e0s1000s.com", "/");
638
639 dFree(exp_date);
640 dFree(server_date);
641}
642
644{
645 char *string;
646 time_t t = time(NULL)-1000;
647 char *server_date = dStrdup(ctime(&t));
648
649 time_t expt = t + 1000;
650 char *exp_date = dStrdup(ctime(&expt));
651
652 string = dStrconcat("name=val; expires=", exp_date, NULL);
653 a_Cookies_set(string, "e0s-1000.com", "/", NULL);
654 expect(__LINE__, "", "http", "e0s-1000.com", "/");
655
656 a_Cookies_set(string, "e0s-1000s.com", "/", server_date);
657 expect(__LINE__, "Cookie: name=val\r\n", "http", "e0s-1000s.com","/");
658
659 expt = t + 500; /* future for the server, past for us */
660 dFree(exp_date);
661 exp_date = dStrdup(ctime(&expt));
662
663 string = dStrconcat("name=val; expires=", exp_date, NULL);
664 a_Cookies_set(string, "e-500s-1000.com", "/", NULL);
665 expect(__LINE__, "", "http", "e-500s-1000.com", "/");
666
667 a_Cookies_set(string, "e-500s-1000s.com", "/", server_date);
668 expect(__LINE__, "Cookie: name=val\r\n", "http", "e-500s-1000s.com", "/");
669
670 expt = t; /* expire at past-for-us server date */
671 dFree(exp_date);
672 exp_date = dStrdup(ctime(&expt));
673
674 string = dStrconcat("name=val; expires=", exp_date, NULL);
675 a_Cookies_set(string, "e-1000s-1000.com", "/", NULL);
676 expect(__LINE__, "", "http", "e-1000s-1000.com", "/");
677
678 a_Cookies_set(string, "e-1000s-1000s.com", "/", server_date);
679 expect(__LINE__, "", "http", "e-1000s-1000s.com", "/");
680
681 dFree(server_date);
682 dFree(exp_date);
683}
684
685static void expires_extremes()
686{
687 time_t t;
688 char *server_date;
689
690 a_Cookies_set("name=val; expires=Fri Dec 13 20:45:52 1801", "expmin.com",
691 "/", NULL);
692 expect(__LINE__, "", "http", "expmin.com", "/");
693
694 a_Cookies_set("name=val; expires=Fri Dec 13 20:45:52 1901", "expmin2.com",
695 "/", NULL);
696 expect(__LINE__, "", "http", "expmin2.com", "/");
697
698 a_Cookies_set("name=val; expires=Wed Dec 31 23:59:59 1969", "expneg.com",
699 "/", NULL);
700 expect(__LINE__, "", "http", "expneg.com", "/");
701
702 a_Cookies_set("name=val; expires=Thu, 01-January-70 00:00:00 GMT",
703 "expepoch.com", "/", NULL);
704 expect(__LINE__, "", "http", "expepoch.com", "/");
705
706 /* TODO: revisit these tests in a few decades */
707 a_Cookies_set("name=val; expires=Tue Jan 19 03:14:07 2038", "expmax.com",
708 "/", NULL);
709 expect(__LINE__, "Cookie: name=val\r\n", "http", "expmax.com", "/");
710
711 a_Cookies_set("name=val; expires=Sun January 1 00:00:00 2040",
712 "pastmax.com", "/", NULL);
713 expect(__LINE__, "Cookie: name=val\r\n", "http", "pastmax.com", "/");
714
715 t = time(NULL)+1000;
716 server_date = dStrdup(ctime(&t));
717
718 a_Cookies_set("name=val; expires=Fri Dec 13 20:45:52 1901", "expmina.com",
719 "/", server_date);
720 expect(__LINE__, "", "http", "expmina.com", "/");
721
722 a_Cookies_set("name=val; expires=Wed Dec 31 23:59:59 1969", "expnega.com",
723 "/", server_date);
724 expect(__LINE__, "", "http", "expnega.com", "/");
725
726 a_Cookies_set("name=val; expires=Thu Jan 1 00:00:00 1970", "expepocha.com",
727 "/", server_date);
728 expect(__LINE__, "", "http", "expepocha.com", "/");
729
730 a_Cookies_set("name=val; expires=Tue Jan 19 03:14:07 2038", "expmaxa.com",
731 "/", server_date);
732 expect(__LINE__, "Cookie: name=val\r\n", "http", "expmaxa.com", "/");
733
734 a_Cookies_set("name=val; expires=Thu, 01-Jan-40 00:00:00 GMT",
735 "pastmaxa.com", "/", server_date);
736 expect(__LINE__, "Cookie: name=val\r\n", "http", "pastmaxa.com", "/");
737
738 t = time(NULL)-1000;
739 dFree(server_date);
740 server_date = dStrdup(ctime(&t));
741
742 a_Cookies_set("name=val; expires=Fri Dec 13 20:45:52 1901", "expminb.com",
743 "/", server_date);
744 expect(__LINE__, "", "http", "expminb.com", "/");
745
746 a_Cookies_set("name=val; expires=Wed Dec 31 23:59:59 1969", "expnegb.com",
747 "/", server_date);
748 expect(__LINE__, "", "http", "expnegb.com", "/");
749
750 a_Cookies_set("name=val; expires=Thu Jan 1 00:00:00 1970", "expepochb.com",
751 "/", server_date);
752 expect(__LINE__, "", "http", "expepochb.com", "/");
753
754 a_Cookies_set("name=val; expires=Tue Jan 19 03:14:07 2038", "expmaxb.com",
755 "/", server_date);
756 expect(__LINE__, "Cookie: name=val\r\n", "http", "expmaxb.com", "/");
757
758 a_Cookies_set("name=val; expires=Sun Jan 1 00:00:00 2040", "pastmaxb.com",
759 "/", server_date);
760 expect(__LINE__, "Cookie: name=val\r\n", "http", "pastmaxb.com", "/");
761
762 dFree(server_date);
763}
764
765/*
766 * On 11 Aug 2009, Dan Winship posted to the http-state list with a bunch of
767 * date formats he'd gathered. Let's work from that. I'll include his comments
768 * below in double quotes.
769 */
771{
772 /* "Revised Netscape spec format" */
773 a_Cookies_set("name=val; expires=Mon, 10-Dec-2037 17:02:24 GMT",
774 "format1.com", "/", NULL);
775 expect(__LINE__, "Cookie: name=val\r\n", "http", "format1.com", "/");
776
777 /* "rfc1123-date" */
778 a_Cookies_set("name=val; expires=Wed, 09 Dec 2037 16:27:23 GMT",
779 "format2.com", "/", NULL);
780 expect(__LINE__, "Cookie: name=val\r\n", "http", "format2.com", "/");
781
782 /* "4-digit-year version of Netscape spec example (see below).
783 * Seems to only come from sites using PHP, but it's not PHP
784 * itself; maybe some framework?"
785 */
786 a_Cookies_set("name=val; expires=Thursday, 01-Jan-2036 00:00:00 GMT",
787 "format3.com", "/", NULL);
788 expect(__LINE__, "Cookie: name=val\r\n", "http", "format3.com", "/");
789
790 /* "The not-quite-asctime format used by Amazon." */
791 a_Cookies_set("name=val; expires=Mon Dec 10 16:32:30 2037 GMT",
792 "format4.com", "/", NULL);
793 expect(__LINE__, "Cookie: name=val\r\n", "http", "format4.com", "/");
794
795 /* "The syntax used by the example text in the Netscape spec,
796 * although the actual grammar uses abbreviated weekday names"
797 */
798 a_Cookies_set("name=val; expires=Wednesday, 01-Jan-37 00:00:00 GMT",
799 "format5.com", "/", NULL);
800 expect(__LINE__, "Cookie: name=val\r\n", "http", "format5.com", "/");
801
802 /* "Original Netscape spec" */
803 a_Cookies_set("name=val; expires=Mon, 10-Dec-37 20:35:03 GMT",
804 "format6.com", "/", NULL);
805 expect(__LINE__, "Cookie: name=val\r\n", "http", "format6.com", "/");
806
807 /* "If this had '01 Jan' it would be an rfc1123-date. This *is* a
808 * legitimate rfc822 date, though not an rfc2822 date because 'GMT'
809 * is deprecated in favor of '+0000' there."
810 */
811 a_Cookies_set("name=val; expires=Wed, 1 Jan 2035 00:00:00 GMT",
812 "format7.com", "/", NULL);
813 expect(__LINE__, "Cookie: name=val\r\n", "http", "format7.com", "/");
814
815 /* "Would match the 'weird php' syntax above if it was '08-Dec'" */
816 a_Cookies_set("name=val; expires=Saturday, 8-Dec-2035 21:24:09 GMT",
817 "format8.com", "/", NULL);
818 expect(__LINE__, "Cookie: name=val\r\n", "http", "format8.com", "/");
819
820 /* "God only knows what they were thinking. This came from a hit-tracker
821 * site, and it's possible that it's just totally broken and no one parses
822 * it 'correctly'"
823 */
824 a_Cookies_set("name=val; expires=Thu, 31 Dec 23:55:55 2037 GMT",
825 "format9.com", "/", NULL);
826 expect(__LINE__, "Cookie: name=val\r\n", "http", "format9.com", "/");
827
828 /* "Another kind of rfc822 / nearly-rfc1123 date, using superfluous
829 * whitespace."
830 */
831 a_Cookies_set("name=val; expires=Sun, 9 Dec 2036 13:42:05 GMT",
832 "formata.com", "/", NULL);
833 expect(__LINE__, "Cookie: name=val\r\n", "http", "formata.com", "/");
834
835 /* "Another kind of 'lets throw components together at random'. The
836 * site that this cookie came has apparently been fixed since then.
837 * (It uses the Netscape spec format now.)"
838 */
839 a_Cookies_set("name=val; expires=Wed Dec 12 2037 08:44:07 GMT-0500 (EST)",
840 "formatb.com", "/", NULL);
841 expect(__LINE__, "Cookie: name=val\r\n", "http", "formatb.com", "/");
842
843 a_Cookies_set("name=val; expires=Sun, 1-Jan-2035 00:00:00 GMT",
844 "formatc.com", "/", NULL);
845 expect(__LINE__, "Cookie: name=val\r\n", "http", "formatc.com", "/");
846
847 /* ...and the remaining handful that he encountered once or twice were
848 * far too broken to deserve our attention (e.g., times like "13:57:2").
849 */
850
851 /* Now here's what github was sending in 2015. */
852 a_Cookies_set("name=val; expires=Sat, 07 Jul 2035 21:41:24 -0000",
853 "formatd.com", "/", NULL);
854 expect(__LINE__, "Cookie: name=val\r\n", "http", "formatd.com", "/");
855
856}
857
858static void path()
859{
860 a_Cookies_set("name=val; path=/", "p1.com", "/", NULL);
861 expect(__LINE__, "Cookie: name=val\r\n", "http", "p1.com", "/");
862
863 a_Cookies_set("name=val; path=/dir1", "p2.com", "/dir2", NULL);
864 expect(__LINE__, "", "http", "p2.com", "/");
865 expect(__LINE__, "", "http", "p2.com", "/d");
866 expect(__LINE__, "Cookie: name=val\r\n", "http", "p2.com", "/dir1");
867 expect(__LINE__, "Cookie: name=val\r\n", "http", "p2.com", "/dir1/");
868 expect(__LINE__, "", "http", "p2.com", "/dir2");
869 expect(__LINE__, "", "http", "p2.com", "/dir11");
870
871 a_Cookies_set("name=val; path=dir1", "p3.com", "/dir2", NULL);
872 expect(__LINE__, "Cookie: name=val\r\n", "http", "p3.com", "/");
873 expect(__LINE__, "Cookie: name=val\r\n", "http", "p3.com", "/dir1");
874 expect(__LINE__, "Cookie: name=val\r\n", "http", "p3.com", "/dir2");
875
876 a_Cookies_set("name=val; path=/dir1/", "p4.com", "/dir2", NULL);
877 expect(__LINE__, "", "http", "p4.com", "/");
878 /* this next one strikes me as a bit odd, personally, but I suppose it's not
879 * a big deal */
880 expect(__LINE__, "", "http", "p4.com", "/dir1");
881 expect(__LINE__, "", "http", "p4.com", "/dir11");
882 expect(__LINE__, "Cookie: name=val\r\n", "http", "p4.com", "/dir1/");
883 expect(__LINE__, "Cookie: name=val\r\n", "http", "p4.com", "/dir1/sub");
884
885 a_Cookies_set("name=val", "p5.com", "/dir/subdir", NULL);
886 expect(__LINE__, "", "http", "p5.com", "/");
887 expect(__LINE__, "", "http", "p5.com", "/bir");
888 expect(__LINE__, "Cookie: name=val\r\n", "http", "p5.com", "/dir");
889 expect(__LINE__, "Cookie: name=val\r\n", "http", "p5.com", "/dir/");
890
891 a_Cookies_set("name=val", "p6.com", "/dir/subdir/", NULL);
892 expect(__LINE__, "", "http", "p6.com", "/dir/");
893 expect(__LINE__, "Cookie: name=val\r\n", "http", "p6.com", "/dir/subdir");
894 expect(__LINE__, "Cookie: name=val\r\n", "http", "p6.com", "/dir/subdir/s");
895}
896
898{
899 const int line_maxlen = 4096;
900 FILE *stream;
901 char *filename;
902 char line[line_maxlen];
904
905 /* Get a file pointer */
906 filename = dStrconcat(dGethomedir(), "/.dillo/cookiesrc", NULL);
907 stream = fopen(filename, "r");
908 dFree(filename);
909
910 if (!stream) {
911 MSG_ERR("Cannot run test; cannot open cookiesrc.\n");
912 return 1;
913 }
914
915 /* Get all lines in the file */
916 while (!feof(stream)) {
917 char *rc;
918
919 line[0] = '\0';
920 rc = fgets(line, line_maxlen, stream);
921 if (!rc && ferror(stream)) {
922 MSG_ERR("Error while reading rule from cookiesrc: %s\n",
923 dStrerror(errno));
924 fclose(stream);
925 return 2;
926 }
927
928 /* Remove leading and trailing whitespaces */
929 dStrstrip(line);
930
931 if (line[0] != '\0' && line[0] != '#') {
932 int domain_end, i = 0;
933 const char *rule;
934
935 /* Get the domain */
936 while (line[i] != '\0' && !dIsspace(line[i]))
937 i++;
938 domain_end = i;
939
940 /* Skip past whitespace */
941 while (dIsspace(line[i]))
942 i++;
943 line[domain_end] = '\0';
944
945 /* Get the rule */
946 rule = line + i;
947 while (line[i] != '\0' && !dIsspace(line[i]))
948 i++;
949 line[i] = '\0';
950
951 if (!dStrAsciiCasecmp(line, "DEFAULT")) {
952 if (!dStrAsciiCasecmp(rule, "ACCEPT") ||
953 !dStrAsciiCasecmp(rule, "ACCEPT_SESSION"))
955 } else {
956 if (!dStrAsciiCasecmp(rule, "DENY"))
957 MSG_WARN("DENY rules in cookiesrc can interfere with test.\n");
958 }
959 }
960 }
961 fclose(stream);
962
963 if (default_deny) {
964 MSG_ERR("Cannot run test with cookiesrc default of deny.\n");
965 return 1;
966 } else {
967 return 0;
968 }
969}
970
971int main()
972{
973 if (Cookies_rc_check()) {
974 MSG("If you change cookiesrc, remember to stop the DPIs via dpidc.\n");
975 return 1;
976 }
977
978 a_Cookies_set("name=val", "ordinary.com", "/", NULL);
979 expect(__LINE__, "Cookie: name=val\r\n", "http", "ordinary.com", "/");
980
981 toomany();
982 maxage();
987
988 a_Cookies_set("name=val; expires=\"Sun Jan 10 00:00:00 2038\"",
989 "quoted-date.org", "/", NULL);
990 expect(__LINE__, "Cookie: name=val\r\n", "http", "quoted-date.org", "/");
991
992 a_Cookies_set("name=val; expires=\"Sun Jan 11 00:00:00 1970\"",
993 "quoted-pastdate.org", "/", NULL);
994 expect(__LINE__, "", "http", "quoted-pastdate.org", "/");
995
996 path();
997
998 /* LEADING/TRAILING DOTS AND A LITTLE PUBLIC SUFFIX */
999 a_Cookies_set("name=val; domain=co.il", "www.co.il", "/", NULL);
1000 expect(__LINE__, "", "http", "www.co.il", "/");
1001
1002 a_Cookies_set("name=val; domain=.co.il", "www.co.il", "/", NULL);
1003 expect(__LINE__, "", "http", "www.co.il", "/");
1004
1005 a_Cookies_set("name=val; domain=co.il.", "www.co.il.", "/", NULL);
1006 expect(__LINE__, "", "http", "www.co.il.", "/");
1007
1008 a_Cookies_set("name=val; domain=.co.il.", "www.co.il.", "/", NULL);
1009 expect(__LINE__, "", "http", ".www.co.il.", "/");
1010
1011 a_Cookies_set("name=val; domain=co.org", "www.co.org", "/", NULL);
1012 expect(__LINE__, "Cookie: name=val\r\n", "http", "www.co.org", "/");
1013
1014 a_Cookies_set("name=val; domain=.cp.org", "www.cp.org", "/", NULL);
1015 expect(__LINE__, "Cookie: name=val\r\n", "http", "www.cp.org", "/");
1016
1017
1018 /* DOTDOMAIN */
1019 a_Cookies_set("name=val; domain=.dotdomain.org", "dotdomain.org", "/",
1020 NULL);
1021 expect(__LINE__, "Cookie: name=val\r\n", "http", "dotdomain.org", "/");
1022 expect(__LINE__, "Cookie: name=val\r\n", "http", "www.dotdomain.org", "/");
1023
1024 /* HOST_ONLY */
1025 a_Cookies_set("name=val; domain=.hostonly.org", "hostonly.org", "/", NULL);
1026 a_Cookies_set("name2=val2", "hostonly.org", "/", NULL);
1027 a_Cookies_set("name3=val3; domain=hostonly.org", "hostonly.org", "/", NULL);
1028 expect(__LINE__, "Cookie: name=val; name2=val2; name3=val3\r\n", "http",
1029 "hostonly.org", "/");
1030 a_Cookies_set("name=new; domain=.hostonly.org", "hostonly.org", "/", NULL);
1031 expect(__LINE__, "Cookie: name=new; name2=val2; name3=val3\r\n", "http",
1032 "hostonly.org", "/");
1033 a_Cookies_set("name2=new2", "hostonly.org", "/", NULL);
1034 expect(__LINE__, "Cookie: name=new; name2=new2; name3=val3\r\n", "http",
1035 "hostonly.org", "/");
1036 a_Cookies_set("name3=new3; domain=hostonly.org", "hostonly.org", "/", NULL);
1037 expect(__LINE__, "Cookie: name=new; name2=new2; name3=new3\r\n", "http",
1038 "hostonly.org", "/");
1039
1040 /* SUBDOMAIN */
1041 a_Cookies_set("name=val; domain=www.subdomain.com", "subdomain.com", "/",
1042 NULL);
1043 a_Cookies_set("name=val; domain=.www.subdomain.com", "subdomain.com", "/",
1044 NULL);
1045 expect(__LINE__, "", "http", "subdomain.com", "/");
1046 expect(__LINE__, "", "http", "www.subdomain.com", "/");
1047
1048 /* SUPERDOMAIN(?) */
1049 a_Cookies_set("name=val; domain=.supdomain.com", "www.supdomain.com", "/",
1050 NULL);
1051 a_Cookies_set("name2=val2; domain=supdomain.com", "www.supdomain.com", "/",
1052 NULL);
1053 expect(__LINE__, "Cookie: name=val; name2=val2\r\n", "http",
1054 "sub2.sub.supdomain.com", "/");
1055 expect(__LINE__, "Cookie: name=val; name2=val2\r\n", "http",
1056 "www.supdomain.com", "/");
1057 expect(__LINE__, "Cookie: name=val; name2=val2\r\n", "http",
1058 "supdomain.com", "/");
1059
1060 /* UNRELATED */
1061 a_Cookies_set("name=val; domain=another.com", "unrelated.com", "/", NULL);
1062 expect(__LINE__, "", "http", "another.com", "/");
1063 a_Cookies_set("name=val; domain=another.com", "a.org", "/", NULL);
1064 expect(__LINE__, "", "http", "another.com", "/");
1065 a_Cookies_set("name=val; domain=another.com", "badguys.com", "/", NULL);
1066 expect(__LINE__, "", "http", "another.com", "/");
1067 a_Cookies_set("name=val; domain=another.com", "more.badguys.com", "/",
1068 NULL);
1069 expect(__LINE__, "", "http", "another.com", "/");
1070 a_Cookies_set("name=val; domain=another.com", "verybadguys.com", "/", NULL);
1071 expect(__LINE__, "", "http", "another.com", "/");
1072
1073 a_Cookies_set("name=val; domain=similar.com", "imilar.com", "/", NULL);
1074 a_Cookies_set("name2=val2; domain=similar.com", "ssimilar.com", "/", NULL);
1075 a_Cookies_set("name3=val3; domain=.similar.com", "imilar.com", "/", NULL);
1076 a_Cookies_set("name4=val4; domain=.similar.com", "timilar.com", "/", NULL);
1077 a_Cookies_set("name4=val4; domain=.similar.com", "tiimilar.com", "/", NULL);
1078 expect(__LINE__, "", "http", "similar.com", "/");
1079
1080 /* SECURE */
1081 a_Cookies_set("name=val; secure", "secure.com", "/", NULL);
1082 expect(__LINE__, "", "http", "secure.com", "/");
1083 expect(__LINE__, "Cookie: name=val\r\n", "https", "secure.com", "/");
1084
1085 /* HTTPONLY */
1086 a_Cookies_set("name=val; HttpOnly", "httponly.net", "/", NULL);
1087 expect(__LINE__, "Cookie: name=val\r\n", "http", "httponly.net", "/");
1088
1089 /* GIBBERISH ATTR IGNORED */
1090 a_Cookies_set("name=val; ldkfals", "gibberish.net", "/", NULL);
1091 expect(__LINE__, "Cookie: name=val\r\n", "http", "gibberish.net", "/");
1092
1093 /* WHITESPACE/DELIMITERS */
1094 a_Cookies_set(" name=val ", "whitespace.net", "/", NULL);
1095 a_Cookies_set("name2=val2;", "whitespace.net", "/", NULL);
1096 expect(__LINE__, "Cookie: name=val; name2=val2\r\n", "http",
1097 "whitespace.net", "/");
1098
1099 /* NAMELESS/VALUELESS */
1100 a_Cookies_set("value", "nonameval.org", "/", NULL);
1101 a_Cookies_set("name=", "nonameval.org", "/", NULL);
1102 a_Cookies_set("name2= ", "nonameval.org", "/", NULL);
1103 expect(__LINE__, "Cookie: name=; name2=\r\n", "http", "nonameval.org", "/");
1104 a_Cookies_set("=val2", "nonameval.org", "/", NULL);
1105 expect(__LINE__, "Cookie: name=; name2=\r\n", "http", "nonameval.org", "/");
1106
1107
1108 /* SOME IP ADDRS */
1109
1110 a_Cookies_set("name=val", "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210",
1111 "/", NULL);
1112 expect(__LINE__, "Cookie: name=val\r\n", "http",
1113 "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", "/");
1114
1115 a_Cookies_set("name=val", "::FFFF:129.144.52.38", "/", NULL);
1116 expect(__LINE__, "Cookie: name=val\r\n", "http", "::FFFF:129.144.52.38",
1117 "/");
1118
1119 a_Cookies_set("name=val", "127.0.0.1", "/", NULL);
1120 expect(__LINE__, "Cookie: name=val\r\n", "http", "127.0.0.1", "/");
1121
1122 a_Cookies_set("name=val; domain=128.0.0.1", "128.0.0.1", "/", NULL);
1123 expect(__LINE__, "Cookie: name=val\r\n", "http", "128.0.0.1", "/");
1124
1125 a_Cookies_set("name=val; domain=130.0.0.1", "129.0.0.1", "/", NULL);
1126 expect(__LINE__, "", "http", "129.0.0.1", "/");
1127 expect(__LINE__, "", "http", "130.0.0.1", "/");
1128
1129 a_Cookies_set("name=val", "2.0.0.1", "/", NULL);
1130 a_Cookies_set("name=bad; domain=22.0.0.1", "2.0.0.1", "/", NULL);
1131 a_Cookies_set("name=bad; domain=.0.0.1", "2.0.0.1", "/", NULL);
1132 a_Cookies_set("name=bad; domain=not-ip.org", "2.0.0.1", "/", NULL);
1133 expect(__LINE__, "", "http", "22.0.0.1", "/");
1134 expect(__LINE__, "", "http", "not-ip.org", "/");
1135 expect(__LINE__, "Cookie: name=val\r\n", "http", "2.0.0.1", "/");
1136
1137#if 0
1138HAD BEEN PLAYING AROUND WITH REAL PUBLIC SUFFIX
1139a_Cookies_set("name=val;domain=sub.sub.yokohama.jp", "sub.sub.yokohama.jp", "/", NULL);
1140MSG("sub sub yokohama should work: %s\n",
1141 a_Cookies_get_query("http", "sub.sub.yokohama.jp", "/"));
1142a_Cookies_set("name=val; domain=sub.tokyo.jp", "sub.sub.tokyo.jp", "/", NULL);
1143MSG("sub tokyo jp should fail: %s\n",
1144 a_Cookies_get_query("http", "sub.sub.tokyo.jp", "/"));
1145a_Cookies_set("name=val; domain=pref.chiba.jp", "sub.pref.chiba.jp", "/", NULL);
1146MSG("pref chiba jp should succeed: %s\n",
1147 a_Cookies_get_query("http", "sub.pref.chiba.jp", "/"));
1148a_Cookies_set("name=val; domain=org", "www.dillo.org", "/", NULL);
1149a_Cookies_set("name=val; domain=org", "dillo.org", "/", NULL);
1150a_Cookies_set("name=val; domain=org", ".dillo.org", "/", NULL);
1151a_Cookies_set("name=val; domain=org.", ".dillo.org", "/", NULL);
1152a_Cookies_set("name=val; domain=org.", ".dillo.org.", "/", NULL);
1153MSG("org should fail: %s\n",
1154 a_Cookies_get_query("http", "www.dillo.org", "/"));
1155#endif
1156
1157 MSG("TESTS: passed: %u failed: %u\n", passed, failed);
1158
1159 MSG("Now that everything is full of fake cookies, you should run "
1160 "'dpidc stop', plus delete cookies.txt if necessary.\n");
1161
1162 return 0;
1163}
unsigned int uint_t
Definition d_size.h:20
unsigned char bool_t
Definition d_size.h:21
char * dGetline(FILE *stream)
Get a line from a FILE stream.
Definition dlib.c:955
char * dStrconcat(const char *s1,...)
Concatenate a NULL-terminated list of strings.
Definition dlib.c:101
void dFree(void *mem)
Definition dlib.c:67
int dStrAsciiCasecmp(const char *s1, const char *s2)
Definition dlib.c:202
char * dStrstrip(char *s)
Remove leading and trailing whitespace.
Definition dlib.c:121
char * dStrdup(const char *s)
Definition dlib.c:76
Dstr * dStr_sized_new(int sz)
Create a new string with a given size.
Definition dlib.c:253
void dStr_free(Dstr *ds, int all)
Free a dillo string.
Definition dlib.c:336
void dStr_append_l(Dstr *ds, const char *s, int l)
Append a C string to a Dstr (providing length).
Definition dlib.c:307
int dUsleep(unsigned long usec)
Portable usleep() function.
Definition dlib.c:994
char * dGethomedir(void)
Return the home directory in a static string (don't free)
Definition dlib.c:933
#define dStrerror
Definition dlib.h:124
static int dIsspace(unsigned char c)
Definition dlib.h:53
#define dReturn_val_if_fail(expr, val)
Definition dlib.h:105
#define dReturn_if(expr)
Definition dlib.h:93
#define TRUE
Definition dlib.h:36
#define FALSE
Definition dlib.h:32
static int dIsxdigit(unsigned char c)
Definition dlib.h:54
static bool_t default_deny
Definition domain.c:26
#define _MSG(...)
Definition cookies.c:57
#define MSG(...)
Definition cookies.c:58
int main(void)
Definition cookies.c:1638
char * a_Dpip_build_cmd(const char *format,...)
Printf like function for building dpip commands.
Definition dpip.c:83
char * a_Dpip_get_attr(const char *tag, const char *attrname)
Task: given a tag and an attribute name, return its value.
Definition dpip.c:192
char * a_Dpi_send_blocking_cmd(const char *server_name, const char *cmd)
Send a command to a dpi server, and block until the answer is got.
Definition dpi.c:773
void a_Cookies_set(Dlist *cookie_strings, const DilloUrl *set_url, const char *date)
Set the value corresponding to the cookie string.
Definition cookies.c:142
char * a_Cookies_get_query(const DilloUrl *query_url, const DilloUrl *requester, int is_root_url)
Return a string containing cookie data for an HTTP query.
Definition cookies.c:185
Definition dlib.h:131
Dstr_char_t * str
Definition dlib.h:134
int len
Definition dlib.h:133
static char SharedKey[32]
Definition cookies.c:56
static void path()
Definition cookies.c:858
static int Dpi_start_dpid(void)
Definition cookies.c:190
static int Dpi_blocking_write(int fd, const char *msg, int msg_len)
Definition cookies.c:166
static uint_t passed
Definition cookies.c:54
#define MSG_ERR(...)
Definition cookies.c:47
static void expires_server_ahead()
Definition cookies.c:591
static int Dpi_read_comm_keys(int *port)
Definition cookies.c:115
static void maxage()
Definition cookies.c:533
static int Dpi_make_socket_fd()
Definition cookies.c:101
static void toomany()
Definition cookies.c:494
static void Dpi_close_fd(int fd)
Definition cookies.c:91
static int Dpi_get_server_port(const char *server_name)
Definition cookies.c:298
static int Dpi_blocking_start_dpid(void)
Definition cookies.c:276
static void expires_extremes()
Definition cookies.c:685
static uint_t failed
Definition cookies.c:53
static void expires_server_behind()
Definition cookies.c:643
#define MSG_WARN(...)
Definition cookies.c:46
int Cookies_rc_check()
Definition cookies.c:897
static void expect(int lineno, const char *exp_reply, const char *scheme, const char *host, const char *path)
Definition cookies.c:481
static char * Dpi_blocking_read(int fd)
Definition cookies.c:65
static int Dpi_check_dpid(int num_tries)
Definition cookies.c:242
static int Dpi_check_dpid_ids()
Definition cookies.c:141
static void expires_date_formats()
Definition cookies.c:770
static int Dpi_connect_socket(const char *server_name)
Definition cookies.c:371