Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,

I've developed en embedded web-server using Snorkel API, where the server displays a form requesting the mobile number from the client using the GET and POST method via index.html.
The problem is after posting the mobile number, I need another form requesting the client to submit a One-Time-Password by also using the GET and POST, but its not working.

HERE, ARE THE TWO REQUIRED FORMS:

C++
/*Overloading a form URL and its corresponding POST*/

char szForm[] =        /*content buffer-containing form*/
  { "<html>\r\n<body>\r\n"
	"<form method=\"post\" action=\"postform.html\">\r\n"
	"Mobile Number:\r\n"
	"<input type=\"text\" name=\"mobilenumber\">\r\n"
	"<br>\r\n"
	"<input type=\"submit\" value=\"submit\">\r\n"
	"</form>\r\n" "</body>\r\n</html>\r\n"
  };

  char otpForm[] =
  { "<html>\r\n<body>\r\n"
	"<form method=\"post\" action=\"postform.html\">\r\n"
	"One Time Password:\r\n"
	"<input type=\"text\" name=\"otp\">\r\n"
	"<br>\r\n"
	"<input type=\"submit\" value=\"submit\">\r\n"
	"</form>\r\n" "</body>\r\n</html>\r\n"
  };


HERE, IS THE POSTFORM FUNCTION:

C++
<pre>call_status_t
postform (snorkel_obj_t http,    //read environment from this object
		  snorkel_obj_t outstream)       // write data to the output stream

  {
   if (snorkel_obj_get(http, snorkel_attrib_post, "mobilenumber", szmobilenumber,sizeof (szmobilenumber)) != SNORKEL_SUCCESS)
		  return HTTP_ERROR;

   snorkel_printf (outstream,"<html><body>YOUR MOBILE NUMBER IS %s </body></html>", szmobilenumber);

}



C++
void main (int argc, char *argv[])
   {
	 int i = 1;
	 int port = 8080;
	 char *pszIndex = 0;
	 char *pszLog = 0;
	 char szExit[10];
	 snorkel_obj_t logobj = 0;
	 snorkel_obj_t http = 0;
	 snorkel_obj_t outstream;
	 int has_ssl=0;

	 for (; i < argc; i++)
	 {
	  if (argv[i][0] == '-' || argv[i][0] == '/')
		{
		  char carg = argv[i][1];

		  switch (carg)
		   {
			case 'p':
			 port = atoi (argv[i + 1]);
			 i++;
			 break;

			case 'l':
			 pszLog = argv[i + 1];
			  i++;
			  break;

			 default:
			  syntax (argv[0]);
			  break;
			}
		 }
	 }
/*
 *
 * create a log file object
 * for logging
 *
 */

 if (pszLog)
  {
   logobj = snorkel_obj_create (snorkel_obj_log, pszLog);
   if (!logobj)
	 {
	   perror ("could not create log file\n");
	   exit (1);
	  }
	snorkel_debug (1);
   }

/*
 *
 * initialize the API
 *
 */
if (snorkel_init () != SNORKEL_SUCCESS)
 {
   perror ("could not initialize snorkel\n");
   exit (1);
 }

/*
 *
 * create our server object
 *
 */
http = snorkel_obj_create (snorkel_obj_server, 4, NULL);
if (!http)
  {
	perror ("could not create http server\n");
	exit (1);
  }

/*
 *
 * set up our listener
 *
 */

 // practice
 //if (snorkel_sysenable_ssl("/Users/Rania/Documents/DIGITAL CERTIFICATES/MServer.pem")==SNORKEL_SUCCESS)
 has_ssl=0;

 if (snorkel_obj_set (http,snorkel_attrib_listener,port, has_ssl) != SNORKEL_SUCCESS)
	{
	  perror ("could not establish listener\n");
	  snorkel_obj_destroy (http);
	  if (logobj)
		 snorkel_obj_destroy (logobj);
	  exit (1);
	 }

 if (snorkel_obj_set(http, snorkel_attrib_ipvers, IPVERS_IPV4,SOCK_SET) != SNORKEL_SUCCESS)
	{
	  fprintf (stderr, "error could not set ip version\n");
	  exit (1);
	 }

  //LISTENING ON ANOTHER PORT
  //-------------------------
  if (snorkel_obj_set (http,snorkel_attrib_listener,7021, 0) != SNORKEL_SUCCESS)
	{
	  perror ("could not establish listener\n");
	  snorkel_obj_destroy (http);
	  if (logobj)
		 snorkel_obj_destroy (logobj);
	  exit (1);
	 }
/*
 *
 * overload some URLs
 *
 */


 if (snorkel_obj_set (http, snorkel_attrib_uri_content, GET,   /* method */
					  "/index.html",   /* URL to overload */
					   szForm,  /* pointer to content buffer */
					   SNORKEL_STORE_AS_REF     /* do not duplicate */
  ) != SNORKEL_SUCCESS)
	{
		perror ("could not overload index.html");
		snorkel_obj_destroy (http);
		if (logobj)
		  snorkel_obj_destroy (logobj);
		exit (1);
	 }

  if (snorkel_obj_set (http,snorkel_attrib_uri, POST,"/postform.html",contenttype_text,postform) != SNORKEL_SUCCESS)
	{
	  perror ("could not overload postform.html");
	  snorkel_obj_destroy (http);
	  if (logobj)
		snorkel_obj_destroy (logobj);
	  exit (1);
	 }
<pre lang="c++">if (snorkel_obj_set (http, snorkel_attrib_uri_content, GET,   /* method */
					  "/index.html",   /* URL to overload */
					   otpForm,  /* pointer to content buffer */
					   SNORKEL_STORE_AS_REF     /* do not duplicate */
  ) != SNORKEL_SUCCESS)
	{
		perror ("could not overload index.html");
		snorkel_obj_destroy (http);
		if (logobj)
		  snorkel_obj_destroy (logobj);
		exit (1);
	 }
fprintf (stderr,"\n\n[HTTP] starting on port %d....", port);

if (pszLog)
{
fprintf (stderr,"\n[HTTP] logging messages to %s\n", pszLog);
}
if (snorkel_obj_start (http) != SNORKEL_SUCCESS)
{
perror ("could not start server\n");
snorkel_obj_destroy (http);
if (logobj)
snorkel_obj_destroy (logobj);
exit (1);
}



C++
fprintf (stderr, "\n[HTTP] started.\n\n"
				"--hit enter to terminate--\n");
 fgets (szExit, sizeof (szExit), stdin);


 //fprintf (stderr, "[HTTP] bye\n");
 /*snorkel_obj_destroy (http);
 if (logobj)
	snorkel_obj_destroy (logobj);
   getch();

}



my question is: how can I display the first form szForm (the form requesting the mobile number), and after submitting the mobile number, I get the second form otpForm (the form that request the OTP)!!
Another question:
can we perform the second GET method on the same index.html, which I've used with my first GET method?

Need help please.

Best Regards.
Posted
Updated 2-Dec-15 17:52pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900