Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This file is part of w3c - wwwcam.
- // w3c is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- // w3c is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- // You should have received a copy of the GNU General Public License
- // along with w3c. If not, see <http://www.gnu.org/licenses/>.
- //
- // (C) Copyright 2015 by Sebastian Büttner <sebastian.buettner@iem.thm.de>
- #include "application.h"
- #include "protocol/http/request/parser.h"
- #include <iostream>
- #include <iomanip>
- #include <thread>
- #include <cstring>
- #include <algorithm>
- #include <sstream>
- application::application(int argc, const char *argv[])
- {
- }
- void application::http_recv_handler(
- net::socket::tcp::child4 &sock, net::socket::buffer buffer)
- {
- std::string expr = std::string(*buffer, buffer.size());
- protocol::http::request::request request;
- protocol::http::request::parser parser(expr);
- if (!parser.parse(request)) {
- std::cout << "Bad request" << std::endl;
- sock.send("HTTP/1.1 400 Bad Request\r\n"
- "Server: W3Cam/1.0.0 (Unix)\r\n"
- "Connection: close\r\n"
- "Content-length: 19\r\n"
- "Content-type: text/html\r\n"
- "\r\n"
- "<h1>Bad Request</h1>");
- return;
- }
- const auto &header = request.header_;
- if (header.connection == "Upgrade")
- {
- std::cout << "Upgrade Header found!" << std::endl;
- if (header.upgrade == "websocket")
- {
- std::cout << "Key: " << header.sec_websocket.key << std::endl;
- std::cout << "Version: " << header.sec_websocket.version << std::endl;
- std::cout << "Protocol:";
- for (auto &protocol : header.sec_websocket.protocol)
- std::cout << " " << protocol;
- std::cout << std::endl;
- }
- } else
- {
- std::cout << "Normal request" << std::endl;
- }
- std::stringstream response, content;
- content << "<h1>Hello " << sock.endpoint().ip << "</h1>";
- response << "HTTP/1.1 200 OK\r\n"
- "Server: W3Cam/1.0.0 (Unix)\r\n"
- "Connection: close\r\n"
- "Content-type: text/html\r\n"
- "Content-length: " << content.str().size() << "\r\n"
- "\r\n"
- << content.str();
- sock.send(response.str());
- }
- int application::run()
- {
- net::connection_pool pool;
- pool.listen(8080);
- pool.on_receive<net::socket::endpoint>() += mem_fn(this, &application::http_recv_handler);
- pool.start_accept();
- for (;;)
- std::this_thread::sleep_for(std::chrono::seconds(1));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement