2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: comstack.c,v 1.4 2004-01-06 20:21:37 adam Exp $
12 #include <yaz/comstack.h>
13 #include <yaz/tcpip.h>
18 #define strncasecmp _strnicmp
21 static const char *cs_errlist[] =
23 "No error or unspecified error",
24 "System (lower-layer) error",
25 "Operation out of state",
26 "No data (operation would block)",
27 "New data while half of old buffer is on the line (flow control)",
32 const char *cs_errmsg(int n)
36 if (n < CSNONE || n > CSLASTERROR) {
37 sprintf(buf, "unknown comstack error %d", n);
41 sprintf(buf, "%s: %s", cs_errlist[n], strerror(errno));
47 const char *cs_strerror(COMSTACK h)
49 return cs_errmsg(h->cerrno);
52 void cs_get_host_args(const char *type_and_host, const char **args)
56 if (*type_and_host && strncmp(type_and_host, "unix:", 5))
59 cp = strstr(type_and_host, "://");
70 COMSTACK cs_create_host(const char *type_and_host, int blocking, void **vp)
72 enum oid_proto proto = PROTO_Z3950;
77 if (strncmp (type_and_host, "tcp:", 4) == 0)
80 host = type_and_host + 4;
82 else if (strncmp (type_and_host, "ssl:", 4) == 0)
84 #if HAVE_OPENSSL_SSL_H
86 host = type_and_host + 4;
91 else if (strncmp (type_and_host, "unix:", 5) == 0)
95 host = type_and_host + 5;
100 else if (strncmp(type_and_host, "http:", 5) == 0)
103 host = type_and_host + 5;
104 if (host[0] == '/' && host[1] == '/')
108 else if (strncmp(type_and_host, "https:", 6) == 0)
110 #if HAVE_OPENSSL_SSL_H
112 host = type_and_host + 6;
113 if (host[0] == '/' && host[1] == '/')
123 host = type_and_host;
126 cs = cs_create (t, blocking, proto);
130 if (!(*vp = cs_straddr(cs, host)))
138 int cs_look (COMSTACK cs)
143 int cs_complete_auto(const unsigned char *buf, int len)
145 if (len > 5 && buf[0] >= 0x20 && buf[0] < 0x7f
146 && buf[1] >= 0x20 && buf[1] < 0x7f
147 && buf[2] >= 0x20 && buf[2] < 0x7f)
149 /* deal with HTTP request/response */
150 int i = 2, content_len = 0, chunked = 0;
155 return i; /* do not allow more than 8K HTTP header */
156 if (buf[i] == '\r' && buf[i+1] == '\n')
159 if (buf[i] == '\r' && buf[i+1] == '\n')
172 for (j = i; j <= i+4; j++)
173 printf ("%c", buf[j]);
181 printf ("XXXXXXXX not there yet 1\n");
182 printf ("i=%d len=%d\n", i, len);
185 } else if (isdigit(buf[i]))
186 chunk_len = chunk_len * 16 +
188 else if (isupper(buf[i]))
189 chunk_len = chunk_len * 16 +
190 (buf[i++] - ('A'-10));
191 else if (islower(buf[i]))
192 chunk_len = chunk_len * 16 +
193 (buf[i++] - ('a'-10));
196 if (buf[i] != '\r' || buf[i+1] != '\n' ||
198 return i+2; /* bad. stop now */
201 /* consider trailing headers .. */
204 if (buf[i] == '\r' && buf[i+1] == '\n' &&
205 buf[i+2] == '\r' && buf[i+3] == '\n')
212 printf ("XXXXXXXXX not there yet 2\n");
213 printf ("i=%d len=%d\n", i, len);
221 { /* not chunked ; inside body */
222 /* i += 2 seems not to work with GCC -O2 ..
223 so i+2 is used instead .. */
224 if (len >= (i+2)+ content_len)
225 return (i+2)+ content_len;
229 else if (i < len - 20 &&
230 !strncasecmp(buf+i, "Transfer-Encoding:", 18))
233 while (buf[i] == ' ')
236 if (!strncasecmp(buf+i, "chunked", 7))
239 else if (i < len - 17 &&
240 !strncasecmp(buf+i, "Content-Length:", 15))
243 while (buf[i] == ' ')
246 while (i <= len-4 && isdigit(buf[i]))
247 content_len = content_len*10 + (buf[i++] - '0');
248 if (content_len < 0) /* prevent negative offsets */
259 return completeBER(buf, len);