2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: comstack.c,v 1.15 2003-10-16 10:18:56 adam Exp $
11 #include <yaz/comstack.h>
12 #include <yaz/tcpip.h>
16 static const char *cs_errlist[] =
18 "No error or unspecified error",
19 "System (lower-layer) error",
20 "Operation out of state",
21 "No data (operation would block)",
22 "New data while half of old buffer is on the line (flow control)",
27 const char *cs_errmsg(int n)
34 const char *cs_strerror(COMSTACK h)
36 return cs_errmsg(h->cerrno);
39 void cs_get_host_args(const char *type_and_host, const char **args)
43 if (*type_and_host && strncmp(type_and_host, "unix:", 5))
46 cp = strstr(type_and_host, "://");
57 COMSTACK cs_create_host(const char *type_and_host, int blocking, void **vp)
59 enum oid_proto proto = PROTO_Z3950;
64 if (strncmp (type_and_host, "tcp:", 4) == 0)
67 host = type_and_host + 4;
69 else if (strncmp (type_and_host, "ssl:", 4) == 0)
71 #if HAVE_OPENSSL_SSL_H
73 host = type_and_host + 4;
78 else if (strncmp (type_and_host, "unix:", 5) == 0)
82 host = type_and_host + 5;
87 else if (strncmp(type_and_host, "http:", 5) == 0)
90 host = type_and_host + 5;
91 if (host[0] == '/' && host[1] == '/')
95 else if (strncmp(type_and_host, "https:", 6) == 0)
97 #if HAVE_OPENSSL_SSL_H
99 host = type_and_host + 6;
100 if (host[0] == '/' && host[1] == '/')
110 host = type_and_host;
113 cs = cs_create (t, blocking, proto);
117 if (!(*vp = cs_straddr(cs, host)))
125 int cs_look (COMSTACK cs)
130 int cs_complete_auto(const unsigned char *buf, int len)
134 if (!buf[0] && !buf[1])
136 if (len > 5 && buf[0] >= 0x20 && buf[0] < 0x7f
137 && buf[1] >= 0x20 && buf[1] < 0x7f
138 && buf[2] >= 0x20 && buf[2] < 0x7f)
140 /* deal with HTTP request/response */
141 int i = 2, content_len = 0, chunked = 0;
145 if (buf[i] == '\r' && buf[i+1] == '\n')
148 if (buf[i] == '\r' && buf[i+1] == '\n')
162 for (j = i; j <= i+4; j++)
163 printf ("%c", buf[j]);
171 printf ("XXXXXXXX not there yet 1\n");
172 printf ("i=%d len=%d\n", i, len);
175 } else if (isdigit(buf[i]))
176 chunk_len = chunk_len * 16 +
178 else if (isupper(buf[i]))
179 chunk_len = chunk_len * 16 +
180 (buf[i++] - ('A'-10));
181 else if (islower(buf[i]))
182 chunk_len = chunk_len * 16 +
183 (buf[i++] - ('a'-10));
186 if (buf[i] != '\r' || buf[i+1] != '\n' ||
188 return i+2; /* bad. stop now */
191 /* consider trailing headers .. */
194 if (buf[i] == '\r' && buf[i+1] == '\n' &&
195 buf[i+2] == '\r' && buf[i+3] == '\n')
202 printf ("XXXXXXXXX not there yet 2\n");
203 printf ("i=%d len=%d\n", i, len);
212 /* i += 2 seems not to work with GCC -O2 ..
213 so i+2 is used instead .. */
214 if (len >= (i+2)+ content_len)
215 return (i+2)+ content_len;
219 else if (i < len - 21 &&
220 !memcmp(buf+i, "Transfer-Encoding: ", 18))
226 if (!memcmp(buf+i, "chunked", 7))
229 else if (i < len - 18 &&
230 !memcmp(buf+i, "Content-Length: ", 15))
236 while (i <= len-4 && isdigit(buf[i]))
237 content_len = content_len*10 + (buf[i++] - '0');
238 if (content_len < 0) /* prevent negative offsets */
249 return completeBER(buf, len);