2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: comstack.c,v 1.2 2003-11-17 10:40:56 mike Exp $
12 #include <yaz/comstack.h>
13 #include <yaz/tcpip.h>
17 static const char *cs_errlist[] =
19 "No error or unspecified error",
20 "System (lower-layer) error",
21 "Operation out of state",
22 "No data (operation would block)",
23 "New data while half of old buffer is on the line (flow control)",
28 const char *cs_errmsg(int n)
32 if (n < CSNONE || n > CSLASTERROR) {
33 sprintf(buf, "unknown comstack error %d", n);
37 sprintf(buf, "%s: %s", cs_errlist[n], strerror(errno));
43 const char *cs_strerror(COMSTACK h)
45 return cs_errmsg(h->cerrno);
48 void cs_get_host_args(const char *type_and_host, const char **args)
52 if (*type_and_host && strncmp(type_and_host, "unix:", 5))
55 cp = strstr(type_and_host, "://");
66 COMSTACK cs_create_host(const char *type_and_host, int blocking, void **vp)
68 enum oid_proto proto = PROTO_Z3950;
73 if (strncmp (type_and_host, "tcp:", 4) == 0)
76 host = type_and_host + 4;
78 else if (strncmp (type_and_host, "ssl:", 4) == 0)
80 #if HAVE_OPENSSL_SSL_H
82 host = type_and_host + 4;
87 else if (strncmp (type_and_host, "unix:", 5) == 0)
91 host = type_and_host + 5;
96 else if (strncmp(type_and_host, "http:", 5) == 0)
99 host = type_and_host + 5;
100 if (host[0] == '/' && host[1] == '/')
104 else if (strncmp(type_and_host, "https:", 6) == 0)
106 #if HAVE_OPENSSL_SSL_H
108 host = type_and_host + 6;
109 if (host[0] == '/' && host[1] == '/')
119 host = type_and_host;
122 cs = cs_create (t, blocking, proto);
126 if (!(*vp = cs_straddr(cs, host)))
134 int cs_look (COMSTACK cs)
139 int cs_complete_auto(const unsigned char *buf, int len)
141 if (len > 5 && buf[0] >= 0x20 && buf[0] < 0x7f
142 && buf[1] >= 0x20 && buf[1] < 0x7f
143 && buf[2] >= 0x20 && buf[2] < 0x7f)
145 /* deal with HTTP request/response */
146 int i = 2, content_len = 0, chunked = 0;
151 return i; /* do not allow more than 8K HTTP header */
152 if (buf[i] == '\r' && buf[i+1] == '\n')
155 if (buf[i] == '\r' && buf[i+1] == '\n')
168 for (j = i; j <= i+4; j++)
169 printf ("%c", buf[j]);
177 printf ("XXXXXXXX not there yet 1\n");
178 printf ("i=%d len=%d\n", i, len);
181 } else if (isdigit(buf[i]))
182 chunk_len = chunk_len * 16 +
184 else if (isupper(buf[i]))
185 chunk_len = chunk_len * 16 +
186 (buf[i++] - ('A'-10));
187 else if (islower(buf[i]))
188 chunk_len = chunk_len * 16 +
189 (buf[i++] - ('a'-10));
192 if (buf[i] != '\r' || buf[i+1] != '\n' ||
194 return i+2; /* bad. stop now */
197 /* consider trailing headers .. */
200 if (buf[i] == '\r' && buf[i+1] == '\n' &&
201 buf[i+2] == '\r' && buf[i+3] == '\n')
208 printf ("XXXXXXXXX not there yet 2\n");
209 printf ("i=%d len=%d\n", i, len);
217 { /* not chunked ; inside body */
218 /* i += 2 seems not to work with GCC -O2 ..
219 so i+2 is used instead .. */
220 if (len >= (i+2)+ content_len)
221 return (i+2)+ content_len;
225 else if (i < len - 21 &&
226 !memcmp(buf+i, "Transfer-Encoding: ", 18))
232 if (!memcmp(buf+i, "chunked", 7))
235 else if (i < len - 18 &&
236 !memcmp(buf+i, "Content-Length: ", 15))
242 while (i <= len-4 && isdigit(buf[i]))
243 content_len = content_len*10 + (buf[i++] - '0');
244 if (content_len < 0) /* prevent negative offsets */
255 return completeBER(buf, len);