libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] Sending file as answer: Closing connection (Stream E


From: Keith Mendoza
Subject: Re: [libmicrohttpd] Sending file as answer: Closing connection (Stream Error)
Date: Sun, 20 May 2012 10:18:07 -0700

Bruno,
In the description for MHD_create_response_from_fd_at_offset in the libmicrohttpd manual (http://www.gnu.org/software/libmicrohttpd/microhttpd.html#microhttpd_002dresponses) it states that 
Depending on your platform, MHD is likely to have been compiled with support for 64-bit files. When you compile your own application, you must make sure that off_t is also a 64-bit value

It also provides suggestions on what you can do to make sure that your application compiles with the proper off_t size. Some browsers have issues with data more than 4GB, and will simply close the TCP connection.

On Sat, May 19, 2012 at 6:11 AM, Bruno Knittel <address@hidden> wrote:
Hi Everyone,

I'm new to libmicrohttpd and I'm having an issue I don't know at all how to start troubleshooting it.
I want to answer an HTTP GET request with the content of a file that is in the same directory as my executable.

I'm using the function MHD_create_response_from_fd_at_offset to create the MHD_Response.
I already checked the file descriptor and the size information I'm passing to this function and it is fine.
I also checked that reading the whole content of the file using the read function works.

However my request is not answered (at least the page does not load in the browser) and the following is
written to stderr: "Closing connection (stream error)".
At the end of this message you will find an extract of my code.

Do you have some hints about what is going wrong here?
Or ideas how I can troubleshoot this?

Many thanks in advance for your help !

My Code:

int main()
{
// Start the web server
       unsigned int flags = MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG;

       webDaemon = MHD_start_daemon (flags,
                                       80,
                                          NULL,         // No access policy handler: connections from any client are allowed
                                          NULL,
                                         &answer_to_connection, // Access handler: answers to requests
                                         NULL,
                                         MHD_OPTION_CONNECTION_TIMEOUT,
                                         (unsigned int) 120,
                                         MHD_OPTION_END);

}

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 **ptr)
{
      struct MHD_Response *response = createMhdResponseForFile("index.html", "text/html");
      if (response == NULL)
      {
       // TODO Create default HTTP 404 Page
      }

      Int ret = MHD_queue_response(connection, httpStatusCode, response);
       MHD_destroy_response(response);
       return ret;

}


MHD_Response* createMhdResponseForFile(const char* path, const char* mime)
{
       struct stat fileStats;

       int fileDescriptor = open(path, O_RDONLY);
       if (fileDescriptor < 0)
       {
               return NULL;
       }

       if (fstat(fileDescriptor, &fileStats) != 0)
       {
               if (close(fileDescriptor) != 0)
               {
                       // TODO Some logging
               }

               return NULL;
       }

       MHD_Response* result = MHD_create_response_from_fd_at_offset(fileStats.st_size,
                                                                                fileDescriptor,
                                                                                0);
       MHD_add_response_header(result, "Content-Type", mime);

       return result;
}


Bruno Knittel
address@hidden





reply via email to

[Prev in Thread] Current Thread [Next in Thread]