Exemplo n.º 1
0
int WebServer::createResponse (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;
    struct Request *request;
    WebSession *session;
    int ret = MHD_NO;
    unsigned int i;
    
    request = (struct Request *)*ptr;
    if (NULL == request)
    {
        request = (struct Request *)calloc (1, sizeof (struct Request));
        if (NULL == request)
        {
            fprintf (stderr, "calloc error: %s\n", strerror (errno));
            return MHD_NO;
        }
        *ptr = request;
        if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
        {
            request->pp = MHD_create_post_processor (connection, 1024, &post_iterator, request);
            if (NULL == request->pp)
            {
                fprintf (stderr, "Failed to setup post processor for `%s'\n",
                         url);
                return MHD_NO; /* internal error */
            }
        }
        return MHD_YES;
    }
    if (NULL == request->session)
    {
        request->session = getSession(connection);
        if (NULL == request->session)
        {
            fprintf (stderr, "Failed to setup session for `%s'\n",
                     url);
            return MHD_NO; /* internal error */
        }
    }
    session = request->session;
    session->start = time (NULL);
    if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
    {
        /* evaluate POST data */
        MHD_post_process (request->pp,
                          upload_data,
                          *upload_data_size);
        /*
        if (0 != *upload_data_size)
        {
            *upload_data_size = 0;
            return MHD_YES;
        }
        */
        /* done with POST data, serve response */
        MHD_destroy_post_processor (request->pp);
        request->pp = NULL;
        method = MHD_HTTP_METHOD_GET; /* fake 'GET' */
        if (NULL != request->post_url)
            url = request->post_url;
        
        
        //handle data
        /* find out which page to serve */
        WebPage * page = NULL;
        for (auto it = _pages.begin(); it != _pages.end(); it++)
        {
            WebPage *current = *it;
            if (strcmp (current->getURL().c_str(), url) == 0)
            {
                page = current;
                break;
            }
        }
        
        if (page != NULL)
        {
            page->handlePost(session, upload_data, *upload_data_size);
        }
    }
    
    if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) ||
        (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) )
    {
        /* find out which page to serve */
        WebPage * page = NULL;
        for (auto it = _pages.begin(); it != _pages.end(); it++)
        {
            WebPage *current = *it;
            if (strcmp (current->getURL().c_str(), url) == 0)
            {
                page = current;
                break;
            }
        }
        
        if (page == NULL)
        {
            page = _notFoundPage;
        }
        
        std::cout << "Serving page" << std::endl;
        
        struct MHD_Response *response;
        
        std::string replyString = page->fillRequest(session);
        // return static form
        response = MHD_create_response_from_buffer (strlen (replyString.c_str()),
                                                    (void *) replyString.c_str(),
                                                    MHD_RESPMEM_MUST_COPY);
        
        if (NULL != response)
        {
            addSessionCookie(session, response);
            MHD_add_response_header (response,
                                     MHD_HTTP_HEADER_CONTENT_ENCODING,
                                     page->getMime().c_str());
            ret = MHD_queue_response (connection,
                                      MHD_HTTP_OK,
                                      response);
            MHD_destroy_response (response);
        }
        
        /*
         
         i=0;
         while ( (pages[i].url != NULL) &&
         (0 != strcmp (pages[i].url, url)) )
         i++;
         ret = pages[i].handler (pages[i].handler_cls,
         pages[i].mime,
         session, connection);
         */
        if (ret != MHD_YES)
            fprintf (stderr, "Failed to create page for `%s'\n", url);
        return ret;
    }
    /* unsupported HTTP method */
    response = MHD_create_response_from_buffer (strlen (METHOD_ERROR),
                                                (void *) METHOD_ERROR,
                                                MHD_RESPMEM_PERSISTENT);
    ret = MHD_queue_response (connection,
                              MHD_HTTP_NOT_ACCEPTABLE,
                              response);
    MHD_destroy_response (response);
    return ret;
}