Commit Graph

303 Commits

Author SHA1 Message Date
Joris Vink 566fefd031 do not http_argument_urldecode for multipart data. 2018-08-16 14:11:28 +02:00
Joris Vink cf1f624367 let filerefs to operate on ms precision for mtime. 2018-07-24 19:56:36 +02:00
Joris Vink 821c1df8ec use method not allowed when required 2018-07-18 16:24:28 +02:00
Joris Vink 916ce222b4 better fix for 5a5d9fd0.
Don't let net_recv_flush() do things as long as the HTTP layer
owns the buffer. When we have sent a response kick the read end
back into gear ourselves by calling net_recv_flush().
2018-07-18 16:10:41 +02:00
Joris Vink 5a5d9fd0c2 alloc space for nb->buf after taking ownership. 2018-07-18 14:36:13 +02:00
Joris Vink 1447f6573f better http header validation. 2018-07-17 20:17:05 +02:00
Joris Vink 616af063e3 Calculate an md over the incoming HTTP body.
This is calculated while the HTTP body is incoming over the wire, once
the body is fully received the digest will be available for the page
handlers to obtain.

You can obtain a hex string for this md via http_body_digest() or
dereferences the http_request and look at http_body_digest manually
for the bytes.
2018-07-17 14:53:55 +02:00
Joris Vink 0726a26c0c Allow restriction of methods for paths.
Now Kore will automatically send a 400 bad request in case the
method was not allowed on the path.
2018-07-17 14:23:57 +02:00
Joris Vink f02f88295c revert b5e122 for now. 2018-07-06 11:21:46 +02:00
Joris Vink 47c1a1d195 set referer to NULL in http_request_new(). 2018-07-05 05:02:49 +00:00
Joris Vink b5e122419b Let http_populate_post() listen to content-type 2018-07-03 08:25:06 +02:00
Joris Vink 4a8d8ab7f8 log referer in accesslog if present. 2018-06-29 22:37:48 +02:00
Joris Vink 72073701b0 Add last-modified and if-modified-since for filemaps. 2018-06-29 09:56:04 +02:00
Joris Vink 521ff6a11d catch more bad ranges in http_argument_urldecode() 2018-06-28 15:39:03 +02:00
Joris Vink 70e945afb7 limit http_argument_urldecode() to sane characters 2018-06-28 15:27:55 +02:00
Joris Vink afd76ff55d Change accesslog format to Combined Log Format. 2018-06-28 14:25:32 +02:00
Joris Vink 80f5425698 Add filemaps.
A filemap is a way of telling Kore to serve files from a directory
much like a traditional webserver can do.

Kore filemaps only handles files. Kore does not generate directory
indexes or deal with non-regular files.

The way files are sent to a client differs a bit per platform and
build options:

default:
  - mmap() backed file transfer due to TLS.

NOTLS=1
  - sendfile() under FreeBSD, macOS and Linux.
  - mmap() backed file for OpenBSD.

The opened file descriptors/mmap'd regions are cached and reused when
appropriate. If a file is no longer in use it will be closed and evicted
from the cache after 30 seconds.

New API's are available allowing developers to use these facilities via:
  void net_send_fileref(struct connection *, struct kore_fileref *);
  void http_response_fileref(struct http_request *, struct kore_fileref *);

Kore will attempt to match media types based on file extensions. A few
default types are built-in. Others can be added via the new "http_media_type"
configuration directive.
2018-06-28 13:27:44 +02:00
Joris Vink 9be72aff57 bump size of http_version array. 2018-06-23 17:23:45 +02:00
Joris Vink 8aaf7aaf79 Alter where the version number comes from.
Now if we are a git repo we fetch the branch name and
commitid to build the version string. If there is no
git repo we'll look at the RELEASE file.
2018-06-22 14:24:42 +02:00
Joris Vink 439a3b36f0 Add kore_strtodouble().
Use it for http_argument_get_float() and http_argument_get_double().
2018-05-04 15:55:35 +02:00
Joris Vink 5487950f63 cut off port from the domain when needed. 2018-04-24 20:11:41 +02:00
Joris Vink d73a9114c0 Improve http_response() for server side errors.
In case http_response() is called with an error code indicating
a server side error (>= 500) do not append any headers set by the
caller.
2018-04-11 13:04:26 +02:00
Joris Vink 6a35a8a455 remove dead code. 2018-04-03 10:57:40 +02:00
Joris Vink 548068d2a0 Add http_request_ms configuration option.
This option allows a user to finetune the number of milliseconds
a worker process will max spend inside the http_process() loop.

By default this is 10ms.
2018-03-14 13:41:17 +01:00
Joris Vink 50c3d07b48 remove http_path_pool and http_host_pool.
No longer used.
2018-02-21 09:11:57 +01:00
Joris Vink dd2dff2318 Rework HTTP and worker processes.
The HTTP layer used to make a copy of each incoming header and its
value for a request. Stop doing that and make HTTP headers zero-copy
all across the board.

This change comes with some api function changes, notably the
http_request_header() function which now takes a const char ** rather
than a char ** out pointer.

This commit also constifies several members of http_request, beware.

Additional rework how the worker processes deal with the accept lock.

Before:
	if a worker held the accept lock and it accepted a new connection
	it would release the lock for others and back off for 500ms before
	attempting to grab the lock again.

	This approach worked but under high load this starts becoming obvious.

Now:
	- workers not holding the accept lock and not having any connections
	  will wait less long before returning from kore_platform_event_wait().

	- workers not holding the accept lock will no longer blindly wait
	  an arbitrary amount in kore_platform_event_wait() but will look
	  at how long until the next lock grab is and base their timeout
	  on that.

	- if a worker its next_lock timeout is up and failed to grab the
	  lock it will try again in half the time again.

	- the worker process holding the lock will when releasing the lock
	  double check if it still has space for newer connections, if it does
	  it will keep the lock until it is full. This prevents the lock from
	  bouncing between several non busy worker processes all the time.

Additional fixes:

- Reduce the number of times we check the timeout list, only do it twice
  per second rather then every event tick.
- Fix solo worker count for TLS (we actually hold two processes, not one).
- Make sure we don't accidentally miscalculate the idle time causing new
  connections under heavy load to instantly drop.
- Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
Joris Vink b3a48f3c15 Let http_request_limit matter.
Before http_request_limit just constrained the number of HTTP
requests we'd deal with in a single http_process_requests() call.

But it should really mean how many maximum HTTP requests are allowed
to be alive in the worker process before we start sending 503s back.

While here, drop the lock timeout for a worker to 100ms down from 500ms
and do not allow a worker to grab the accept lock if their HTTP request
queue is full.

This makes things much more pleasant memory wise as the http_request_pool
won't just grow over time.
2018-02-13 11:56:51 +01:00
Joris Vink 548348f553 2018 2018-01-20 22:51:06 +01:00
Joris Vink b95b623e72 Allow param blocks to be marked as "querystring"
Before params get would mean querystring and anything else
would just count toward a www-encoded body.

Now you can prefix the params block with "qs" indicating that
those configured parameters are allowed to occur in the query
string regardless of the method used.

This means you can do something like:

params qs:post /uri {
	...
}

to specify what the allowed parameters are in the querystring for
a POST request towards /uri.

inspired by and properly fixes #205.
2018-01-16 18:47:50 +01:00
Joris Vink 915b8e1d3c Use kore_bufs on the stack rather than the pools. 2018-01-15 22:31:54 +01:00
rouzier f0f1296265 Add patch support (#217)
Add PATCH to supported verbs in config and what not.
2018-01-02 22:27:59 +01:00
Joris Vink ae4201c647 make r const 2017-08-08 09:11:41 +02:00
Joris Vink 6415670753 set CONN_CLOSE_EMPTY for early HTTP errors.
while here fix missing connection response headers for errors.
2017-07-04 10:55:11 +02:00
Joris Vink 8e359ede13 flush out send buffer in http_error_response(). 2017-07-04 10:42:14 +02:00
Stanislav Yudin b73343aea4 add HTTP_METHOD_OPTIONS as another supported http method. (#186) 2017-04-04 09:37:19 +02:00
Joris Vink c545a922a1 Preserve the full host header under req->host.
Additionally make this header available via http_request_header().

prompted by #184
2017-03-30 09:38:23 +02:00
Joris Vink 59f7e85f45 Decouple pgsql from the http layer.
When the pgsql layer was introduced it was tightly coupled with the
http layer in order to make async work fluently.

The time has come to split these up and follow the same method we
used for tasks, allowing either http requests to be tied to a pgsql
data structure or a simple callback function.

This also reworks the internal queueing of pgsql requests until
connections to the db are available again.

The following API functions were changes:
	- kore_pgsql_query_init() -> kore_pgsql_setup()
		no longer takes an http_request parameter.
	- NEW kore_pgsql_init()
		must be called before operating on an kore_pgsql structure.
	- NEW kore_pgsql_bind_request()
		binds an http_request to a kore_pgsql data structure.
	- NEW kore_pgsql_bind_callback()
		binds a callback to a kore_pgsql data structure.

With all of this you can now build kore with PGSQL=1 NOHTTP=1.

The pgsql/ example has been updated to reflect these changes and
new features.
2017-03-24 12:53:07 +01:00
Joris Vink ec901d0339 Make http_body_rewind() public.
Also let this function reset offset and lengths for http_body_read().

Make sure of this function in the python code so req.body can be called
multiple times in succession.
2017-03-13 11:17:55 +01:00
Joris Vink 1f4aec43d9 toread is unsigned, it won't ever fall < 0. 2017-03-13 11:02:46 +01:00
Joris Vink 3ae9bb7ae9 change type of maxage. 2017-03-10 14:36:51 +01:00
Joris Vink 4db51d7846 screw it, rework interface for cookies.
The only reason you would want to directly modify the cookie
after creating it should be to unset the HTTPONLY or SECURE flags
if that is what you *really* want to do.

Change http_response_cookie() to take all required parameters instead
of having to marshall those in yourself after.

Now you set a sane default cookie in one shot:

http_response_cookie(req, "key", "value", "/", 0, -1, NULL);

Which would create a session cookie key=value for / under the current domain.
2017-03-10 14:31:08 +01:00
Joris Vink 3d24b65268 Change default http_cookie behaviour.
We now default to httponly & secure for newly created cookies.

This should've been the default all along.

The http_response_cookie() no longer returns a pointer to http_cookie
but rather takes it as a parameter and will populate the pointer with
the newly created http_cookie if not NULL.

Additionally http_response_cookie() automatically sets the domain
based on the http_request passed into the function.
2017-03-10 14:20:40 +01:00
Joris Vink 2f670ce777 don't log failure if unlinking fails with ENOENT 2017-02-22 17:52:38 +01:00
Joris Vink fc6b3bf740 Split up kore cli tools into new binary.
Having the create, build, run tools baked into the kore binary
made things harder then they had to be for multiple projects with
each different build flavors.

So move away this functionality into a new "kodev" (name may change)
binary that is installed next to kore.

The new build tools will automatically pick up the correct flavors
the kore binary it points to is installed with. Or for single builds
what flavors where enabled.

The new tool also will honor looking into PREFIX for the kore binary
when doing a `kodev run`.

Additionally add a new command "info" that shows some basic info
about your project and how it will be built. For example it will
show you the flavors of the kore binary installed on the system
or the flavors you configured for a single binary build.

Obligitory, hacking on a plane comment.
2017-02-19 00:52:29 -05:00
Joris Vink d2cfc2b554 shuffle headers around. 2017-02-07 23:21:18 +01:00
Joris Vink b8c6cddc3d Revert "TAILQ_FOREACH_SAFE() exists so use it."
Because some asshole distributions claim to have a sane queue.h
implementation while they do not.
2017-02-07 22:44:20 +01:00
Joris Vink 0ea911140e TAILQ_FOREACH_SAFE() exists so use it. 2017-02-07 22:35:09 +01:00
Joris Vink 233d5d1708 remove unneeded NULL check before calling free. 2017-02-07 22:08:04 +01:00
Joris Vink 1296802e06 fixup isspace arguments. 2017-02-07 22:06:14 +01:00
Joris Vink 8b9ea825eb cookie improvements.
- split up writing of cookies into its own function.
- turn maxage into a signed int and use -1 for it not being set.
- lots of style fixes
- remove HTTP_COOKIE_DEFAULT, just pass 0 if you don't want flags.
2017-02-07 22:03:06 +01:00
Stanislav Yudin f4ac8c2955 Cookies and arguments parsing improvements (#166)
Add new cookie API for handling of cookies.
2017-02-07 21:49:10 +01:00
Joris Vink f80a9cbc6c actually urldecode parameter names. 2017-02-06 22:47:32 +01:00
Joris Vink ace8c4e80c Add asynchronous pgsql query support to python.
This commit adds the ability to use python "await" to suspend
execution of your page handler until the query sent to postgresql
has returned a result.

This is built upon the existing asynchrous query framework Kore had.

With this you can now write stuff like:

async def page(req):
	result = await req.pgsql("db", "SELECT name FROM table");
	req.response(200, json.dumps(result).encode("utf-8"))

The above code will fire off a query and suspend itself so Kore can
take care of business as usual until the query is successful at which
point Kore will jump back into the handler and resume.

This does not use threading, it's purely based on Python's excellent
coroutines and generators and Kore its built-in pgsql support.
2017-02-06 11:42:53 +01:00
Joris Vink 0250c8ecba style 2017-02-01 21:20:43 +01:00
Joris Vink f56938283d Merge branch 'master' of github.com:jorisvink/kore 2017-01-25 22:23:34 +01:00
Joris Vink bbcdec82fc Add initial python support.
Based on work done by Stanislav Yudin.
2017-01-24 20:18:12 +01:00
Yorick de Wid f7e8954609 BSD required AF_INET* headers (#171) 2017-01-18 22:24:49 +01:00
Joris Vink 98148155dc bump copyright year 2017-01-18 10:27:44 +01:00
Joris Vink 2f8c173042 Improve ipv6 host parsing if we get a literal.
Fixes #169.
2017-01-18 10:27:04 +01:00
Yorick de Wid 565bf5bdec Set server version (#157) 2017-01-13 13:03:47 +01:00
Joris Vink 57840a8366 Deal with the Host header in a way IPv6 hosts work
Fixes #164.
2017-01-11 11:01:58 +01:00
Joris Vink 31d14d028a add compile-time configurable mime types.
these types are used for the new builtin asset_serve_* functions.

must be defined in conf/build.conf.
2016-12-26 23:37:05 +01:00
Joris Vink d783a1d22d Add auto generated serving functions for assets.
These functions are created by the cli tool when building
and follow the naming format: asset_serve_<name>_<ext>().

Those serving functions can be used directly in handlers and
callthrough to a http_serveable() function that uses the SHA1
of the asset as its ETag and automatically checks for if-none-match.
2016-12-26 21:15:03 +01:00
Joris Vink facc8b9d6c set req->owner to NULL when the connection removes it. 2016-12-26 20:08:53 +01:00
Joris Vink 0b92afe53d Explicitly set offset to 0 for http_file. 2016-07-27 16:43:05 +02:00
Joris Vink 43fec8678e kore_buf_create -> kore_buf_alloc. 2016-07-14 12:34:29 +02:00
Joris Vink 4ad50caa29 Large changes to the memory subsystem in kore.
- Change pools to use mmap() for allocating regions.
- Change kore_malloc() to use pools for commonly sized objects.
  (split into multiple of 2 buckets, starting at 8 bytes up to 8192).
- Rename kore_mem_free() to kore_free().

The preallocated pools will hold up to 128K of elements per block size.

In case a larger object is to be allocated kore_malloc() will use
malloc() instead.
2016-07-12 13:54:14 +02:00
Joris Vink d30921103b Code cleanup, several API breaking changes in here 2016-07-08 10:03:41 +02:00
Joris Vink 2dfd22a79a Change kore_buf_stringify() a bit.
Takes a size_t pointer as its second argument now, if not
NULL this will be populated with the length of the string
that is being returned.
2016-06-02 07:08:19 +02:00
Joris Vink fb6512259c small style changes. 2016-03-21 15:30:11 +01:00
Ángel González 1bda217d57 Based on #115 pull request, adds http_method_text, similar to http_status_text 2016-03-21 15:12:27 +01:00
Joris Vink f7b0b7c7ac Sometimes content_length wasn't properly updated. 2016-02-22 14:08:21 +01:00
Joris Vink 8fcf4762f4 Improve http_body_recv().
- If we fail to write to our temporary file error instead of fatal.
- Return KORE_RESULT_ERROR on other errors as well.
2016-02-13 15:41:37 +01:00
Joris Vink 12e1820a4f Unbreak normal POST parsing.
It was broken for > 2 fields due to recent large changes,
unfortunately i didn't catch this until now.
2016-02-06 15:32:23 +01:00
Joris Vink f4d00645ed Merge pull request #99 from oneswig/master
Add resource management as part of the kore shutdown process.
2016-02-01 19:51:27 +01:00
Joris Vink 1f5e482b8a Build option changes.
- Build with -O2 unless NOOPT is set to 1.
- Hide -g behind DEBUG instead of always building with it.
- Explicitely set the standard used to c99, use pedantic.
2016-02-01 15:33:40 +01:00
Stig Telfer 55d2451b90 Add cleanup of http_body_path pool 2016-02-01 10:10:04 +00:00
Stig Telfer 2ac6e7d41d Merge branch 'master' into oneswig 2016-01-24 13:46:38 +00:00
Stig Telfer ec73c35952 Add missing C library headers 2016-01-22 11:08:13 +00:00
Joris Vink 192f458f97 remove unused variable. 2016-01-18 11:43:44 +01:00
Joris Vink fcb86ddb8b Massive rework of HTTP layer.
This commit is a flag day, your old modules will almost certainly
need to be updated in order to build properly with these changes.

Summary of changes:

- Offload HTTP bodies to disk if they are large (inspired by #100).
  (disabled by default)
- The http_argument_get* macros now takes an explicit http_request parameter.
- Kore will now throw 404 errors almost immediately after an HTTP request
  has come in instead of waiting until all data has arrived.

API changes:

- http_argument_get* macros now require an explicit http_request parameter.
  (no more magic invokations).
- http_generic_404() is gone
- http_populate_arguments() is gone
- http_body_bytes() is gone
- http_body_text() is gone
- http_body_read() has been added
- http_populate_post() has been added
- http_populate_get() has been added
- http_file_read() has been added
- http_file_rewind() has been added
- http_file_lookup() no longer takes name, fname, data and len parameters.
- http_file_lookup() now returns a struct http_file pointer.
- http_populate_multipart_form() no longer takes an secondary parameter.

New configuration options:

- http_body_disk_offload:
	Number of bytes after which Kore will offload the HTTP body to
	disk instead of retaining it in memory. If 0 this feature is
	disabled. (Default: 0)

- http_body_disk_path:
	The path where Kore will store temporary HTTP body files.
	(this directory does not get created if http_body_disk_offload is 0).

New example:

The upload example has been added, demonstrating how to deal with file
uploads from a multipart form.
2016-01-18 11:30:22 +01:00
Joris Vink e580e6678e Simplify checking filename component. 2016-01-08 21:19:37 +01:00
Joris Vink ce874f09ea Cut memory usage for incoming HTTP bodies in half.
Kore pre-allocates a kore_buf for the full size of the
incoming HTTP body ... but also was passing the full
size to the net_recv_reset() function.

Instead of this, properly read smaller chunks from the
network and append them to the body buffer as they roll in.
2016-01-08 20:56:21 +01:00
Joris Vink 0647901ef5 Improve http_body_max directive a bit.
Allow setting it to 0 which will disable HTTP requests
that have a body (POST/PUT).

Reduce default http_body_max to 1MB by default, 10MB seems large.

Revisit to this code inspired by #100.
2016-01-08 17:54:40 +01:00
Joris Vink 0c47574fe9 If a body is too large send a 413 instead of 411. 2016-01-08 17:47:15 +01:00
Joris Vink f2d8834e8e After receiving a body, prime the netbuf again.
Not doing this results in nothing being read anymore
from the connection after a POST with a content-length of > 0.

Found by Xuning Niu.
2016-01-07 10:40:33 +01:00
Stig Telfer 18d3cc032d rename *_fini to *_cleanup 2016-01-04 21:40:14 +00:00
Joris Vink c4b1206ae3 Bump copyright to 2016. 2016-01-04 12:58:51 +01:00
Stig Telfer 0c51d9da53 Add resource management as part of the kore shutdown process. 2015-12-29 19:39:39 +00:00
Joris Vink 6ac15d37cb Make sure user agent is still set properly. 2015-12-10 09:25:22 +01:00
Joris Vink 4fd6d8a7a4 Correct usage of http_request_header().
Since latest change we no longer need free its result.
2015-11-29 17:22:30 +01:00
Joris Vink 7bdae240cf Change semantics for http_request_header().
The result returned by this function no longer needs to
be freed by the caller.
2015-11-29 14:19:44 +01:00
Joris Vink 769c78a6e8 Introduce NOHTTP=1 build option.
This basically turns off the HTTP layer for Kore. It does not
compile in anything for HTTP.

This allows Kore to be used as a network application platform as well.
Added an example for this called nohttp.

Other changes that sneaked in while hacking on this:
* Use calloc(), kill pendantic malloc option.
* Killed off SPDY/3.1 support completely, will be superseded by http2

Note that comes with massive changes to a lot of the core API
functions provided by Kore, these might break your application.
2015-11-27 16:22:50 +01:00
Joris Vink a20fb00789 req can be NULL if we are coming from an error path. 2015-08-02 16:53:40 +02:00
Joris Vink be3fa17201 Do not send connection:close for websocket upgrades. 2015-06-30 16:43:21 +02:00
Cleve Lendon 18becccd5a Fix indentation. 2015-06-30 18:07:45 +09:00
Cleve Lendon fef3ee8efe Fix websockets. Connection must be Upgrade, not keep-alive, Upgrade 2015-06-26 12:14:01 +09:00
Joris Vink a97e18bbe9 Shuffle some HTTP_REQUEST_NO_CONTENT_LENGTH around 2015-05-19 09:13:29 +02:00
Joris Vink e33710b26a Wrap expression with some additional parenthesis. 2015-05-19 09:04:46 +02:00
Quentin Perez ff4faa98b7 fix-null-dereference
http.c:1214, req->method
http.c:1272, req->flags
2015-05-18 23:42:50 +02:00