aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/test_quiesce_stream.c
blob: dbfd982be30dfa0b8d739d173ac9d9ce808d3728 (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
/*
     This file is part of libmicrohttpd
     Copyright (C) 2016 Christian Grothoff
     Copyright (C) 2016-2021 Evgeny Grin (Karlson2k)

     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., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/
/**
 * @file test_quiesce_stream.c
 * @brief  Testcase for libmicrohttpd quiescing
 * @author Markus Doppelbauer
 * @author Christian Grothoff
 * @author Karlson2k (Evgeny Grin)
 */
#include "mhd_options.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <microhttpd.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#elif defined(_WIN32)
#include <windows.h>
#define sleep(s) (Sleep ((s) * 1000), 0)
#endif /* _WIN32 */


static volatile unsigned int request_counter;


static void
http_PanicCallback (void *cls,
                    const char *file,
                    unsigned int line,
                    const char *reason)
{
  (void) cls;    /* Unused. Silent compiler warning. */
  fprintf (stderr,
           "PANIC: exit process: %s at %s:%u\n",
           reason,
           file,
           line);
  exit (EXIT_FAILURE);
}


static void *
resume_connection (void *arg)
{
  struct MHD_Connection *connection = arg;

  /* fprintf (stderr, "Calling resume\n"); */
  MHD_resume_connection (connection);
  return NULL;
}


static void
suspend_connection (struct MHD_Connection *connection)
{
  pthread_t thread_id;
  int status;

  /* fprintf (stderr, "Calling suspend\n"); */
  MHD_suspend_connection (connection);
  status = pthread_create (&thread_id,
                           NULL,
                           &resume_connection,
                           connection);
  if (0 != status)
  {
    fprintf (stderr,
             "Could not create thead\n");
    exit (EXIT_FAILURE);
  }
  pthread_detach (thread_id);
}


struct ContentReaderUserdata
{
  int bytes_written;
  struct MHD_Connection *connection;
};


static ssize_t
http_ContentReaderCallback (void *cls,
                            uint64_t pos,
                            char *buf,
                            size_t max)
{
  static const char alphabet[] =
    "\nABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  struct ContentReaderUserdata *userdata = cls;
  (void) pos; (void) max;  /* Unused. Silent compiler warning. */

  if (userdata->bytes_written >= 1024)
  {
    fprintf (stderr,
             "finish: %d\n",
             request_counter);
    return MHD_CONTENT_READER_END_OF_STREAM;
  }
  userdata->bytes_written++;
  buf[0] = alphabet[userdata->bytes_written % (sizeof(alphabet) - 1)];
  suspend_connection (userdata->connection);

  return 1;
}


static void
free_crc_data (void *crc_data)
{
  struct ContentReaderUserdata *userdata = crc_data;

  free (userdata);
}


static enum MHD_Result
http_AccessHandlerCallback (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 **req_cls)
{
  enum MHD_Result ret;
  struct MHD_Response *response;
  (void) cls; (void) url;                        /* Unused. Silent compiler warning. */
  (void) method; (void) version; (void) upload_data; /* Unused. Silent compiler warning. */
  (void) upload_data_size;                       /* Unused. Silent compiler warning. */

  /* Never respond on first call */
  if (NULL == *req_cls)
  {
    struct ContentReaderUserdata *userdata;
    fprintf (stderr,
             "start: %d\n",
             ++request_counter);

    userdata = malloc (sizeof(struct ContentReaderUserdata));

    if (NULL == userdata)
      return MHD_NO;
    userdata->bytes_written = 0;
    userdata->connection = connection;
    *req_cls = userdata;
    return MHD_YES;
  }

  /* Second call: create response */
  response
    = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
                                         32 * 1024,
                                         &http_ContentReaderCallback,
                                         *req_cls,
                                         &free_crc_data);
  ret = MHD_queue_response (connection,
                            MHD_HTTP_OK,
                            response);
  MHD_destroy_response (response);

  suspend_connection (connection);
  return ret;
}


int
main (void)
{
  int port;
  char command_line[1024];
  /* Flags */
  unsigned int daemon_flags
    = MHD_USE_INTERNAL_POLLING_THREAD
      | MHD_USE_AUTO
      | MHD_ALLOW_SUSPEND_RESUME
      | MHD_USE_ITC;
  struct MHD_Daemon *daemon;

  if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    port = 0;
  else
    port = 1470;

  /* Panic callback */
  MHD_set_panic_func (&http_PanicCallback,
                      NULL);


  /* Create daemon */
  daemon = MHD_start_daemon (daemon_flags,
                             port,
                             NULL,
                             NULL,
                             &http_AccessHandlerCallback,
                             NULL,
                             MHD_OPTION_END);
  if (NULL == daemon)
    return 1;
  if (0 == port)
  {
    const union MHD_DaemonInfo *dinfo;
    dinfo = MHD_get_daemon_info (daemon, MHD_DAEMON_INFO_BIND_PORT);
    if ((NULL == dinfo) || (0 == dinfo->port) )
    {
      MHD_stop_daemon (daemon); return 32;
    }
    port = (int) dinfo->port;
  }
  snprintf (command_line,
            sizeof (command_line),
            "curl -s http://127.0.0.1:%d",
            port);

  if (0 != system (command_line))
  {
    MHD_stop_daemon (daemon);
    return 1;
  }
  /* wait for a request */
  while (0 == request_counter)
    (void) sleep (1);

  fprintf (stderr,
           "quiesce\n");
  MHD_quiesce_daemon (daemon);

  /* wait a second */
  (void) sleep (1);

  fprintf (stderr,
           "stopping daemon\n");
  MHD_stop_daemon (daemon);

  return 0;
}