Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpinet] Add simple web server #7527

Merged
merged 14 commits into from
Dec 14, 2024
Prev Previous commit
Next Next commit
WIP Windows fix
  • Loading branch information
PeterJohnson committed Dec 14, 2024
commit 8b96b17fe06ad711e1aa6dbd2136021f912c9eec
27 changes: 27 additions & 0 deletions wpinet/src/main/native/cpp/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "wpinet/WebServer.h"

#include <unistd.h>

Check failure on line 7 in wpinet/src/main/native/cpp/WebServer.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

Cannot open include file: 'unistd.h': No such file or directory

#include <memory>
#include <string>
Expand All @@ -12,6 +12,7 @@

#include <fmt/format.h>
#include <wpi/DenseMap.h>
#include <wpi/MemoryBuffer.h>
#include <wpi/Signal.h>
#include <wpi/StringMap.h>
#include <wpi/fs.h>
Expand Down Expand Up @@ -46,7 +47,11 @@

std::string m_path;
};
} // namespace

#if 1 //def _WIN32
#else
namespace {
class SendfileReq : public uv::RequestImpl<SendfileReq, uv_fs_t> {
public:
SendfileReq(uv_file out, uv_file in, int64_t inOffset, size_t len)
Expand Down Expand Up @@ -109,6 +114,7 @@
req->Keep();
}
}
#endif

static std::string_view GetMimeType(std::string_view ext) {
static const wpi::StringMap<std::string> map{
Expand Down Expand Up @@ -145,6 +151,26 @@
std::string_view contentType,
fs::path filename,
std::string_view extraHeader) {
#if 1 // def _WIN32
auto membuf = wpi::MemoryBuffer::GetFile(filename.string());
if (!membuf) {
SendError(404);
return;
}

wpi::SmallVector<uv::Buffer, 4> toSend;
wpi::raw_uv_ostream os{toSend, 4096};
BuildHeader(os, code, codeText, contentType, (*membuf)->size(), extraHeader);
SendData(os.bufs(), false);
m_stream.Write(
{{(*membuf)->GetBuffer()}},
[closeAfter = !m_keepAlive, stream = &m_stream,
membuf = std::shared_ptr{std::move(*membuf)}](auto, uv::Error) {
if (closeAfter) {
stream->Close();
}
});
#else
// open file
std::error_code ec;
auto infile = fs::OpenFileForRead(filename, ec);
Expand Down Expand Up @@ -193,6 +219,7 @@
stream->SetBlocking(false);
}
});
#endif
}

void MyHttpConnection::ProcessRequest() {
Expand Down
Loading