Commit Graph
60 Commits
Author SHA1 Message Date
Johannes Weiß d94ab56d58 workaround ab (ApacheBench) in keep-alive mode (#260)
Motivation:

`ab -k` behaves weirdly: it'll send an HTTP/1.0 request with keep-alive set
but stops doing anything at all if the server doesn't also set Connection: keep-alive
which our example HTTP1Server didn't do.

Modifications:

In the HTTP1Server example if we receive an HTTP/1.0 request with
Connection: keep-alive, we'll now set keep-alive too.

Result:

ab -k doesn't get stuck anymore.
2018-04-10 18:28:10 +02:00
Cory Benfield 5b2ad2d921 Add support for automatic HTTP error reporting. (#268)
Motivation:

Currently the HTTP decoders can throw errors, but they will be ignored
and lead to a simple EOF. That's not ideal: in most cases we should make
a best-effort attempt to send a 4XX error code before we shut the client
down.

Modifications:

Provided a new ChannelHandler that generates 400 errors when the HTTP
decoder fails.
Added a flag to automatically add that handler to the channel pipeline.
Added the handler to the HTTP sample server.
Enabled integration test 12.

Result:

Easier error handling for HTTP servers.
2018-04-07 10:13:31 +02:00
Matt Eaton 164b007e0e Fixed minor typos and updated force unwraps. (#273)
Motivation:

I wanted to make sure that I fixed the minor typos and that I addressed
the feedback that was provided to me in regards to force unwrapping.
I also wanted to make sure my error messages were accurate.

Modifications:

Fixed a few minor typos and updated the force unwrap error messages.

Result:

Fixed typos and guard statements with accurate error messages.
2018-04-04 12:57:31 +01:00
Matt Eaton 66e4fdfbba Fixed a few minor typos and some force unwraps (#259)
fix some typos, force unwraps and inconsistencies

Motivation:
Reviewing the code I found a few typos that hindered readability, some unnecessary force unwraps and inconsistencies in the codebase.

Modifications:

- The main file in NIOWebSocketServer now accepts command line arguments
and binds to a passed in host and port like the other server examples do.
- Minor typo fixes
- removed one force unwrap

Result:

- The result of my change will bring the NIOWebSocketServer more inline
with how the NIOHTTP1 and NIOChat servers examples are setup.
- less force unwrapping
- fewer typos

[message edited by @weissi]
2018-04-03 11:44:14 +01:00
Tibor Bödecs 1f83a1d5cb Changed EventLoop{Future,Promise}<()> occurances to EventLoop{Future,Promise}<Void> (#211) (#219)
Motivation:

Fix inconsistency of <Void> and <()> occurances in the codebase.

Modifications:

Changed EventLoop{Future,Promise}<()> occurances to EventLoop{Future,Promise}<Void>

Result:

Consistent code style for EventLoopPromise and EventLoopFuture.
2018-03-22 17:36:10 +01:00
Bas Broek afa1037410 Prefer passing functions to flatMap
Motivation:

Passing in a function directly makes the code a bit more concise.

Modifications:

Changing `flatMap { Int($0) }` to `flatMap(Int.init)`.

Result:

More concise code.
2018-03-19 14:55:53 +01:00
Bas Broek a095decbc2 Prefer if case over switch
Motivation:

There are quite a few `switch`es that cover just two cases. Explicitly state this via an `if case` + `else` structure.

Modifications:

Changes something like:

switch a {
case .b:
  return c
default:
  return d
}

to

if case .b = a {
  return c
} else {
  return d
}

Result:

This should prevent misinterpretation and thus potential bugs in the future.
2018-03-19 14:45:32 +01:00
Bas Broek b180113d81 Improve switch readability
Motivation:
This changes two things:
- it removes the negation of non-used associated types (the removal of `(_)`).
- it prefers exhausting `switch`es over `default`ing.

This change does not impact bahavior; it is pure refactoring.

Modifications:

Omitting needless `(_)`'s and explicitly exhausting switches.

Result:

This change is pure refactoring.
2018-03-19 14:43:38 +01:00
Cory Benfield 4c0ed16cde Add new, better helper method for configuring HTTP servers. (#159)
Motivation:

The current HTTP server helpers are not modular, meaning that each
time we add a new first-party HTTP handler we need to add a brand new
method. Additionally, they do not currently compose, so if you want
assistance with HTTP pipelining *and* to support HTTP upgrade you are
out of luck.

Modifications:

Deprecate the two existing server helpers.

Add a new server helper that can be extended to support all of the
possible features that users may want in their HTTP pipelines.

Result:

Users will have a single place to go that can be used to configure
their HTTP server pipeline, and that understands how the different
handlers fit together in a complete pipeline.
2018-03-16 02:13:13 +09:00
Johannes Weiß 2cac305e3e work bug in latest Swift 4.1 with closures taking () (SR-7191) (#149)
Motivation:

In a few instances, we have code that ignores a `()` argument in a
closure with this syntax:

    { (_: Void) in

whilst that's a bit odd, it shouldn't be wrong and Swift 4.0.x is also
totally happy with it. The latest Swift 4.1 candidates however are not
and also there's a better way of spelling this:

    { () in

and in some cases even just

    {

Modifications:

Removed instances of `{ (_: Void) in`

Result:

Compiles with `swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-12-a`
2018-03-14 08:41:03 +01:00
Cory Benfield 860a7d40a3 Add channel handler for server side pipelining. (#62)
Motivation:

HTTP pipelining can be tricky to handle properly on the server side.
In particular, it's very easy to write out of order or inconsistently
mutate state. Users often need help to handle this appropriately.

Modifications:

Added a HTTPServerPipelineHandler that only lets one request through
at a time.

Result:

Better servers that are more able to handle HTTP pipelining
2018-03-13 15:21:12 +00:00
Cory Benfield 9ed09c0a5c Our sample HTTP server should respect HTTP keep-alive. (#115)
Motivation:

Currently our HTTP server almost always just assumes that keep-alive
is enabled. It really shouldn't, it should validate more carefully.
Additionally, it mishandles TCP half-close in a way that breaks with
netcat.

Modifications:

Added keep-alive tracking to all handlers and ensured that connections
will be closed after a response to a keep-alive request has been sent.
Also add support for handling TCP half-close.

Result:

Non-keep-alive connections will be closed by the server in all cases.
Half-closed connections will be appropriately managed.
2018-03-12 13:54:58 +01:00
Cory Benfield 684ae3d933 Add READMEs for the various sample applications (#117) 2018-03-09 09:06:06 -08:00
Arjun Nayini 5c2d98cd8f Clean up some code style violations. (#76)
Motivation:

Get the code to adhere better to style standards.

Modifications:

Fixed colon violation, unused arg, unneeded initialization,
and spacing nits.

Result:

No behavioral change, only code style changes.
2018-03-05 19:15:54 +09:00
Johannes Weiß b4d2bfc720 default example servers to use as many threads as cores (#78)
Motivation:

Our example servers used an arbitrary number of threads (the http one
just 1 :|) but `System.coreCount` is a much better default and also
demoes that API.

Modifications:

Changed the example servers to use `System.coreCount` threads.

Result:

Use more of the hardware you paid for.
2018-03-03 10:09:41 +09:00
Johannes Weiss c7e47e75f3 remove flush promises
Motivation:

We have multiple reasons why flush promises weren't great, for example:

- writeAndFlush sounds like it's a write + flush optimisation but in
  reality it was more expensive (2 extra promises)
- the semantics were never quite clear
- lots of implementation complexity, especially for the datagram channel

Modifications:

- removed the flush promises in the API
- this deliberately doesn't do the PendingWritesManager simplifications
  that this unlocks

Result:

flush doesn't have a promise anymore.
2018-02-20 16:13:41 +00:00
Johannes Weiß 64eac6177c make inbound channel operations non-throwing
Motivation:

Except for blocking methods (which shouldn't be used on the event loop),
there aren't that many error cases that we should just send through the
pipeline automatically. Best proof for that is that nothing in the code
base did that. Therefore these methods shouldn't be throwing.

If the user does indeed want to catch and fire an error through the
pipeline that's still super simple `ctx.fireErrorCaught(error)`.

Modifications:

removed `throws` from the `ChannelInboundHandler` methods.

Result:

better
2018-02-19 18:10:21 +00:00
Johannes Weiss 68724be6e6 fix Channel thread-safety (local/remoteAddress)
Motivation:

There were a couple of places in the Channel implementations that just
weren't thread-safe at all, namely:

- localAddress
- remoetAddress
- parent

those are now fixed. I also re-ordered the code so it should be easier
to maintain in the future (the `// MARK` markers were totally
incorrect).

Modifications:

- made Channel.{local,remote}Address return a future
- made Channel.parent a `let`
- unified more of `SocketChannel` and `DatagramSocketChannel`
- fixed the `// MARK`s by reordering code
- annotated methods/properties that are in the Channel API and need to
be thread-safe

Result:

slightly more thread-safety :)
2018-02-20 15:18:51 +00:00
Johannes Weiss 26582a3e97 redundant let elimination
Motivation:

`let _ = ...` is redundant and should be `_ =`

Modifications:

removed redundant `let`s

Result:

saved disk space ;)
2018-02-19 17:41:52 +00:00
Johannes Weiss 225951e7c3 remove Foundation dependency
Motivation:

Foundation is problematic for a few reasons:
- its implementation is different on Linux and on macOS which means our
  macOS tests might be inaccurate
- on macOS it uses ObjC Foundation which means the autorelease pool
  might get populated
- it links the world on Linux which means we can't do static
  binaries at all

Modifications:

removed the last bits of Foundation dependency

Result:

no Foundation dependency
2018-02-19 17:22:38 +00:00
Johannes Weiss 4a09182a39 rewrite closures that just ignore their parameter & change whenComplete
to not return a value

Motivation:

We recently had a bug where we had `EventLoopFuture<EventLoopFuture<()>>` which didn't make any sense. The compiler couldn't catch that problem because we just ignored a closure's argument like this:

    future.then { _ in
        ...
    }

which is dangerous. For closures that take an empty tuple, the `_ in`
isn't actually required and the others should state the type they want
to ignore.

And most whenComplete calls can be better (and often shorter) expressed
by other combinators.

Modifications:

remove pretty much all closures which just blanket ignore their
parameter.

Result:

- no closures which just ignore their parameter without at least stating
  its type.
- rewrote all whenCompletes that actually used the value
2018-02-16 17:28:43 +00:00
Norman Maurer 55e99b603d Expose BlockingIOThreadPool
* Expose BlockingIOThreadPool

Motivation:

Sometimes we need to execute some blocking IO. For this we should expose the BlockingIOThreadPool that can be used.

Modifications:

- Factor out BlockingIOThreadPool
- Added tests
- Correctly start threadpool before execute NonBlockingIO tests.

Result:

Possible to do blocking IO.

* Corrys comment

* Correctly start pool before using it
2018-02-16 14:53:19 +01:00
Norman Maurer 3f4a755de1 Remove no needed return statements and cleanup examples.
Motivation:

We do not need to use return in the closures used in the examples.

Modifications:

- Remove return
- Remove bug where we set the same channel option two times in one example

Result:

Cleaner code.
2018-02-16 10:50:57 +01:00
Johannes Weiß 5cb13b21e6 make FileRegion a value type and behave much more like ByteBuffer
Motivation:

Previously `FileRegion` was a special snow flake, a bit like
`ByteBuffer` but also totally different. That caused surprise and made
`PendingWritesManager` even harder. It was also used to manage the
lifetime of a file descriptor but only sort of.

Modifications:

We now have a `FileHandle` which is a one-to-one mapping with a file
descriptor and its lifetime must be managed appropriately.

Result:

hopefully less bugs and fd leaks.
2018-02-06 16:48:35 +00:00
Johannes Weiss eaa8ffaee4 remove redundant labels
Motivation:

Lots of our most important operations had redundant labels like

    func write(data: NIOAny)

the `data: ` label doesn't add anything meaningful and therefore it
should be removed.

Modifications:

removed lots of redundant labels

Result:

less redundant labels
2018-02-13 11:13:30 +00:00
Johannes Weiß 85b45e6912 make function names for connect/bind/SocketAddress more Swift-like 2018-02-12 15:09:22 +00:00
Norman Maurer aa9728b852 Use Int for port to make API more consistent
Motivation:

In ClientBootstrap we use Int to represent the port but in ServerBootstrap we use Int32. As swift usually always use Int we should just use Int everywhere to represent the port.

Modifications:

Change Int32 to Int for port

Result:

More consistent API
2018-02-12 13:39:01 +01:00
Norman Maurer 18a8467dfe Make parameter naming between connect / bind consistent.
Motivation:

We should have consistent parameter naming.

Modifications:

Use to path: everywhere.

Result:

Consistent naming.
2018-02-12 14:21:07 +01:00
Johannes Weiss 734904d0f4 clean up the (proto)types for EventLoopFuture
* clean up the (proto)types for EventLoopFuture

* Remove more unnecessary parens
2018-02-09 16:03:28 +00:00
Johannes Weiss 755ea5aedc non-blocking file IO 2018-02-07 17:25:45 +00:00
Johannes Weiss eab7d314f3 make Channel non optional in ChannelHandlerContext 2018-01-30 12:03:51 +00:00
Johannes Weiß c79c5ba364 re-export Darwin/Glibc and reduce number of #if os(...) 2018-01-09 15:08:17 +00:00
Norman Maurer 8f1ad37edd Use camel case for ChannelOption factory values and add docs 2017-12-20 14:18:07 +01:00
Johannes Weiss dfd25bf576 refactor Channel initialisation & *Bootstrap docs 2017-12-13 15:42:54 +00:00
Johannes Weiß eaf658691c some grammar fixes 2017-12-19 15:23:26 +00:00
Cory Benfield 3614d09a4b Provide a nicer setup helper for HTTP connections 2017-12-13 15:38:40 +00:00
Johannes Weiß 33fe401322 rename Future/Promise to EventLoopFuture/EventLoopPromise 2017-11-21 17:41:36 +00:00
Johannes Weiß f79aa7ef20 fix integration tests for Linux 2017-11-21 13:30:31 +00:00
Johannes Weiß 202e1c37d7 port integration tests for HTTP server 2017-10-10 19:41:01 +01:00
Johannes Weiß 31cd14eaad fix inEventLoop race and makes TSan happy 2017-11-10 14:56:33 -08:00
Norman Maurer 976f4752df Add HTTPRequestEncoder / HTTPResponseDecoder implementation and tests 2017-11-02 13:50:46 +01:00
Norman Maurer c22e5ead6b Remove ByteBufferAllocator constructor argument from HttpResponseEncoder
Motivation:

We can allocate the buffer on the fly if needed, no need to add the constructor argument.

Modifications:

Just allocate the buffer in the methods itself that need it.

Result:

Easier to construct encoder and cleaner code.
2017-11-02 12:56:00 +01:00
Norman Maurer 22a35b2bdb Add FileRegion support when using HTTP
Motivation:

Often people want to transfer large files over http which should be done using sendfile(...) for best performance.

Modifications:

- Add support for using FileRegion when sending body parts.
- Add tests

Result:

Be able to serve files without loading these into memory
2017-11-01 16:02:14 +01:00
Johannes Weiß 6173bee782 Split IOData into NIOAny & IOData (ByteBuffer | FileRegion) 2017-10-25 19:31:27 +01:00
Daniel Dunbar 87a47c88fc Prefer let bindings in port binding parsing. 2017-10-27 23:49:33 -07:00
Johannes Weiss 3bef8a4012 turn import Foundation comments into code :) 2017-10-12 16:33:50 +01:00
Cory Benfield 1f52bac1f9 Add support for HTTP trailers 2017-10-04 13:49:03 +01:00
Johannes Weiss 1adb581f59 remove more Foundation imports 2017-10-10 16:25:43 +01:00
Johannes Weiss a73d46f218 HTTP/1.1 Chunked Encoding
* HTTP/1.1 Chunked Encoding

* fix MarkedCircularBuffer: if not marked do not try to move mark when removing first

* fix HTTPRequestDecoder: dispatch callouts after parsing is complete

* fix HTTPTest: feed pipeline the inbound data correctly
2017-09-25 15:33:02 +01:00
Johannes Weiß 8e06697362 fix EventLoop shutdown by introducing shutdownGracefully 2017-09-20 17:15:06 +01:00