Home | History | Annotate | Download | only in testcurl
      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2009 Christian Grothoff
      4 
      5      libmicrohttpd is free software; you can redistribute it and/or modify
      6      it under the terms of the GNU General Public License as published
      7      by the Free Software Foundation; either version 2, or (at your
      8      option) any later version.
      9 
     10      libmicrohttpd is distributed in the hope that it will be useful, but
     11      WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      General Public License for more details.
     14 
     15      You should have received a copy of the GNU General Public License
     16      along with libmicrohttpd; see the file COPYING.  If not, write to the
     17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     18      Boston, MA 02111-1307, USA.
     19 */
     20 
     21 /**
     22  * @file daemontest_termination.c
     23  * @brief  Testcase for libmicrohttpd tolerating client not closing immediately
     24  * @author hollosig
     25  */
     26 #define PORT	12345
     27 
     28 #include "platform.h"
     29 #include <stdio.h>
     30 #include <string.h>
     31 #include <stdint.h>
     32 #include <stdarg.h>
     33 #include <stdlib.h>
     34 #include <sys/types.h>
     35 #include <microhttpd.h>
     36 #include <unistd.h>
     37 #include <curl/curl.h>
     38 
     39 #ifndef __MINGW32__
     40 #include <sys/select.h>
     41 #include <sys/socket.h>
     42 #endif
     43 
     44 #ifdef _WIN32
     45 #ifndef WIN32_LEAN_AND_MEAN
     46 #define WIN32_LEAN_AND_MEAN 1
     47 #endif /* !WIN32_LEAN_AND_MEAN */
     48 #include <windows.h>
     49 #endif
     50 
     51 static int
     52 connection_handler (void *cls,
     53                     struct MHD_Connection *connection,
     54                     const char *url,
     55                     const char *method,
     56                     const char *version,
     57                     const char *upload_data, size_t * upload_data_size,
     58                     void **ptr)
     59 {
     60   static int i;
     61 
     62   if (*ptr == NULL)
     63     {
     64       *ptr = &i;
     65       return MHD_YES;
     66     }
     67 
     68   if (*upload_data_size != 0)
     69     {
     70       (*upload_data_size) = 0;
     71       return MHD_YES;
     72     }
     73 
     74   struct MHD_Response *response =
     75     MHD_create_response_from_buffer (strlen ("Response"), "Response",
     76 				     MHD_RESPMEM_PERSISTENT);
     77   int ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
     78   MHD_destroy_response (response);
     79 
     80   return ret;
     81 }
     82 
     83 static size_t
     84 write_data (void *ptr, size_t size, size_t nmemb, void *stream)
     85 {
     86   return size * nmemb;
     87 }
     88 
     89 int
     90 main ()
     91 {
     92   struct MHD_Daemon *daemon;
     93 
     94   daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
     95                              PORT,
     96                              NULL,
     97                              NULL, connection_handler, NULL, MHD_OPTION_END);
     98 
     99   if (daemon == NULL)
    100     {
    101       fprintf (stderr, "Daemon cannot be started!");
    102       exit (1);
    103     }
    104 
    105   CURL *curl = curl_easy_init ();
    106   //curl_easy_setopt(curl, CURLOPT_POST, 1L);
    107   char url[255];
    108   sprintf (url, "http://127.0.0.1:%d", PORT);
    109   curl_easy_setopt (curl, CURLOPT_URL, url);
    110   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data);
    111 
    112   CURLcode success = curl_easy_perform (curl);
    113   if (success != 0)
    114     {
    115       fprintf (stderr, "CURL Error");
    116       exit (1);
    117     }
    118   /* CPU used to go crazy here */
    119   sleep (1);
    120 
    121   curl_easy_cleanup (curl);
    122   MHD_stop_daemon (daemon);
    123 
    124   return 0;
    125 }
    126