annotate dpi/dpiutil.c @ 0:6ee11bf9e3ea

Initial revision
author jcid
date Sun, 07 Oct 2007 00:36:34 +0200
parents
children d9e7b35430de
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 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
4 * Copyright 2004 Jorge Arellano Cid <jcid@dillo.org>
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 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 char *Escape_uri_str(const char *str, char *p_esc_set)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 static const char *hex = "0123456789ABCDEF";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 char *p, *esc_set;
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
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 static const char *unsafe_chars = "&<>\"'";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 static const char *unsafe_rep[] =
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 { "&amp;", "&lt;", "&gt;", "&quot;", "&#39;" };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 static const int unsafe_rep_len[] = { 5, 4, 4, 6, 5 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 * Escape unsafe characters as html entities.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 * Return value: New escaped string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 char *Escape_html_str(const char *str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 char *p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 Dstr *dstr = dStr_sized_new(64);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 for (i = 0; str[i]; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 if ((p = strchr(unsafe_chars, str[i])))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 dStr_append(dstr, unsafe_rep[p - unsafe_chars]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 else
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 dStr_append_c(dstr, str[i]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 p = dstr->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 dStr_free(dstr, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 return p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 * Unescape a few HTML entities (inverse of Escape_html_str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 * Return value: New unescaped string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 char *Unescape_html_str(const char *str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 int i, j, k;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 char *u_str = dStrdup(str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 if (!strchr(str, '&'))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 return u_str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 for (i = 0, j = 0; str[i]; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 if (str[i] == '&') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 for (k = 0; k < 5; ++k) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 if (!dStrncasecmp(str + i, unsafe_rep[k], unsafe_rep_len[k])) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 i += unsafe_rep_len[k] - 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 u_str[j++] = (k < 5) ? unsafe_chars[k] : str[i];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 u_str[j++] = str[i];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 u_str[j] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 return u_str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 * Filter '\n', '\r', "%0D" and "%0A" from the authority part of an FTP url.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 * This helps to avoid a SMTP relaying hack. This filtering could be done
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 * only when port == 25, but if the mail server is listening on another
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 * port it wouldn't work.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 * Note: AFAIS this should be done by wget.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 char *Filter_smtp_hack(char *url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 char c;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 if (strlen(url) > 6) { /* ftp:// */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 for (i = 6; (c = url[i]) && c != '/'; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 if (c == '\n' || c == '\r') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 memmove(url + i, url + i + 1, strlen(url + i));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 --i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 } else if (c == '%' && url[i+1] == '0' &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 (tolower(url[i+2]) == 'a' || tolower(url[i+2]) == 'd')) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136 memmove(url + i, url + i + 3, strlen(url + i + 2));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 --i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 return url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 /* Streamed Sockets API (not mandatory) ----------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 * Create and initialize the SockHandler structure
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 SockHandler *sock_handler_new(int fd_in, int fd_out, int flush_sz)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 SockHandler *sh = dNew(SockHandler, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 /* init descriptors and streams */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 sh->fd_in = fd_in;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 sh->fd_out = fd_out;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 sh->out = fdopen(fd_out, "w");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 /* init buffer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 sh->buf_max = 8 * 1024;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 sh->buf = dNew(char, sh->buf_max);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 sh->buf_sz = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 sh->flush_sz = flush_sz;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165 return sh;
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 * Streamed write to socket
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 * Return: 0 on success, 1 on error.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
172 int sock_handler_write(SockHandler *sh, int flush,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
173 const char *Data, size_t DataSize)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
174 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
175 int retval = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 /* append to buf */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178 while (sh->buf_max < sh->buf_sz + DataSize) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
179 sh->buf_max <<= 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180 sh->buf = dRealloc(sh->buf, sh->buf_max);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182 memcpy(sh->buf + sh->buf_sz, Data, DataSize);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
183 sh->buf_sz += DataSize;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
184 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
185 MSG("sh->buf=%p, sh->buf_sz=%d, sh->buf_max=%d, sh->flush_sz=%d\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186 sh->buf, sh->buf_sz, sh->buf_max, sh->flush_sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188 /**/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
189 #if 0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
190 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
191 uint_t i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 /* Test dpip's stream handling by chopping data into characters */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
193 for (i = 0; i < sh->buf_sz; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
194 fputc(sh->buf[i], sh->out);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
195 fflush(sh->out);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
196 usleep(50);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198 if (i == sh->buf_sz) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
199 sh->buf_sz = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
200 retval = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
201 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
202 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
203 #else
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204 /* flush data if necessary */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
205 if (flush || sh->buf_sz >= sh->flush_sz) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
206 if (sh->buf_sz && fwrite (sh->buf, sh->buf_sz, 1, sh->out) != 1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
207 perror("[sock_handler_write]");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
208 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
209 fflush(sh->out);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
210 sh->buf_sz = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
211 retval = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
212 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
214 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
215 retval = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
216 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
217 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
218 return retval;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
219 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
220
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
221 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
222 * Convenience function.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
223 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
224 int sock_handler_write_str(SockHandler *sh, int flush, const char *str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
225 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
226 return sock_handler_write(sh, flush, str, strlen(str));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
227 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
228
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
229 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
230 * Return a newlly allocated string with the contents read from the socket.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
231 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
232 char *sock_handler_read(SockHandler *sh)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
233 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
234 ssize_t st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
235 char buf[16384];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
236
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
237 /* can't use fread() */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
238 do
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
239 st = read(sh->fd_in, buf, 16384);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
240 while (st < 0 && errno == EINTR);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
241
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
242 if (st == -1)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
243 perror("[sock_handler_read]");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
244
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
245 return (st > 0) ? dStrndup(buf, (uint_t)st) : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
246 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
247
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
248 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
249 * Close this socket for reading and writing.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
250 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
251 void sock_handler_close(SockHandler *sh)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
252 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
253 /* flush before closing */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
254 sock_handler_write(sh, 1, "", 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
255
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
256 fclose(sh->out);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
257 close(sh->fd_out);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
258 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
259
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
260 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
261 * Free the SockHandler structure
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
262 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
263 void sock_handler_free(SockHandler *sh)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
264 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
265 dFree(sh->buf);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
266 dFree(sh);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
267 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
268
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
269 /* ------------------------------------------------------------------------ */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
270