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