From 0eb5bfba3ca44211aeaac0663ec7477d7c47fa8b Mon Sep 17 00:00:00 2001 From: Christian Mauderer Date: Tue, 3 Jul 2012 10:42:17 +0200 Subject: libtests/mghttpd01: New test --- testsuites/libtests/mghttpd01/test-http-client.c | 95 ++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 testsuites/libtests/mghttpd01/test-http-client.c (limited to 'testsuites/libtests/mghttpd01/test-http-client.c') diff --git a/testsuites/libtests/mghttpd01/test-http-client.c b/testsuites/libtests/mghttpd01/test-http-client.c new file mode 100644 index 0000000000..6e78457468 --- /dev/null +++ b/testsuites/libtests/mghttpd01/test-http-client.c @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2012 embedded brains GmbH. All rights reserved. + * + * embedded brains GmbH + * Obere Lagerstr. 30 + * 82178 Puchheim + * Germany + * + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + */ + +#include "test-http-client.h" + +#include +#include +#include +#include +#include +#include + +void httpc_init_context( + httpc_context *ctx +) +{ + ctx->socket = -1; + ctx->fd = NULL; +} + +bool httpc_open_connection( + httpc_context *ctx, + char *targethost, + int targetport +) +{ + struct sockaddr_in addr; + + struct hostent *server; + struct servent *service; + + ctx->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if(ctx->socket < 0) { return false; } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(targetport); + + server = gethostbyname(targethost); + if(server == NULL) { return false; } + memcpy(&addr.sin_addr.s_addr, server->h_addr, (size_t) server->h_length); + + if(connect(ctx->socket, (struct sockaddr *)&addr, sizeof(addr)) != 0) + { + return false; + } + + ctx->fd = fdopen(ctx->socket,"rw"); + if(ctx->fd == NULL) { return false; } + + return true; +} + +bool httpc_close_connection( + httpc_context *ctx +) +{ + if(close(ctx->socket) != 0) + { + return false; + } + + return true; +} + +bool httpc_send_request( + httpc_context *ctx, + char *request, + char *response, + int responsesize +) +{ + int size = strlen(request); + char lineend[] = " HTTP/1.1\r\n\r\n"; + + write(ctx->socket, request, size); + write(ctx->socket, lineend, sizeof(lineend)); + + size = read(ctx->socket, response, responsesize-1); + response[size] = '\0'; + + return true; +} + -- cgit v1.2.3