1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
11 #include <yaz/comstack.h>
12 #include <yaz/tcpip.h>
14 static void tst_http_request(void)
17 /* no content, no headers */
18 const char *http_buf =
19 /*123456789012345678 */
24 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
25 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
26 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 16), 0);
27 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 17), 0);
28 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 18), 18);
29 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 19), 18);
32 /* one header, no content */
33 const char *http_buf =
34 /*123456789012345678 */
40 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
41 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
42 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 34), 0);
43 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 35), 35);
44 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 36), 35);
47 /* one content-length header, length 0 */
48 const char *http_buf =
49 /*123456789012345678 */
51 "Content-Length: 0\r\n"
55 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
56 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
57 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 35), 0);
58 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 37), 37);
59 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 38), 37);
62 /* one content-length header, length 5 */
63 const char *http_buf =
64 /*123456789012345678 */
66 "Content-Length: 5\r\n"
71 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
72 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
73 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 41), 0);
74 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 42), 42);
75 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 43), 42);
78 /* LF only in GET, one content-length header, length 5 */
79 const char *http_buf =
80 /*123456789012345678 */
82 "Content-Length: 5\r\n"
87 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
88 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
89 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 40), 0);
90 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 41), 41);
91 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 42), 41);
94 /* LF only in all places, one content-length header, length 5 */
95 const char *http_buf =
96 /*123456789012345678 */
101 "GET / HTTP/1.0\r\n";
103 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
104 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
105 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 38), 0);
106 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 39), 39);
107 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 40), 39);
111 /* one header, unknown transfer-encoding (no content) */
112 const char *http_buf =
113 /*12345678901234567890123456789 */
115 "Transfer-Encoding: chunke_\r\n"
117 "GET / HTTP/1.0\r\n";
119 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 45), 0);
120 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 46), 46);
124 /* one header, one chunk */
125 const char *http_buf =
126 /*12345678901234567890123456789 */
128 "Transfer-Encoding: chunked\r\n"
133 "GET / HTTP/1.0\r\n";
135 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 58), 0);
136 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 59), 59);
140 /* one header, two chunks */
141 const char *http_buf =
142 /*12345678901234567890123456789 */
144 "Transfer-Encoding: chunked\r\n"
151 "GET / HTTP/1.0\r\n";
153 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 64), 0);
154 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 65), 65);
158 static void tst_http_response(void)
161 /* unlimited content, no headers */
162 const char *http_buf =
163 /*123456789012345678 */
164 "HTTP/1.1 200 OK\r\n"
166 "HTTP/1.1 200 OK\r\n";
168 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
169 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
170 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 24), 0);
173 /* response, content */
174 const char *http_buf =
175 /*123456789012345678 */
176 "HTTP/1.1 200 OK\r\n"
177 "Content-Length: 2\r\n"
180 "HTTP/1.1 200 OK\r\n";
182 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
183 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
184 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 39), 0);
185 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 40), 40);
186 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 41), 40);
190 /** \brief COMSTACK synopsis from manual, doc/comstack.xml */
191 static int comstack_example(const char *server_address_str)
195 int size = 0, length_incoming;
196 void *server_address_ip;
199 char *protocol_package = "GET / HTTP/1.0\r\n\r\n";
200 int protocol_package_length = strlen(protocol_package);
202 stack = cs_create(tcpip_type, 1, PROTO_HTTP);
204 perror("cs_create"); /* use perror() here since we have no stack yet */
208 server_address_ip = cs_straddr(stack, server_address_str);
209 if (!server_address_ip)
211 fprintf(stderr, "cs_straddr: address could not be resolved\n");
215 status = cs_connect(stack, server_address_ip);
217 fprintf(stderr, "cs_connect: %s\n", cs_strerror(stack));
221 status = cs_put(stack, protocol_package, protocol_package_length);
223 fprintf(stderr, "cs_put: %s\n", cs_strerror(stack));
227 /* Now get a response */
229 length_incoming = cs_get(stack, &buf, &size);
230 if (!length_incoming) {
231 fprintf(stderr, "Connection closed\n");
233 } else if (length_incoming < 0) {
234 fprintf(stderr, "cs_get: %s\n", cs_strerror(stack));
239 fwrite(buf, length_incoming, 1, stdout);
249 int main (int argc, char **argv)
251 YAZ_CHECK_INIT(argc, argv);
254 comstack_example(argv[1]);
263 * c-file-style: "Stroustrup"
264 * indent-tabs-mode: nil
266 * vim: shiftwidth=4 tabstop=8 expandtab