annotate dpip/dpip.c @ 2048:5060d415a85a

clickable menu items (even those introducing submenus) MUST have callbacks I clicked on the "Panel size" item itself instead of any of the options in its submenu, and: Segfault!
author corvid <corvid@lavabit.com>
date Thu, 26 May 2011 02:51:18 +0000
parents 042e61e198bc
children 16e52ecf20a8
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * File: dpip.c
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
35
d9e7b35430de Updated copyright lines
jcid
parents: 0
diff changeset
4 * Copyright 2005-2007 Jorge Arellano Cid <jcid@dillo.org>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
13 #include <errno.h>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 #include <stdio.h>
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
15 #include <stdlib.h>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 #include <stdarg.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 #include <string.h>
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
18 #include <ctype.h>
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
19 #include <unistd.h> /* for close */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
20 #include <fcntl.h> /* for fcntl */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 #include "dpip.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 #include "d_size.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
25 #define RBUF_SZ 16*1024
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
26 //#define RBUF_SZ 1
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
27
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
28 #define DPIP_TAG_END " '>"
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
29 #define DPIP_MODE_SWITCH_TAG "cmd='start_send_page' "
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
30 #define MSG_ERR(...) fprintf(stderr, "[dpip]: " __VA_ARGS__)
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
31
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
32 /*
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
33 * Local variables
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
34 */
1141
corvid <corvid@lavabit.com>
parents: 306
diff changeset
35 static const char Quote = '\'';
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
36
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 * Basically the syntax of a dpip tag is:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 * "<"[*alpha] *(<name>"="Quote<escaped_value>Quote) " "Quote">"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 * 1.- No space is allowed around the "=" sign between a name and its value.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 * 2.- The Quote character is not allowed in <name>.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 * 3.- Attribute values stuff Quote as QuoteQuote.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 * e.g. (with ' as Quote):
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 * <a='b' b='c' '> OK
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 * <dpi a='b i' b='12' '> OK
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 * <a='>' '> OK
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 * <a='ain''t no doubt' '> OK
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 * <a='ain''t b=''no'' b='' doubt' '> OK
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 * <a = '>' '> Wrong
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 * Notes:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 * Restriction #1 is for easy finding of end of tag (EOT=Space+Quote+>).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 * Restriction #2 can be removed, but what for? ;)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59 * The functions here provide for this functionality.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 typedef enum {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 SEEK_NAME,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 MATCH_NAME,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65 SKIP_VALUE,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 SKIP_QUOTE,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 FOUND
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 } DpipTagParsingState;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 /* ------------------------------------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 * Printf like function for building dpip commands.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 * It takes care of dpip escaping of its arguments.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 * NOTE : It ONLY accepts string parameters, and
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 * only one %s per parameter.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 char *a_Dpip_build_cmd(const char *format, ...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 va_list argp;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 char *p, *q, *s;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 Dstr *cmd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 /* Don't allow Quote characters in attribute names */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 if (strchr(format, Quote))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 cmd = dStr_sized_new(64);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 dStr_append_c(cmd, '<');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 va_start(argp, format);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 for (p = q = (char*)format; *q; ) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 p = strstr(q, "%s");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 if (!p) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 dStr_append(cmd, q);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 /* Copy format's part */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 while (q != p)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 dStr_append_c(cmd, *q++);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 q += 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 dStr_append_c(cmd, Quote);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 /* Stuff-copy of argument */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 s = va_arg (argp, char *);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 for ( ; *s; ++s) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 dStr_append_c(cmd, *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 if (*s == Quote)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 dStr_append_c(cmd, *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 dStr_append_c(cmd, Quote);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 va_end(argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 dStr_append_c(cmd, ' ');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 dStr_append_c(cmd, Quote);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 dStr_append_c(cmd, '>');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 p = cmd->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 dStr_free(cmd, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 return p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 /*
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
124 * Task: given a tag, its size and an attribute name, return the
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
125 * attribute value (stuffing of ' is removed here).
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
126 *
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 * Return value: the attribute value, or NULL if not present or malformed.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 */
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
129 char *a_Dpip_get_attr_l(const char *tag, size_t tagsize, const char *attrname)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 uint_t i, n = 0, found = 0;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
132 const char *p, *q, *start;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
133 char *r, *s, *val = NULL;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 DpipTagParsingState state = SEEK_NAME;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
136 if (!tag || !tagsize || !attrname || !*attrname)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 for (i = 1; i < tagsize && !found; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 switch (state) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 case SEEK_NAME:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 if (tag[i] == attrname[0] && (tag[i-1] == ' ' || tag[i-1] == '<')) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 n = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 state = MATCH_NAME;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 } else if (tag[i] == Quote && tag[i-1] == '=')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 state = SKIP_VALUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 case MATCH_NAME:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 if (tag[i] == attrname[n])
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 ++n;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 else if (tag[i] == '=' && !attrname[n])
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 state = FOUND;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 else
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 state = SEEK_NAME;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 case SKIP_VALUE:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 if (tag[i] == Quote)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 state = (tag[i+1] == Quote) ? SKIP_QUOTE : SEEK_NAME;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 case SKIP_QUOTE:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 state = SKIP_VALUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 case FOUND:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 found = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
169 if (found) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 p = start = tag + i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171 while ((q = strchr(p, Quote)) && q[1] == Quote)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
172 p = q + 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
173 if (q && q[1] == ' ') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
174 val = dStrndup(start, (uint_t)(q - start));
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
175 for (r = s = val; (*r = *s); ++r, ++s)
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
176 if (s[0] == Quote && s[0] == s[1])
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
177 ++s;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
179 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180 return val;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
183 /*
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
184 * Task: given a tag and an attribute name, return its value.
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
185 * Return value: the attribute value, or NULL if not present or malformed.
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
186 */
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
187 char *a_Dpip_get_attr(const char *tag, const char *attrname)
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
188 {
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
189 return (tag ? a_Dpip_get_attr_l(tag, strlen(tag), attrname) : NULL);
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
190 }
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1141
diff changeset
191
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
192 /*
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
193 * Check whether the given 'auth' string equals what dpid saved.
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
194 * Return value: 1 if equal, -1 otherwise
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
195 */
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
196 int a_Dpip_check_auth(const char *auth_tag)
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
197 {
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
198 char SharedSecret[32];
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
199 FILE *In;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
200 char *fname, *rcline = NULL, *tail, *cmd, *msg;
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
201 int i, port, ret = -1;
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
202
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
203 /* sanity checks */
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
204 if (!auth_tag ||
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
205 !(cmd = a_Dpip_get_attr(auth_tag, "cmd")) || strcmp(cmd, "auth") ||
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
206 !(msg = a_Dpip_get_attr(auth_tag, "msg"))) {
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
207 return ret;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
208 }
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
209
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
210 fname = dStrconcat(dGethomedir(), "/.dillo/dpid_comm_keys", NULL);
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
211 if ((In = fopen(fname, "r")) == NULL) {
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
212 MSG_ERR("[a_Dpip_check_auth] %s\n", dStrerror(errno));
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
213 } else if ((rcline = dGetline(In)) == NULL) {
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
214 MSG_ERR("[a_Dpip_check_auth] empty file: %s\n", fname);
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
215 } else {
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
216 port = strtol(rcline, &tail, 10);
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
217 for (i = 0; *tail && isxdigit(tail[i+1]); ++i)
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
218 SharedSecret[i] = tail[i+1];
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
219 SharedSecret[i] = 0;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
220 if (strcmp(msg, SharedSecret) == 0)
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
221 ret = 1;
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
222 }
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
223 if (In)
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
224 fclose(In);
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
225 dFree(rcline);
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
226 dFree(fname);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
227 dFree(msg);
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
228 dFree(cmd);
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
229
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
230 return ret;
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
231 }
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
232
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
233 /* --------------------------------------------------------------------------
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
234 * Dpip socket API ----------------------------------------------------------
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
235 */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
236
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
237 /*
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
238 * Create and initialize a dpip socket handler
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
239 */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
240 Dsh *a_Dpip_dsh_new(int fd_in, int fd_out, int flush_sz)
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
241 {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
242 Dsh *dsh = dNew(Dsh, 1);
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
243
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
244 /* init descriptors and streams */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
245 dsh->fd_in = fd_in;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
246 dsh->fd_out = fd_out;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
247
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
248 /* init buffer */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
249 dsh->wrbuf = dStr_sized_new(8 *1024);
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
250 dsh->rdbuf = dStr_sized_new(8 *1024);
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
251 dsh->flush_sz = flush_sz;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
252 dsh->mode = DPIP_TAG;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
253 if (fcntl(dsh->fd_in, F_GETFL) & O_NONBLOCK)
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
254 dsh->mode |= DPIP_NONBLOCK;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
255 dsh->status = 0;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
256
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
257 return dsh;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
258 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
259
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
260 /*
1391
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
261 * Return value: 1..DataSize sent, -1 eagain, or -3 on big Error
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
262 */
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
263 static int Dpip_dsh_write(Dsh *dsh, int nb, const char *Data, int DataSize)
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
264 {
1412
0dbf5fa97600 Compiler happiness for old versions of gcc
Jorge Arellano Cid <jcid@dillo.org>
parents: 1393
diff changeset
265 int req_mode, old_flags = 0, st, ret = -3, sent = 0;
1391
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
266
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
267 req_mode = (nb) ? DPIP_NONBLOCK : 0;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
268 if ((dsh->mode & DPIP_NONBLOCK) != req_mode) {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
269 /* change mode temporarily... */
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
270 old_flags = fcntl(dsh->fd_out, F_GETFL);
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
271 fcntl(dsh->fd_out, F_SETFL,
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
272 (nb) ? O_NONBLOCK | old_flags : old_flags & ~O_NONBLOCK);
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
273 }
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
274
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
275 while (1) {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
276 st = write(dsh->fd_out, Data + sent, DataSize - sent);
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
277 if (st < 0) {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
278 if (errno == EINTR) {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
279 continue;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
280 } else if (errno == EAGAIN) {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
281 dsh->status = DPIP_EAGAIN;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
282 ret = -1;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
283 break;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
284 } else {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
285 MSG_ERR("[Dpip_dsh_write] %s\n", dStrerror(errno));
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
286 dsh->status = DPIP_ERROR;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
287 break;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
288 }
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
289 } else {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
290 sent += st;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
291 if (nb || sent == DataSize) {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
292 ret = sent;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
293 break;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
294 }
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
295 }
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
296 }
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
297
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
298 if ((dsh->mode & DPIP_NONBLOCK) != req_mode) {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
299 /* restore old mode */
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
300 fcntl(dsh->fd_out, F_SETFL, old_flags);
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
301 }
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
302
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
303 return ret;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
304 }
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
305
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
306 /*
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
307 * Streamed write to socket
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
308 * Return: 0 on success, 1 on error.
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
309 */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
310 int a_Dpip_dsh_write(Dsh *dsh, int flush, const char *Data, int DataSize)
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
311 {
1391
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
312 int ret = 1;
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
313
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
314 /* append to buf */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
315 dStr_append_l(dsh->wrbuf, Data, DataSize);
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
316
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
317 if (!flush || dsh->wrbuf->len == 0)
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
318 return 0;
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
319
1391
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
320 ret = Dpip_dsh_write(dsh, 0, dsh->wrbuf->str, dsh->wrbuf->len);
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
321 if (ret == dsh->wrbuf->len) {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
322 dStr_truncate(dsh->wrbuf, 0);
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
323 ret = 0;
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
324 }
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
325
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
326 return ret;
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
327 }
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
328
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
329 /*
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1566
diff changeset
330 * Return value: 0 on success or empty buffer,
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
331 * 1..DataSize sent, -1 eagain, or -3 on big Error
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
332 */
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
333 int a_Dpip_dsh_tryflush(Dsh *dsh)
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
334 {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
335 int st;
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
336
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
337 if (dsh->wrbuf->len == 0) {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
338 st = 0;
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
339 } else {
1391
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
340 st = Dpip_dsh_write(dsh, 1, dsh->wrbuf->str, dsh->wrbuf->len);
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
341 if (st > 0) {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
342 /* update internal buffer */
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
343 dStr_erase(dsh->wrbuf, 0, st);
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
344 }
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
345 }
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
346 return (dsh->wrbuf->len == 0) ? 0 : st;
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
347 }
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
348
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
349 /*
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
350 * Return value: 1..DataSize sent, -1 eagain, or -3 on big Error
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
351 */
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
352 int a_Dpip_dsh_trywrite(Dsh *dsh, const char *Data, int DataSize)
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
353 {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
354 int st;
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
355
1391
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
356 if ((st = Dpip_dsh_write(dsh, 1, Data, DataSize)) > 0) {
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
357 /* update internal buffer */
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
358 if (st < DataSize)
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
359 dStr_append_l(dsh->wrbuf, Data + st, DataSize - st);
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
360 }
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
361 return st;
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
362 }
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
363
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
364 /*
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
365 * Convenience function.
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
366 */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
367 int a_Dpip_dsh_write_str(Dsh *dsh, int flush, const char *str)
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
368 {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
369 return a_Dpip_dsh_write(dsh, flush, str, (int)strlen(str));
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
370 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
371
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
372 /*
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
373 * Read raw data from the socket into our buffer in
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
374 * either BLOCKING or NONBLOCKING mode.
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
375 */
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
376 static void Dpip_dsh_read(Dsh *dsh, int blocking)
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
377 {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
378 char buf[RBUF_SZ];
1412
0dbf5fa97600 Compiler happiness for old versions of gcc
Jorge Arellano Cid <jcid@dillo.org>
parents: 1393
diff changeset
379 int req_mode, old_flags = 0, st, ret = -3, nb = !blocking;
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
380
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
381 dReturn_if (dsh->status == DPIP_ERROR || dsh->status == DPIP_EOF);
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
382
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
383 req_mode = (nb) ? DPIP_NONBLOCK : 0;
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
384 if ((dsh->mode & DPIP_NONBLOCK) != req_mode) {
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
385 /* change mode temporarily... */
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
386 old_flags = fcntl(dsh->fd_in, F_GETFL);
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
387 fcntl(dsh->fd_in, F_SETFL,
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
388 (nb) ? O_NONBLOCK | old_flags : old_flags & ~O_NONBLOCK);
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
389 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
390
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
391 while (1) {
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
392 st = read(dsh->fd_in, buf, RBUF_SZ);
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
393 if (st < 0) {
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
394 if (errno == EINTR) {
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
395 continue;
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
396 } else if (errno == EAGAIN) {
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
397 dsh->status = DPIP_EAGAIN;
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
398 ret = -1;
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
399 break;
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
400 } else {
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
401 MSG_ERR("[Dpip_dsh_read] %s\n", dStrerror(errno));
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
402 dsh->status = DPIP_ERROR;
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
403 break;
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
404 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
405 } else if (st == 0) {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
406 dsh->status = DPIP_EOF;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
407 break;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
408 } else {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
409 /* append to buf */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
410 dStr_append_l(dsh->rdbuf, buf, st);
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
411 if (blocking)
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
412 break;
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
413 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
414 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
415
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
416 if ((dsh->mode & DPIP_NONBLOCK) != req_mode) {
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
417 /* restore old mode */
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
418 fcntl(dsh->fd_out, F_SETFL, old_flags);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
419 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
420
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
421 /* assert there's no more data in the wire...
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
422 * (st < buf upon interrupt || st == buf and no more data) */
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
423 if (blocking)
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
424 Dpip_dsh_read(dsh, 0);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
425 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
426
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
427 /*
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
428 * Return a newlly allocated string with the next dpip token in the socket.
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
429 * Return value: token string on success, NULL otherwise
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
430 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
431 char *a_Dpip_dsh_read_token(Dsh *dsh, int blocking)
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
432 {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
433 char *p, *ret = NULL;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
434
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
435 /* Read all available data without blocking */
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
436 Dpip_dsh_read(dsh, 0);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
437
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
438 /* switch mode upon request */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
439 if (dsh->mode & DPIP_LAST_TAG)
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
440 dsh->mode = DPIP_RAW;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
441
1566
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
442 if (blocking) {
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
443 if (dsh->mode & DPIP_TAG) {
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
444 /* Only wait for data when the tag is incomplete */
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
445 if (!strstr(dsh->rdbuf->str, DPIP_TAG_END)) {
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
446 do {
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
447 Dpip_dsh_read(dsh, 1);
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
448 p = strstr(dsh->rdbuf->str, DPIP_TAG_END);
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
449 } while (!p && dsh->status == EAGAIN);
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
450 }
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
451
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
452 } else if (dsh->mode & DPIP_RAW) {
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
453 /* Wait for data when the buffer is empty and there's no EOF yet */
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
454 while (dsh->rdbuf->len == 0 && dsh->status != DPIP_EOF)
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
455 Dpip_dsh_read(dsh, 1);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
456 }
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
457 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
458
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
459 if (dsh->mode & DPIP_TAG) {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
460 /* return a full tag */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
461 if ((p = strstr(dsh->rdbuf->str, DPIP_TAG_END))) {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
462 ret = dStrndup(dsh->rdbuf->str, p - dsh->rdbuf->str + 3);
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
463 dStr_erase(dsh->rdbuf, 0, p - dsh->rdbuf->str + 3);
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
464 if (strstr(ret, DPIP_MODE_SWITCH_TAG))
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
465 dsh->mode |= DPIP_LAST_TAG;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
466 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
467 } else {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
468 /* raw mode, return what we have "as is" */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
469 if (dsh->rdbuf->len > 0) {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
470 ret = dStrndup(dsh->rdbuf->str, dsh->rdbuf->len);
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
471 dStr_truncate(dsh->rdbuf, 0);
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
472 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
473 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
474
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
475 return ret;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
476 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
477
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
478 /*
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
479 * Close this socket for reading and writing.
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
480 * (flush pending data)
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
481 */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
482 void a_Dpip_dsh_close(Dsh *dsh)
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
483 {
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
484 int st;
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
485
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
486 /* flush internal buffer */
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
487 a_Dpip_dsh_write(dsh, 1, "", 0);
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
488
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
489 /* close fds */
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
490 while((st = close(dsh->fd_in)) < 0 && errno == EINTR) ;
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
491 if (st < 0)
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
492 MSG_ERR("[a_Dpip_dsh_close] close: %s\n", dStrerror(errno));
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
493 if (dsh->fd_out != dsh->fd_in) {
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
494 while((st = close(dsh->fd_out)) < 0 && errno == EINTR) ;
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
495 if (st < 0)
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
496 MSG_ERR("[a_Dpip_dsh_close] close: %s\n", dStrerror(errno));
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
497 }
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
498 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
499
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
500 /*
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
501 * Free the SockHandler structure
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
502 */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
503 void a_Dpip_dsh_free(Dsh *dsh)
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
504 {
1393
dcf1ec78121a Made downloads dpi use dsh auth
Jorge Arellano Cid <jcid@dillo.org>
parents: 1392
diff changeset
505 dReturn_if (dsh == NULL);
dcf1ec78121a Made downloads dpi use dsh auth
Jorge Arellano Cid <jcid@dillo.org>
parents: 1392
diff changeset
506
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
507 dStr_free(dsh->wrbuf, 1);
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
508 dStr_free(dsh->rdbuf, 1);
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
509 dFree(dsh);
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
510 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
511