libmicrohttpd
[Top][All Lists]
Advanced

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

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


From: Bruno Knittel
Subject: [libmicrohttpd] Sending file as answer: Closing connection (Stream Error)
Date: Sat, 19 May 2012 13:11:18 +0000

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]