Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* simplepost https://www.gnu.org/software/libmicrohttpd/
- * cc simplepost.c -o simplepost -I$PATH_TO_LIBMHD_INCLUDES -L$PATH_TO_LIBMHD_LIBS -lmicrohttpd
- * */
- #include <sys/types.h>
- #ifndef _WIN32
- #include <sys/select.h>
- #include <sys/socket.h>
- #else
- #include <winsock2.h>
- #endif
- #include <microhttpd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #if defined(_MSC_VER) && _MSC_VER+0 <= 1800
- /* Substitution is OK while return value is not used */
- #define snprintf _snprintf
- #endif
- #define PORT 8888
- #define POSTBUFFERSIZE 512
- #define MAXNAMESIZE 20
- #define MAXANSWERSIZE 512
- //Definiciones para GET = 0 y POST = 1
- #define GET 0
- #define POST 1
- struct connection_info_struct
- {
- int connectiontype;
- char *answerstring;
- struct MHD_PostProcessor *postprocessor;
- };
- /*******************************************************************************************************************************/
- //Analisis de la conexion por terminal de servidor
- int print_out_key(void *cls,enum MHD_ValueKind kind,const char *key,const char *value){
- printf ("%s : %s \n",key,value);
- return MHD_YES;
- }
- /*******************************************************************************************************************************/
- //Diferentes cadenas texto en formato html
- //Pagina inicial nombre es la variable que lee el nombre a mezclar en la respuesta
- //El navegador muestra una direccion ip con /namepost ex. 192.168.6.150:8888/namepost
- const char *askpage = "<html><body>\
- Su nombre ?<br>\
- <form action=\"/namepost\" method=\"post\">\
- <input name=\"nombre\" type=\"text\">\
- <input type=\"submit\" value=\" Send \"></form>\
- </body></html>";
- //Respuesta al introducir el nombre , mezcla Bienvenido con el nombre en entrada anterior
- const char *greetingpage =
- "<html><body><h1> Bienvenido , %s!</center></h1></body></html>";
- //Pagina de Error
- const char *errorpage =
- "<html><body> Error ! Esto parece no ser correcto.</body></html>";
- /*******************************************************************************************************************************/
- static int send_page (struct MHD_Connection *connection, const char *page) //Crea la pagina de respuesta , con la info html apropiada
- {
- int ret;
- struct MHD_Response *response;
- response =MHD_create_response_from_buffer (strlen (page), (void *) page,MHD_RESPMEM_PERSISTENT);
- if (!response) { return MHD_NO;}
- ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
- MHD_destroy_response (response);
- return ret;
- }
- /*******************************************************************************************************************************/
- //Rellena en la estructura connection_info_struct->answerstrign con el nombre recuperado
- static int iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
- const char *filename, const char *content_type,
- const char *transfer_encoding, const char *data, uint64_t off,
- size_t size)
- {
- struct connection_info_struct *con_info = coninfo_cls;
- (void)kind; /* Unused. Silent compiler warning. */
- (void)filename; /* Unused. Silent compiler warning. */
- (void)content_type; /* Unused. Silent compiler warning. */
- (void)transfer_encoding; /* Unused. Silent compiler warning. */
- (void)off; /* Unused. Silent compiler warning. */
- //key contiene nombre y data el nombre introducido
- printf ("\n Name = %s \n",data);
- if (0 == strcmp (key, "nombre"))//Busca la cadena nombre en const char *key
- {
- if ((size > 0) && (size <= MAXNAMESIZE))//Si el tamno es mayor de 0 y menor 20
- {
- char *answerstring;
- answerstring = malloc (MAXANSWERSIZE);//Reserva memoria para la respuesta
- if (!answerstring) {return MHD_NO;}//Error no hay respuesta
- //Crea una respuesta answerstring cpn Bienvenido + data
- snprintf (answerstring, MAXANSWERSIZE, greetingpage, data);//Bienvenido + nombre
- con_info->answerstring = answerstring;
- }
- else
- con_info->answerstring = NULL;
- return MHD_NO;
- }
- return MHD_YES;
- }
- /*******************************************************************************************************************************/
- static void request_completed (void *cls, struct MHD_Connection *connection,
- void **con_cls, enum MHD_RequestTerminationCode toe)
- {
- struct connection_info_struct *con_info = *con_cls;
- (void)cls; /* Unused. Silent compiler warning. */
- (void)connection; /* Unused. Silent compiler warning. */
- (void)toe; /* Unused. Silent compiler warning. */
- if (NULL == con_info) {return;}
- if (con_info->connectiontype == POST)
- {
- MHD_destroy_post_processor (con_info->postprocessor);
- if (con_info->answerstring)
- free (con_info->answerstring);
- }
- free (con_info);
- *con_cls = NULL;
- }
- /*******************************************************************************************************************************/
- static int answer_to_connection (void *cls, struct MHD_Connection *connection,const char *url, const char *method,
- const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
- {
- (void)cls; /* Unused. Silent compiler warning. */
- (void)url; /* Unused. Silent compiler warning. */
- (void)version; /* Unused. Silent compiler warning. */
- //Solo para debug
- MHD_get_connection_values(connection,MHD_HEADER_KIND,&print_out_key,NULL);//Analisis de la conexion web por terminal en servidor , no necesario
- /**************************************************************************/
- if (NULL == *con_cls)
- {
- struct connection_info_struct *con_info;//Structura para compartir informacion tipo de conexion,cadena respuesta ,MHD_PostProcesseur ..
- con_info = malloc (sizeof (struct connection_info_struct));//Reserva memoria para esta estructura
- if (NULL == con_info) { return MHD_NO;}
- con_info->answerstring = NULL;//Cadena de respuesta por el momento NULL
- if (0 == strcmp (method, "POST"))//metodo POST
- {
- //rellena la struct connection_info_struct
- con_info->postprocessor =MHD_create_post_processor (connection, POSTBUFFERSIZE,iterate_post, (void *) con_info);
- if (NULL == con_info->postprocessor) //Error ! No hay post_processor
- {
- free (con_info);
- return MHD_NO;
- }
- con_info->connectiontype = POST;
- }
- else
- con_info->connectiontype = GET;
- *con_cls = (void *) con_info;
- return MHD_YES;
- }
- // Metodo Get envia pagina inicial (askpage) de pregunta Su nombre ? Send ..;
- if (0 == strcmp (method, "GET")){ return send_page (connection, askpage); }
- if (0 == strcmp (method, "POST"))//metodo POST
- {
- struct connection_info_struct *con_info = *con_cls;
- if (*upload_data_size != 0)
- {
- MHD_post_process (con_info->postprocessor, upload_data,
- *upload_data_size);
- *upload_data_size = 0;
- return MHD_YES;
- }
- else if (NULL != con_info->answerstring)
- return send_page (connection, con_info->answerstring);//pagina con_info->answerstring de pregunta Su nombre ? Send ..;
- }
- return send_page (connection, errorpage);//pagina ERROR
- }
- /*******************************************************************************************************************************/
- int main ()
- {
- struct MHD_Daemon *daemon;
- daemon = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL,&answer_to_connection, NULL,
- MHD_OPTION_NOTIFY_COMPLETED, request_completed,
- NULL, MHD_OPTION_END);
- if (NULL == daemon) {return 1;}
- (void) getchar ();
- MHD_stop_daemon (daemon);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement