1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 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 /* no content, no headers */
177 const char *http_buf =
178 /*123456789012345678 */
179 "HTTP/1.1 204 OK\r\n"
181 "HTTP/1.1 200 OK\r\n";
183 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
184 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
185 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 18), 0);
186 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 19), 19);
187 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 24), 19);
190 /* response, content */
191 const char *http_buf =
192 /*123456789012345678 */
193 "HTTP/1.1 200 OK\r\n"
194 "Content-Length: 2\r\n"
197 "HTTP/1.1 200 OK\r\n";
199 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 1), 0);
200 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 2), 0);
201 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 39), 0);
202 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 40), 40);
203 YAZ_CHECK_EQ(cs_complete_auto(http_buf, 41), 40);
207 /** \brief COMSTACK synopsis from manual, doc/comstack.xml */
208 static int comstack_example(const char *server_address_str)
212 int size = 0, length_incoming;
213 void *server_address_ip;
216 char *protocol_package = "GET / HTTP/1.0\r\n\r\n";
217 int protocol_package_length = strlen(protocol_package);
219 stack = cs_create(tcpip_type, 1, PROTO_HTTP);
221 perror("cs_create"); /* use perror() here since we have no stack yet */
225 server_address_ip = cs_straddr(stack, server_address_str);
226 if (!server_address_ip)
228 fprintf(stderr, "cs_straddr: address could not be resolved\n");
232 status = cs_connect(stack, server_address_ip);
234 fprintf(stderr, "cs_connect: %s\n", cs_strerror(stack));
238 status = cs_put(stack, protocol_package, protocol_package_length);
240 fprintf(stderr, "cs_put: %s\n", cs_strerror(stack));
244 /* Now get a response */
246 length_incoming = cs_get(stack, &buf, &size);
247 if (!length_incoming) {
248 fprintf(stderr, "Connection closed\n");
250 } else if (length_incoming < 0) {
251 fprintf(stderr, "cs_get: %s\n", cs_strerror(stack));
256 fwrite(buf, length_incoming, 1, stdout);
266 int main (int argc, char **argv)
268 YAZ_CHECK_INIT(argc, argv);
271 comstack_example(argv[1]);
280 * c-file-style: "Stroustrup"
281 * indent-tabs-mode: nil
283 * vim: shiftwidth=4 tabstop=8 expandtab