2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: comstack.c,v 1.15 2005-06-25 15:46:03 adam Exp $
10 * \brief Implements Generic COMSTACK functions
17 #include <yaz/comstack.h>
18 #include <yaz/tcpip.h>
23 #define strncasecmp _strnicmp
26 static const char *cs_errlist[] =
28 "No error or unspecified error",
29 "System (lower-layer) error",
30 "Operation out of state",
31 "No data (operation would block)",
32 "New data while half of old buffer is on the line (flow control)",
37 const char *cs_errmsg(int n)
41 if (n < CSNONE || n > CSLASTERROR) {
42 sprintf(buf, "unknown comstack error %d", n);
46 sprintf(buf, "%s: %s", cs_errlist[n], strerror(errno));
52 const char *cs_strerror(COMSTACK h)
54 return cs_errmsg(h->cerrno);
57 void cs_get_host_args(const char *type_and_host, const char **args)
61 if (*type_and_host && strncmp(type_and_host, "unix:", 5))
64 cp = strstr(type_and_host, "://");
75 COMSTACK cs_create_host(const char *type_and_host, int blocking, void **vp)
77 enum oid_proto proto = PROTO_Z3950;
82 if (strncmp (type_and_host, "tcp:", 4) == 0)
85 host = type_and_host + 4;
87 else if (strncmp (type_and_host, "ssl:", 4) == 0)
89 #if HAVE_OPENSSL_SSL_H
91 host = type_and_host + 4;
96 else if (strncmp (type_and_host, "unix:", 5) == 0)
100 host = type_and_host + 5;
105 else if (strncmp(type_and_host, "http:", 5) == 0)
108 host = type_and_host + 5;
109 while (host[0] == '/')
113 else if (strncmp(type_and_host, "https:", 6) == 0)
115 #if HAVE_OPENSSL_SSL_H
117 host = type_and_host + 6;
118 while (host[0] == '/')
128 host = type_and_host;
130 cs = cs_create (t, blocking, proto);
134 if (!(*vp = cs_straddr(cs, host)))
142 int cs_look (COMSTACK cs)
147 #define CHUNK_DEBUG 0
148 int cs_complete_auto(const unsigned char *buf, int len)
150 if (len > 5 && buf[0] >= 0x20 && buf[0] < 0x7f
151 && buf[1] >= 0x20 && buf[1] < 0x7f
152 && buf[2] >= 0x20 && buf[2] < 0x7f)
154 /* deal with HTTP request/response */
155 int i = 2, content_len = 0, chunked = 0;
157 /* if dealing with HTTP responses - then default
158 content length is unlimited (socket close) */
159 if (!memcmp(buf, "HTTP/", 5))
166 return i; /* do not allow more than 8K HTTP header */
168 if (buf[i] == '\r' && buf[i+1] == '\n')
171 if (buf[i] == '\r' && buf[i+1] == '\n')
175 /* inside chunked body .. */
178 int j, chunk_len = 0;
186 for (j = i; j <= i+4; j++)
187 printf ("%c", buf[j]);
191 /* read chunk length */
196 printf ("XXXXXXXX not there yet 1\n");
197 printf ("i=%d len=%d\n", i, len);
200 } else if (isdigit(buf[i]))
201 chunk_len = chunk_len * 16 +
203 else if (isupper(buf[i]))
204 chunk_len = chunk_len * 16 +
205 (buf[i++] - ('A'-10));
206 else if (islower(buf[i]))
207 chunk_len = chunk_len * 16 +
208 (buf[i++] - ('a'-10));
211 /* move forward until CRLF - skip chunk ext */
213 while (buf[i] != '\r' && buf[i+1] != '\n')
216 return 0; /* need more buffer .. */
218 return i; /* enough.. stop */
223 printf ("XXXXXX chunk_len=%d\n", chunk_len);
226 return i+2; /* bad chunk_len */
231 /* consider trailing headers .. */
234 if (buf[i] == '\r' && buf[i+1] == '\n' &&
235 buf[i+2] == '\r' && buf[i+3] == '\n')
242 printf ("XXXXXXXXX not there yet 2\n");
243 printf ("i=%d len=%d\n", i, len);
248 { /* not chunked ; inside body */
249 /* i += 2 seems not to work with GCC -O2 ..
250 so i+2 is used instead .. */
251 if (content_len == -1)
252 return 0; /* no content length */
253 else if (len >= (i+2)+ content_len)
255 return (i+2)+ content_len;
260 else if (i < len - 20 &&
261 !strncasecmp((const char *) buf+i, "Transfer-Encoding:", 18))
264 while (buf[i] == ' ')
267 if (!strncasecmp((const char *) buf+i, "chunked", 7))
270 else if (i < len - 17 &&
271 !strncasecmp((const char *)buf+i, "Content-Length:", 15))
274 while (buf[i] == ' ')
277 while (i <= len-4 && isdigit(buf[i]))
278 content_len = content_len*10 + (buf[i++] - '0');
279 if (content_len < 0) /* prevent negative offsets */
290 return completeBER(buf, len);
295 * indent-tabs-mode: nil
297 * vim: shiftwidth=4 tabstop=8 expandtab