Blame doc/examples/largepost.c

Packit 875988
/* Feel free to use this example code in any way
Packit 875988
   you see fit (Public Domain) */
Packit 875988
Packit 875988
#include <sys/types.h>
Packit 875988
#ifndef _WIN32
Packit 875988
#include <sys/select.h>
Packit 875988
#include <sys/socket.h>
Packit 875988
#else
Packit 875988
#include <winsock2.h>
Packit 875988
#endif
Packit 875988
#include <stdio.h>
Packit 875988
#include <stdlib.h>
Packit 875988
#include <string.h>
Packit 875988
#include <microhttpd.h>
Packit 875988
Packit 875988
#ifdef _MSC_VER
Packit 875988
#ifndef strcasecmp
Packit 875988
#define strcasecmp(a,b) _stricmp((a),(b))
Packit 875988
#endif /* !strcasecmp */
Packit 875988
#endif /* _MSC_VER */
Packit 875988
Packit 875988
#if defined(_MSC_VER) && _MSC_VER+0 <= 1800
Packit 875988
/* Substitution is OK while return value is not used */
Packit 875988
#define snprintf _snprintf
Packit 875988
#endif
Packit 875988
Packit 875988
#define PORT            8888
Packit 875988
#define POSTBUFFERSIZE  512
Packit 875988
#define MAXCLIENTS      2
Packit 875988
Packit 875988
enum ConnectionType
Packit 875988
  {
Packit 875988
    GET = 0,
Packit 875988
    POST = 1
Packit 875988
  };
Packit 875988
Packit 875988
static unsigned int nr_of_uploading_clients = 0;
Packit 875988
Packit 875988
Packit 875988
/**
Packit 875988
 * Information we keep per connection.
Packit 875988
 */
Packit 875988
struct connection_info_struct
Packit 875988
{
Packit 875988
  enum ConnectionType connectiontype;
Packit 875988
Packit 875988
  /**
Packit 875988
   * Handle to the POST processing state.
Packit 875988
   */
Packit 875988
  struct MHD_PostProcessor *postprocessor;
Packit 875988
Packit 875988
  /**
Packit 875988
   * File handle where we write uploaded data.
Packit 875988
   */
Packit 875988
  FILE *fp;
Packit 875988
Packit 875988
  /**
Packit 875988
   * HTTP response body we will return, NULL if not yet known.
Packit 875988
   */
Packit 875988
  const char *answerstring;
Packit 875988
Packit 875988
  /**
Packit 875988
   * HTTP status code we will return, 0 for undecided.
Packit 875988
   */
Packit 875988
  unsigned int answercode;
Packit 875988
};
Packit 875988
Packit 875988
Packit 875988
const char *askpage = "<html><body>\n\
Packit 875988
                       Upload a file, please!
\n\
Packit 875988
                       There are %u clients uploading at the moment.
\n\
Packit 875988
                       <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\
Packit 875988
                       <input name=\"file\" type=\"file\">\n\
Packit 875988
                       <input type=\"submit\" value=\" Send \"></form>\n\
Packit 875988
                       </body></html>";
Packit 875988
const char *busypage =
Packit 875988
  "<html><body>This server is busy, please try again later.</body></html>";
Packit 875988
const char *completepage =
Packit 875988
  "<html><body>The upload has been completed.</body></html>";
Packit 875988
const char *errorpage =
Packit 875988
  "<html><body>This doesn't seem to be right.</body></html>";
Packit 875988
const char *servererrorpage =
Packit 875988
  "<html><body>Invalid request.</body></html>";
Packit 875988
const char *fileexistspage =
Packit 875988
  "<html><body>This file already exists.</body></html>";
Packit 875988
const char *fileioerror =
Packit 875988
  "<html><body>IO error writing to disk.</body></html>";
Packit 875988
const char* const postprocerror =
Packit 875988
  "<html><head><title>Error</title></head><body>Error processing POST data</body></html>";
Packit 875988
Packit 875988
Packit 875988
static int
Packit 875988
send_page (struct MHD_Connection *connection,
Packit 875988
           const char *page,
Packit 875988
           int status_code)
Packit 875988
{
Packit 875988
  int ret;
Packit 875988
  struct MHD_Response *response;
Packit 875988
Packit 875988
  response =
Packit 875988
    MHD_create_response_from_buffer (strlen (page),
Packit 875988
                                     (void *) page,
Packit 875988
				     MHD_RESPMEM_MUST_COPY);
Packit 875988
  if (!response)
Packit 875988
    return MHD_NO;
Packit 875988
  MHD_add_response_header (response,
Packit 875988
                           MHD_HTTP_HEADER_CONTENT_TYPE,
Packit 875988
                           "text/html");
Packit 875988
  ret = MHD_queue_response (connection,
Packit 875988
                            status_code,
Packit 875988
                            response);
Packit 875988
  MHD_destroy_response (response);
Packit 875988
Packit 875988
  return ret;
Packit 875988
}
Packit 875988
Packit 875988
Packit 875988
static int
Packit 875988
iterate_post (void *coninfo_cls,
Packit 875988
              enum MHD_ValueKind kind,
Packit 875988
              const char *key,
Packit 875988
              const char *filename,
Packit 875988
              const char *content_type,
Packit 875988
              const char *transfer_encoding,
Packit 875988
              const char *data,
Packit 875988
              uint64_t off,
Packit 875988
              size_t size)
Packit 875988
{
Packit 875988
  struct connection_info_struct *con_info = coninfo_cls;
Packit 875988
  FILE *fp;
Packit 875988
  (void)kind;               /* Unused. Silent compiler warning. */
Packit 875988
  (void)content_type;       /* Unused. Silent compiler warning. */
Packit 875988
  (void)transfer_encoding;  /* Unused. Silent compiler warning. */
Packit 875988
  (void)off;                /* Unused. Silent compiler warning. */
Packit 875988
Packit 875988
  if (0 != strcmp (key, "file"))
Packit 875988
    {
Packit 875988
      con_info->answerstring = servererrorpage;
Packit 875988
      con_info->answercode = MHD_HTTP_BAD_REQUEST;
Packit 875988
      return MHD_YES;
Packit 875988
    }
Packit 875988
Packit 875988
  if (! con_info->fp)
Packit 875988
    {
Packit 875988
      if (0 != con_info->answercode) /* something went wrong */
Packit 875988
        return MHD_YES;
Packit 875988
      if (NULL != (fp = fopen (filename, "rb")))
Packit 875988
        {
Packit 875988
          fclose (fp);
Packit 875988
          con_info->answerstring = fileexistspage;
Packit 875988
          con_info->answercode = MHD_HTTP_FORBIDDEN;
Packit 875988
          return MHD_YES;
Packit 875988
        }
Packit 875988
      /* NOTE: This is technically a race with the 'fopen()' above,
Packit 875988
         but there is no easy fix, short of moving to open(O_EXCL)
Packit 875988
         instead of using fopen(). For the example, we do not care. */
Packit 875988
      con_info->fp = fopen (filename, "ab");
Packit 875988
      if (!con_info->fp)
Packit 875988
        {
Packit 875988
          con_info->answerstring = fileioerror;
Packit 875988
          con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
Packit 875988
          return MHD_YES;
Packit 875988
        }
Packit 875988
    }
Packit 875988
Packit 875988
  if (size > 0)
Packit 875988
    {
Packit 875988
      if (! fwrite (data, sizeof (char), size, con_info->fp))
Packit 875988
        {
Packit 875988
          con_info->answerstring = fileioerror;
Packit 875988
          con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
Packit 875988
          return MHD_YES;
Packit 875988
        }
Packit 875988
    }
Packit 875988
Packit 875988
  return MHD_YES;
Packit 875988
}
Packit 875988
Packit 875988
Packit 875988
static void
Packit 875988
request_completed (void *cls,
Packit 875988
                   struct MHD_Connection *connection,
Packit 875988
                   void **con_cls,
Packit 875988
                   enum MHD_RequestTerminationCode toe)
Packit 875988
{
Packit 875988
  struct connection_info_struct *con_info = *con_cls;
Packit 875988
  (void)cls;         /* Unused. Silent compiler warning. */
Packit 875988
  (void)connection;  /* Unused. Silent compiler warning. */
Packit 875988
  (void)toe;         /* Unused. Silent compiler warning. */
Packit 875988
Packit 875988
  if (NULL == con_info)
Packit 875988
    return;
Packit 875988
Packit 875988
  if (con_info->connectiontype == POST)
Packit 875988
    {
Packit 875988
      if (NULL != con_info->postprocessor)
Packit 875988
        {
Packit 875988
          MHD_destroy_post_processor (con_info->postprocessor);
Packit 875988
          nr_of_uploading_clients--;
Packit 875988
        }
Packit 875988
Packit 875988
      if (con_info->fp)
Packit 875988
        fclose (con_info->fp);
Packit 875988
    }
Packit 875988
Packit 875988
  free (con_info);
Packit 875988
  *con_cls = NULL;
Packit 875988
}
Packit 875988
Packit 875988
Packit 875988
static int
Packit 875988
answer_to_connection (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,
Packit 875988
                      size_t *upload_data_size,
Packit 875988
                      void **con_cls)
Packit 875988
{
Packit 875988
  (void)cls;               /* Unused. Silent compiler warning. */
Packit 875988
  (void)url;               /* Unused. Silent compiler warning. */
Packit 875988
  (void)version;           /* Unused. Silent compiler warning. */
Packit 875988
Packit 875988
  if (NULL == *con_cls)
Packit 875988
    {
Packit 875988
      /* First call, setup data structures */
Packit 875988
      struct connection_info_struct *con_info;
Packit 875988
Packit 875988
      if (nr_of_uploading_clients >= MAXCLIENTS)
Packit 875988
        return send_page (connection,
Packit 875988
                          busypage,
Packit 875988
                          MHD_HTTP_SERVICE_UNAVAILABLE);
Packit 875988
Packit 875988
      con_info = malloc (sizeof (struct connection_info_struct));
Packit 875988
      if (NULL == con_info)
Packit 875988
        return MHD_NO;
Packit 875988
      con_info->answercode = 0; /* none yet */
Packit 875988
      con_info->fp = NULL;
Packit 875988
Packit 875988
      if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST))
Packit 875988
        {
Packit 875988
          con_info->postprocessor =
Packit 875988
            MHD_create_post_processor (connection,
Packit 875988
                                       POSTBUFFERSIZE,
Packit 875988
                                       &iterate_post,
Packit 875988
                                       (void *) con_info);
Packit 875988
Packit 875988
          if (NULL == con_info->postprocessor)
Packit 875988
            {
Packit 875988
              free (con_info);
Packit 875988
              return MHD_NO;
Packit 875988
            }
Packit 875988
Packit 875988
          nr_of_uploading_clients++;
Packit 875988
Packit 875988
          con_info->connectiontype = POST;
Packit 875988
        }
Packit 875988
      else
Packit 875988
        {
Packit 875988
          con_info->connectiontype = GET;
Packit 875988
        }
Packit 875988
Packit 875988
      *con_cls = (void *) con_info;
Packit 875988
Packit 875988
      return MHD_YES;
Packit 875988
    }
Packit 875988
Packit 875988
  if (0 == strcasecmp (method, MHD_HTTP_METHOD_GET))
Packit 875988
    {
Packit 875988
      /* We just return the standard form for uploads on all GET requests */
Packit 875988
      char buffer[1024];
Packit 875988
Packit 875988
      snprintf (buffer,
Packit 875988
                sizeof (buffer),
Packit 875988
                askpage,
Packit 875988
                nr_of_uploading_clients);
Packit 875988
      return send_page (connection,
Packit 875988
                        buffer,
Packit 875988
                        MHD_HTTP_OK);
Packit 875988
    }
Packit 875988
Packit 875988
  if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST))
Packit 875988
    {
Packit 875988
      struct connection_info_struct *con_info = *con_cls;
Packit 875988
Packit 875988
      if (0 != *upload_data_size)
Packit 875988
        {
Packit 875988
          /* Upload not yet done */
Packit 875988
          if (0 != con_info->answercode)
Packit 875988
            {
Packit 875988
              /* we already know the answer, skip rest of upload */
Packit 875988
              *upload_data_size = 0;
Packit 875988
              return MHD_YES;
Packit 875988
            }
Packit 875988
          if (MHD_YES !=
Packit 875988
              MHD_post_process (con_info->postprocessor,
Packit 875988
                                upload_data,
Packit 875988
                                *upload_data_size))
Packit 875988
            {
Packit 875988
              con_info->answerstring = postprocerror;
Packit 875988
              con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
Packit 875988
            }
Packit 875988
          *upload_data_size = 0;
Packit 875988
Packit 875988
          return MHD_YES;
Packit 875988
        }
Packit 875988
      /* Upload finished */
Packit 875988
      if (NULL != con_info->fp)
Packit 875988
        {
Packit 875988
          fclose (con_info->fp);
Packit 875988
          con_info->fp = NULL;
Packit 875988
        }
Packit 875988
      if (0 == con_info->answercode)
Packit 875988
        {
Packit 875988
          /* No errors encountered, declare success */
Packit 875988
          con_info->answerstring = completepage;
Packit 875988
          con_info->answercode = MHD_HTTP_OK;
Packit 875988
        }
Packit 875988
      return send_page (connection,
Packit 875988
                        con_info->answerstring,
Packit 875988
                        con_info->answercode);
Packit 875988
    }
Packit 875988
Packit 875988
  /* Note a GET or a POST, generate error */
Packit 875988
  return send_page (connection,
Packit 875988
                    errorpage,
Packit 875988
                    MHD_HTTP_BAD_REQUEST);
Packit 875988
}
Packit 875988
Packit 875988
Packit 875988
int
Packit 875988
main ()
Packit 875988
{
Packit 875988
  struct MHD_Daemon *daemon;
Packit 875988
Packit 875988
  daemon = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD,
Packit 875988
                             PORT, NULL, NULL,
Packit 875988
                             &answer_to_connection, NULL,
Packit 875988
                             MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL,
Packit 875988
                             MHD_OPTION_END);
Packit 875988
  if (NULL == daemon)
Packit 875988
    {
Packit 875988
      fprintf (stderr,
Packit 875988
               "Failed to start daemon\n");
Packit 875988
      return 1;
Packit 875988
    }
Packit 875988
  (void) getchar ();
Packit 875988
  MHD_stop_daemon (daemon);
Packit 875988
  return 0;
Packit 875988
}