-
Notifications
You must be signed in to change notification settings - Fork 844
use an explicit 'mimetype' argument for getclipboard #11632
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
Closed
Closed
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
dfa9974
use an explicit 'mimetype' argument for getclipboard
caolanm b6e8316
foo
caolanm 8061004
more
caolanm 6cb7318
another step
caolanm d2bb69d
nope
caolanm 1108e2a
store case
caolanm 2245398
works
caolanm 25c9d75
is always filename
caolanm 1d46fb0
remove clip file
caolanm 9ab2bac
better
caolanm a1b6765
more
caolanm 21faad4
try remove
caolanm dd5bf45
try remove
caolanm dcde71e
cleanup clipboard files when no longer wanted
caolanm 36e7f14
lost setting this recently
caolanm d9da417
sneak up on it
caolanm edd03b7
start message is thrown away
caolanm 3563a43
can be const
caolanm 4de6b48
bundle checks out of the way
caolanm cb0c749
handle Expect
caolanm 54d3732
finish refactor
caolanm b400148
drop a variable
caolanm 53bba98
streaming condition
caolanm 1c23eba
interim
caolanm 1146c2c
move this, merge with checkChunks IMO
caolanm 57038cf
foo
caolanm 1d4ee21
reuse
caolanm 282c462
finishedMessage
caolanm c272d9a
socketEraseConsumedBytes
caolanm a7fbc0c
use streamed message
caolanm 18ff956
compiles
caolanm 5652f51
works
caolanm cc6db8a
just have one of these
caolanm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -1487,6 +1487,8 @@ class Session final : public ProtocolHandlerInterface | |
{ | ||
LOG_ERR("Error while invoking onFinished client callback: " << exc.what()); | ||
} | ||
|
||
_onFinished = nullptr; | ||
} | ||
|
||
/// Set up a new request and response. | ||
|
@@ -1891,12 +1893,38 @@ class ServerSession final : public ProtocolHandlerInterface | |
/// regardless of the reason (error, timeout, completion). | ||
void setFinishedHandler(FinishedCallback onFinished) { _onFinished = std::move(onFinished); } | ||
|
||
std::shared_ptr<ServerSession> shared_from_this() | ||
{ | ||
return std::static_pointer_cast<ServerSession>(ProtocolHandlerInterface::shared_from_this()); | ||
} | ||
|
||
void callOnFinished() | ||
{ | ||
fprintf(stderr, "ServerSession onFinished\n"); | ||
|
||
if (!_onFinished) | ||
return; | ||
|
||
LOG_TRC("onFinished calling client"); | ||
std::shared_ptr<ServerSession> self = shared_from_this(); | ||
try | ||
{ | ||
_onFinished(self); | ||
} | ||
catch (const std::exception& exc) | ||
{ | ||
LOG_ERR("Error while invoking onFinished client callback: " << exc.what()); | ||
} | ||
} | ||
|
||
using ResponseHeaders = http::Header::Container; | ||
|
||
/// Start an asynchronous upload from a file. | ||
/// Return true when it dispatches the socket to the SocketPoll. | ||
/// Note: when reusing this ServerSession, it is assumed that the socket | ||
/// is already added to the SocketPoll on a previous call (do not | ||
/// use multiple SocketPoll instances on the same ServerSession). | ||
bool asyncUpload(std::string fromFile, std::string mimeType, int start, int end, bool startIsSuffix, http::StatusCode statusCode = http::StatusCode::OK) | ||
bool asyncUpload(std::string fromFile, ResponseHeaders responseHeaders, int start, int end, bool startIsSuffix, http::StatusCode statusCode = http::StatusCode::OK) | ||
{ | ||
_start = start; | ||
_end = end; | ||
|
@@ -1923,33 +1951,34 @@ class ServerSession final : public ProtocolHandlerInterface | |
} | ||
|
||
_size = sb.st_size; | ||
_data = std::move(fromFile); | ||
_mimeType = std::move(mimeType); | ||
_filename = std::move(fromFile); | ||
_responseHeaders = std::move(responseHeaders); | ||
LOG_ASSERT_MSG(!getMimeType().empty(), "Missing Content-Type"); | ||
|
||
int firstBytePos = getStart(); | ||
|
||
if (lseek(_fd, firstBytePos, SEEK_SET) < 0) | ||
LOG_SYS("Failed to seek " << _data << " to " << firstBytePos << " because: " << strerror(errno)); | ||
LOG_SYS("Failed to seek " << _filename << " to " << firstBytePos << " because: " << strerror(errno)); | ||
else | ||
_pos = firstBytePos; | ||
|
||
return true; | ||
} | ||
|
||
/// Start an asynchronous upload of a whole file | ||
bool asyncUpload(std::string fromFile, std::string mimeType) | ||
bool asyncUpload(std::string fromFile, ResponseHeaders responseHeaders) | ||
{ | ||
return asyncUpload(std::move(fromFile), std::move(mimeType), 0, -1, false); | ||
return asyncUpload(std::move(fromFile), std::move(responseHeaders), 0, -1, false); | ||
} | ||
|
||
/// Start a partial asynchronous upload from a file based on the contents of a "Range" header | ||
bool asyncUpload(std::string fromFile, std::string mimeType, const std::string_view rangeHeader) | ||
bool asyncUpload(std::string fromFile, ResponseHeaders responseHeaders, const std::string_view rangeHeader) | ||
{ | ||
const size_t equalsPos = rangeHeader.find('='); | ||
if (equalsPos == std::string::npos) return asyncUpload(std::move(fromFile), std::move(mimeType)); | ||
if (equalsPos == std::string::npos) return asyncUpload(std::move(fromFile), std::move(responseHeaders)); | ||
|
||
const std::string_view unit = rangeHeader.substr(0, equalsPos); | ||
if (unit != "bytes") return asyncUpload(std::move(fromFile), std::move(mimeType)); | ||
if (unit != "bytes") return asyncUpload(std::move(fromFile), std::move(responseHeaders)); | ||
|
||
const std::string_view range = rangeHeader.substr(equalsPos + 1); | ||
|
||
|
@@ -1975,7 +2004,7 @@ class ServerSession final : public ProtocolHandlerInterface | |
catch (std::invalid_argument&) {} | ||
catch (std::out_of_range&) {} | ||
|
||
return asyncUpload(std::move(fromFile), std::move(mimeType), start, end, | ||
return asyncUpload(std::move(fromFile), std::move(responseHeaders), start, end, | ||
startIsSuffix, http::StatusCode::PartialContent); | ||
} | ||
|
||
|
@@ -1988,7 +2017,7 @@ class ServerSession final : public ProtocolHandlerInterface | |
|
||
// FIXME: does not support ranges that specify multiple comma-separated values | ||
|
||
return asyncUpload(std::move(fromFile), std::move(mimeType), start, end, | ||
return asyncUpload(std::move(fromFile), std::move(responseHeaders), start, end, | ||
startIsSuffix, http::StatusCode::PartialContent); | ||
} | ||
|
||
|
@@ -2032,15 +2061,15 @@ class ServerSession final : public ProtocolHandlerInterface | |
<< " socket)"; | ||
os << indent << "\tconnected: " << _connected; | ||
os << indent << "\tstartTime: " << Util::getTimeForLog(now, _startTime); | ||
os << indent << "\tmimeType: " << _mimeType; | ||
os << indent << "\tmimeType: " << getMimeType(); | ||
os << indent << "\tstatusCode: " << getReasonPhraseForCode(_statusCode); | ||
os << indent << "\tsize: " << _size; | ||
os << indent << "\tpos: " << _pos; | ||
os << indent << "\tstart: " << _start; | ||
os << indent << "\tend: " << _end; | ||
os << indent << "\tstartIsSuffix: " << _startIsSuffix; | ||
os << indent; | ||
HexUtil::dumpHex(os, _data, "\tdata:\n", Util::replace(indent + '\t', "\n", "").c_str()); | ||
HexUtil::dumpHex(os, _filename, "\tfilename:\n", Util::replace(indent + '\t', "\n", "").c_str()); | ||
os << '\n'; | ||
|
||
// We are typically called from the StreamSocket, so don't | ||
|
@@ -2063,8 +2092,10 @@ class ServerSession final : public ProtocolHandlerInterface | |
|
||
LOG_DBG("Sending header with size " << getSendSize()); | ||
http::Response httpResponse(_statusCode); | ||
for (const auto& header : _responseHeaders) | ||
httpResponse.set(header.first, header.second); | ||
fprintf(stderr, "async send clipboard of len %d\n", getSendSize()); | ||
httpResponse.set("Content-Length", std::to_string(getSendSize())); | ||
httpResponse.set("Content-Type", _mimeType); | ||
httpResponse.set("Accept-Ranges", "bytes"); | ||
httpResponse.set("Content-Range", "bytes " + std::to_string(getStart()) + "-" + std::to_string(getEnd() - 1) + '/' + | ||
std::to_string(_size)); | ||
|
@@ -2144,7 +2175,7 @@ class ServerSession final : public ProtocolHandlerInterface | |
const auto size = std::min({sizeof(buffer), capacity, (size_t)(getEnd() - _pos)}); | ||
int n; | ||
while ((n = ::read(_fd, buffer, size)) < 0 && errno == EINTR) | ||
LOG_TRC("EINTR reading from " << _data); | ||
LOG_TRC("EINTR reading from " << _filename); | ||
|
||
if (n <= 0 || _pos >= getEnd()) | ||
{ | ||
|
@@ -2186,16 +2217,28 @@ class ServerSession final : public ProtocolHandlerInterface | |
} | ||
|
||
_connected = false; | ||
callOnFinished(); | ||
} | ||
|
||
int sendTextMessage(const char*, const size_t, bool) const override { return 0; } | ||
int sendBinaryMessage(const char*, const size_t, bool) const override { return 0; } | ||
|
||
std::string getMimeType() const | ||
{ | ||
const auto it = std::find_if(_responseHeaders.begin(), _responseHeaders.end(), | ||
[](const Header::Pair& pair) -> bool { | ||
return Util::iequal(pair.first, "Content-Type"); | ||
}); | ||
if (it != _responseHeaders.end()) | ||
return it->second; | ||
return std::string(); | ||
} | ||
|
||
private: | ||
http::Header::Container _responseHeaders; ///< The data Content-Type. | ||
std::chrono::microseconds _timeout; | ||
std::chrono::steady_clock::time_point _startTime; | ||
std::string _data; ///< Data to upload, if not from a file, OR, the filename (if _pos == -1). | ||
std::string _mimeType; ///< The data Content-Type. | ||
std::string _filename; ///< The filename (if _fd != -1) | ||
int _pos; ///< The current position in the data string. | ||
int _size; ///< The size of the data in bytes. | ||
int _fd; ///< The descriptor of the file to upload. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Commented-out code Note
Copilot Autofix
AI 3 months ago
To address the issue, we need to either remove the commented-out code or reinstate it. Since the code appears in a destructor, it is likely intended to clean up resources (e.g., removing a file). If this functionality is still required, the line should be reinstated. Otherwise, it should be removed entirely to avoid confusion. For this fix, we will reinstate the line, assuming that the cleanup operation is necessary.