Advertisement
PaulPaulAga

Untitled

Apr 20th, 2020
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. void ServeRequest(const HttpRequest &req, ostream &os)
  2.     {
  3.         if (req.method == "POST")
  4.         {
  5.             if (req.path == "/add_user")
  6.             {
  7.                 comments_.emplace_back();
  8.                 auto response = to_string(comments_.size() - 1);
  9.                 os << "HTTP/1.1 200 OK\n" << "Content-Length: " << response.size() << "\n" << "\n"
  10.                    << response;
  11.             }
  12.             else if (req.path == "/add_comment")
  13.             {
  14.                 auto[user_id, comment] = ParseIdAndContent(req.body);
  15.  
  16.                 if (!last_comment || last_comment->user_id != user_id)
  17.                 {
  18.                     last_comment = LastCommentInfo{user_id, 1};
  19.                 }
  20.                 else if (++last_comment->consecutive_count > 3)
  21.                 {
  22.                     banned_users.insert(user_id);
  23.                 }
  24.  
  25.                 if (banned_users.count(user_id) == 0)
  26.                 {
  27.                     comments_[user_id].push_back(string(comment));
  28.                     os << "HTTP/1.1 200 OK\n\n";
  29.                 }
  30.                 else
  31.                 {
  32.                     os << "HTTP/1.1 302 Found\n\n"
  33.                           "Location: /captcha\n"
  34.                           "\n";
  35.                 }
  36.             }
  37.             else if (req.path == "/checkcaptcha")
  38.             {
  39.                 if (auto[id, response] = ParseIdAndContent(req.body); response == "42")
  40.                 {
  41.                     banned_users.erase(id);
  42.                     if (last_comment && last_comment->user_id == id)
  43.                     {
  44.                         last_comment.reset();
  45.                     }
  46.                     os << "HTTP/1.1 200 OK\n\n";
  47.                 }
  48.             }
  49.             else
  50.             {
  51.                 os << "HTTP/1.1 404 Not found\n\n";
  52.             }
  53.         }
  54.         else if (req.method == "GET")
  55.         {
  56.             if (req.path == "/user_comments")
  57.             {
  58.                 auto user_id = FromString<size_t>(req.get_params.at("user_id"));
  59.                 string response;
  60.                 for (const string &c : comments_[user_id])
  61.                 {
  62.                     response += c + '\n';
  63.                 }
  64.  
  65.                 os << "HTTP/1.1 200 OK\n" << "Content-Length: " << response.size() << response;
  66.             }
  67.             else if (req.path == "/captcha")
  68.             {
  69.                 os << "HTTP/1.1 200 OK\n" << "Content-Length: 80\n" << "\n"
  70.                    << "What's the answer for The Ultimate Question of Life, the Universe, and Everything?";
  71.             }
  72.             else
  73.             {
  74.                 os << "HTTP/1.1 404 Not found\n\n";
  75.             }
  76.         }
  77.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement