Advertisement
Guest User

Simple post example for libmicrohttpd

a guest
Oct 28th, 2014
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <iostream>
  6. #include <cstring>
  7. #include <microhttpd.h>
  8.  
  9. using namespace std;
  10.  
  11. struct ConnectionData
  12. {
  13.     bool is_parsing;
  14.     stringstream read_post_data;
  15. };
  16.  
  17.  
  18. int handle_request(void *cls, struct MHD_Connection *connection,
  19.                    const char *url,
  20.                    const char *method, const char *version,
  21.                    const char *upload_data,
  22.                    size_t *upload_data_size, void **con_cls)
  23. {
  24.     int http_code;
  25.     string output;
  26.     string content_type = "text/plain";
  27.    
  28.     if (strcmp(method, MHD_HTTP_METHOD_POST) == 0)
  29.     {
  30.         ConnectionData* connection_data = NULL;
  31.        
  32.         connection_data = static_cast<ConnectionData*>(*con_cls);
  33.         if (NULL == connection_data)
  34.         {
  35.             connection_data = new ConnectionData();
  36.             connection_data->is_parsing = false;
  37.             *con_cls = connection_data;
  38.         }
  39.        
  40.         if (!connection_data->is_parsing)
  41.         {
  42.             // First this method gets called with *upload_data_size == 0
  43.             // just to let us know that we need to start receiving POST data
  44.             connection_data->is_parsing = true;
  45.             return MHD_YES;
  46.         }
  47.         else
  48.         {
  49.             if (*upload_data_size != 0)
  50.             {
  51.                 // Receive the post data and write them into the bufffer
  52.                 connection_data->read_post_data << string(upload_data, *upload_data_size);
  53.                 *upload_data_size = 0;
  54.                 return MHD_YES;
  55.             }
  56.             else
  57.             {
  58.                 // *upload_data_size == 0 so all data have been received
  59.                 output = "Received data:\n\n";
  60.                 output += connection_data->read_post_data.str();
  61.                
  62.                 delete connection_data;
  63.                 connection_data = NULL;
  64.                 *con_cls = NULL;
  65.             }
  66.         }
  67.     }
  68.     else
  69.     {
  70.         http_code = MHD_HTTP_NOT_FOUND;
  71.         content_type = "text/plain";
  72.         output = "Unknown request method";
  73.     }
  74.  
  75.     const char* output_const = output.c_str();
  76.  
  77.     struct MHD_Response *response = MHD_create_response_from_buffer(
  78.                     strlen(output_const), (void*)output_const, MHD_RESPMEM_MUST_COPY);
  79.  
  80.     MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, content_type.c_str());
  81.  
  82.     int ret = MHD_queue_response(connection, http_code, response);
  83.  
  84.     MHD_destroy_response(response);
  85.    
  86.     return ret;
  87. }
  88.  
  89. int main (int argc, char** argv)
  90. {
  91.  
  92.     MHD_Daemon* daemon = MHD_start_daemon(
  93.                     MHD_USE_SELECT_INTERNALLY, 8001, NULL, NULL,
  94.                     &handle_request, NULL, MHD_OPTION_END);
  95.  
  96.     if(!daemon) {
  97.         cerr << "Failed to start HTTP server " << endl;
  98.         return 1;
  99.     }
  100.  
  101.     // Handle requests until we're asked to stop
  102.     for(;;) {
  103.         MHD_run (daemon);
  104.         usleep(1000*1000);
  105.     }
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement