aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/test_get_response_cleanup.c
blob: 39d7ece671f1e6eb457a634e37a003d20c87b75c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* DO NOT CHANGE THIS LINE */
/*
     This file is part of libmicrohttpd
     (C) 2007, 2009 Christian Grothoff

     libmicrohttpd is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     libmicrohttpd is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libmicrohttpd; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file daemontest_get_response_cleanup.c
 * @brief  Testcase for libmicrohttpd response cleanup
 * @author Christian Grothoff
 */

#include "MHD_config.h"
#include "platform.h"
#include <microhttpd.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

#ifndef WINDOWS
#include <sys/socket.h>
#include <unistd.h>
#endif

#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif /* !WIN32_LEAN_AND_MEAN */
#include <windows.h>
#endif

#define TESTSTR "/* DO NOT CHANGE THIS LINE */"

static int oneone;

static int ok;


static pid_t
fork_curl (const char *url)
{
  pid_t ret;

  ret = fork();
  if (ret != 0)
    return ret;
  execlp ("curl", "curl", "-s", "-N", "-o", "/dev/null", "-GET", url, NULL);
  fprintf (stderr, 
	   "Failed to exec curl: %s\n",
	   strerror (errno));
  _exit (-1);  
}

static void
kill_curl (pid_t pid)
{
  int status;

  //fprintf (stderr, "Killing curl\n");
  kill (pid, SIGTERM);
  waitpid (pid, &status, 0);
}


static ssize_t
push_callback (void *cls, uint64_t pos, char *buf, size_t max)
{
  if (max == 0)
    return 0;
  buf[0] = 'd';
  return 1;
}

static void
push_free_callback (void *cls)
{
  int *ok = cls;

  //fprintf (stderr, "Cleanup callback called!\n");
  *ok = 0;
}


static int
ahc_echo (void *cls,
          struct MHD_Connection *connection,
          const char *url,
          const char *method,
          const char *version,
          const char *upload_data, size_t *upload_data_size,
          void **unused)
{
  static int ptr;
  const char *me = cls;
  struct MHD_Response *response;
  int ret;

  //fprintf (stderr, "In CB: %s!\n", method);
  if (0 != strcmp (me, method))
    return MHD_NO;              /* unexpected method */
  if (&ptr != *unused)
    {
      *unused = &ptr;
      return MHD_YES;
    }
  *unused = NULL;
  response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 
						32 * 1024,
						&push_callback,
						&ok,
						&push_free_callback);
  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  MHD_destroy_response (response);
  if (ret == MHD_NO)
    abort ();
  return ret;
}


static int
testInternalGet ()
{
  struct MHD_Daemon *d;
  pid_t curl;

  ok = 1;
  d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
                        11080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
  if (d == NULL)
    return 1;
  curl = fork_curl ("http://127.0.0.1:11080/");
  sleep (1);
  kill_curl (curl);
  sleep (1);
  // fprintf (stderr, "Stopping daemon!\n");
  MHD_stop_daemon (d);
  if (ok != 0)
    return 2;
  return 0;
}

static int
testMultithreadedGet ()
{
  struct MHD_Daemon *d;
  pid_t curl;

  d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
                        1081, NULL, NULL, &ahc_echo, "GET",
			MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 2,
			MHD_OPTION_END);
  if (d == NULL)
    return 16;
  ok = 1;
  //fprintf (stderr, "Forking cURL!\n");
  curl = fork_curl ("http://127.0.0.1:1081/");
  sleep (1);
  kill_curl (curl);
  sleep (1);
  curl = fork_curl ("http://127.0.0.1:1081/");
  sleep (1);
  if (ok != 0)
    {
      kill_curl (curl);
      MHD_stop_daemon (d);
      return 64;
    }
  kill_curl (curl);
  sleep (1);
  //fprintf (stderr, "Stopping daemon!\n");
  MHD_stop_daemon (d);
  if (ok != 0)
    return 32;

  return 0;
}

static int
testMultithreadedPoolGet ()
{
  struct MHD_Daemon *d;
  pid_t curl;

  d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
                        1081, NULL, NULL, &ahc_echo, "GET",
                        MHD_OPTION_THREAD_POOL_SIZE, 4, MHD_OPTION_END);
  if (d == NULL)
    return 64;
  ok = 1;
  curl = fork_curl ("http://127.0.0.1:1081/");
  sleep (1);
  kill_curl (curl);
  sleep (1);
  //fprintf (stderr, "Stopping daemon!\n");
  MHD_stop_daemon (d);
  if (ok != 0)
    return 128;
  return 0;
}

static int
testExternalGet ()
{
  struct MHD_Daemon *d;
  fd_set rs;
  fd_set ws;
  fd_set es;
  MHD_socket max;
  time_t start;
  struct timeval tv;
  pid_t curl;

  d = MHD_start_daemon (MHD_USE_DEBUG,
                        1082, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
  if (d == NULL)
    return 256;
  curl = fork_curl ("http://127.0.0.1:1082/");
  
  start = time (NULL);
  while ((time (NULL) - start < 2))
    {
      max = 0;
      FD_ZERO (&rs);
      FD_ZERO (&ws);
      FD_ZERO (&es);
      if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
        {
          MHD_stop_daemon (d);
          return 4096;
        }
      tv.tv_sec = 0;
      tv.tv_usec = 1000;
      select (max + 1, &rs, &ws, &es, &tv);
      MHD_run (d);
    }
  kill_curl (curl);
  start = time (NULL);
  while ((time (NULL) - start < 2))
    {
      max = 0;
      FD_ZERO (&rs);
      FD_ZERO (&ws);
      FD_ZERO (&es);
      if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
        {
          MHD_stop_daemon (d);
          return 4096;
        }
      tv.tv_sec = 0;
      tv.tv_usec = 1000;
      select (max + 1, &rs, &ws, &es, &tv);
      MHD_run (d);
    }
  // fprintf (stderr, "Stopping daemon!\n");
  MHD_stop_daemon (d);
  if (ok != 0)
    return 1024;
  return 0;
}


int
main (int argc, char *const *argv)
{
  unsigned int errorCount = 0;

  oneone = NULL != strstr (argv[0], "11");
  errorCount += testInternalGet ();
  errorCount += testMultithreadedGet ();
  errorCount += testMultithreadedPoolGet ();
  errorCount += testExternalGet ();
  if (errorCount != 0)
    fprintf (stderr, "Error (code: %u)\n", errorCount);
  return errorCount != 0;       /* 0 == pass */
}