Commit graph

253 commits

Author SHA1 Message Date
Laslo Hunhold
283bf91a0e Add normabspath() to replace realpath(), making quark work with musl
Compiled against musl, quark will not work as musl needs the presence
of procfs to process paths in realpath().
We could wait for it to be implemented[0] or also notice that we don't
want to overengineer the target-resolving. I don't think it's very
suckless if we deploy such a huge infrastructure to resolve paths.

To counteract this and given there are no good solutions available, I
set out to write the function normabspath(), which normalizes an
absolute path.
It is idempotent and works on the buffer passed to it. We don't need a
target, as the resulting resolved path is guaranteed to be of equal
length or shorter. This requires a memcpy in our case before calling it,
but I see it as a nice demonstration of the possibilities and it might
prove to be useful for other projects.

Not requiring a target buffer (that needs to have its length specified),
the one-string-call also simplifies the calling semantics drasticly.

With this function in place, quark works with musl. Statically linked,
stripped and with -Os, it only weighs 102K.

[0]: http://www.openwall.com/lists/musl/2016/11/03/5
2017-06-21 10:35:40 +02:00
Laslo Hunhold
f4db83f68c Use dprintf() instead of snprintf()+sendbuffer()
The aim was to write quark without any mallocs. This was successful, but
proved to be a bit ugly looking at how we construct data to be sent.
Before this change, we had static buffers in each function that needed
them and filled them up, possibly risking overflow.
After that, we sent them off using our own function sendbuffer(), which
in itself represented a buffering mechanism.
Using dprintf, which is POSIX 2008, we can send things off directly,
with no need for sendbuffer() or buffers for these things.
This way we can factor out sendbuffer(), dropping a few more LOCs.

Thanks Hiltjo for the suggestion!
2017-06-21 07:56:40 +02:00
Laslo Hunhold
6347e2ec3e Rewrite quark from the ground up again
I noticed that the data structures didn't allow a flexible handling of
the code while trying to extend it to support if-modified-since-responses.
To tackle this, I refactored the data structures and proceeded to
rewrite the server from the ground up, implementing all present features
plus fixing a lot of bugs and introducing the 304 header handling as
requested by many people.

Please report bugs if you find them.

While at it, I refactored the build system as well and updated all
surrounding files respectively.
2017-06-20 21:40:00 +02:00
FRIGN
29d53f65b7 Support encoding UTF-8 characters
There's no loss doing it so, as FF = 255 and the upper half of the
unsigned char range is exactly where we feel at home with UTF-8.
2016-09-05 20:10:16 +02:00
FRIGN
5ecd11fa3d Use sizeof() instead of magic constants 2016-09-05 00:08:25 +02:00
FRIGN
6996e702e8 Use enum for HTTP stati and improve data structures 2016-09-04 23:58:20 +02:00
FRIGN
89d7bbb9c3 Use die() in usage() 2016-09-04 23:09:02 +02:00
FRIGN
12ebbc5ded Add missing newline
Thanks to Ali H. Fardan for reporting this!
2016-09-04 20:16:51 +02:00
FRIGN
f04929e729 Add a few more mime types 2016-09-02 11:09:49 +02:00
FRIGN
cd2893965f Stop hiding what we're doing in the Makefile
On Gentoo for instance they are literally stripping the @'s from our
Makefiles to make the output usable. It makes sense to remove this
abstraction.
2016-09-02 10:49:42 +02:00
FRIGN
11481ed250 Simplify Makefile and package properly 2016-09-02 10:44:43 +02:00
FRIGN
0ce86bba15 Initial commit of quark rewrite
Roughly 700 LOC (half of the old quark on the Hiltjo branch) in size,
this rewrite supports partial content and other good stuff that will
make it fun again to use quark for simple static purposes.
The error checking is rigorous and strict and it will report proper
error codes back to the client whenever there was a problem or the
request was invalid in some way.

A cool feature is the support for listening on a UNIX-domain socket,
which will in the long run allow us to solve problems with virtual hosts
and other things in separate programs. But until then, this should be
robust enough for most use-cases.

This resets quark's version to 0, but this was no problem as there
haven't been any quark releases yet.

Feedback is appreciated.
2016-09-02 09:59:02 +02:00
FRIGN
0e8cac1ee4 Prepare repository for rewrite
It was sad to see that quark never got the attention it deserved in my
opinion. However, there were good reasons why that was the case.

The project lost focus by trying to add CGI support, which in all
fairness worked only half of the time.
For the rest of the use cases, a static server to make it dead simple to
publish a directory, it was also pretty bad, given it does not support
partial content. Seeking in a mp3 was impossible and it was very
frustrating.

Long ago we discussed in the team how exciting it would be to test out
new concepts of having a web server that listens on a UNIX-domain
socket, potentially allowing new concepts for realizing virtual hosts
and other things.

It took me half a year to make the decision to rewrite quark, so it is
now time to purge the repo and push the initial commit.
2016-09-02 09:46:01 +02:00
FRIGN
4c14a02754 Fix syntax error in config.def.h
Thanks to Boris Egorov for finding this!
2015-05-24 16:49:42 +02:00
sin
7a587cfa36 Simplify config.mk
A separate INCS/LIBS is not really necessary here and confuses
the common case.
2014-12-01 18:18:58 +01:00
sin
5b089b98a6 Undef MIN() because it screams on OpenBSD 2014-12-01 17:36:45 +01:00
sin
2a83d925d7 Only set SO_REUSEADDR on the listening fd 2014-12-01 17:36:34 +01:00
Hiltjo Posthuma
b017f90846 bind: re-use address, setsockopt SO_REUSEADDR 2014-11-30 23:35:59 +01:00
Hiltjo Posthuma
70359ae2e9 config: add some more common mime-types 2014-11-30 23:35:48 +01:00
Hiltjo Posthuma
192f44046c code-style fixes 2014-11-30 23:35:37 +01:00
Ivan Delalande
e42bb27846 change the behavior of docroot
Change the behavior of docroot, which is now used as a prefix path for
all file operations related to static files. And add chrootdir, which is
just the old docroot behavior and allows to control the path into which
quark will chroot.

Not having properly distinct configuration variables for chroot,
document root and CGI root was specially annoying since commit 2822488
which allowed users to retrieve the CGI script or binary by just
guessing its path, since quark was chrooting into docroot before
anything else, and thus the CGI script/binary was in the user accessible
path.

This is implemented by moving the reqbuf buffer in the middle of a
bigger buffer, reqpath. That buffer contains the value of docroot at its
beginning and reqbuf simply points to the first byte after this value.
2014-11-30 23:35:10 +01:00
FRIGN
4674ccde50 Revert "request: ignore empty request in log"
This reverts commit 68f51ac37a.

The idea is good, but we just didn't yet get the right format
for the reading-cycle, which effectively keeps offset at 0
when it's all done in one read.

Let's call it a day and get back to the drawing-boards
tomorrow.
2014-08-25 00:59:57 +02:00
FRIGN
a86c7f10e3 Revert "always null-terminate reqbuf buffer"
This reverts commit 73d8b456eb.
2014-08-25 00:53:10 +02:00
Hiltjo Posthuma
68f51ac37a request: ignore empty request in log 2014-08-25 00:34:24 +02:00
FRIGN
73d8b456eb always null-terminate reqbuf buffer
thanks Hiltjo for the patch
2014-08-25 00:33:00 +02:00
sin
29c8cfa201 Rename fd to listenfd for readability
Avoid confusion in functions that use 'fd' and shadow the
global fd.
2014-08-22 11:55:36 +02:00
sin
de74d9b8fe Set up process limits and socket timeouts
(Until we have non-blocking sockets + poll())
2014-08-22 11:55:33 +02:00
sin
4f18d89f22 No need to do a socket release here
1s is too hardcore and impacts performance.
2014-08-22 11:55:26 +02:00
sin
293eb4489d Fix off-by-one 2014-08-22 11:55:22 +02:00
FRIGN
5dac8de322 Allow dir-listing to be a configurable option 2014-08-20 20:47:29 +02:00
FRIGN
ca2c107f30 Remove whitespace from manual 2014-08-20 20:44:13 +02:00
Hiltjo Posthuma
c00949d37d quark.1: add manpage
quark.c: alphabeticly sort options, like in the manpage.
Makefile: add manpage rule to Makefile and fix make dist.
2014-08-20 20:42:58 +02:00
Hiltjo Posthuma
43c4400a77 update README 2014-08-20 20:42:54 +02:00
Hiltjo Posthuma
9fd46fd50e read header until \r\n\r\n or \n\n 2014-08-20 20:42:50 +02:00
Hiltjo Posthuma
19fbedc39a add option -l to enable dirlisting, now disabled by default...
... alphasort direntries using scandir().
2014-08-20 20:42:46 +02:00
Hiltjo Posthuma
fa113f8b47 cgi: parse Status: header
CGI applications can specify a HTTP status to output with the Status:
header. For simplicity the CGI application must use this header on the
first line. With this change cloning git repositories over HTTP with
cgit works.

in config.mk specify _GNU_SOURCE and _POSIX_C_SOURCE instead of
_GNU_SOURCE, this is for getline().
2014-08-20 12:58:10 +02:00
FRIGN
8b6bdd39ff Remove "@strip $@" from the Makefile
LDFLAGS -s already strips.
Thanks, Hiltjo!
2014-08-16 22:05:43 +02:00
sin
7ecf520b30 Fix potentially uninitialized vars 2014-08-16 12:09:00 +02:00
sin
df8437d4f6 Do not indent switch cases
There's only one way to format switch statements.

http://cm.bell-labs.com/cm/cs/who/dmr/ctut.pdf - page 16
2014-08-15 19:51:44 +02:00
Hiltjo Posthuma
2822488312 improve CGI support
- set some more environment variables (PATH_INFO for example is used by
  cgit). Also set REMOTE_ADDR, SERVER_PORT, SERVER_SOFTWARE.
- when a file is requested in cgi mode serve it, if it doesn't exist,
  pass it to the CGI script (needed for cgit image/CSS).
2014-08-14 17:57:03 +02:00
Hiltjo Posthuma
159cdb3e0b small style and CGI message fix 2014-08-14 17:51:32 +02:00
FRIGN
bd9d7e3493 Fortify the sanity-checks
Hiltjo made a good start, but we need to check both the pointers
and the first value to make sure that a NULL-pointer is not
passed to the functions.
2014-08-14 17:48:37 +02:00
Hiltjo Posthuma
8aa3e3f48f config: allow to set options as application arguments 2014-08-14 17:43:46 +02:00
FRIGN
3962978bdd Increase clarity of the len-check 2014-08-14 14:18:40 +02:00
FRIGN
67750f916f check for len(reqbuf) = 0
Probably never the case, but we want to cleanly catch this error
instead of bringing up a segfault with reqbuf[-1].
2014-08-14 13:58:36 +02:00
FRIGN
72d02aa5b1 Ignore EPIPE while writing file to socket
If the client decides not to listen, it's not that much of a problem.
Don't flood the logs with "Broken pipe"-messages by silently letting
this "error" pass.
2014-08-14 13:14:08 +02:00
sin
8fdfa36914 Ignore SIGPIPE
If there's no reading end (client closed connection) and quark
tries to write to the socket, it will get a SIGPIPE.  Just ignore it.
2014-08-14 09:57:03 +02:00
sin
5d20e3dc27 No need for MAXBUFLEN+1
Return -1 if writedata() fails.
2014-08-14 09:56:23 +02:00
sin
21ec61991f Ensure mod is properly initialized
Fixes a segfault.
2014-08-14 09:54:37 +02:00
FRIGN
a1fa707eec Fix streaming errors
Streaming a file (through mplayer for instance), the socket would
block, because mplayer fills its buffer sequentially.
We would've never gotten to a write(.., n) == n.

Instead, do it like we read from files and accept the fact clients
can accept data chunk-wise, too.

The reason why this error went unnoticed is that I added a faulty
printf-directive (%ls for ssize_t), which silently produced
no output.
Thanks to sin for fixing the %ls -> %zd error, as it made me look
at the code again.
2014-08-14 09:47:23 +02:00