Skip to content

Commit

Permalink
tests: client/server rewrites
Browse files Browse the repository at this point in the history
For `server.c` the changes are fairly minor since it was already
a relatively straight-forward and self-contained example:

* Handle a potential `EAGAIN` `demo_result` from `write_tls()`.
* Add a `server.h` that is presently unused, but allows keeping the
  compilation rules simple by treating server/client symmetrically.
* Break the connection handling loop when we've both sent a response
  and the rustls connection requires no more writes. This effectively
  closes the connection after a response has been written, without
  waiting on the peer to do so. We want to do this since we don't
  process the HTTP request to learn if the client wanted `Connection:
  keep-alive` or `Connection: close`.

For `client.c`, the changes are more extensive:

* Add a `client.h` so we can forward declare everything interesting.
  This allows `client.c` to match our preferred Rust standard of "top
  down ordering"
* Extract out a `demo_client_options` struct and a `options_from_env()`
  function for handling options based on the environment.
* Extract out a `new_tls_config()` function that takes a pointer to
  `demo_client_options` and returns a `rustls_client_config`.
* Extract out a `demo_client_request_options` struct for per-request
  options (hostname, port, path, whether to use vectored I/O).
* Pull out a `demo_client_connection` struct for managing the state
  associated with a connection (socket fd, rustls_connection, conndata,
  closing stae, etc).
* Rework existing logic around the new types.
* Simplify the request handling to better match tls-client-mio.rs in the
  Rustls examples. Notably we _do not_ process the HTTP response,
  instead we just read whatever data we get and blast it to stdout.
  A new timeout on `select()` ensures that if the server doesn't close
  the connection after writing a response we will time out waiting for
  more data and do it ourselves. With the update to server.c to close
  the connection after writing a response this won't kick in, but is
  helpful for testing against servers that may let the conn linger even
  though we send `Connection: close`.
* Care is taken to still treat unclean closure as an error condition.
* Various other small improvements are made where possible.
  • Loading branch information
cpu committed Dec 9, 2024
1 parent debdc40 commit d23c5cb
Show file tree
Hide file tree
Showing 5 changed files with 774 additions and 365 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ src/rustls.h: src/*.rs cbindgen.toml
target/$(PROFILE)/librustls_ffi.a: src/*.rs Cargo.toml
RUSTFLAGS="-C metadata=rustls-ffi" ${CARGO} build $(CARGOFLAGS)

target/%.o: tests/%.c tests/common.h | target
target/%.o: tests/%.c tests/%.h tests/common.h | target
$(CC) -o $@ -c $< $(CFLAGS)

target/client: target/client.o target/common.o target/$(PROFILE)/librustls_ffi.a
Expand Down
Loading

0 comments on commit d23c5cb

Please sign in to comment.