Advertisement
Guest User

Casa_srv_2_9.cpp

a guest
Jan 6th, 2017
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. #include <cpprest/http_listener.h>
  2. #include <cpprest/json.h>
  3. #if _DEBUG
  4. #pragma comment(lib, "cpprest140d_2_9")
  5. #else
  6. #pragma comment(lib, "cpprest140_2_9")
  7. #endif
  8.  
  9. using namespace web;
  10. using namespace web::http;
  11. using namespace web::http::experimental::listener;
  12.  
  13. #include <iostream>
  14. #include <map>
  15. #include <set>
  16. #include <string>
  17. using namespace std;
  18.  
  19. #define TRACE(msg)            wcout << msg
  20. #define TRACE_ACTION(a, k, v) wcout << a << L" (" << k << L", " << v << L")\n"
  21.  
  22. map<utility::string_t, utility::string_t> dictionary;
  23.  
  24. /* handlers implementation */
  25. void handle_get(http_request request)
  26. {
  27.     TRACE(L"\nhandle GET\n");
  28.  
  29.     json::value answer;
  30.  
  31.     for (auto const & p : dictionary)
  32.     {
  33.         answer[p.first] = json::value(p.second);
  34.     }
  35.  
  36.     request.reply(status_codes::OK, answer);
  37. }
  38.  
  39. /* for PUT, POST, DEL below */
  40. void handle_request(http_request request,
  41.     function<void(json::value &, json::value &)> action)
  42. {
  43.     json::value answer;
  44.  
  45.     request
  46.         .extract_json()
  47.         .then([&answer, &action](pplx::task<json::value> task) {
  48.         try
  49.         {
  50.             auto & jvalue = task.get();
  51.  
  52.             if (!jvalue.is_null())
  53.             {
  54.                 action(jvalue, answer);
  55.             }
  56.         }
  57.         catch (http_exception const & e)
  58.         {
  59.             wcout << e.what() << endl;
  60.         }
  61.     })
  62.         .wait();
  63.  
  64.     request.reply(status_codes::OK, answer);
  65. }
  66.  
  67. void handle_post(http_request request)
  68. {
  69.     TRACE("\nhandle POST\n");
  70.  
  71.     handle_request(
  72.         request,
  73.         [](json::value & jvalue, json::value & answer)
  74.     {
  75.         for (auto const & e : jvalue.as_array())
  76.         {
  77.             if (e.is_string())
  78.             {
  79.                 auto key = e.as_string();
  80.  
  81.                 auto pos = dictionary.find(key);
  82.                 if (pos == dictionary.end())
  83.                 {
  84.                     answer[key] = json::value(L"<nil>");
  85.                 }
  86.                 else
  87.                 {
  88.                     answer[pos->first] = json::value(pos->second);
  89.                 }
  90.             }
  91.         }
  92.     }
  93.     );
  94. }
  95.  
  96. void handle_put(http_request request)
  97. {
  98.     TRACE("\nhandle PUT\n");
  99.  
  100.     handle_request(
  101.         request,
  102.         [](json::value & jvalue, json::value & answer)
  103.     {
  104.         for (auto const & e : jvalue.as_object())
  105.         {
  106.             if (e.second.is_string())
  107.             {
  108.                 auto key = e.first;
  109.                 auto value = e.second.as_string();
  110.  
  111.                 if (dictionary.find(key) == dictionary.end())
  112.                 {
  113.                     TRACE_ACTION(L"added", key, value);
  114.                     answer[key] = json::value(L"<put>");
  115.                 }
  116.                 else
  117.                 {
  118.                     TRACE_ACTION(L"updated", key, value);
  119.                     answer[key] = json::value(L"<updated>");
  120.                 }
  121.  
  122.                 dictionary[key] = value;
  123.             }
  124.         }
  125.     }
  126.     );
  127. }
  128.  
  129. void handle_del(http_request request)
  130. {
  131.     TRACE("\nhandle DEL\n");
  132.  
  133.     handle_request(
  134.         request,
  135.         [](json::value & jvalue, json::value & answer)
  136.     {
  137.         set<utility::string_t> keys;
  138.         for (auto const & e : jvalue.as_array())
  139.         {
  140.             if (e.is_string())
  141.             {
  142.                 auto key = e.as_string();
  143.  
  144.                 auto pos = dictionary.find(key);
  145.                 if (pos == dictionary.end())
  146.                 {
  147.                     answer[key] = json::value(L"<failed>");
  148.                 }
  149.                 else
  150.                 {
  151.                     TRACE_ACTION(L"deleted", pos->first, pos->second);
  152.                     answer[key] = json::value(L"<deleted>");
  153.                     keys.insert(key);
  154.                 }
  155.             }
  156.         }
  157.  
  158.         for (auto const & key : keys)
  159.             dictionary.erase(key);
  160.     }
  161.     );
  162. }
  163.  
  164. int main()
  165. {
  166.     http_listener listener(L"http://localhost/restdemo");
  167.  
  168.     listener.support(methods::GET, handle_get);
  169.     listener.support(methods::POST, handle_post);
  170.     listener.support(methods::PUT, handle_put);
  171.     listener.support(methods::DEL, handle_del);
  172.  
  173.     try
  174.     {
  175.         listener
  176.             .open()
  177.             .then([&listener]() {TRACE(L"\nstarting to listen\n"); })
  178.             .wait();
  179.  
  180.         while (true);
  181.     }
  182.     catch (exception const & e)
  183.     {
  184.         wcout << e.what() << endl;
  185.     }
  186.  
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement