Blame src/testcurl/test_termination.c

Packit 875988
/*
Packit 875988
     This file is part of libmicrohttpd
Packit 875988
     Copyright (C) 2009 Christian Grothoff
Packit 875988
Packit 875988
     libmicrohttpd is free software; you can redistribute it and/or modify
Packit 875988
     it under the terms of the GNU General Public License as published
Packit 875988
     by the Free Software Foundation; either version 2, or (at your
Packit 875988
     option) any later version.
Packit 875988
Packit 875988
     libmicrohttpd is distributed in the hope that it will be useful, but
Packit 875988
     WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 875988
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 875988
     General Public License for more details.
Packit 875988
Packit 875988
     You should have received a copy of the GNU General Public License
Packit 875988
     along with libmicrohttpd; see the file COPYING.  If not, write to the
Packit 875988
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Packit 875988
     Boston, MA 02110-1301, USA.
Packit 875988
*/
Packit 875988
Packit 875988
/**
Packit 875988
 * @file daemontest_termination.c
Packit 875988
 * @brief  Testcase for libmicrohttpd tolerating client not closing immediately
Packit 875988
 * @author hollosig
Packit 875988
 */
Packit 875988
Packit 875988
#include "platform.h"
Packit 875988
#include <stdio.h>
Packit 875988
#include <string.h>
Packit 875988
#include <stdint.h>
Packit 875988
#include <stdarg.h>
Packit 875988
#include <stdlib.h>
Packit 875988
#include <sys/types.h>
Packit 875988
#include <microhttpd.h>
Packit 875988
#include <unistd.h>
Packit 875988
#include <curl/curl.h>
Packit 875988
Packit 875988
#ifndef __MINGW32__
Packit 875988
#include <sys/select.h>
Packit 875988
#include <sys/socket.h>
Packit 875988
#endif
Packit 875988
Packit 875988
#ifdef _WIN32
Packit 875988
#ifndef WIN32_LEAN_AND_MEAN
Packit 875988
#define WIN32_LEAN_AND_MEAN 1
Packit 875988
#endif /* !WIN32_LEAN_AND_MEAN */
Packit 875988
#include <windows.h>
Packit 875988
#endif
Packit 875988
Packit 875988
static int
Packit 875988
connection_handler (void *cls,
Packit 875988
                    struct MHD_Connection *connection,
Packit 875988
                    const char *url,
Packit 875988
                    const char *method,
Packit 875988
                    const char *version,
Packit 875988
                    const char *upload_data, size_t * upload_data_size,
Packit 875988
                    void **ptr)
Packit 875988
{
Packit 875988
  static int i;
Packit 875988
  (void)cls;(void)url;                          /* Unused. Silent compiler warning. */
Packit 875988
  (void)method;(void)version;(void)upload_data; /* Unused. Silent compiler warning. */
Packit 875988
  (void)upload_data_size;                       /* Unused. Silent compiler warning. */
Packit 875988
Packit 875988
  if (*ptr == NULL)
Packit 875988
    {
Packit 875988
      *ptr = &i;
Packit 875988
      return MHD_YES;
Packit 875988
    }
Packit 875988
Packit 875988
  if (*upload_data_size != 0)
Packit 875988
    {
Packit 875988
      (*upload_data_size) = 0;
Packit 875988
      return MHD_YES;
Packit 875988
    }
Packit 875988
Packit 875988
  struct MHD_Response *response =
Packit 875988
    MHD_create_response_from_buffer (strlen ("Response"), "Response",
Packit 875988
				     MHD_RESPMEM_PERSISTENT);
Packit 875988
  int ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
Packit 875988
  MHD_destroy_response (response);
Packit 875988
Packit 875988
  return ret;
Packit 875988
}
Packit 875988
Packit 875988
static size_t
Packit 875988
write_data (void *ptr, size_t size, size_t nmemb, void *stream)
Packit 875988
{
Packit 875988
  (void)ptr;(void)stream;       /* Unused. Silent compiler warning. */
Packit 875988
  return size * nmemb;
Packit 875988
}
Packit 875988
Packit 875988
int
Packit 875988
main (void)
Packit 875988
{
Packit 875988
  struct MHD_Daemon *daemon;
Packit 875988
  int port;
Packit 875988
Packit 875988
  if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
Packit 875988
    port = 0;
Packit 875988
  else
Packit 875988
    port = 1490;
Packit 875988
Packit 875988
Packit 875988
  daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
Packit 875988
                             port,
Packit 875988
                             NULL,
Packit 875988
                             NULL, connection_handler, NULL, MHD_OPTION_END);
Packit 875988
Packit 875988
  if (daemon == NULL)
Packit 875988
    {
Packit 875988
      fprintf (stderr, "Daemon cannot be started!");
Packit 875988
      exit (1);
Packit 875988
    }
Packit 875988
  if (0 == port)
Packit 875988
    {
Packit 875988
      const union MHD_DaemonInfo *dinfo;
Packit 875988
      dinfo = MHD_get_daemon_info (daemon, MHD_DAEMON_INFO_BIND_PORT);
Packit 875988
      if (NULL == dinfo || 0 == dinfo->port)
Packit 875988
        { MHD_stop_daemon (daemon); return 32; }
Packit 875988
      port = (int)dinfo->port;
Packit 875988
    }
Packit 875988
Packit 875988
  CURL *curl = curl_easy_init ();
Packit 875988
  /* curl_easy_setopt(curl, CURLOPT_POST, 1L); */
Packit 875988
  char url[255];
Packit 875988
  sprintf (url, "http://127.0.0.1:%d", port);
Packit 875988
  curl_easy_setopt (curl, CURLOPT_URL, url);
Packit 875988
  curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data);
Packit 875988
Packit 875988
  CURLcode success = curl_easy_perform (curl);
Packit 875988
  if (success != 0)
Packit 875988
    {
Packit 875988
      fprintf (stderr, "CURL Error");
Packit 875988
      exit (1);
Packit 875988
    }
Packit 875988
  /* CPU used to go crazy here */
Packit 875988
  (void)sleep (1);
Packit 875988
Packit 875988
  curl_easy_cleanup (curl);
Packit 875988
  MHD_stop_daemon (daemon);
Packit 875988
Packit 875988
  return 0;
Packit 875988
}