X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=test%2Ftst_comstack.c;h=eee72f8fecce9d63500a26339eb7885934a94b3a;hp=4c2ed58d67718101fdde1968725c3c7354c0a141;hb=ed31c923f03ec124060972f5351b8b33e07a2e13;hpb=377e50914cd78d52e77032a3eaf8972f23b4e7b9 diff --git a/test/tst_comstack.c b/test/tst_comstack.c index 4c2ed58..eee72f8 100644 --- a/test/tst_comstack.c +++ b/test/tst_comstack.c @@ -1,15 +1,15 @@ -/* - * Copyright (C) 1995-2007, Index Data ApS +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 Index Data * See the file LICENSE for details. - * - * $Id: tst_comstack.c,v 1.2 2007-10-09 06:00:56 adam Exp $ */ #include +#include #include #include #include +#include static void tst_http_request(void) { @@ -187,11 +187,71 @@ static void tst_http_response(void) } } +/** \brief COMSTACK synopsis from manual, doc/comstack.xml */ +static int comstack_example(const char *server_address_str) +{ + COMSTACK stack; + char *buf = 0; + int size = 0, length_incoming; + void *server_address_ip; + int status; + + char *protocol_package = "GET / HTTP/1.0\r\n\r\n"; + int protocol_package_length = strlen(protocol_package); + + stack = cs_create(tcpip_type, 1, PROTO_HTTP); + if (!stack) { + perror("cs_create"); /* use perror() here since we have no stack yet */ + return -1; + } + + server_address_ip = cs_straddr(stack, server_address_str); + if (!server_address_ip) + { + fprintf(stderr, "cs_straddr: address could not be resolved\n"); + return -1; + } + + status = cs_connect(stack, server_address_ip); + if (status != 0) { + fprintf(stderr, "cs_connect: %s\n", cs_strerror(stack)); + return -1; + } + + status = cs_put(stack, protocol_package, protocol_package_length); + if (status) { + fprintf(stderr, "cs_put: %s\n", cs_strerror(stack)); + return -1; + } + + /* Now get a response */ + + length_incoming = cs_get(stack, &buf, &size); + if (!length_incoming) { + fprintf(stderr, "Connection closed\n"); + return -1; + } else if (length_incoming < 0) { + fprintf(stderr, "cs_get: %s\n", cs_strerror(stack)); + return -1; + } + + /* Print result */ + fwrite(buf, length_incoming, 1, stdout); + + /* clean up */ + cs_close(stack); + if (buf) + free(buf); + return 0; +} + int main (int argc, char **argv) { YAZ_CHECK_INIT(argc, argv); YAZ_CHECK_LOG(); + if (argc == 2) + comstack_example(argv[1]); tst_http_request(); tst_http_response(); YAZ_CHECK_TERM; @@ -200,6 +260,7 @@ int main (int argc, char **argv) /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab