
Show HN: LEAR – A simple and fast HTTP server to serve static resources - mbien
https://github.com/Glorf/lear
======
quangio
It's great to see people still using C to make web servers !

But can you clarify what are the "state-of-the-art technologies and
solutions?"

I just skim the project and think this is not a lot better in term of
performance than other servers like Nginx or H2O. There is also Lwan[0], which
uses epoll too and has a cool coroutine, router implementation (I implemented
a framework for Kotlin/Native on top of it[1]).

Another framework having really fast and fancy technologies in it is
Seastar[2]. I think implementing kernel bypass, AIO, DPSK stuff makes it the
fastest web server around currently. May be you can try to push the boundary
with those things, SPDK, aggressive polling, fast router...?

[0]: [https://github.com/lpereira/lwan](https://github.com/lpereira/lwan) [1]:
[https://github.com/KwangIO/kwang](https://github.com/KwangIO/kwang) [2]:
[https://github.com/scylladb/seastar](https://github.com/scylladb/seastar)

~~~
newaccoutnas
If C based web servers float your boat, have you seen one written in asm?
[https://2ton.com.au/rwasa/](https://2ton.com.au/rwasa/)

------
userbinator
If you're looking for low-hanging-fruit to optimise, there's a ton of "foo =
create_string(<some constant>)" and then "delete_string(foo)" not long
afterwards in the code.

I also think I've found a bug: the code seems to assume that the colon in a
header will be followed by one space (see parse_request_line in http.c) but
according to
[https://tools.ietf.org/html/rfc7230#section-3.2](https://tools.ietf.org/html/rfc7230#section-3.2)
and my experience, that space is optional and there may be multiple of them.

(This is part of the reason I'm not a fan of text-based protocols: parsing is
full of annoying edge-cases.)

~~~
zokier
I feel like it should be possible to avoid dynamic memory allocation
completely during the request processing/parsing. Sure, fixed-sized buffers
generally imply some memory overhead, but I'd think the overall effect on
performance would be beneficial.

~~~
bwindels
This is what I'm trying to do with wwwee [1], a low-resource web server
written in Rust. The request / response buffer is a growing anonymous mmap
mapping, but all parsing (http headers, ...) and decoding (base64, JSON) is
done borrowing from the buffer. It works especially well in Rust, with it's
borrow checker preventing use-after-free.

1: [https://github.com/bwindels/wwwee/](https://github.com/bwindels/wwwee/)

------
continuations
\- What are the advantages of using this over something more established such
as Nginx or H2O?

\- README mentioned "fully non-blocking architecture," this only refers to
network IO, correct? My understanding is that Linux doesn't have truly non-
blocking file IO. Is that right?

~~~
mbien
\- Main advantage is simplicity, but architecture is in fact very similar to
nginx. So, server should be faster than nginx but scale similarly. But it's
all still highly DIY

\- You're right, I mean non-blocking architecture of network IO.

~~~
sigi45
Lucky enough 'should be' is not a accepted benchmark ;)

Don't get me wrong, nginx is big and has more experience. Different headers,
compression, security etc.

I would not even advice people using something like LEAR alone for the fact of
undetected security issues.

~~~
mbien
I agree, I don't claim it's ready Please, treat it as interesting project to
follow that has just started, and is surely not meant to be deployed in
production environments, at least not in this state

------
tjpnz
I think what's missing from this is "Show HN". I don't think anybody is
suggesting it be used in place of NGINX or H2O - not yet.

------
kureikain
I'm not a C expert. Trying to understand if this offer other novel techniques.

So in Go we already have this capability in

    
    
        http.FileServer(http.Dir("static"))
    
    

Also, in Rust, actix web has similar built-in function:

[https://github.com/actix/actix-
website/blob/master/content/d...](https://github.com/actix/actix-
website/blob/master/content/docs/static-files.md)

Again, I'm a noob in C but I really want to understand state of the art in
file serving.

I imagine it isn't as easy as it's for thing like server a big file by reading
byte by byte and write the response. It has to have some tricks here which I
don't know.

------
haney
Kind of off topic but it doesn’t look like there are any tests in this repo.
Is that common in the open source C community? I see several people suggesting
benchmarks but what about functional tests? I don’t write much C and am
curious about community norms.

------
rkeene2
In this same vein is Filed, it does the minimal amount of work possible before
handing off to the kernel to sendfile() the static resource.

[http://filed.rkeene.org/](http://filed.rkeene.org/)

------
jimjag
Kind of sad that compliance never given as much importance as it should
have... What good is a fast server if it doesn't fully comply w/ the spec?
Luckily, there are some, like Apache httpd, that do both.

------
Traubenfuchs
Why use a multiprocess architecture instead of a multithreaded one?

~~~
mbien
TBH, it was simpler in implementation and maintance, while creating just a
little bit slower than pthread on initialization time.

------
mbien
I'm truly impressed how wide the response is. It makes me sure that this
project is truly worth continuing. Thanks HN!

------
known
Check performance of khttpd [https://github.com/cobyism/edimax-
br-6528n/tree/master/linux...](https://github.com/cobyism/edimax-
br-6528n/tree/master/linux-2.4.18_1200_96C/net/khttpd) in
[http://www.fenrus.demon.nl/performance.html](http://www.fenrus.demon.nl/performance.html)

------
StreamBright
Why not to use Nginx for this task?

------
guidedlight
This would be very useful for performance testing

~~~
BlackLotus89
Was one of the first things I thought as well.

Looking at the github page I'm wondering how it performs compared to something
like nginx. Since I won't use something without https for production,
performance testing seems the only real usecase...

~~~
mbien
https is one of the first features I'd like to implement in the near future :)
Do you know if there is some framework that I could test my server with,
against eg. nginx? I was looking for one to do profiling, but am unsure if
there exist some that allows you to create high traffic with many clients.

~~~
CapacitorSet
You can use `ab`. I'll post an issue about that.

------
matte_black
Any benchmarks?

~~~
mbien
Currently we have only what CapacitorSet prepared:
[https://github.com/Glorf/lear/issues/1](https://github.com/Glorf/lear/issues/1)
I'll try to fix HTTP/1.0 support and post some more detailed benchmark in few
hours

------
mirimir
This would be great for Tor .onion servers, yes?

~~~
detaro
Why? Performance doesn't matter for that use-case. Maybe less features means
more secure, on the other hand established projects (or even the basic HTTP
implementations in other languages stdlibs) are probably better covered
security-review wise. (edit: unless I missed it, it doesn't even check against
directory traversal?)

~~~
mirimir
Yes, less attack surface is good. So is low resource usage.

I used to like tinyhttpd, but Debian dropped it, and I find it painful to
build, so I use nginx now.

