2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2018-08-20 13:11:39 -07:00
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
|
|
#include "wpi/EventLoopRunner.h"
|
|
|
|
|
#include "wpi/HttpServerConnection.h"
|
|
|
|
|
#include "wpi/UrlParser.h"
|
|
|
|
|
#include "wpi/raw_ostream.h"
|
|
|
|
|
#include "wpi/uv/Loop.h"
|
|
|
|
|
#include "wpi/uv/Tcp.h"
|
|
|
|
|
|
|
|
|
|
namespace uv = wpi::uv;
|
|
|
|
|
|
|
|
|
|
class MyHttpServerConnection : public wpi::HttpServerConnection {
|
|
|
|
|
public:
|
|
|
|
|
explicit MyHttpServerConnection(std::shared_ptr<uv::Stream> stream)
|
|
|
|
|
: HttpServerConnection(stream) {}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void ProcessRequest() override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void MyHttpServerConnection::ProcessRequest() {
|
|
|
|
|
wpi::errs() << "HTTP request: '" << m_request.GetUrl() << "'\n";
|
|
|
|
|
wpi::UrlParser url{m_request.GetUrl(),
|
|
|
|
|
m_request.GetMethod() == wpi::HTTP_CONNECT};
|
|
|
|
|
if (!url.IsValid()) {
|
|
|
|
|
// failed to parse URL
|
|
|
|
|
SendError(400);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi::StringRef path;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (url.HasPath()) {
|
|
|
|
|
path = url.GetPath();
|
|
|
|
|
}
|
2018-08-20 13:11:39 -07:00
|
|
|
wpi::errs() << "path: \"" << path << "\"\n";
|
|
|
|
|
|
|
|
|
|
wpi::StringRef query;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (url.HasQuery()) {
|
|
|
|
|
query = url.GetQuery();
|
|
|
|
|
}
|
2018-08-20 13:11:39 -07:00
|
|
|
wpi::errs() << "query: \"" << query << "\"\n";
|
|
|
|
|
|
|
|
|
|
const bool isGET = m_request.GetMethod() == wpi::HTTP_GET;
|
|
|
|
|
if (isGET && path.equals("/")) {
|
|
|
|
|
// build HTML root page
|
|
|
|
|
wpi::SmallString<256> buf;
|
|
|
|
|
wpi::raw_svector_ostream os{buf};
|
|
|
|
|
os << "<html><head><title>WebServer Example</title></head>";
|
|
|
|
|
os << "<body><p>This is an example root page from the webserver.";
|
|
|
|
|
os << "</body></html>";
|
|
|
|
|
SendResponse(200, "OK", "text/html", os.str());
|
|
|
|
|
} else {
|
|
|
|
|
SendError(404, "Resource not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
// Kick off the event loop on a separate thread
|
|
|
|
|
wpi::EventLoopRunner loop;
|
|
|
|
|
loop.ExecAsync([](uv::Loop& loop) {
|
|
|
|
|
auto tcp = uv::Tcp::Create(loop);
|
|
|
|
|
|
|
|
|
|
// bind to listen address and port
|
|
|
|
|
tcp->Bind("", 8080);
|
|
|
|
|
|
|
|
|
|
// when we get a connection, accept it and start reading
|
|
|
|
|
tcp->connection.connect([srv = tcp.get()] {
|
|
|
|
|
auto tcp = srv->Accept();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!tcp) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-08-20 13:11:39 -07:00
|
|
|
wpi::errs() << "Got a connection\n";
|
|
|
|
|
auto conn = std::make_shared<MyHttpServerConnection>(tcp);
|
|
|
|
|
tcp->SetData(conn);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// start listening for incoming connections
|
|
|
|
|
tcp->Listen();
|
|
|
|
|
|
|
|
|
|
wpi::errs() << "Listening on port 8080\n";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// wait for a keypress to terminate
|
|
|
|
|
std::getchar();
|
|
|
|
|
}
|