Commit graph

121 commits

Author SHA1 Message Date
Quentin Rameau
e592bbc0fe Integrate compiled regex into vhost array 2017-07-11 13:45:57 +02:00
Laslo Hunhold
233bf68b4b Style fix
Thanks Quentin!
2017-07-11 13:03:24 +02:00
Laslo Hunhold
000553d8c5 Add vhost support
As given in the config, we match a regex of hosts to a canonical host
which points to an internal directory.
Regexes are compiled on initialization, so we can error out early.
The rest is just modifications to use relative directories rather than
absolute ones, as we chdir() into the vhost directories dynamically.

Given we normalize the targets beforehand, there is no danger of
malformed requests escaping the vhost-context.
2017-07-11 12:55:39 +02:00
Laslo Hunhold
9e9facc9bf Properly serve empty files 2017-07-11 12:33:21 +02:00
Laslo Hunhold
af61e37a3a Check inet_pton() for system error 2017-07-09 00:11:30 +02:00
Laslo Hunhold
d598da3d52 Send absolute path when redirecting
The logic is a little more complicated, but we need it for some future
changes.
2017-07-07 13:33:43 +02:00
Hiltjo Posthuma
fc9f02cee3 memcpy regions may not overlap, use memmove
for example a path: "/../../" made quark abort().
2017-07-05 19:34:25 +02:00
Laslo Hunhold
bdff59a5f6 Check for UNIX-doman socket name truncation 2017-07-04 22:44:47 +02:00
Laslo Hunhold
d5a6e978b9 Small style fix 2017-07-04 22:26:17 +02:00
Laslo Hunhold
7f391b6134 Import strtonum() and get rid of atoi() 2017-07-04 22:21:36 +02:00
Laslo Hunhold
c0a2c5de0a Improve timestamp() semantics
Thanks Hiltjo! We are now in the enterprise(tm)!
2017-07-04 21:47:28 +02:00
Hiltjo Posthuma
eda2c150e4 improve byte-range support
this fixes the lower and upper offset and adds a 416 (range not satisfiable
status). It returns the filesize if it cannot be satisfied as the standard
suggests so the client can do something.
2017-07-04 21:45:46 +02:00
Hiltjo Posthuma
9b8e2bdeb6 byte-range: fix range check for upper limit
the range check was done after the check lower > upper
so if it meets these conditions: lower <= upper and
lower > st.st_size then lower could still be > upper.
2017-07-04 21:01:44 +02:00
Hiltjo Posthuma
a092d9aa4b the result of encode() and decode() is unused 2017-06-28 20:20:10 +02:00
Hiltjo Posthuma
f5270aa241 initialize request fields used as output in log
this could crash quark or print gibberish if the connection is closed
before parsing these fields: r->target is uninitialized, but printed
in the log.
2017-06-28 20:20:10 +02:00
Laslo Hunhold
5030846333 Rename filetype() to suffix() 2017-06-28 08:40:34 +02:00
Laslo Hunhold
1e65a4848e Fix dir-check in dirent
We cannot do this, as it falsely added a / to a link pointing to a
socket.
2017-06-28 08:29:31 +02:00
Hiltjo Posthuma
c369738fa4 make directory listing a run-time flag
remove const in the config.h since it can be changed at run-time now.
2017-06-27 23:31:08 +02:00
Hiltjo Posthuma
fc0873853c minor code-style consistency fixes
:)
2017-06-27 23:31:08 +02:00
Hiltjo Posthuma
750f8381ce in the directory listing add / as suffix to save a redirect check 2017-06-27 23:31:08 +02:00
Hiltjo Posthuma
b6559eb3e7 fix memleak in scandir() 2017-06-27 23:31:08 +02:00
Hiltjo Posthuma
d1589c96fa header parsing: allow tabs are whitespace, compare case-insensitive
this is more conforming the HTTP spec
2017-06-27 23:31:08 +02:00
Hiltjo Posthuma
2021da9bed send binary Content-Type by default (octet-stream) 2017-06-27 23:31:08 +02:00
Hiltjo Posthuma
3efce07af9 fix memory leak in sendfile()
FILE *fp is not closed.
2017-06-27 23:31:08 +02:00
Hiltjo Posthuma
a551445fba scandir: sort directories and show filetype
sort in order: directory or non-directory, filename (case-sensitive).
show filetypes after filename:

- / for directory
- @ for symlink
- | for pipe
- = for socket
- etc
2017-06-27 23:31:08 +02:00
Hiltjo Posthuma
ee10f83391 send status code for method not allowed if not GET or HEAD 2017-06-27 23:31:08 +02:00
Hiltjo Posthuma
102cf09cc5 add newline after </html> 2017-06-27 23:31:08 +02:00
Hiltjo Posthuma
241449454f remove unused fields and redundant check for HTTP method 2017-06-27 23:31:08 +02:00
Laslo Hunhold
bd2e9e66ff Fix normabspath() to handle consecutive '..'s properly
The approach with lastp only works if we only go back one dir at a time.
Of course, this cannot be assumed, so we traverse the path backwards
looking for the previous /.
2017-06-21 11:02:35 +02:00
Laslo Hunhold
58450ffced Fix wording a little bit 2017-06-21 10:37:39 +02:00
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
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
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
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