Advertisement
bueddl

Untitled

Sep 26th, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. // This file is part of w3c - wwwcam.
  2.  
  3. // w3c is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7.  
  8. // w3c is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12.  
  13. // You should have received a copy of the GNU General Public License
  14. // along with w3c. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // (C) Copyright 2015 by Sebastian Büttner <sebastian.buettner@iem.thm.de>
  17.  
  18. #include "application.h"
  19. #include "protocol/http/request/parser.h"
  20.  
  21. #include <iostream>
  22. #include <iomanip>
  23. #include <thread>
  24. #include <cstring>
  25. #include <algorithm>
  26. #include <sstream>
  27.  
  28. application::application(int argc, const char *argv[])
  29. {
  30. }
  31.  
  32. void application::http_recv_handler(
  33.     net::socket::tcp::child4 &sock, net::socket::buffer buffer)
  34. {
  35.     std::string expr = std::string(*buffer, buffer.size());
  36.  
  37.     protocol::http::request::request request;
  38.     protocol::http::request::parser parser(expr);
  39.    
  40.     if (!parser.parse(request)) {
  41.         std::cout << "Bad request" << std::endl;
  42.         sock.send("HTTP/1.1 400 Bad Request\r\n"
  43.                             "Server: W3Cam/1.0.0 (Unix)\r\n"
  44.                             "Connection: close\r\n"
  45.                             "Content-length: 19\r\n"
  46.                             "Content-type: text/html\r\n"
  47.                             "\r\n"
  48.                             "<h1>Bad Request</h1>");
  49.         return;
  50.     }
  51.  
  52.     const auto &header = request.header_;
  53.     if (header.connection == "Upgrade")
  54.     {
  55.         std::cout << "Upgrade Header found!" << std::endl;
  56.         if (header.upgrade == "websocket")
  57.         {
  58.             std::cout << "Key: " << header.sec_websocket.key << std::endl;
  59.             std::cout << "Version: " << header.sec_websocket.version << std::endl;
  60.             std::cout << "Protocol:";
  61.             for (auto &protocol : header.sec_websocket.protocol)
  62.                 std::cout << " " << protocol;
  63.             std::cout << std::endl;
  64.         }
  65.     } else
  66.     {
  67.         std::cout << "Normal request" << std::endl;
  68.     }
  69.  
  70.     std::stringstream response, content;
  71.     content << "<h1>Hello " << sock.endpoint().ip << "</h1>";
  72.     response << "HTTP/1.1 200 OK\r\n"
  73.                             "Server: W3Cam/1.0.0 (Unix)\r\n"
  74.                             "Connection: close\r\n"
  75.                             "Content-type: text/html\r\n"
  76.                             "Content-length: " << content.str().size() << "\r\n"
  77.                             "\r\n"
  78.                             << content.str();
  79.                        
  80.     sock.send(response.str());
  81. }
  82.  
  83. int application::run()
  84. {
  85.     net::connection_pool pool;
  86.     pool.listen(8080);
  87.    
  88.     pool.on_receive<net::socket::endpoint>() += mem_fn(this, &application::http_recv_handler);
  89.  
  90.     pool.start_accept();
  91.  
  92.     for (;;)
  93.         std::this_thread::sleep_for(std::chrono::seconds(1));
  94.  
  95.   return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement