Thread: Problem with CGI and embedded html

  1. #1
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79

    Problem with CGI and embedded html

    Hi.

    I've got a problem running a cgi-program with embedded html files. It works when running it from the command line, but not when running it in lighttpd. Then I get a 500 - Internal server error [lighttpd errorlog: (mod_cgi.c.607) cgi died, pid: 12688].
    What am I doing wrong here?

    index.html
    Code:
    <html>
    	<body>
    		<h1>Hello world!</h1>
    	</body>
    </html>
    I make a object file of it by
    Code:
    objcopy -I binary -O elf64-x86-64 -B i386 index.html index.o
    main.c
    Code:
    #include <stdio.h>
    
    extern char _binary_index_html_start[];
    extern char _binary_index_html_size;
    
    int main(int argc, char** argv) {
    
    	printf("Content-type: text/html\r\nContent-size: %u\r\n\r\n",
    			&_binary_index_html_size);
    
    	fwrite(_binary_index_html_start, 1,
    			(size_t) &_binary_index_html_size, stdout);
    
    	return 0;
    
    }
    Code:
    gcc -o prob main.c index.o
    Thanks for help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > extern char _binary_index_html_start[];
    > extern char _binary_index_html_size;
    It seems very odd that the size of the object in question has a type of char

    Assuming this is your mistake, and the size is of type int or size_t, then you need to do something like
    Code:
        printf("Content-type: text/html\r\nContent-size: %u\r\n\r\n",
                (unsigned int)_binary_index_html_size);
     
        fwrite(_binary_index_html_start, 1,
                (size_t)_binary_index_html_size, stdout);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    I don't know fully how the objcopy object-file and the linker works it out, but that's how you've got to do it to make it work at the command line. I guess the declaration can be of any type.
    I find it's odd that you get the size of the data by looking upp the address of the reference, but that's how it works.

    Is there other (more logical) ways of embedding data into an executable?

  4. #4
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    Here's a little more correctly expressed example of main.c. Still the same problem though...

    Code:
    #include <stdio.h>
    
    extern char _binary_index_html_start[];
    extern char _binary_index_html_end[];
    
    int main(int argc, char** argv) {
    
    	size_t len;
    
    	len = _binary_index_html_end - _binary_index_html_start;
    
    	printf("Content-type: text/html\r\nContent-size: %u\r\n\r\n", len);
    
    	fwrite(_binary_index_html_start, 1, len, stdout);
    
    	return 0;
    
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    So does this work as a CGI program?
    Code:
    int main ( ) {
        printf(
    "<html>\n"
    "    <body>\n"
    "        <h1>Hello world!</h1>\n"
    "    </body>\n"
    "</html>\n"
    );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    Not without a little modification.

    Code:
    #include <stdio.h>
    
    int main ( ) {
        printf(
    "\r\n\r\n"
    "<html>\n"
    "    <body>\n"
    "        <h1>Hello world!</h1>\n"
    "    </body>\n"
    "</html>\n"
    );
        return 0;
    }
    That works. Got to give the end-of-header separator to the webserver before the content, or an internal error occures.

    I'm using xxd to generate C source of the files instead. That works. So there is something fishy about the objcopy way of doing it. It's strange that the program works flawless in command line, but not as a cgi (in neither Lighttpd ot Apache).
    Last edited by Fader_Berg; 07-11-2016 at 09:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with embedded if else statements
    By jesterisdead in forum C Programming
    Replies: 10
    Last Post: 04-20-2014, 04:37 PM
  2. Embedded Linux & Embedded Java
    By jdomm95 in forum Linux Programming
    Replies: 4
    Last Post: 10-02-2012, 09:01 AM
  3. Replies: 7
    Last Post: 11-04-2011, 10:31 AM
  4. problem with strstr func...embedded system
    By kievari in forum C Programming
    Replies: 2
    Last Post: 03-27-2010, 03:43 AM
  5. Problem with embedded SQL in C/C++ (ECPG)
    By NeuralClone in forum C Programming
    Replies: 4
    Last Post: 10-21-2005, 05:16 PM

Tags for this Thread