annotate dpi/dpiutil.c @ 1305:29b4740d571e

Fixed URL unescaping in the datauri DPI
author Jorge Arellano Cid <jcid@dillo.org>
date Tue, 08 Sep 2009 14:16:54 -0400
parents 7a76f872ce73
children 8f6d5a94ac0e
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * File: dpiutil.c
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
35
d9e7b35430de Updated copyright lines
jcid
parents: 0
diff changeset
4 * Copyright 2004-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
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 #include <unistd.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15 #include <stdarg.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 #include <ctype.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18 #include <errno.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 #include <sys/socket.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 #include "dpiutil.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 * Debugging macros
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 #define _MSG(...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 #define MSG(...) printf("[dpiutil.c]: " __VA_ARGS__)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 /* Escaping/De-escaping ---------------------------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 * Escape URI characters in 'esc_set' as %XX sequences.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 * Return value: New escaped string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 */
306
7a76f872ce73 - Eliminated gcc 4.2.3 warnings on 64bit OS.
jcid
parents: 35
diff changeset
36 char *Escape_uri_str(const char *str, const char *p_esc_set)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 {
306
7a76f872ce73 - Eliminated gcc 4.2.3 warnings on 64bit OS.
jcid
parents: 35
diff changeset
38 static const char *esc_set, *hex = "0123456789ABCDEF";
7a76f872ce73 - Eliminated gcc 4.2.3 warnings on 64bit OS.
jcid
parents: 35
diff changeset
39 char *p;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 Dstr *dstr;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 esc_set = (p_esc_set) ? p_esc_set : "%#:' ";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 dstr = dStr_sized_new(64);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 for (i = 0; str[i]; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 if (str[i] <= 0x1F || str[i] == 0x7F || strchr(esc_set, str[i])) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 dStr_append_c(dstr, '%');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 dStr_append_c(dstr, hex[(str[i] >> 4) & 15]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 dStr_append_c(dstr, hex[str[i] & 15]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 dStr_append_c(dstr, str[i]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 p = dstr->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 dStr_free(dstr, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 return p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59
1305
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
60 /*
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
61 * Unescape %XX sequences in a string.
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
62 * Return value: a new unescaped string
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
63 */
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
64 char *Unescape_uri_str(const char *s)
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
65 {
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
66 char *p, *buf = dStrdup(s);
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
67
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
68 if (strchr(s, '%')) {
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
69 for (p = buf; (*p = *s); ++s, ++p) {
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
70 if (*p == '%' && isxdigit(s[1]) && isxdigit(s[2])) {
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
71 *p = (isdigit(s[1]) ? (s[1] - '0') : toupper(s[1]) - 'A' + 10)*16;
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
72 *p += isdigit(s[2]) ? (s[2] - '0') : toupper(s[2]) - 'A' + 10;
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
73 s += 2;
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
74 }
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
75 }
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
76 }
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
77
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
78 return buf;
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
79 }
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
80
29b4740d571e Fixed URL unescaping in the datauri DPI
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
81
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 static const char *unsafe_chars = "&<>\"'";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 static const char *unsafe_rep[] =
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 { "&amp;", "&lt;", "&gt;", "&quot;", "&#39;" };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 static const int unsafe_rep_len[] = { 5, 4, 4, 6, 5 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 * Escape unsafe characters as html entities.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 * Return value: New escaped string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 char *Escape_html_str(const char *str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 char *p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 Dstr *dstr = dStr_sized_new(64);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 for (i = 0; str[i]; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 if ((p = strchr(unsafe_chars, str[i])))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 dStr_append(dstr, unsafe_rep[p - unsafe_chars]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 else
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 dStr_append_c(dstr, str[i]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 p = dstr->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 dStr_free(dstr, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 return p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 * Unescape a few HTML entities (inverse of Escape_html_str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 * Return value: New unescaped string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 char *Unescape_html_str(const char *str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 int i, j, k;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 char *u_str = dStrdup(str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 if (!strchr(str, '&'))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 return u_str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 for (i = 0, j = 0; str[i]; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 if (str[i] == '&') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 for (k = 0; k < 5; ++k) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 if (!dStrncasecmp(str + i, unsafe_rep[k], unsafe_rep_len[k])) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 i += unsafe_rep_len[k] - 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 u_str[j++] = (k < 5) ? unsafe_chars[k] : str[i];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 u_str[j++] = str[i];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 u_str[j] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136 return u_str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 * Filter '\n', '\r', "%0D" and "%0A" from the authority part of an FTP url.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 * This helps to avoid a SMTP relaying hack. This filtering could be done
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 * only when port == 25, but if the mail server is listening on another
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 * port it wouldn't work.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 * Note: AFAIS this should be done by wget.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 char *Filter_smtp_hack(char *url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 char c;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 if (strlen(url) > 6) { /* ftp:// */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 for (i = 6; (c = url[i]) && c != '/'; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 if (c == '\n' || c == '\r') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 memmove(url + i, url + i + 1, strlen(url + i));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 --i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 } else if (c == '%' && url[i+1] == '0' &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 (tolower(url[i+2]) == 'a' || tolower(url[i+2]) == 'd')) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 memmove(url + i, url + i + 3, strlen(url + i + 2));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 --i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 return url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167 /* Streamed Sockets API (not mandatory) ----------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
169 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 * Create and initialize the SockHandler structure
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
172 SockHandler *sock_handler_new(int fd_in, int fd_out, int flush_sz)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
173 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
174 SockHandler *sh = dNew(SockHandler, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
175
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176 /* init descriptors and streams */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 sh->fd_in = fd_in;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178 sh->fd_out = fd_out;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
179 sh->out = fdopen(fd_out, "w");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181 /* init buffer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182 sh->buf_max = 8 * 1024;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
183 sh->buf = dNew(char, sh->buf_max);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
184 sh->buf_sz = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
185 sh->flush_sz = flush_sz;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187 return sh;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
189
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
190 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
191 * Streamed write to socket
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 * Return: 0 on success, 1 on error.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
193 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
194 int sock_handler_write(SockHandler *sh, int flush,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
195 const char *Data, size_t DataSize)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
196 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 int retval = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
199 /* append to buf */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
200 while (sh->buf_max < sh->buf_sz + DataSize) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
201 sh->buf_max <<= 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
202 sh->buf = dRealloc(sh->buf, sh->buf_max);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
203 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204 memcpy(sh->buf + sh->buf_sz, Data, DataSize);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
205 sh->buf_sz += DataSize;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
206 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
207 MSG("sh->buf=%p, sh->buf_sz=%d, sh->buf_max=%d, sh->flush_sz=%d\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
208 sh->buf, sh->buf_sz, sh->buf_max, sh->flush_sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
209 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
210 /**/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
211 #if 0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
212 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213 uint_t i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
214 /* Test dpip's stream handling by chopping data into characters */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
215 for (i = 0; i < sh->buf_sz; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
216 fputc(sh->buf[i], sh->out);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
217 fflush(sh->out);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
218 usleep(50);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
219 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
220 if (i == sh->buf_sz) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
221 sh->buf_sz = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
222 retval = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
223 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
224 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
225 #else
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
226 /* flush data if necessary */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
227 if (flush || sh->buf_sz >= sh->flush_sz) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
228 if (sh->buf_sz && fwrite (sh->buf, sh->buf_sz, 1, sh->out) != 1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
229 perror("[sock_handler_write]");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
230 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
231 fflush(sh->out);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
232 sh->buf_sz = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
233 retval = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
234 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
235
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
236 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
237 retval = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
238 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
239 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
240 return retval;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
241 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
242
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
243 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
244 * Convenience function.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
245 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
246 int sock_handler_write_str(SockHandler *sh, int flush, const char *str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
247 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
248 return sock_handler_write(sh, flush, str, strlen(str));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
249 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
250
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
251 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
252 * Return a newlly allocated string with the contents read from the socket.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
253 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
254 char *sock_handler_read(SockHandler *sh)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
255 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
256 ssize_t st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
257 char buf[16384];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
258
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
259 /* can't use fread() */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
260 do
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
261 st = read(sh->fd_in, buf, 16384);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
262 while (st < 0 && errno == EINTR);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
263
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
264 if (st == -1)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
265 perror("[sock_handler_read]");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
266
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
267 return (st > 0) ? dStrndup(buf, (uint_t)st) : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
268 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
269
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
270 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
271 * Close this socket for reading and writing.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
272 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
273 void sock_handler_close(SockHandler *sh)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
274 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
275 /* flush before closing */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
276 sock_handler_write(sh, 1, "", 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
277
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
278 fclose(sh->out);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
279 close(sh->fd_out);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
280 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
281
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
282 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
283 * Free the SockHandler structure
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
284 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
285 void sock_handler_free(SockHandler *sh)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
286 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
287 dFree(sh->buf);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
288 dFree(sh);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
289 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
290
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
291 /* ------------------------------------------------------------------------ */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
292