blob: 4398e047a3a378f6004c6311ac2810025633850a [file] [log] [blame]
Austin Schuh24adb6b2015-09-06 17:37:40 -07001// Copyright (c) 2013, Matt Godbolt
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// Redistributions of source code must retain the above copyright notice, this
8// list of conditions and the following disclaimer.
9//
10// Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24// POSSIBILITY OF SUCH DAMAGE.
25
26#include "seasocks/PageHandler.h"
27#include "seasocks/PrintfLogger.h"
28#include "seasocks/Server.h"
29#include "seasocks/StringUtil.h"
30
31#include <cstring>
32#include <iostream>
33#include <memory>
34#include <set>
35#include <sstream>
36#include <string>
37
38using namespace seasocks;
39
40class MyPageHandler: public PageHandler {
41public:
42 virtual std::shared_ptr<Response> handle(const Request& request) {
43 if (request.verb() == Request::Post) {
44 std::string content(request.content(), request.content() + request.contentLength());
45 return Response::textResponse("Thanks for the post. You said: " + content);
46 }
47 if (request.verb() != Request::Get) return Response::unhandled();
48 std::ostringstream ostr;
49 ostr << "<html><head><title>seasocks example</title></head>"
50 "<body>Hello, " << request.credentials()->attributes["fullName"] << "! You asked for " << request.getRequestUri()
51 << " and your ip is " << request.getRemoteAddress().sin_addr.s_addr
52 << " and a random number is " << rand()
53 << "<form action=/post method=post><input type=text name=value1></input><br>"
54 << "<input type=submit value=Submit></input></form>"
55 << "</body></html>";
56 return Response::htmlResponse(ostr.str());
57 }
58};
59
60int main(int argc, const char* argv[]) {
61 std::shared_ptr<Logger> logger(new PrintfLogger(Logger::INFO));
62
63 Server server(logger);
64 server.addPageHandler(std::make_shared<MyPageHandler>());
65
66 server.serve("src/ws_test_web", 9090);
67 return 0;
68}