Multiple Simultaneous requests

Apr 29, 2015 at 11:38 AM
Edited Apr 29, 2015 at 11:39 AM
I have created a mini lightweight web server using Casablanca SDKs. This is the source code.
int _tmain(int argc, _TCHAR* argv[])
{
    uri_builder uri(L"http://*:2001/");
    http_listener listener(uri.to_uri());
    ____utility::string_t strPost = L"";____

    std::cout << "Welcome to Casablanca" << std::endl;

    CheckOSVersion();

    listener.support(methods::GET, [](http_request req)
    {
        HKEY hKey;
        LONG lRes = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\ProjectName", 0, KEY_READ, &hKey);

        bool bExistsAndSuccess (lRes == ERROR_SUCCESS);
        bool bDoesNotExistsSpecifically (lRes == ERROR_FILE_NOT_FOUND);

        std::wstring strValueOfBinDir;
        GetStringRegKey(hKey, L"Encryption", strValueOfBinDir, L"1");

        GIDSdkWriteXML();

        http_response response (status_codes::OK);

        response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
        __response.set_body(L"Welcome to Casablanca", U("text/html"));
__
        req.set_request_uri(L"requestpath");
        req.reply(response);
    });

    listener.support(methods::PUT,[](http_request req)
    {
        std::cout << "Serving PUT" << std::endl;
        req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is a POST Request.)</body></html>"), 
            U("text/html"));
    });

__  listener.support(methods::POST,[](http_request req)
    {
        std::cout << "Serving POST" << std::endl;
        req.extract_string(true).then([req](utility::string_t body)
        {
            std::wcout << body << std::endl;
        });
    }); 
__
    try
    {
        listener.open().wait();
        fgetc(stdin);
        listener.close().wait();
    }
    catch(const http_exception &e)
    {
        const auto &msg = e.what();
        const auto &error_code = e.error_code();
        //listener.close().wait();
    }

    listener.close().wait();
}
If you take a look at portion highlighted in bold, the POST and GET request. Now this server receives receives request from browser addins and a web service, that resides in a remote server. Now I want to send different GET request string to the agent and the server.

In order to accomplish this, my plain is to send a different POST request from the browser addins and the web service .

std::wcout << body << std::endl;

Store that request in a string and the based using the conditional statement, based on value of the string, set the response

Example
If (StrPost == "1")
            response.set_body(L"Welcome to Casablanca - browser bar", U("text/html"));
If (StrPost == "2")
            response.set_body(L"Welcome to Casablanca - Web Service", U("text/html"));
But there is a problem, if I declare a local varialbe within the main function (right before url builder), and use iut this way.

std::cout << "Serving POST" << std::endl;
    req.extract_string(true).then([req](utility::string_t body)
    {
                                                  StrPost = body
        std::wcout << body << std::endl;                    });
I get this error.
Error   3   error C3493: 'strPost cannot be implicitly captured because no default capture mode has been specified
How can I overcome this issue to accomplish my task?
Apr 29, 2015 at 5:35 PM
Hi maverick786us,

You need to capture the strPost variable in order to be able to access it inside your lambda function. The compiler error message is accurate. From what you are mentioning it sounds like you want to capture it by reference. In general I'd be careful about modifying global variables in your request handlers.

Steve
Apr 30, 2015 at 5:08 AM
stevetgates wrote:
Hi maverick786us,

You need to capture the strPost variable in order to be able to access it inside your lambda function. The compiler error message is accurate. From what you are mentioning it sounds like you want to capture it by reference. In general I'd be careful about modifying global variables in your request handlers.

Steve
If I declare it inside Lambada of POST, then its access will remain inside POST code blocks, I need to access its value in GET code block too, thats why I declared it a locally within the function, so that its available throughout the function.