daily pastebin goal
40%
SHARE
TWEET

POST Libmicrohttpd TEST

AntonioVillanueva Dec 19th, 2018 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* simplepost  https://www.gnu.org/software/libmicrohttpd/
  2.  * cc simplepost.c -o simplepost -I$PATH_TO_LIBMHD_INCLUDES -L$PATH_TO_LIBMHD_LIBS -lmicrohttpd
  3.  *  */
  4.    
  5.  
  6. #include <sys/types.h>
  7. #ifndef _WIN32
  8. #include <sys/select.h>
  9. #include <sys/socket.h>
  10. #else
  11. #include <winsock2.h>
  12. #endif
  13. #include <microhttpd.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17.  
  18. #if defined(_MSC_VER) && _MSC_VER+0 <= 1800
  19. /* Substitution is OK while return value is not used */
  20. #define snprintf _snprintf
  21. #endif
  22.  
  23. #define PORT            8888
  24. #define POSTBUFFERSIZE  512
  25. #define MAXNAMESIZE     20
  26. #define MAXANSWERSIZE   512
  27.  
  28. //Definiciones para GET = 0  y POST = 1
  29. #define GET             0
  30. #define POST            1
  31.  
  32. struct connection_info_struct
  33. {
  34.   int connectiontype;
  35.   char *answerstring;
  36.   struct MHD_PostProcessor *postprocessor;
  37. };
  38.  
  39. /*******************************************************************************************************************************/
  40. //Analisis de la conexion por terminal de servidor
  41. int print_out_key(void *cls,enum MHD_ValueKind kind,const char *key,const char *value){
  42.     printf ("%s : %s \n",key,value);
  43.     return MHD_YES;
  44. }
  45.  
  46. /*******************************************************************************************************************************/
  47. //Diferentes cadenas texto en formato html
  48. //Pagina inicial nombre es la variable que lee el nombre a mezclar en la respuesta
  49. //El navegador muestra una direccion ip con /namepost ex. 192.168.6.150:8888/namepost
  50. const char *askpage = "<html><body>\
  51.                       Su nombre ?<br>\
  52.                       <form action=\"/namepost\" method=\"post\">\
  53.                       <input name=\"nombre\" type=\"text\">\
  54.                       <input type=\"submit\" value=\" Send \"></form>\
  55.                       </body></html>";
  56.                        
  57. //Respuesta al introducir el nombre , mezcla Bienvenido con el nombre en entrada anterior
  58. const char *greetingpage =
  59.   "<html><body><h1> Bienvenido , %s!</center></h1></body></html>";
  60.  
  61. //Pagina de Error
  62. const char *errorpage =
  63.   "<html><body> Error ! Esto parece no ser correcto.</body></html>";
  64.  
  65. /*******************************************************************************************************************************/
  66. static int send_page (struct MHD_Connection *connection, const char *page) //Crea la pagina de respuesta , con la info html apropiada
  67. {
  68.   int ret;
  69.   struct MHD_Response *response;
  70.  
  71.  
  72.   response =MHD_create_response_from_buffer (strlen (page), (void *) page,MHD_RESPMEM_PERSISTENT);
  73.  
  74.   if (!response) { return MHD_NO;}
  75.  
  76.   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  77.   MHD_destroy_response (response);
  78.  
  79.   return ret;
  80. }
  81.  
  82. /*******************************************************************************************************************************/
  83. //Rellena en la estructura connection_info_struct->answerstrign con el nombre recuperado
  84. static int iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
  85.               const char *filename, const char *content_type,
  86.               const char *transfer_encoding, const char *data, uint64_t off,
  87.               size_t size)
  88. {
  89.   struct connection_info_struct *con_info = coninfo_cls;
  90.   (void)kind;               /* Unused. Silent compiler warning. */
  91.   (void)filename;           /* Unused. Silent compiler warning. */
  92.   (void)content_type;       /* Unused. Silent compiler warning. */
  93.   (void)transfer_encoding;  /* Unused. Silent compiler warning. */
  94.   (void)off;                /* Unused. Silent compiler warning. */
  95.  
  96. //key contiene nombre y data el nombre introducido
  97.     printf ("\n Name = %s \n",data);
  98.  
  99.   if (0 == strcmp (key, "nombre"))//Busca la cadena nombre en const char *key
  100.     {
  101.       if ((size > 0) && (size <= MAXNAMESIZE))//Si el tamno es  mayor de 0 y menor 20
  102.         {
  103.           char *answerstring;
  104.           answerstring = malloc (MAXANSWERSIZE);//Reserva memoria para la respuesta
  105.           if (!answerstring) {return MHD_NO;}//Error no hay respuesta
  106.  
  107.             //Crea una respuesta answerstring cpn Bienvenido + data
  108.           snprintf (answerstring, MAXANSWERSIZE, greetingpage, data);//Bienvenido + nombre
  109.           con_info->answerstring = answerstring;
  110.         }
  111.       else
  112.         con_info->answerstring = NULL;
  113.  
  114.       return MHD_NO;
  115.     }
  116.  
  117.   return MHD_YES;
  118. }
  119.  
  120. /*******************************************************************************************************************************/
  121.  
  122. static void request_completed (void *cls, struct MHD_Connection *connection,
  123.                    void **con_cls, enum MHD_RequestTerminationCode toe)
  124. {
  125.   struct connection_info_struct *con_info = *con_cls;
  126.   (void)cls;         /* Unused. Silent compiler warning. */
  127.   (void)connection;  /* Unused. Silent compiler warning. */
  128.   (void)toe;         /* Unused. Silent compiler warning. */
  129.  
  130.   if (NULL == con_info) {return;}
  131.  
  132.   if (con_info->connectiontype == POST)
  133.     {
  134.       MHD_destroy_post_processor (con_info->postprocessor);
  135.       if (con_info->answerstring)
  136.         free (con_info->answerstring);
  137.     }
  138.  
  139.   free (con_info);
  140.   *con_cls = NULL;
  141. }
  142.  
  143. /*******************************************************************************************************************************/
  144. static int answer_to_connection (void *cls, struct MHD_Connection *connection,const char *url, const char *method,
  145.                       const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
  146. {
  147.   (void)cls;               /* Unused. Silent compiler warning. */
  148.   (void)url;               /* Unused. Silent compiler warning. */
  149.   (void)version;           /* Unused. Silent compiler warning. */
  150.  
  151.   //Solo para debug
  152.     MHD_get_connection_values(connection,MHD_HEADER_KIND,&print_out_key,NULL);//Analisis de la conexion web por terminal en servidor , no necesario  
  153.    
  154.   /**************************************************************************/
  155.  
  156.   if (NULL == *con_cls)
  157.     {
  158.       struct connection_info_struct *con_info;//Structura para compartir informacion tipo de conexion,cadena respuesta ,MHD_PostProcesseur ..
  159.  
  160.       con_info = malloc (sizeof (struct connection_info_struct));//Reserva memoria para esta estructura
  161.       if (NULL == con_info) { return MHD_NO;}
  162.      
  163.       con_info->answerstring = NULL;//Cadena de respuesta por el momento NULL
  164.  
  165.       if (0 == strcmp (method, "POST"))//metodo POST
  166.         {
  167.         //rellena la struct connection_info_struct
  168.           con_info->postprocessor =MHD_create_post_processor (connection, POSTBUFFERSIZE,iterate_post, (void *) con_info);
  169.  
  170.           if (NULL == con_info->postprocessor) //Error ! No hay post_processor
  171.             {
  172.               free (con_info);
  173.               return MHD_NO;
  174.             }
  175.  
  176.           con_info->connectiontype = POST;
  177.         }
  178.       else
  179.         con_info->connectiontype = GET;
  180.  
  181.       *con_cls = (void *) con_info;
  182.  
  183.       return MHD_YES;
  184.     }
  185.  
  186.  
  187. // Metodo Get envia pagina inicial (askpage)  de pregunta Su nombre ?  Send ..;
  188.    
  189.   if (0 == strcmp (method, "GET")){ return send_page (connection, askpage); }
  190.  
  191.   if (0 == strcmp (method, "POST"))//metodo POST
  192.     {
  193.       struct connection_info_struct *con_info = *con_cls;
  194.  
  195.       if (*upload_data_size != 0)
  196.         {
  197.           MHD_post_process (con_info->postprocessor, upload_data,
  198.                             *upload_data_size);
  199.           *upload_data_size = 0;
  200.  
  201.           return MHD_YES;
  202.         }
  203.       else if (NULL != con_info->answerstring)
  204.         return send_page (connection, con_info->answerstring);//pagina con_info->answerstring  de pregunta Su nombre ?  Send ..;
  205.     }
  206.  
  207.   return send_page (connection, errorpage);//pagina ERROR
  208. }
  209. /*******************************************************************************************************************************/
  210. int main ()
  211. {
  212.   struct MHD_Daemon *daemon;
  213.  
  214.   daemon = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL,&answer_to_connection, NULL,
  215.                              MHD_OPTION_NOTIFY_COMPLETED, request_completed,
  216.                              NULL, MHD_OPTION_END);
  217.                              
  218.   if (NULL == daemon) {return 1;}
  219.  
  220.   (void) getchar ();
  221.  
  222.   MHD_stop_daemon (daemon);
  223.  
  224.   return 0;
  225. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top