Thread: FastCGI scenario to write Content-Type header of request to a textfile

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    53

    FastCGI scenario to write Content-Type header of request to a textfile

    My code: #include <fcgi_stdio.h>#include <stdlib.h>int main(void){ int l - Pastebin.com
    Used library GitHub - FastCGI-Archives/fcgi2: FastCGI.com fcgi2 Development Kit fork from http://repo.or.cz/fcgi2.git + last snapshot on Linux with Lighttpd.
    Web-server has rights for writing.
    I am putting png file to the form and clicking button. Then, there is a textfile with non-zero size created. But no one text editor can read it as a text. Why? What am I don't ng wrong? Thank you.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    This is not my area of expertise, but the first thing you should do is split the massive printf() function into multiple printf() calls!

    Also post your code as plain text, here in Code Blocks.
    Last edited by rstanley; 11-16-2022 at 10:19 AM.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Nothing wrong with big format strings on printf() function. Try this:

    Code:
    #include <fcgi_stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ( void )
    {
      int len = 0;
    
      while ( FCGI_Accept() >= 0 )
      {
        FILE *fp;
    
       // Change mode to append. Testing errno...
        errno = 0;
        fp = fopen ( "/wwwdata/example.txt", "a+" );
        if ( errno )
          printf ( "Content-type: text/html\r\n\r\n<html><body><h1>Error</h1><br/>%s</body>\r\n",
                    strerror ( errno ) );
    
        // Output actual content only if file could be opened.
        if ( fp )
        {
          char *content_type = getenv ( "CONTENT_TYPE" );
          char *contentLength = getenv ( "CONTENT_LENGTH" );
    
          if ( contentLength )
            len = atoi ( contentLength );
          else
            len = 0;
    
          // Added \r\n at the end of the string.
          printf ( "Content-type: text/html\r\n"
                   "\r\n"
                   "<!DOCTYPE html>"
                   "<html><head><meta content='text/html;charset=utf-8' http-equiv='Content-Type'><meta charset='utf-8' http-equiv='encoding'>"
                   "<script type='text/javascript'>"
                   "function f(){var x = new XMLHttpRequest();x.open('POST', '/i/a.out', true);x.timeout = 300000;"
                   "var f = new FormData();"
                   "f.append('f', window.document.getElementById('f').files[0]);"
                   "x.send(f);"
                   "}</script></head><body>"
                   "<b>%d</b><input type='file' id='f' name='f' accept='image/png'><p onClick='f();'>PRESS!</p></body></html>\r\n",
                   len );
    
          // Why to write each char individually if content_type is a string?
          if ( len > 0 && content_type )
            fprintf ( fp, "%s\n", content_type );
    
          fclose ( fp );
        }
    
        FCGI_Finish();
      }
    }

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    flp1969:

    It was better for the OP to reformat his code himself, rather than do all the work for him.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by rstanley View Post
    It was better for the OP to reformat his code himself, rather than do all the work for him.
    Since he provided the code I think there is no harm on showing, essentially, the same code with minor modifications.
    I don't do anyone's "work". (and his code has obvious bugs I didn't address here! He clearly didn't bother to read the CGI 1.1 and FastCGI specs)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I am putting png file to the form and clicking button.
    OK.

    Not sure why your POST command has "a.out" in it then.

    > Then, there is a textfile with non-zero size created. But no one text editor can read it as a text. Why?
    Just calling it example.txt doesn't make it a text file.

    Your fprintf will happy scribble any byte value except \0

    If you want to know what's in the file, then load it into a hex editor.
    Maybe you have something like this at the start of the file.
    Code:
    $ hd example.png | head
    00000000  89 50 4e 47 0d 0a 1a 0a  00 00 00 0d 49 48 44 52  |.PNG........IHDR|
    Noting that because you're using fprintf, all the \0 would be stripped out.


    Also, opening the file in "a+" mode just means it's going to grow in size each time you run the code.

    Also, formatting.
    Code:
    const char *script =
        "function f()"
        "{"
        "  var x = new XMLHttpRequest();"
        "  x.open('POST', '/i/a.out', true);"
        "  x.timeout = 300000;"
        "  var f = new FormData();"
        "  f.append('f', window.document.getElementById('f').files[0]);"
        "  x.send(f);"
        "}";
    
    printf(
        "Content-type: text/html\r\n"
        "\r\n"
        "<!DOCTYPE html>"
        "<html>"
        "  <head>"
        "    <meta content='text/html;charset=utf-8' http-equiv='Content-Type'>"
        "    <meta charset='utf-8' http-equiv='encoding'>"
        "    <script type='text/javascript'>"
        "    %s"
        "    </script>"
        "  </head>"
        "  <body>"
        "    <b>%d</b>"
        "    <input type='file' id='f' name='f' accept='image/png'>"
        "    <p onClick='f();'>PRESS!</p>"
        "  </body>"
        "</html>\r\n",
        script, len );
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implementing type safety in a C-style callback scenario
    By Abyssion in forum C++ Programming
    Replies: 8
    Last Post: 03-15-2017, 02:59 PM
  2. how to know that a file content type?
    By kapil1089thekin in forum C Programming
    Replies: 1
    Last Post: 02-01-2011, 06:05 PM
  3. Replies: 2
    Last Post: 09-01-2010, 03:54 PM
  4. How to write the content of a Textbox in a txt
    By emtec in forum Windows Programming
    Replies: 0
    Last Post: 07-25-2010, 06:09 AM

Tags for this Thread