Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Problem to display image in web page

268 views
Skip to first unread message

Panchali Gupta

unread,
Oct 3, 2013, 6:59:12 AM10/3/13
to mongoos...@googlegroups.com
Hi, I am new to Mongoose and I am facing a problem to display images in web pages. If i use the following I get the html form elements correctly, but the image is not displayed. Everytime it shows failed to load the URL.

static const char *html_form =
  "<html><body>"
  "<img src='test.PNG'/>"
  "<form method='POST' action='/handle_post_request'>"
  "Switch-1: <input type='text' name='input_1' /> "
  "Switch-2: <input type='text' name='input_2' /> "
  "<input type='submit'/>"
  "</form></body></html>";

I also tried by calling a web page that contains an image (us.html) but this approach also failed. It is showing a blank page every time

static const char *html = "/us.html";

        static int begin_request_handler(struct mg_connection *conn) {   
              mg_printf(conn, "HTTP/1.0 200 OK\r\n"
              "Content-Length: %d\r\n"
              "Content-Type: text/html\r\n\r\n"
              "Location: %s\r\n\r\n",(int) strlen(html), html);
             }

int main(void) {
    .....
  const char *options[] = {"listening_ports", "443"};
  struct mg_callbacks callbacks;
    .....
  memset(&callbacks, 0, sizeof(callbacks));
  callbacks.begin_request = begin_request_handler;
  ctx = mg_start(&callbacks, NULL, options);
.....
  }

I even failed to display the above html file as the index page.

Kindly help me out.
Regards

Sergey Lyubka

unread,
Oct 3, 2013, 12:54:54 PM10/3/13
to mongoose-users
There are 2 ways to serve image data:
  1. You can let mongoose send image data. In this case, set "document_root" option and place image file into the document root. Return 0 from begin_request() when image URI is requested/
  2. Call mg_send_file() function to send image data from begin_request() and return 1.


--
You received this message because you are subscribed to the Google Groups "mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-user...@googlegroups.com.
To post to this group, send email to mongoos...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/groups/opt_out.

Panchali Gupta

unread,
Oct 5, 2013, 3:44:09 AM10/5/13
to mongoos...@googlegroups.com

Thanks a ton!!!! The following code finally worked:



static const char *html_form =
  "<html><body>"
  "<img src='test.PNG'/>"
  "<form method='POST' action='/handle_post_request'>"
  "Switch-1: <input type='text' name='input_1' /> <br/>"
  "Switch-2: <input type='text' name='input_2' /> <br/>"
  "<input type='submit' value='OK'/>"
  "</form></body></html>";

 
 
static int begin_request_handler(struct mg_connection *conn) {
const struct mg_request_info *ri = mg_get_request_info(conn);

 if (!strcmp(ri->uri, "/handle_post_request")) {
 //..................
 }

 else if (!strcmp(ri->uri, "/test.PNG")) {
    mg_send_file(conn,"test.PNG");
 }
 else
 {
        mg_printf(conn, "HTTP/1.0 200 OK/r/n"
                "Content-Length: %d/r/n"

                "Content-Type: text/html\r\n\r\n"
                "%s",
                (int) strlen(html_form), html_form);
       
 }
 
 return 1;
}



int main(void) {

Sam Kunun

unread,
Feb 17, 2014, 9:46:13 PM2/17/14
to mongoos...@googlegroups.com
Hello,
 
Sorry to dig old thread.
I'm having the same problem on Mongoose 5.2, where I'm not able to display images in html pages.
I'm embedding it in C code, and it seems, mg_send_file is no longer available.
What is the alternative of mg_send_file to display images ?
 
Thank you.

Sergey Lyubka

unread,
Feb 18, 2014, 2:56:50 AM2/18/14
to mongoose-users
Hi Sam,
are you embedding image data directly into the executable?

Sergey


Sam Kunun

unread,
Feb 18, 2014, 3:12:27 AM2/18/14
to mongoos...@googlegroups.com
Hi,
 
Thank you for your prompt reply.
No, the image data is not embedded to executable.
 
After testing, i found out that now i'm able to display image using the first option.
"You can let mongoose send image data. In this case, set "document_root" option and place image file into the document root. Return 0 from begin_request() when image URI is requested/"
 
Is there any other alternative ?
 
Thank you.
 
~~sam

Sergey Lyubka

unread,
Feb 18, 2014, 4:36:46 AM2/18/14
to mongoose-users
On Tue, Feb 18, 2014 at 8:12 AM, Sam Kunun <sam....@gmail.com> wrote:
Hi,
 
Thank you for your prompt reply.
No, the image data is not embedded to executable.
 
After testing, i found out that now i'm able to display image using the first option.
"You can let mongoose send image data. In this case, set "document_root" option and place image file into the document root. Return 0 from begin_request() when image URI is requested/"
 
Is there any other alternative ?

Well, there are only two options: either mongoose sends the file, or user code does.
So the alternative is to send a file yourself: read in a loop and mg_write_data() several times,
or mmap() and send it by one mg_write_data() call.

Sergey.

Maurizio Gambaruto

unread,
Oct 21, 2015, 3:27:03 AM10/21/15
to mongoose-users
Hello,
Sorry to dig old thread.
I'm having the same problem on Mongoose 5.7,
I m not find call "mg write_data", which called I can use now?

thank you all

Sergey Lyubka

unread,
Oct 21, 2015, 3:53:57 AM10/21/15
to mongoose-users
Hi Maurizio,
could you publish your code please?

--
You received this message because you are subscribed to the Google Groups "mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-user...@googlegroups.com.
To post to this group, send email to mongoos...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/d/optout.

Maurizio Gambaruto

unread,
Oct 21, 2015, 4:17:00 AM10/21/15
to mongoose-users
Yes:
//--------------------------------------------------------
static void ev_handler_new(struct mg_connection *nc, int ev, void *p)
{
 struct http_message *hm = (struct http_message *) p; 
 char content[20000] = "";  
 char *pszContenetx=NULL;
 char *pszContenuto=NULL;
 char *pszTemp = NULL;
  int content_length =0; 
  int iDimensione_data = 0;
  //-------------------------------------------------------
 char buf[500] = ""; 
  time_t         curt;
  struct tm*     loct; 
  //------------------------------------------------------  
  curt = time (NULL);
  loct = localtime (&curt);
  strftime (buf, 500, "%d_%m_%Y %H:%M:%S", loct);
  //------------------------------------------------------    
  //------------------------------------------------------  
  iDimensione_data=strlen(buf); 
  //------------------------------------------------------
    if (ev == MG_EV_HTTP_REQUEST)
    {
      if (mg_vcmp(&hm->uri, "/hello") == 0)
      {          
       pszContenetx=(char *) malloc(strlen("Hello! CASE LINUX SERVER "))
       pszContenuto=(char *) malloc(strlen(pszContenetx)+ iDimensione_data +strlen(" DATE: ")+ 3);
       sprintf(pszContenuto,"%s%s%s",pszContenetx," DATE: ",buf);   
       content_length=strlen(pszContenuto);         
       mg_printf(nc, "HTTP/1.1 200 OK\r\n" "Content-Type: text/html \r\n" "Content-Length: %d\r\n" "\r\n" "%s",content_length, pszContenuto);
       mg_serve_http(nc, hm, s_http_server_opts);      
      }
      else
      {
           pszTemp=strdup(returnHtmlPageText());
           pszContenuto=(char *) malloc(strlen(pszTemp)+ iDimensione_data + 3);
           sprintf(pszContenuto,pszTemp,buf);
           content_length=strlen(pszContenuto);          
           mg_printf(nc,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s",content_length, pszContenuto);
           mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */          
      }     
    }
}

int main(int argc, char *argv[])
{
 //------------------------------------------------------
  struct mg_mgr mgr;
  struct mg_connection *nc;   
  cs_stat_t st; 
  //------------------------------------------------------
  mg_mgr_init(&mgr, NULL);
  nc = mg_bind(&mgr, s_http_port, ev_handler_new); 
  mg_set_protocol_http_websocket(nc);
  //------------------------------------------------------
  if(argc>1)
  {
    printf("case VERIFY \n");     
    s_http_server_opts.document_root = "./verifica";  // root_dir=verifica into root dir exist PIPPO.png
    s_http_server_opts.enable_directory_listing = "yes";
    s_http_server_opts.custom_mime_types = "text/html";
  }
  else
  {
    printf("case image \n");
    s_http_server_opts.document_root = ".";  // Serve current directory
    s_http_server_opts.enable_directory_listing = "yes";
    s_http_server_opts.custom_mime_types = "text/html";
  }
  if (mg_stat(s_http_server_opts.document_root, &st) != 0)
  {
    printf("Cannot find web_root directory, exiting\n");
    exit(1);
  }
  printf("Starting web server on port %s version %d\n", s_http_port,6);
  for (;;) {   
    mg_mgr_poll(&mgr, 2);
  }
  mg_mgr_free(&mgr);
  return 0;
}

char *returnHtmlPageText()
{
  char *pszHtmlPage=NULL;
  char *pszHeaderFile = NULL;
  char *pszBody=NULL;
  char *pszBody1=NULL;
  char *pszBody2=NULL; 
  char *pszBodyDateTemp = NULL;
  char *pszBodyDate = NULL;   
 char buf[500] = ""; 
  time_t         curt;
  struct tm*     loct;
  int iDimensione_data = 0;
  //------------------------------------------------------  
  curt = time (NULL);
  loct = localtime (&curt);
  strftime (buf, 500, "%d_%m_%Y %H:%M:%S", loct);
  //------------------------------------------------------                            
  pszHeaderFile=strdup("<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\" /><title>HELLO</title></head>");
pszBody=strdup("<body>     "
"  <div class=\"content\">"
"    <img src='PIPPO.png' style=\"float:right; height: 50px; border-radius: 3px;\">"
"    <h1>GOOD MORNING! This root directroy </h1>"
"    <table border=\"5\">"
"     <tr>"
"      <td>"
"       <table border=\"2\">"
"        <tr><td><img src='PIPPO.png' style=\"float:right; height: 50px; border-radius: 3px;\"></td></tr>"
"           <tr><td><img src='PIPPO.png' style=\"float:right; height: 50px; border-radius: 3px;\"></td></tr>"
"           <tr><td><img src=\"PIPPO.png\" style=\"float:right; height: 50px; border-radius: 3px;\"></td></tr>"
"        </table></td></tr><tr><td>DATE:%s</td></tr></table></div></body></html>");
 //-------------------------------------------------------------------------------
 pszHtmlPage=(char *) malloc(strlen(pszHeaderFile)+strlen(pszBody)+1);
 sprintf(pszHtmlPage,"%s%s",pszHeaderFile,pszBody);
 //-------------------------------------------------------------------------------
return pszHtmlPage;
}

//--------------------------------------------------------

Sergey Lyubka

unread,
Oct 31, 2015, 3:32:11 AM10/31/15
to mongoose-users
Hi Maurizio,

Don't mix mg_serve_http() with your own output.
Either send the data yourself (mg_send(), mg_printf(), etc) or call mg_serve_http().

Maurizio Gambaruto

unread,
Nov 3, 2015, 6:18:10 AM11/3/15
to mongoose-users
Thanks I was wrong .
I have two questions, if possible.
0)With the code downloaded the free version , can I manage permissions with .htaccess file and sessions ? if you can have an example ?
0)With the code downloaded the free version , can I manage permissions two doors, one door and another door http https ? If you can have an example?

Thanks a lot.
And sorry for the inconvenience

Sergey Lyubka

unread,
Nov 5, 2015, 4:30:03 AM11/5/15
to mongoose-users
Hi Maurizio,

Mongoose does not support .htaccess, that is an Apache server feature.

Maurizio Gambaruto

unread,
Jan 19, 2016, 3:27:18 AM1/19/16
to mongoose-users
Hello,
Thank you,
And sorry again disorder.

I have one more question.
After mg printf and MG_F_SEND_AND_CLOSE, memory and automatically freed?
Or you need to free it.
Perhè known that after a while the memory grows.
thank you
Hello
Reply all
Reply to author
Forward
0 new messages