forked from RPi-Distro/pi-gen
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
webserver: Add arbitary file upload support (#139)
This also supports automatically extracting .zip and .tar.gz files.
- Loading branch information
1 parent
642c3ff
commit 0d56919
Showing
8 changed files
with
314 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/*----------------------------------------------------------------------------*/ | ||
/* Copyright (c) 2019 FIRST. All Rights Reserved. */ | ||
/* Open Source Software - may be modified and shared by FRC teams. The code */ | ||
/* must be accompanied by the FIRST BSD license file in the root directory of */ | ||
/* the project. */ | ||
/*----------------------------------------------------------------------------*/ | ||
|
||
#include "UploadHelper.h" | ||
|
||
#include <fcntl.h> | ||
#include <stdlib.h> | ||
#include <sys/stat.h> | ||
#include <unistd.h> | ||
|
||
#include <wpi/raw_ostream.h> | ||
|
||
UploadHelper::UploadHelper(UploadHelper&& oth) | ||
: m_filename{std::move(oth.m_filename)}, | ||
m_fd{oth.m_fd}, | ||
m_text{oth.m_text} { | ||
oth.m_fd = -1; | ||
} | ||
|
||
UploadHelper& UploadHelper::operator=(UploadHelper&& oth) { | ||
m_filename = std::move(oth.m_filename); | ||
m_fd = oth.m_fd; | ||
oth.m_fd = -1; | ||
m_text = oth.m_text; | ||
return *this; | ||
} | ||
|
||
bool UploadHelper::Open(wpi::StringRef filename, bool text, | ||
std::function<void(wpi::StringRef)> onFail) { | ||
m_text = text; | ||
m_hasEol = true; | ||
m_filename = filename; | ||
// make it a C string | ||
m_filename.push_back(0); | ||
m_filename.pop_back(); | ||
|
||
m_fd = mkstemp(m_filename.data()); | ||
if (m_fd < 0) { | ||
wpi::SmallString<64> msg; | ||
msg = "could not open temporary file: "; | ||
msg += std::strerror(errno); | ||
onFail(msg); | ||
} | ||
return m_fd >= 0; | ||
} | ||
|
||
void UploadHelper::Write(wpi::ArrayRef<uint8_t> contents) { | ||
if (m_fd < 0) return; | ||
// write contents | ||
wpi::raw_fd_ostream out(m_fd, false); | ||
if (m_text) { | ||
wpi::StringRef str(reinterpret_cast<const char*>(contents.data()), | ||
contents.size()); | ||
// convert any Windows EOL to Unix | ||
for (;;) { | ||
size_t idx = str.find("\r\n"); | ||
if (idx == wpi::StringRef::npos) break; | ||
out << str.slice(0, idx) << '\n'; | ||
str = str.slice(idx + 2, wpi::StringRef::npos); | ||
} | ||
out << str; | ||
m_hasEol == str.empty() || str.back() == '\n'; | ||
} else { | ||
out << contents; | ||
} | ||
} | ||
|
||
void UploadHelper::Close() { | ||
if (m_fd < 0) return; | ||
// ensure text file ends with EOL | ||
if (m_text && !m_hasEol) { | ||
wpi::raw_fd_ostream out(m_fd, false); | ||
out << '\n'; | ||
} | ||
::close(m_fd); | ||
m_fd = -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/*----------------------------------------------------------------------------*/ | ||
/* Copyright (c) 2019 FIRST. All Rights Reserved. */ | ||
/* Open Source Software - may be modified and shared by FRC teams. The code */ | ||
/* must be accompanied by the FIRST BSD license file in the root directory of */ | ||
/* the project. */ | ||
/*----------------------------------------------------------------------------*/ | ||
|
||
#ifndef RPICONFIGSERVER_UPLOADHELPER_H_ | ||
#define RPICONFIGSERVER_UPLOADHELPER_H_ | ||
|
||
#include <stdint.h> | ||
|
||
#include <functional> | ||
|
||
#include <wpi/ArrayRef.h> | ||
#include <wpi/SmallString.h> | ||
#include <wpi/StringRef.h> | ||
|
||
class UploadHelper { | ||
public: | ||
UploadHelper() = default; | ||
~UploadHelper() { Close(); } | ||
|
||
UploadHelper(const UploadHelper&) = delete; | ||
UploadHelper& operator=(const UploadHelper&) = delete; | ||
|
||
UploadHelper(UploadHelper&& oth); | ||
UploadHelper& operator=(UploadHelper&& oth); | ||
|
||
explicit operator bool() const { return m_fd != -1; } | ||
|
||
const char* GetFilename() { return m_filename.c_str(); } | ||
int GetFD() const { return m_fd; } | ||
|
||
bool Open(wpi::StringRef filename, bool text, | ||
std::function<void(wpi::StringRef)> onFail); | ||
void Write(wpi::ArrayRef<uint8_t> contents); | ||
void Close(); | ||
|
||
private: | ||
wpi::SmallString<128> m_filename; | ||
int m_fd = -1; | ||
bool m_text; | ||
bool m_hasEol; | ||
}; | ||
|
||
#endif // RPICONFIGSERVER_UPLOADHELPER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.