-
Notifications
You must be signed in to change notification settings - Fork 25
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
Add option to start interactive server for Parser dialect conversion #768
Open
frabert
wants to merge
3
commits into
master
Choose a base branch
from
frabert/server
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,208
−24
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,103 @@ | ||
// Copyright (c) 2024-present, Trail of Bits, Inc. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <span> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#include <stdio.h> | ||
|
||
#include <gap/core/crtp.hpp> | ||
|
||
#include "vast/Util/Warnings.hpp" | ||
|
||
namespace vast::server { | ||
class connection_closed : public std::runtime_error | ||
{ | ||
public: | ||
connection_closed() : std::runtime_error("Connection closed") {} | ||
|
||
connection_closed(const char *what) : std::runtime_error(what) {} | ||
}; | ||
|
||
struct io_adapter | ||
{ | ||
virtual ~io_adapter() = default; | ||
|
||
virtual void close() {} | ||
|
||
virtual size_t read_some(std::span< char > dst) = 0; | ||
virtual size_t write_some(std::span< const char > dst) = 0; | ||
|
||
// Upon completion, `dst` is filled with data. | ||
void read_all(std::span< char > dst) { | ||
while (!dst.empty()) { | ||
size_t nread = read_some(dst); | ||
dst = dst.subspan(nread); | ||
} | ||
} | ||
|
||
// Upon completion, all of the data in `src` is written to the client.. | ||
void write_all(std::span< const char > src) { | ||
while (!src.empty()) { | ||
size_t nwritten = write_some(src); | ||
src = src.subspan(nwritten); | ||
} | ||
} | ||
|
||
char read() { | ||
char res[1]; | ||
read_all(res); | ||
return res[0]; | ||
} | ||
}; | ||
|
||
class file_adapter final : public io_adapter | ||
{ | ||
FILE *ifd; | ||
FILE *ofd; | ||
|
||
public: | ||
file_adapter(FILE *ifd = stdin, FILE *ofd = stdout) : ifd(ifd), ofd(ofd) { | ||
VAST_ASSERT(ifd != nullptr); | ||
VAST_ASSERT(ofd != nullptr); | ||
|
||
setvbuf(ofd, NULL, _IONBF, 0); | ||
} | ||
|
||
size_t read_some(std::span< char > dst) override { | ||
size_t nread = fread(dst.data(), 1, dst.size_bytes(), ifd); | ||
if (nread == 0 && (feof(ifd) || ferror(ifd))) { | ||
throw connection_closed{}; | ||
} | ||
return nread; | ||
} | ||
|
||
size_t write_some(std::span< const char > src) override { | ||
size_t nwritten = fwrite(src.data(), 1, src.size_bytes(), ofd); | ||
if (src.size() != 0 && nwritten == 0) { | ||
throw connection_closed{}; | ||
} | ||
return nwritten; | ||
} | ||
}; | ||
|
||
class sock_adapter final : public io_adapter | ||
{ | ||
public: | ||
struct impl; | ||
|
||
size_t read_some(std::span< char > dst) override; | ||
size_t write_some(std::span< const char > src) override; | ||
~sock_adapter(); | ||
void close() override; | ||
|
||
static std::unique_ptr< sock_adapter > create_unix_socket(const std::string &path); | ||
|
||
private: | ||
std::unique_ptr< struct impl > pimpl; | ||
sock_adapter(std::unique_ptr< impl > pimpl); | ||
}; | ||
} // namespace vast::server |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not work as
dst
is not output parameter and you overwrite it.You are only lucky it is used with single character span.
Also why to introduce unnecessary complexity when only thing you use is read_byte?
read_some
is also nowhere else used then in read of single byte.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the thing about
dst
being overwrittenre
read_some
:read_some
is the only semantic that most APIs offer, but for convenience sake I also wantread_all
semantics, for example when reading a request's body.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you expect to be in
dst
at the end ofread_all
? You rewrite it at every iteration what is invisible from outside.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dst
is aspan
-- it's essentially a pointerchar*
. It's equivalent todst.subspan
returns a new span that points to the same data butnread
characters aheadThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I misunderstood it. I guess it works :)