2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: comstack.c,v 1.7 2004-04-28 22:44:59 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;
78 #if HAVE_OPENSSL_SSL_H
79 "cs_create_host SSL\n"
84 if (strncmp (type_and_host, "tcp:", 4) == 0)
87 host = type_and_host + 4;
89 else if (strncmp (type_and_host, "ssl:", 4) == 0)
91 #if HAVE_OPENSSL_SSL_H
93 host = type_and_host + 4;
98 else if (strncmp (type_and_host, "unix:", 5) == 0)
102 host = type_and_host + 5;
107 else if (strncmp(type_and_host, "http:", 5) == 0)
110 host = type_and_host + 5;
111 if (host[0] == '/' && host[1] == '/')
115 else if (strncmp(type_and_host, "https:", 6) == 0)
117 #if HAVE_OPENSSL_SSL_H
119 host = type_and_host + 6;
120 if (host[0] == '/' && host[1] == '/')
130 host = type_and_host;
133 cs = cs_create (t, blocking, proto);
137 if (!(*vp = cs_straddr(cs, host)))
145 int cs_look (COMSTACK cs)
150 #define CHUNK_DEBUG 0
151 int cs_complete_auto(const unsigned char *buf, int len)
153 if (len > 5 && buf[0] >= 0x20 && buf[0] < 0x7f
154 && buf[1] >= 0x20 && buf[1] < 0x7f
155 && buf[2] >= 0x20 && buf[2] < 0x7f)
157 /* deal with HTTP request/response */
158 int i = 2, content_len = 0, chunked = 0;
163 return i; /* do not allow more than 8K HTTP header */
164 if (buf[i] == '\r' && buf[i+1] == '\n')
167 if (buf[i] == '\r' && buf[i+1] == '\n')
171 /* inside chunked body .. */
174 int j, chunk_len = 0;
182 for (j = i; j <= i+4; j++)
183 printf ("%c", buf[j]);
187 /* read chunk length */
192 printf ("XXXXXXXX not there yet 1\n");
193 printf ("i=%d len=%d\n", i, len);
196 } else if (isdigit(buf[i]))
197 chunk_len = chunk_len * 16 +
199 else if (isupper(buf[i]))
200 chunk_len = chunk_len * 16 +
201 (buf[i++] - ('A'-10));
202 else if (islower(buf[i]))
203 chunk_len = chunk_len * 16 +
204 (buf[i++] - ('a'-10));
207 /* move forward until CRLF - skip chunk ext */
209 while (buf[i] != '\r' && buf[i+1] != '\n')
212 return 0; /* need more buffer .. */
214 return i; /* enough.. stop */
219 printf ("XXXXXX chunk_len=%d\n", chunk_len);
222 return i+2; /* bad chunk_len */
227 /* consider trailing headers .. */
230 if (buf[i] == '\r' && buf[i+1] == '\n' &&
231 buf[i+2] == '\r' && buf[i+3] == '\n')
238 printf ("XXXXXXXXX not there yet 2\n");
239 printf ("i=%d len=%d\n", i, len);
244 { /* not chunked ; inside body */
245 /* i += 2 seems not to work with GCC -O2 ..
246 so i+2 is used instead .. */
247 if (len >= (i+2)+ content_len)
248 return (i+2)+ content_len;
252 else if (i < len - 20 &&
253 !strncasecmp(buf+i, "Transfer-Encoding:", 18))
256 while (buf[i] == ' ')
259 if (!strncasecmp(buf+i, "chunked", 7))
262 else if (i < len - 17 &&
263 !strncasecmp(buf+i, "Content-Length:", 15))
266 while (buf[i] == ' ')
269 while (i <= len-4 && isdigit(buf[i]))
270 content_len = content_len*10 + (buf[i++] - '0');
271 if (content_len < 0) /* prevent negative offsets */
282 return completeBER(buf, len);