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

Ticket30007 refactor control response formatting #956

Closed
wants to merge 31 commits into from

Conversation

Labels
None yet
Projects
None yet
3 participants
@tlyu
Copy link
Contributor

@tlyu tlyu commented Apr 15, 2019

overview:

  • split low level control reply formatting out of control_fmt.c
  • create abstractions to format the control protocol response syntax so that individual events or commands don't roll their own formatting
  • use Coccinelle to transform existing explicit formatting into calls to the new functions
  • some manual cleanup, both of whitespace quirks introduced by Coccinelle and some manual edits that it's not economical to write a Coccinelle script to accomplish
nmathewson and others added 30 commits Apr 12, 2019
Use a table-based lookup to find the right command handler.  This
will serve as the basement for several future improvements, as we
improve the API for parsing commands.
There _is_ an underlying logic to these commands, but it isn't
wholly uniform, given years of tweaks and changes.  Fortunately I
think there is a superset that will work.

This commit adds a parser for some of the most basic cases -- the
ones currently handled by getargs_helper() and some of the
object-taking ones.  Soon will come initial tests; then I'll start using
the parser.

After that, I'll expand the parser to handle the other cases that come
up in the controller protocol.
This is preliminary work for fixing 29984; no behavior has changed.
The first line break in particular was mishandled: it was discarded
if no arguments came before it, which made it impossible to
distinguish arguments from the first line of the body.

To solve this, we need to allocate a copy of the command rather than
using NUL to separate it, since we might have "COMMAND\n" as our input.

Fixes ticket 29984.
(This should be all of the command that work nicely with positional
arguments only.)

Some of these commands should probably treat extra arguments as
incorrect, but for now I'm trying to be careful not to break
any existing users.
The two options are mutually exclusive, since otherwise an entry
like "Foo" would be ambiguous.  We want to have the ability to treat
entries like this as keys, though, since some controller commands
interpret them as flags.
This should let us handle all (or nearly all) of the remaining
commands.
Here we get to throw away a LOT of unused code, since most of the
old parsing was redundant with kvline.
This command does not fit perfectly with the others, since its
second argument is optional and may contain equal signs.  Still,
it's probably better to squeeze it into the new metaformat, since
doing so allows us to remove several pieces of the old
command-parsing machinery.
This function decodes something different from the usual c-escaped
format.

It is only used in controller authorization.
These two presented their own challenge, because of their use of
QString, and their distinguished handling of quoted versus
non-quoted values.
Now that the legacy handlers are gone, we can simplify the
structures and macros here.
Split the core reply formatting code out of control_fmt.c into
control_proto.c.  The remaining code in control_format.c deals with
specific subsystems and will eventually move to join those subsystems.
Refer to control-spec.txt grammar productions in comments in
control_proto.c for clarity.
Create a set of abstractions for controller commands and events to
output replies to the control channel.  The control protocol has a
relatively consistent SMTP-like structure, so it's helpful when code
that implements control commands and events doesn't explicitly format
everything on its own.
Manually fix up some reply-generating code that the Coccinelle scripts
won't match.  Some more complicated ones remain -- these are mostly
ones that accumulate data to send, and then call connection_buf_add()
or connection_write_str_to_buf() directly.
Clean up some minor formatting quirks after the Coccinelle run.
@tlyu
Copy link
Contributor Author

@tlyu tlyu commented Apr 15, 2019

This includes the commits of #940. The manual merge commit is because GitHub doesn't seem willing to make a trial merge commit automatically for this branch.

@coveralls
Copy link

@coveralls coveralls commented Apr 15, 2019

Pull Request Test Coverage Report for Build 4742

  • 123 of 583 (21.1%) changed or added relevant lines in 9 files are covered.
  • 105 unchanged lines in 6 files lost coverage.
  • Overall coverage increased (+0.1%) to 62.389%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/core/mainloop/connection.c 0 1 0.0%
src/lib/encoding/kvline.c 20 21 95.24%
src/lib/encoding/confline.c 0 3 0.0%
src/feature/control/control_getinfo.c 0 9 0.0%
src/feature/control/control.c 0 27 0.0%
src/lib/encoding/qstring.c 0 32 0.0%
src/feature/control/control_auth.c 0 47 0.0%
src/feature/control/control_proto.c 42 100 42.0%
src/feature/control/control_cmd.c 61 343 17.78%
Files with Coverage Reduction New Missed Lines %
src/feature/control/control.c 1 10.89%
src/feature/control/control_auth.c 3 12.09%
src/feature/dirauth/shared_random.c 3 85.64%
src/feature/control/control_getinfo.c 4 27.75%
src/feature/dirauth/dirvote.c 9 64.74%
src/feature/control/control_cmd.c 85 15.7%
Totals Coverage Status
Change from base Build 4732: 0.1%
Covered Lines: 46265
Relevant Lines: 74156

💛 - Coveralls

@tlyu
Copy link
Contributor Author

@tlyu tlyu commented Apr 26, 2019

Superseded by #984, which is a rebased version.

@tlyu tlyu closed this Apr 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment