annotate dpip/dpip.c @ 2104:3e7e5395f0bc

non-ASCII keybindings Alexander Voigt has kindly done some testing, and it seems that this makes bindings to most keys on a German keyboard possible -- except those that need AltGr don't work yet.
author corvid <corvid@lavabit.com>
date Thu, 23 Jun 2011 19:24:11 +0000
parents 16e52ecf20a8
children
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);
2103
16e52ecf20a8 Eliminated a pack of 22 compiler warnings (gcc-4.6.1 amd64)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1776
diff changeset
217 if (tail && port != 0) {
16e52ecf20a8 Eliminated a pack of 22 compiler warnings (gcc-4.6.1 amd64)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1776
diff changeset
218 for (i = 0; *tail && isxdigit(tail[i+1]); ++i)
16e52ecf20a8 Eliminated a pack of 22 compiler warnings (gcc-4.6.1 amd64)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1776
diff changeset
219 SharedSecret[i] = tail[i+1];
16e52ecf20a8 Eliminated a pack of 22 compiler warnings (gcc-4.6.1 amd64)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1776
diff changeset
220 SharedSecret[i] = 0;
16e52ecf20a8 Eliminated a pack of 22 compiler warnings (gcc-4.6.1 amd64)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1776
diff changeset
221 if (strcmp(msg, SharedSecret) == 0)
16e52ecf20a8 Eliminated a pack of 22 compiler warnings (gcc-4.6.1 amd64)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1776
diff changeset
222 ret = 1;
16e52ecf20a8 Eliminated a pack of 22 compiler warnings (gcc-4.6.1 amd64)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1776
diff changeset
223 }
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
224 }
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
225 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
226 fclose(In);
1384
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
227 dFree(rcline);
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
228 dFree(fname);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
229 dFree(msg);
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
230 dFree(cmd);
1384
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 return ret;
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
233 }
6c8346ed1073 Introduce basic shared-secret-based authentication
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
234
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
235 /* --------------------------------------------------------------------------
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
236 * Dpip socket API ----------------------------------------------------------
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
237 */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
238
1385
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 * 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
241 */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
242 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
243 {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
244 Dsh *dsh = dNew(Dsh, 1);
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
245
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
246 /* init descriptors and streams */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
247 dsh->fd_in = fd_in;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
248 dsh->fd_out = fd_out;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
249
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
250 /* init buffer */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
251 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
252 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
253 dsh->flush_sz = flush_sz;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
254 dsh->mode = DPIP_TAG;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
255 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
256 dsh->mode |= DPIP_NONBLOCK;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
257 dsh->status = 0;
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 return dsh;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
260 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
261
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
262 /*
1391
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
263 * 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
264 */
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
265 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
266 {
1412
0dbf5fa97600 Compiler happiness for old versions of gcc
Jorge Arellano Cid <jcid@dillo.org>
parents: 1393
diff changeset
267 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
268
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
269 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
270 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
271 /* 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
272 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
273 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
274 (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
275 }
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
276
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
277 while (1) {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
278 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
279 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
280 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
281 continue;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
282 } 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
283 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
284 ret = -1;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
285 break;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
286 } else {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
287 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
288 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
289 break;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
290 }
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
291 } else {
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
292 sent += st;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
293 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
294 ret = sent;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
295 break;
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 }
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
299
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
300 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
301 /* 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
302 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
303 }
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 return ret;
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
306 }
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
307
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
308 /*
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
309 * Streamed write to socket
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
310 * 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
311 */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
312 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
313 {
1391
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
314 int ret = 1;
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
315
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
316 /* append to buf */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
317 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
318
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
319 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
320 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
321
1391
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
322 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
323 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
324 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
325 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
326 }
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
327
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
328 return ret;
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
329 }
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
330
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
331 /*
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1566
diff changeset
332 * 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
333 * 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
334 */
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
335 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
336 {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
337 int st;
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
338
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
339 if (dsh->wrbuf->len == 0) {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
340 st = 0;
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
341 } else {
1391
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
342 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
343 if (st > 0) {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
344 /* update internal buffer */
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
345 dStr_erase(dsh->wrbuf, 0, st);
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
346 }
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 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
349 }
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
350
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 * 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
353 */
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
354 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
355 {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
356 int st;
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
357
1391
f8b26ef0ebf8 Unified the internal dsh API implementation into a unique write function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
358 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
359 /* update internal buffer */
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
360 if (st < DataSize)
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
361 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
362 }
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
363 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
364 }
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
365
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
366 /*
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
367 * Convenience function.
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 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
370 {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
371 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
372 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
373
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
374 /*
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
375 * 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
376 * 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
377 */
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
378 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
379 {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
380 char buf[RBUF_SZ];
2103
16e52ecf20a8 Eliminated a pack of 22 compiler warnings (gcc-4.6.1 amd64)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1776
diff changeset
381 int req_mode, old_flags = 0, st, nb = !blocking;
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
382
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
383 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
384
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
385 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
386 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
387 /* change mode temporarily... */
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
388 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
389 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
390 (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
391 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
392
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
393 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
394 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
395 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
396 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
397 continue;
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
398 } else if (errno == EAGAIN) {
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
399 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
400 break;
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
401 } else {
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
402 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
403 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
404 break;
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
405 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
406 } else if (st == 0) {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
407 dsh->status = DPIP_EOF;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
408 break;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
409 } else {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
410 /* append to buf */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
411 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
412 if (blocking)
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
413 break;
1385
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 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
416
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
417 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
418 /* 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
419 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
420 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
421
1392
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
422 /* 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
423 * (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
424 if (blocking)
ca4ca7b8d096 Unified the internal dsh API implementation into a unique read function
Jorge Arellano Cid <jcid@dillo.org>
parents: 1391
diff changeset
425 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
426 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
427
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
428 /*
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
429 * 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
430 * 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
431 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
432 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
433 {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
434 char *p, *ret = NULL;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
435
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
436 /* 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
437 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
438
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
439 /* switch mode upon request */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
440 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
441 dsh->mode = DPIP_RAW;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
442
1566
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
443 if (blocking) {
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
444 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
445 /* 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
446 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
447 do {
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
448 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
449 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
450 } 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
451 }
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
452
ef497b102b93 Fix a_Dpip_dsh_read_token() for long blocking reads
Jorge Arellano Cid <jcid@dillo.org>
parents: 1412
diff changeset
453 } 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
454 /* 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
455 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
456 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
457 }
1385
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
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
460 if (dsh->mode & DPIP_TAG) {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
461 /* return a full tag */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
462 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
463 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
464 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
465 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
466 dsh->mode |= DPIP_LAST_TAG;
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
467 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
468 } else {
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
469 /* 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
470 if (dsh->rdbuf->len > 0) {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
471 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
472 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
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
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
476 return ret;
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 /*
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
480 * 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
481 * (flush pending data)
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
482 */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
483 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
484 {
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
485 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
486
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
487 /* flush internal buffer */
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
488 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
489
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
490 /* 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
491 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
492 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
493 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
494 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
495 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
496 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
497 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
498 }
1385
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 /*
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
502 * Free the SockHandler structure
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
503 */
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
504 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
505 {
1393
dcf1ec78121a Made downloads dpi use dsh auth
Jorge Arellano Cid <jcid@dillo.org>
parents: 1392
diff changeset
506 dReturn_if (dsh == NULL);
dcf1ec78121a Made downloads dpi use dsh auth
Jorge Arellano Cid <jcid@dillo.org>
parents: 1392
diff changeset
507
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
508 dStr_free(dsh->wrbuf, 1);
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
509 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
510 dFree(dsh);
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
511 }
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
512