1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
14 #include <yaz/comstack.h>
15 #include <yaz/tcpip.h>
17 static void tst_http_request(void)
20 /* no content, no headers */
21 const char *http_buf =
22 /*123456789012345678 */
27 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
28 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
29 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 16), 0);
30 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 17), 0);
31 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 18), 18);
32 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 19), 18);
35 /* one header, no content */
36 const char *http_buf =
37 /*123456789012345678 */
43 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
44 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
45 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 34), 0);
46 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 35), 35);
47 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 36), 35);
50 /* one content-length header, length 0 */
51 const char *http_buf =
52 /*123456789012345678 */
54 "Content-Length: 0\r\n"
58 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
59 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
60 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 35), 0);
61 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 37), 37);
62 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 38), 37);
65 /* one content-length header, length 5 */
66 const char *http_buf =
67 /*123456789012345678 */
69 "Content-Length: 5\r\n"
74 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
75 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
76 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 41), 0);
77 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 42), 42);
78 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 43), 42);
81 /* LF only in GET, one content-length header, length 5 */
82 const char *http_buf =
83 /*123456789012345678 */
85 "Content-Length: 5\r\n"
90 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
91 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
92 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 40), 0);
93 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 41), 41);
94 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 42), 41);
97 /* LF only in all places, one content-length header, length 5 */
98 const char *http_buf =
99 /*123456789012345678 */
101 "Content-Length: 5\n"
104 "GET / HTTP/1.0\r\n";
106 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
107 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
108 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 38), 0);
109 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 39), 39);
110 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 40), 39);
114 /* one header, unknown transfer-encoding (no content) */
115 const char *http_buf =
116 /*12345678901234567890123456789 */
118 "Transfer-Encoding: chunke_\r\n"
120 "GET / HTTP/1.0\r\n";
122 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 45), 0);
123 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 46), 46);
127 /* one header, one chunk */
128 const char *http_buf =
129 /*12345678901234567890123456789 */
131 "Transfer-Encoding: chunked\r\n"
136 "GET / HTTP/1.0\r\n";
138 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 58), 0);
139 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 59), 59);
143 /* one header, two chunks */
144 const char *http_buf =
145 /*12345678901234567890123456789 */
147 "Transfer-Encoding: chunked\r\n"
154 "GET / HTTP/1.0\r\n";
156 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 64), 0);
157 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 65), 65);
161 static void tst_http_response(void)
164 /* unlimited content, no headers */
165 const char *http_buf =
166 /*123456789012345678 */
167 "HTTP/1.1 200 OK\r\n"
169 "HTTP/1.1 200 OK\r\n";
171 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
172 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
173 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 24), 0);
176 /* response, content */
177 const char *http_buf =
178 /*123456789012345678 */
179 "HTTP/1.1 200 OK\r\n"
180 "Content-Length: 2\r\n"
183 "HTTP/1.1 200 OK\r\n";
185 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
186 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
187 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 39), 0);
188 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 40), 40);
189 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 41), 40);
193 /** \brief COMSTACK synopsis from manual, doc/comstack.xml */
194 static int comstack_example(const char *server_address_str)
198 int size = 0, length_incoming;
199 void *server_address_ip;
202 char *protocol_package = "GET / HTTP/1.0\r\n\r\n";
203 int protocol_package_length = strlen(protocol_package);
205 stack = cs_create(tcpip_type, 1, PROTO_HTTP);
207 perror("cs_create"); /* use perror() here since we have no stack yet */
211 server_address_ip = cs_straddr(stack, server_address_str);
212 if (!server_address_ip)
214 fprintf(stderr, "cs_straddr: address could not be resolved\n");
218 status = cs_connect(stack, server_address_ip);
220 fprintf(stderr, "cs_connect: %s\n", cs_strerror(stack));
224 status = cs_put(stack, protocol_package, protocol_package_length);
226 fprintf(stderr, "cs_put: %s\n", cs_strerror(stack));
230 /* Now get a response */
232 length_incoming = cs_get(stack, &buf, &size);
233 if (!length_incoming) {
234 fprintf(stderr, "Connection closed\n");
236 } else if (length_incoming < 0) {
237 fprintf(stderr, "cs_get: %s\n", cs_strerror(stack));
242 fwrite(buf, length_incoming, 1, stdout);
252 int main (int argc, char **argv)
254 YAZ_CHECK_INIT(argc, argv);
257 comstack_example(argv[1]);
266 * c-file-style: "Stroustrup"
267 * indent-tabs-mode: nil
269 * vim: shiftwidth=4 tabstop=8 expandtab