Commit 0307fae8 authored by Ciarán Ó Rourke's avatar Ciarán Ó Rourke
Browse files

Attempts at get request

parent 99e5e4b6
/*
* Copyright (c) Facebook, Inc. and its affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "get_request_handler.h"
#include <cstdio>
#include <stdlib.h>
#include <folly/FileUtil.h>
#include <folly/executors/GlobalExecutor.h>
#include <folly/io/async/EventBaseManager.h>
#include <proxygen/httpserver/RequestHandler.h>
#include <proxygen/httpserver/ResponseBuilder.h>
#include "../S3_header.h"
#include "../fiphobo/FiPhoExceptions.h"
namespace fiphoboserver {
void StaticHandler::onRequest(
std::unique_ptr<proxygen::HTTPMessage> headers) noexcept
{
if (headers->getMethod() != proxygen::HTTPMethod::GET) {
proxygen::ResponseBuilder(downstream_)
.status(400, "Bad method")
.body("Only GET is supported")
.sendWithEOM();
return;
}
#ifdef DEBUG
std::cout << "Got the following GET request: \n";
std::string path = headers->getPath();
std::cout << path << '\n';
headers->getHeaders().forEach(
[&](const std::string& header, const std::string& val) {
std::cout << header << ": " << val << '\n';
});
#endif
s3_header.setHeaders(std::move(headers));
try {
#ifdef DEBUG
std::cout << "Bucket: " << s3_header.getBucket() << '\n';
std::cout << "Key: " << s3_header.getKey() << '\n';
std::cout << std::endl;
#endif
phobos.setBucketName(s3_header.getBucket());
#ifdef DEBUG
std::cout << "Send get to phobos layer" << std::endl;
#endif
phobos.startGetOperation(s3_header.getKey());
}
catch (const std::system_error& ex) {
proxygen::ResponseBuilder(downstream_)
.status(404, "Not Found")
.body(folly::to<std::string>(
"Could not find ", headers->getPathAsStringPiece(),
" ex=", folly::exceptionStr(ex)))
.sendWithEOM();
return;
}
catch (const fiphoexceptions::PhobosException& ex) {
proxygen::ResponseBuilder(downstream_)
.status(409, "Conflict")
.body(ex.what())
.sendWithEOM();
return;
}
proxygen::ResponseBuilder(downstream_).status(200, "Ok").send();
readFileScheduled_ = true;
folly::getCPUExecutor()->add(
std::bind(&StaticHandler::readFile, this,
folly::EventBaseManager::get()->getEventBase()));
}
void StaticHandler::readFile(folly::EventBase* evb)
{
#ifdef DEBUG
std::cout << "Getting data from FIFO... " << std::endl;
#endif
std::unique_ptr<folly::IOBuf> buf;
try
{
buf = phobos.getDataFromFIFO(64);
}
catch(fiphoexceptions::FIFOException& fifoExcp)
{
proxygen::ResponseBuilder(downstream_)
.status(409, "Conflict")
.body(fifoExcp.what())
.sendWithEOM();
return;
}
evb->runInEventBaseThread([this, body=buf.move()] () mutable {
ResponseBuilder(downstream_)
.body(std::move(body))
.send();
});
}
}
void StaticHandler::onBody(std::unique_ptr<folly::IOBuf> /*body*/) noexcept
{
// ignore, only support GET
}
void StaticHandler::onEOM() noexcept {}
void StaticHandler::onUpgrade(proxygen::UpgradeProtocol /*protocol*/) noexcept
{
// handler doesn't support upgrades
}
void StaticHandler::requestComplete() noexcept
{
delete this;
}
void StaticHandler::onError(proxygen::ProxygenError /*err*/) noexcept
{
delete this;
}
} // namespace fiphoboserver
/*
* Copyright (c) Facebook, Inc. and its affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <folly/File.h>
#include <folly/Memory.h>
#include <proxygen/httpserver/RequestHandler.h>
#include "../S3_header.h"
#include "../fiphobo/fiphobo.h"
namespace proxygen {
class ResponseHandler;
}
namespace fiphoboserver {
class StaticHandler : public proxygen::RequestHandler {
public:
void onRequest(
std::unique_ptr<proxygen::HTTPMessage> headers) noexcept override;
void StaticHandler::readFile(folly::EventBase* evb)
void onBody(std::unique_ptr<folly::IOBuf> body) noexcept override;
void onEOM() noexcept override;
void onUpgrade(proxygen::UpgradeProtocol proto) noexcept override;
void requestComplete() noexcept override;
void onError(proxygen::ProxygenError err) noexcept override;
private:
S3_header s3_header;
FiPhobo phobos;
};
} // namespace fiphoboserver
......@@ -42,7 +42,8 @@ class HandlerFactory : public proxygen::RequestHandlerFactory {
void onServerStop() noexcept override {}
proxygen::RequestHandler* onRequest(
proxygen::RequestHandler*, proxygen::HTTPMessage* headers) noexcept override
proxygen::RequestHandler*,
proxygen::HTTPMessage* headers) noexcept override
{
if (headers->getMethod() == proxygen::HTTPMethod::GET) {
return new fiphoboserver::StaticHandler;
......@@ -89,6 +90,9 @@ int main(int argc, char* argv[])
server.bind(IPs);
// Start HTTPServer mainloop in a separate thread
#ifdef DEBUG
std::cout << "main: Starting HTTPServer thread" << std::endl;
#endif
std::thread t([&]() { server.start(); });
t.join();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment