libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

responseheaders.c (2839B)


      1 /* Feel free to use this example code in any way
      2    you see fit (Public Domain) */
      3 
      4 #include <sys/types.h>
      5 #ifndef _WIN32
      6 #include <sys/select.h>
      7 #include <sys/socket.h>
      8 #else
      9 #include <winsock2.h>
     10 #endif
     11 #include <microhttpd.h>
     12 #include <time.h>
     13 #include <sys/stat.h>
     14 #include <fcntl.h>
     15 #include <string.h>
     16 #include <stdio.h>
     17 
     18 #define PORT 8888
     19 #define FILENAME "picture.png"
     20 #define MIMETYPE "image/png"
     21 
     22 static enum MHD_Result
     23 answer_to_connection (void *cls, struct MHD_Connection *connection,
     24                       const char *url, const char *method,
     25                       const char *version, const char *upload_data,
     26                       size_t *upload_data_size, void **req_cls)
     27 {
     28   struct MHD_Response *response;
     29   int fd;
     30   enum MHD_Result ret;
     31   struct stat sbuf;
     32   (void) cls;               /* Unused. Silent compiler warning. */
     33   (void) url;               /* Unused. Silent compiler warning. */
     34   (void) version;           /* Unused. Silent compiler warning. */
     35   (void) upload_data;       /* Unused. Silent compiler warning. */
     36   (void) upload_data_size;  /* Unused. Silent compiler warning. */
     37   (void) req_cls;           /* Unused. Silent compiler warning. */
     38 
     39   if (0 != strcmp (method, "GET"))
     40     return MHD_NO;
     41 
     42   if ( (-1 == (fd = open (FILENAME, O_RDONLY))) ||
     43        (0 != fstat (fd, &sbuf)) )
     44   {
     45     const char *errorstr =
     46       "<html><body>An internal server error has occurred!\
     47                               </body></html>";
     48     /* error accessing file */
     49     if (fd != -1)
     50       (void) close (fd);
     51     response =
     52       MHD_create_response_from_buffer_static (strlen (errorstr), errorstr);
     53     if (NULL != response)
     54     {
     55       ret =
     56         MHD_queue_response (connection, MHD_HTTP_INTERNAL_SERVER_ERROR,
     57                             response);
     58       MHD_destroy_response (response);
     59 
     60       return ret;
     61     }
     62     else
     63       return MHD_NO;
     64   }
     65   response =
     66     MHD_create_response_from_fd_at_offset64 ((size_t) sbuf.st_size,
     67                                              fd,
     68                                              0);
     69   if (MHD_YES !=
     70       MHD_add_response_header (response,
     71                                MHD_HTTP_HEADER_CONTENT_TYPE,
     72                                MIMETYPE))
     73   {
     74     fprintf (stderr,
     75              "Failed to set content type header!\n");
     76     /* return response without content encoding anyway ... */
     77   }
     78   ret = MHD_queue_response (connection,
     79                             MHD_HTTP_OK,
     80                             response);
     81   MHD_destroy_response (response);
     82 
     83   return ret;
     84 }
     85 
     86 
     87 int
     88 main (void)
     89 {
     90   struct MHD_Daemon *daemon;
     91 
     92   daemon = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL,
     93                              &answer_to_connection, NULL, MHD_OPTION_END);
     94   if (NULL == daemon)
     95     return 1;
     96 
     97   (void) getchar ();
     98 
     99   MHD_stop_daemon (daemon);
    100 
    101   return 0;
    102 }