2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: comstack.c,v 1.12 2003-02-21 12:08:57 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 COMSTACK cs_create_host(const char *type_and_host, int blocking, void **vp)
41 enum oid_proto proto = PROTO_Z3950;
46 if (strncmp (type_and_host, "tcp:", 4) == 0)
49 host = type_and_host + 4;
51 else if (strncmp (type_and_host, "ssl:", 4) == 0)
53 #if HAVE_OPENSSL_SSL_H
55 host = type_and_host + 4;
60 else if (strncmp (type_and_host, "unix:", 5) == 0)
64 host = type_and_host + 5;
69 else if (strncmp(type_and_host, "http:", 5) == 0)
72 host = type_and_host + 5;
73 if (host[0] == '/' && host[1] == '/')
77 else if (strncmp(type_and_host, "https:", 6) == 0)
79 #if HAVE_OPENSSL_SSL_H
81 host = type_and_host + 6;
82 if (host[0] == '/' && host[1] == '/')
95 cs = cs_create (t, blocking, proto);
99 if (!(*vp = cs_straddr(cs, host)))
107 int cs_look (COMSTACK cs)
112 int cs_complete_auto(const unsigned char *buf, int len)
116 if (!buf[0] && !buf[1])
118 if (len > 5 && buf[0] >= 0x20 && buf[0] < 0x7f
119 && buf[1] >= 0x20 && buf[1] < 0x7f
120 && buf[2] >= 0x20 && buf[2] < 0x7f)
122 /* deal with HTTP request/response */
123 int i = 2, content_len = 0;
127 if (buf[i] == '\r' && buf[i+1] == '\n')
130 if (buf[i] == '\r' && buf[i+1] == '\n')
132 /* i += 2 seems not to work with GCC -O2 ..
133 so i+2 is used instead .. */
134 if (len >= (i+2)+ content_len)
135 return (i+2)+ content_len;
140 if (!memcmp(buf+i, "Content-Length:", 15))
146 while (i <= len-4 && isdigit(buf[i]))
147 content_len = content_len*10 + (buf[i++] - '0');
148 if (content_len < 0) /* prevent negative offsets */
158 return completeBER(buf, len);