Home | History | Annotate | Download | only in examples
      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007 Christian Grothoff (and other contributing authors)
      4 
      5      This library is free software; you can redistribute it and/or
      6      modify it under the terms of the GNU Lesser General Public
      7      License as published by the Free Software Foundation; either
      8      version 2.1 of the License, or (at your option) any later version.
      9 
     10      This library is distributed in the hope that it will be useful,
     11      but WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      Lesser General Public License for more details.
     14 
     15      You should have received a copy of the GNU Lesser General Public
     16      License along with this library; if not, write to the Free Software
     17      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     18 */
     19 
     20 /**
     21  * @file fileserver_example.c
     22  * @brief minimal example for how to use libmicrohttpd to serve files
     23  * @author Christian Grothoff
     24  */
     25 
     26 #include "platform.h"
     27 #include <microhttpd.h>
     28 #include <unistd.h>
     29 
     30 #define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>"
     31 
     32 static ssize_t
     33 file_reader (void *cls, uint64_t pos, char *buf, size_t max)
     34 {
     35   FILE *file = cls;
     36 
     37   (void)  fseek (file, pos, SEEK_SET);
     38   return fread (buf, 1, max, file);
     39 }
     40 
     41 static void
     42 free_callback (void *cls)
     43 {
     44   FILE *file = cls;
     45   fclose (file);
     46 }
     47 
     48 static int
     49 ahc_echo (void *cls,
     50           struct MHD_Connection *connection,
     51           const char *url,
     52           const char *method,
     53           const char *version,
     54           const char *upload_data,
     55 	  size_t *upload_data_size, void **ptr)
     56 {
     57   static int aptr;
     58   struct MHD_Response *response;
     59   int ret;
     60   FILE *file;
     61   struct stat buf;
     62 
     63   if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
     64     return MHD_NO;              /* unexpected method */
     65   if (&aptr != *ptr)
     66     {
     67       /* do never respond on first call */
     68       *ptr = &aptr;
     69       return MHD_YES;
     70     }
     71   *ptr = NULL;                  /* reset when done */
     72   if (0 == stat (&url[1], &buf))
     73     file = fopen (&url[1], "rb");
     74   else
     75     file = NULL;
     76   if (file == NULL)
     77     {
     78       response = MHD_create_response_from_buffer (strlen (PAGE),
     79 						  (void *) PAGE,
     80 						  MHD_RESPMEM_PERSISTENT);
     81       ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
     82       MHD_destroy_response (response);
     83     }
     84   else
     85     {
     86       response = MHD_create_response_from_callback (buf.st_size, 32 * 1024,     /* 32k page size */
     87                                                     &file_reader,
     88                                                     file,
     89                                                     &free_callback);
     90       if (response == NULL)
     91 	{
     92 	  fclose (file);
     93 	  return MHD_NO;
     94 	}
     95       ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
     96       MHD_destroy_response (response);
     97     }
     98   return ret;
     99 }
    100 
    101 int
    102 main (int argc, char *const *argv)
    103 {
    104   struct MHD_Daemon *d;
    105 
    106   if (argc != 2)
    107     {
    108       printf ("%s PORT\n", argv[0]);
    109       return 1;
    110     }
    111   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
    112                         atoi (argv[1]),
    113                         NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
    114   if (d == NULL)
    115     return 1;
    116   (void) getc (stdin);
    117   MHD_stop_daemon (d);
    118   return 0;
    119 }
    120