You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm making a basic web server to serve a local network using a Raspberry Pi, and I'm having trouble serving my HTML and assets over HTTP (to initialize the website before I open a websocket in JS). Everything works over localhost, but when I open the website to the local network and try to access it over a remote PC, it only partially loads my images. The file stream is aborted (onAborted() is called) halfway through loading the image.
Code snippets are below, but this is the first thing I have gotten to work at all using tryEnd(). I tried replicating what was in this example from uWebSockets.js, it didn't work, but it also tries to set flags in HttpResponse (res.id, line 34, for example) that don't exist in the C++ version.
Server: Raspberry Pi 4B+, 8GB RAM, Raspberry Pi OS 11 (Debian Bullseye)
Client: Chrome on Windows 10
#define web_root "/path/to/web/folder"
int main() {
uWS::App app = uWS::App();
app.get("/*", [](auto * res, auto * req) {
std::cout << "http called " << req->getUrl() << "\n";
std::string path(req->getUrl());
std::string ext = "";
if(path.find('.') != std::string::npos) {
ext = path.substr(path.find_last_of('.'));
}
if(path.at(0) != '/') {
path.insert(0, "/"); //HTTPFileStream parameter convention -- every path starts with "/" (i.e. "/images/foo.png")
}
if(path.at(path.length() - 1) == '/') { //if no file specified, assume index.html
res->writeHeader("Content-Type", "text/html");
path += "index.html";
}
else if(ext == ".css") {
res->writeHeader("Content-Type", "text/css");
HTTPFileStream::stream_file(res, "/styles/main.css"); //only one css file, possibly change in the future.
return;
}
else if(ext == ".js") {
res->writeHeader("Content-Type", "text/javascript");
}
else if(ext == ".jpeg" || ext == ".jpg") {
res->writeHeader("Content-Type", "image/jpeg");
}
else if(ext == ".png") {
res->writeHeader("Content-Type", "image/png");
}
else if(ext == ".ico") {
res->writeHeader("Content-Type", "image/vnd.microsoft.icon");
}
else {
res->writeHeader("Content-Type", "text/plain");
}
if(HTTPFileStream::stream_file(res, path) == -1) {
res->writeStatus("404 not found.");
res->end("File not found.");
}
});
...
}
//HTTPFileStream::stream_file()
int stream_file(uWS::HttpResponse<false> * res, std::string relative_path) {
if(!std::filesystem::exists(web_root + relative_path)) return -1;
std::ifstream file;
int file_size = std::filesystem::file_size(web_root + relative_path);
file.open(web_root + relative_path, std::ios_base::in | std::ios_base::binary);
char * data = (char *)malloc(file_size);
file.read(data, file_size);
std::string_view data_str(data, file_size); //string_view works like a pointer to data, so we can't free() data yet
file.close();
//MUST be called before onWriteable()! Otherwise onWriteable()'s callback will never run!
std::pair<bool, bool> end = res->tryEnd(data_str, file_size, false); //std::pair<ok, done> -- done means transferred full buffer?
bool done = end.second;
if(done) {
free(data);
}
else if (!done) {
res->onWritable([res, &file, file_size, data_str](int offset) {
return res->tryEnd(data_str, file_size, false).second;
})->onAborted([res, data, relative_path]() {
std::cout << "server.exe: HTTP file stream aborted. File: " << relative_path << std::endl;
free(data);
res->close();
});
}
return 0;
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm making a basic web server to serve a local network using a Raspberry Pi, and I'm having trouble serving my HTML and assets over HTTP (to initialize the website before I open a websocket in JS). Everything works over localhost, but when I open the website to the local network and try to access it over a remote PC, it only partially loads my images. The file stream is aborted (
onAborted()
is called) halfway through loading the image.Code snippets are below, but this is the first thing I have gotten to work at all using tryEnd(). I tried replicating what was in this example from uWebSockets.js, it didn't work, but it also tries to set flags in HttpResponse (res.id, line 34, for example) that don't exist in the C++ version.
Server: Raspberry Pi 4B+, 8GB RAM, Raspberry Pi OS 11 (Debian Bullseye)
Client: Chrome on Windows 10
Beta Was this translation helpful? Give feedback.
All reactions