source: rtems/testsuites/libtests/mghttpd01/test-http-client.c @ 3acc619

4.115
Last change on this file since 3acc619 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 1.8 KB
Line 
1/*
2 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Obere Lagerstr. 30
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15
16#include <sys/types.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <netinet/tcp.h>
20#include <netdb.h>
21#include <unistd.h>
22#include <string.h>
23
24#include "test-http-client.h"
25
26void httpc_init_context(
27  httpc_context *ctx
28)
29{
30  ctx->socket = -1;
31  ctx->fd = NULL;
32}
33
34bool httpc_open_connection(
35  httpc_context *ctx,
36  char *targethost,
37  int targetport
38)
39{
40  struct sockaddr_in addr;
41
42  struct hostent *server;
43
44  ctx->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
45  if(ctx->socket < 0) { return false; }
46
47  memset(&addr, 0, sizeof(addr));
48  addr.sin_family = AF_INET;
49  addr.sin_port = htons(targetport);
50
51  server = gethostbyname(targethost);
52  if(server == NULL) { return false; }
53  memcpy(&addr.sin_addr.s_addr, server->h_addr, (size_t) server->h_length);
54
55  if(connect(ctx->socket, (struct sockaddr *)&addr, sizeof(addr)) != 0)
56  {
57    return false;
58  }
59
60  ctx->fd = fdopen(ctx->socket,"rw");
61  if(ctx->fd == NULL) { return false; }
62
63  return true;
64}
65
66bool httpc_close_connection(
67  httpc_context *ctx
68)
69{
70  if(close(ctx->socket) != 0)
71  {
72    return false;
73  }
74
75  return true;
76}
77
78bool httpc_send_request(
79  httpc_context *ctx,
80  char *request,
81  char *response,
82  int responsesize
83)
84{
85  int size = strlen(request);
86  char lineend[] = " HTTP/1.1\r\n\r\n";
87
88  write(ctx->socket, request, size);
89  write(ctx->socket, lineend, sizeof(lineend));
90
91  size = read(ctx->socket, response, responsesize-1);
92  response[size] = '\0';
93
94  return true;
95}
96
Note: See TracBrowser for help on using the repository browser.