Commit Graph

990 Commits

Author SHA1 Message Date
Joris Vink f348feef82 Add pgsql_queue_limit configuration option.
Limits the number of queued asynchronous queries kore will allow
before starting to return errors for KORE_PGSQL_ASYNC setups.

By default set to 1000.

Avoids uncontrolled growth of the pgsql_queue_wait pool.
2018-02-13 13:21:27 +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 f8b3915ee1 make kore_websocket_send() slightly faster.
build the frame in a kore_buf on the stack and use net_send_stream()
to push it out rather then making yet another copy in net_send().

saves several cpu cycles before the frame starts going out as
we're not allocating a kore_buf, its contents and then memcpy()ing
that contents to a new netbuf.
2017-12-24 17:28:17 +01:00
Joris Vink 2eab2f1ed7 Revert "automatically resolve existing symbols upon reload."
This isn't ready yet.
2017-08-31 17:11:24 +02:00
Joris Vink ed4ef22f1b automatically resolve existing symbols upon reload.
doing this allows us to get rid of the validator reload
and handler reload as well as fixing websocket runtime
callbacks which were never being resolved upon module reloads.
2017-08-31 16:26:36 +02:00
Joris Vink f958d86616 remember the onload callback like we used too. 2017-08-30 15:11:07 +02:00
Joris Vink 40c93e66a2 Add memory tagging.
This allows modules that have global pointers to upon reload repopulate
those with the addresses of when they were first created.

Meaning it now is easier to write modules that can be reloaded if those
modules kept global state one way or the other.

This should only be used at module init/reload time and is very simple:

if ((ptr = kore_mem_lookup(MY_ID_VALUE)) == NULL) {
	ptr = kore_malloc_tagged(length, MY_ID_VALUE);
	/* initialize for the first time. */
}

If we were in a reload the kore_mem_lookup() will return the original address
returned by the initial kore_malloc_tagged() call for MY_ID_VALUE.
2017-08-30 11:47:26 +02:00
Joris Vink 4893a030a6 small pgsql fixes.
- make sure conn_count per pgsqldb structure is initialized to 0.
- allow pgsql_conn_max to be 0, meaning just create a new connection
  if none was free.
2017-08-21 14:25:09 +02:00
Joris Vink ae4201c647 make r const 2017-08-08 09:11:41 +02:00
Joris Vink 6b60e4c6cc set nb to 0 2017-07-24 08:19:57 +02:00
Joris Vink fcc55453c7 massively simplify base64 encoding. 2017-07-24 08:19:03 +02:00
Joris Vink 8215277483 small pgsql fix.
if we fail at rolling back an in-error transaction on a connection
just remove that connection and go back to rescanning rather then
returning an error to the caller, there may be more functional
connections in the pipeline.
2017-07-13 11:02:41 +02:00
Joris Vink 3cea669a2c call pgsql_conn_cleanup() in case of an error in rollback. 2017-07-11 15:19:44 +02:00
Joris Vink 7f1a9b8092 Several postgresql improvements.
- Make pgsql_conn_count count per database rather then globally.
  This means you now define the number of clients *per* database registered
  rather then the number of clients in total of all databases.

- In case a connection is in failed transaction state Kore will now
  automatically rollback the transaction before placing that connection
  back in the connection pool.
2017-07-11 15:11:13 +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
Joris Vink ae9694fb1d Fix build with libressl again.
LibreSSL defines OPENSSL_VERSION_NUMBER as 0x20000000L but does not have
the 1.1.0 API so we have to carefully check for LIBRESSL_VERSION_TEXT as
well before using that new API.
2017-05-27 11:57:46 +02:00
Joris Vink 95daf3a62b Add support for openssl 1.1.0 release line.
Eventually I will phase out 1.0.2 down the line to get rid of the
nightmare that is the 2 different APIs.

This commit adds full support for building kore with 1.1.0e while
retaining the privsep keymanager support.

based on excellent work done by @hiwk.
2017-05-22 14:31:38 +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 a3ed3bf7eb Convert pgsql-sync example after pgsql changes.
Only check if we bound something if we're asynchronous.
2017-03-24 13:00:05 +01: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 add6d724e3 expose connection address to python. 2017-03-24 12:42:40 +01:00
Joris Vink ed9a34ce95 cleanup net layer a bit.
this change was long overdue and finally gets rid of the legacy crap
we had laying around to appease openssl's read/write functions.
2017-03-16 09:54:46 +01:00
Joris Vink 3fdc1113c3 formatting 2017-03-15 09:24:29 +01:00
Joris Vink fb8035297d Allow workers to listen on different ports.
This commit allows worker processes to have individual listeners
not configured by the kore configuration.

If you do the following:
	- do not configure listeners in your .conf file
	- call kore_server_bind() in kore_worker_configure()

the workers will no longer fight over accept locks as the configured
ports no longer conflict with each other.

This allows me to create X amount of instances of a worker process that
are each individually accessible via unique ports.
2017-03-13 15:32:04 +01:00
Joris Vink ffbabed7d6 Move websocket_send() to pyconnection.
While here convert all instances of y* for incoming binary data to y#
since we won't be changing it at all and avoids some unneeded copying.
2017-03-13 13:30:57 +01:00
Joris Vink 4a56424842 Improve python websocket handling a bit.
Convert the data parameter to a string if the op was WEBSOCKET_OP_TEXT
or convert to bytes if op was WEBSOCKET_OP_BINARY so the callee does
not have to do this anymore.
2017-03-13 12:29:42 +01:00
Joris Vink 0b84a9c1d4 expose c.disconnect() and c.fd to Python. 2017-03-13 12:22:50 +01:00
Joris Vink af899f15e0 Set error if http_body_rewind() fails. 2017-03-13 11:19:58 +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 c87a9286b4 reword a bit 2017-03-09 16:59:14 +01:00
Joris Vink 0ca08114bb unbreak flavor cflags 2017-03-09 16:55:04 +01:00
Joris Vink 41924d0262 make sure len isn't 0 from cli_file_read(). 2017-03-06 23:16:59 +01:00
Joris Vink 56d451ebcf bump 2017-03-06 22:49:26 +01:00
Joris Vink f8c9e736e2 don't depend on \n for NUL-terminating, just do it. 2017-03-06 22:48:19 +01:00
Joris Vink 0364cc893c Write new rand_file immediately at startup. 2017-03-06 10:55:25 +01:00
Joris Vink 8aaf7866c8 Do not unlink assets.h after building.
as per suggestion in #175
2017-03-06 10:42:07 +01:00
Joris Vink 7ec26a8c30 switch asset checksum to SHA256.
while here constify the asset modified time, contents and length.
2017-03-06 10:33:44 +01:00
Joris Vink c136c5ffc5 python_calloc() no longer needs manual memset. 2017-02-27 21:34:26 -08:00
Joris Vink 73fc58163d add warning + RAND_poll() to callback. 2017-02-27 21:31:09 -08:00
Joris Vink f57ca7dcc2 Let workers fetch entropy from keymgr.
At bootup and every 1800 seconds after that the worker processes will
ask the keymgr for new entropy that they will seed into their PRNG.

Additionally once received the worker calls RAND_poll() to grab
more entropy from the system to be mixed in.
2017-02-27 21:28:35 -08:00
Joris Vink 60a3f60a92 Allow MSG framework to pass 0 byte messages. 2017-02-27 21:27:50 -08:00
Joris Vink 0fe570ef41 cleanse any intermediate buffers for keymgr. 2017-02-27 21:05:56 -08:00
Joris Vink 6ba7390cc9 Add rand_file configuration option for keymgr.
This option allows the user to specify a file to be used for
seeding the PRNG initially and to write random bytes at exit.

The option is only available if kore has TLS enabled (by default).

If you enable this option Kore will refuse to start if there is
a problem with the file specified (not found, not a file, invalid size, etc).

While here let the keymgr process call RAND_poll() every half hour
to grab more system entropy and seed it into the PRNG.
2017-02-27 20:58:04 -08:00
Joris Vink 27da1dd741 fix semantics for kore_calloc().
We were not returning zeroed out memory from kore_calloc() which goes
against what calloc() does. Skip performance for now and simply just
memset() the returned pointer from kore_malloc().

This should be sufficient enough for now.
2017-02-27 19:50:06 -08:00
Joris Vink 7f11e37161 Add kore_sockopt().
Use it where we before were using setsockopt().
2017-02-22 13:23:30 -08:00
Joris Vink 00ce970efe Be less noisy about module reloading.
No longer show "not reloading foo" if timestamps didn't change.
Be more clear about a module forcing us to not reload it.
2017-02-22 12:48:06 -08:00
Joris Vink d09eb5e107 bump copyright 2017-02-22 21:37:14 +01:00
Joris Vink e89e644d10 call PyErr_Clear() before PyImport_ReloadModule(). 2017-02-22 20:54:57 +01:00
Joris Vink 93ab52dcf5 Add a reload command to the cli tool.
This will look at the kore.pid file in the current directory
and send a SIGHUP signal to it. It's mostly a handy shortcut
since you could of course do a kill -HUP `cat kore.pid` easily.
2017-02-22 20:38:24 +01:00
Joris Vink 970356a3e9 always write the pidfile even in foreground 2017-02-22 20:38:07 +01:00
Joris Vink 6e6d2f005d Remove leftover feature defines from kodev. 2017-02-22 20:13:59 +01:00
Joris Vink ead0a4199c add little hint for kodev. 2017-02-22 20:09:11 +01:00
Joris Vink 541870c10a Improve keymgr_await_data().
Before this function would block client I/O and existing HTTP requests
until the keymgr process responsed with a result.

This commit changes that behaviour and makes this function call
the http_process() function if we end up waiting for the keymgr.

This means that while waiting for a response we at least start
making headway with existing HTTP requests if the response is
not immediate.
2017-02-22 18:30:50 +01:00
Joris Vink c6ca68f3f2 export http_body_path via python 2017-02-22 17:52:57 +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 a0c545884f even more ssl to tls terminology. 2017-02-11 21:33:09 +01:00
Joris Vink 98f0c41f38 more ssl -> tls wording 2017-02-11 21:30:38 +01:00
Joris Vink 9be3c22196 like other flavors, log if python is enabled. 2017-02-09 13:38:09 +01:00
Joris Vink 37443df6b0 Slightly change kore_parent_configure() again.
- Call it before loading configurations when built as single binaries.
- Call it right before forking workers when built as a dso.
2017-02-09 13:36:08 +01:00
Joris Vink 311e658d57 call to kore_parent_configure() before config. 2017-02-09 13:29:33 +01:00
Joris Vink 625d50b8dd use rootdir when making paths to features/ldflags. 2017-02-09 11:45:11 +01:00
Joris Vink e895446dfa Improve single binary builds.
Allow kore build to pickup the required libraries for kore when
building single binaries so that you no longer have to manually
add them to ldflags.
2017-02-09 11:30:44 +01:00
Joris Vink d2cfc2b554 shuffle headers around. 2017-02-07 23:21:18 +01:00
Joris Vink 4e9399d553 use SSL_OP_SINGLE_ECDH_USE. 2017-02-07 23:17:11 +01:00
Joris Vink e9b4f966c2 expose new cookie stuff to python.
req.populate_cookies()
value = req.cookie("name")
2017-02-07 22:54:42 +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 5a16bdbe65 whitespace 2017-02-06 22:53:09 +01:00
Joris Vink f80a9cbc6c actually urldecode parameter names. 2017-02-06 22:47:32 +01:00
Joris Vink 604fae3a01 unfuck parsing http_body_max. 2017-02-06 21:28:33 +01:00
Joris Vink c25c1c5281 pgsql: don't do a PQcancel() if it's not needed. 2017-02-06 20:01:16 +01:00
Joris Vink 09b0ae6d42 rename local sin to avoid potential conflicts. 2017-02-06 19:54:50 +01:00
Joris Vink b8b8373702 when exiting clear any lingering python exceptions. 2017-02-06 16:54:38 +01:00
Joris Vink 01e3ef6cd3 no need to log ignored signals. 2017-02-06 12:13:34 +01:00
Joris Vink 6592eddcfb add stdint.h 2017-02-06 12:13:29 +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 80698ee40a add kore_mem_cleanup() to cleanup pools at exit. 2017-02-06 11:42:23 +01:00
Joris Vink de7a6d4855 pgsql improvements.
- adds new cleanup function that workers will call.
- adds kore_pgsql_nfields() to return number of fields in result.
- add kore_pgsql_fieldname() to return name of a given field.

This commit also changes the behaviour of pgsql_conn_release() in
that it will now cancel the active query before releasing the connection.

This makes sure that if long running queries are active they are hopefully
cancelled if an http request is removed while such queries are still running.
2017-02-06 11:40:33 +01:00
Joris Vink c3d2984939 fix parameters to kore_log(). 2017-02-06 11:39:50 +01:00
Joris Vink 6578fc6589 expose http_file interface to python. 2017-02-01 21:23:56 +01:00
Joris Vink 0250c8ecba style 2017-02-01 21:20:43 +01:00
Joris Vink 3d8e0dabc0 expose kore_server_bind() and fatal() to python 2017-02-01 17:12:52 +01:00
Joris Vink 0c0a9371bd Change kore_preload() and kore_onload().
Renamed both of them:
	kore_preload -> kore_parent_configure
	kore_onload  -> kore_worker_configure

These functions will now always be called if they are defined in any module
regardless of your application being built as a single binary or not.
2017-02-01 17:12:11 +01:00
Joris Vink bb9dbbec12 typo. 2017-01-30 22:47:41 +01:00
Joris Vink 006f764d96 when getting OP_CLOSE disable reads. 2017-01-30 22:35:34 +01:00
Joris Vink 587a663f31 reorder ws disconnect order a bit. 2017-01-30 22:25:08 +01:00
Joris Vink 8b26ecdf14 more websocket improvements.
- send a CLOSE opcode if we haven't yet when removing a ws client.
- ignore unsolicited PONG.
2017-01-30 22:21:57 +01:00
Joris Vink 30666618f8 expose kore_websocket_send() to python. 2017-01-30 22:00:03 +01:00
Joris Vink e4d87b72a0 websocket improvements.
- properly send a CLOSE control frame back if the client sent us one.
- make sure we can send messages that are exactly USHRT_MAX bytes long.
2017-01-30 21:52:22 +01:00
Joris Vink 6bfba49ba5 cleanup data if required. 2017-01-30 21:50:36 +01:00
Joris Vink f78e8f2fd2 allow None as source for kore.websocket_broadcast.
This allows kore.websocket_broadcast() to be used to relay the
messages back to all clients rather then all except the sender.
2017-01-30 21:48:58 +01:00
Joris Vink 6a43dd529c export WEBSOCKET_OP_TEXT and WEBSOCKET_OP_BINARY. 2017-01-30 21:02:58 +01:00
Joris Vink 7bc8bb42e2 implement kore_websockat_broadcast. 2017-01-30 20:30:42 +01:00
Joris Vink 7510834968 initial fudging to add websockets to python 2017-01-29 22:57:34 +01:00
Joris Vink 1ecb777d41 only remove the pgsql if it was scheduled.
fixes #172.
2017-01-29 10:34:53 +01:00
Joris Vink c06ecf1c70 Teach single binaries about SIGHUP.
Make sure kore_onload() and kore_preload() can be called
either in native or python runtime.
2017-01-26 13:26:55 +01:00
Joris Vink 3023ca0e57 Allow single binaries to load modules again. 2017-01-26 13:13:23 +01:00
Joris Vink dcb5fd842f Expose get/post params via req.argument for python. 2017-01-26 12:01:28 +01:00
Joris Vink 1db3cd96d8 allow reloading of python modules on SIGHUP. 2017-01-26 11:13:09 +01:00
Joris Vink 21f9c29f31 style + move pyhttp_dealloc() into !KORE_NO_HTTP. 2017-01-25 22:33:39 +01:00
Joris Vink f56938283d Merge branch 'master' of github.com:jorisvink/kore 2017-01-25 22:23:34 +01:00
Joris Vink 5d09214591 only rebuild with "kore build" when we can. 2017-01-25 22:21:40 +01:00
Joris Vink a9537bc6ec add python flavor string to -v 2017-01-25 22:21:30 +01:00
Joris Vink 9976022a47 let python use kore backed memory allocators. 2017-01-25 22:07:58 +01:00
Joris Vink e47806c669 allow memory allocators to deal with 0 bytes.
we just increase the lenght requested to 8 bytes if we get a 0 byte request.

additionally when kore_realloc() is called check if we actually have
to do the work, if not just return the original pointer.
2017-01-25 22:07:08 +01:00
Joris Vink 620cc231dc validators now work in python. 2017-01-25 21:17:12 +01:00
Joris Vink 7c74a0824b push all integers exported to python in a struct. 2017-01-25 20:56:57 +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 86d85dd10b Message shutdown request (#168)
* Set server version

* Message shutdown request

* Update messaging example

* Set server verion in generic example

* Allow shutdown with NOHTTP=1
2017-01-17 18:18:01 +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 10929189a0 make sure we can call pgsql_conn_cleanup() properly. 2017-01-10 15:29:03 +01:00
Joris Vink f5ad76a2c3 better description. 2016-12-29 20:15:01 +01:00
Joris Vink fe7237fd74 Take over linker from env if present. 2016-12-29 20:14:16 +01:00
Joris Vink 6faed21a24 Revert "Invoke platform alternatives #158 (#160)"
This reverts commit 857a3cfb79.
2016-12-29 20:12:46 +01:00
Yvan Sraka 857a3cfb79 Invoke platform alternatives #158 (#160) 2016-12-28 09:29:13 +01:00
Joris Vink c16461c6c1 adjust format after changing it earlier. 2016-12-27 09:45:27 +01:00
Joris Vink be5d1f310f initialize mime_types when swapping flavors as well. 2016-12-27 09:39:06 +01:00
Joris Vink 8b528e0334 Add asset_sha1_* and asset_serve_* to assets.h. 2016-12-27 09:22:13 +01:00
Joris Vink 4f9044c6e0 fix count in kore_pgsql_query_params().
should fix #159.
2016-12-27 09:10:31 +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 543a329ef6 run the connection timeout test per event loop. 2016-12-26 20:03:01 +01:00
Joris Vink 194b575ebf Add SHA1 digests for assets. 2016-12-22 19:44:35 +01:00
Joris Vink 583d75edcb remove unneeded parenthesis. 2016-12-05 14:45:29 +01:00
Joris Vink ae28f51d56 ERR_clear_error() in front of SSL_accept as well. 2016-12-05 14:44:58 +01:00
Joris Vink cb13190e0d Call ERR_clear_error() before SSL_read() and SSL_write() calls. 2016-12-05 14:24:22 +01:00
Joris Vink 16166d37bd remove debug macro 2016-12-04 16:52:11 +01:00
Joris Vink 652833a979 set worker_active_connections to 0 always.
fixes a bug where if a worker died it contained garbled data...
2016-12-04 16:49:42 +01:00
Joris Vink cb62a6a8a8 Clamp netwait to maximum 100ms. 2016-12-04 12:16:27 +01:00
Tobias Kortkamp 950977f2be Fix fd leak in cli_buildopt_parse() (#156)
The build.conf file is left open when using kore run.
2016-12-01 09:01:51 +01:00
Tobias Kortkamp 7eced6f035 Fix #153 (#155)
Compiling with Clang 3.9 on FreeBSD raises the following error (see [1]):
```
src/pgsql.c:222:17: error: passing an object that undergoes default argument promotion to 'va_start'
      has undefined behavior [-Werror,-Wvarargs]
        va_start(args, count);
                       ^
src/pgsql.c:217:45: note: parameter of type 'u_int8_t' (aka 'unsigned char') is declared here
    const char *query, int result, u_int8_t count, ...)
```
More information about this warning can be found on [2].

To solve this we can change the type of `count` to `int` in
`kore_pgsql_query_params()` and `kore_pgsql_v_query_params()`. This
also matches the signatures of both `PQexecParams()` [3] and
`PQsendQueryParams()` [4].

[1] https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=214639
[2] https://www.securecoding.cert.org/confluence/display/cplusplus/EXP58-CPP.+Pass+an+object+of+the+correct+type+to+va_start
[3] https://www.postgresql.org/docs/9.2/static/libpq-exec.html
[4] https://www.postgresql.org/docs/9.2/static/libpq-async.html
2016-11-19 12:56:51 +01:00
Joris Vink 5b379a9185 add a kore_preload() call for single binaries.
This is run on the parent before workers are spawned.
2016-11-17 10:05:16 +01:00
Joris Vink 5abb56d6fb remove unused define. 2016-11-04 09:21:09 +01:00
Joris Vink 5e1cb53f13 use a copy of args when falling back to vasprintf. 2016-11-04 09:16:05 +01:00
Joris Vink 552d59248c constify delim in kore_split_string(). 2016-10-06 16:50:41 +02:00
Joris Vink 4a72877f4e count active connections properly 2016-10-06 16:50:02 +02:00
Tobias Kortkamp c071d64bdd Fix #144. Always initialize va_list in kore_pgsql_query_params. (#146) 2016-09-05 16:22:46 +02:00
Joris Vink db4f6cf01f align pointers from kore_malloc() properly. 2016-07-28 14:52:15 +02:00
Joris Vink 0b92afe53d Explicitly set offset to 0 for http_file. 2016-07-27 16:43:05 +02:00
Raphaël Monrouzeau 06d9ca96da JSONRPC Updated to last kore_buf* commit 2016-07-15 13:17:30 +02:00
Raphaël Monrouzeau 32ac27d4c3 JSONRPC Reverted explicit deallocation calls
The possibility to call jsonrpc_destroy_request was left. Someone may
want to abruptly interrupt the process of its request for some odd
reason, in that case an exlicit call still would be to be made.
2016-07-15 13:08:08 +02:00
Raphaël Monrouzeau 7a4e4223c4 JSONRPC If support compiled in log it at start 2016-07-15 13:08:08 +02:00
Raphaël Monrouzeau a818555c96 JSONRPC use size_t to get in line with last commit
Kore now uses size_t for kore_buf and http_response() so response size
check is useless and u_int32_t variables should be converted.
2016-07-15 13:08:08 +02:00
Raphaël Monrouzeau 016dc27346 JSONRPC Made request destruction explicit
Caller has now to destroy jsonrpc_request after use. This permits them
to read / inspect it after having responded.
2016-07-15 13:08:08 +02:00
Raphaël Monrouzeau 8c78b28be3 JSONRPC Removed upload size limit check
A true application dependant limit check would require stream parsing.
As the limit enforcement was done, it added nothing of value compared
to HTTP request limit check, which is in Kore already.
2016-07-15 13:08:08 +02:00
Raphaël Monrouzeau 3366ec6573 Required params to be structured as per spec 2016-07-15 13:08:08 +02:00
Raphaël Monrouzeau 4ffe43cf87 Compliantly don't return anything to notifications
And don't return anything either if protocol doesn't match.
2016-07-15 13:08:08 +02:00
Raphaël Monrouzeau db02e990ea JSON-RPC support for Kore.
The API surface is very limited. Jsonrpc support reads request from HTTP
body and such can't be activated if NOHTTP=1. At the moment there is no
websocket support either (which is a shame). It depends upon the
third-party Yajl library.

Errors can be emitted using jsonrpc_error() and results using
jsonrpc_result(), for the later you'll have to provide a callback which
will write the inner of the result object.

If errors happen during the response write process, no further error
output will be attempted and an HTTP error 500 will be returned.

Read the provided example for getting a better idea of the API.
2016-07-15 13:08:08 +02:00
Joris Vink 00b64033ca More buf improvements.
- make sure kore_buf_alloc() and kore_buf_init() buffers behave
  the same way in regards to calling kore_buf_release() and what not.
2016-07-14 12:41:39 +02:00
Joris Vink 43fec8678e kore_buf_create -> kore_buf_alloc. 2016-07-14 12:34:29 +02:00
Joris Vink b28b60c2ff buf API changes.
- rename kore_buf_destroy() to kore_buf_cleanup().
- rename kore_buf_create() to kore_buf_alloc().
2016-07-14 12:33:13 +02:00
Joris Vink 3b0477cf97 kore_mem_free -> kore_free 2016-07-14 12:29:49 +02:00
Joris Vink 7cf73b5fa5 Merge branch 'kore_buf_noalloc' of https://github.com/raphaelmonrouzeau/kore into raphaelmonrouzeau-kore_buf_noalloc 2016-07-14 12:28:51 +02:00
Joris Vink 33c0b3c753 Make pools thread-safe if built with TASKS.
Use CAS to implement primitive spinlock ontop of the pool
if we have been built with TASKS=1.

This allows tasks to safely use kore_malloc() and kore_free()
since those are now backed by pools.
2016-07-12 16:30:57 +02:00
Joris Vink 3c43d1203c Move towards size_t in the pool API. 2016-07-12 14:01:02 +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
Raphaël Monrouzeau db3cf28d22 Added kore_buf no alloc functions.
kore_buf_create and kore_buf_free do use kore_malloc. But sometimes you
may embed a kore_buf inside a structure and would like a single way to
initialize / destroy it.
2016-07-09 12:30:24 +02:00
Joris Vink d30921103b Code cleanup, several API breaking changes in here 2016-07-08 10:03:41 +02:00
Joris Vink e96ed9ad66 Change default domain for kore create skeleton. 2016-07-07 12:36:08 +02:00
Joris Vink d2c30fd70b for single binaries use progname as the log id. 2016-07-06 22:02:10 +02:00
Joris Vink 9abf15a498 Make kore_onload() to be done per worker.
Mimics the behaviour of dso builds.
2016-07-06 21:59:17 +02:00
Joris Vink 39a5f21986 Allow "kore build" to produce single binaries.
Producing single binaries can now be done with building with
"kore build". To get started edit your build.conf and add the
following directives:

single_binary = yes
kore_source = /path/to/kore

optionally you can add kore_flavor to instruct how kore should
be built:

kore_flavor = NOTLS=1

When doing this your build.conf must also include the correct
linking options as the linking is now done fully by kore build.

The binary produced will include your configuration and takes
over a few of kore its command line flags (such as -f, -n or -r).
2016-07-06 16:16:15 +02:00
Joris Vink 27acc51241 Improve kore_strlcpy().
Make it return the original length of the input string so the caller
can check for truncation. Also guard against len being 0 as this would
not do anything with the destination string (not even NUL terminate it).
2016-07-04 11:41:37 +02:00
Joris Vink 589c7667be Cleanup cli.c a bit.
- Define CXXFLAGS_MAX as CFLAGS_MAX as we use CFLAGS_MAX usually.
- Fix formatting of a few blocks of code.
2016-06-29 16:25:52 +02:00
Corbin Hughes 91d29a9914 Better name consistency and only print CXXFLAGS if compiling C++ 2016-06-27 22:33:51 -05:00
Corbin Hughes 698b3f374f Compile and link C++ with g++ instead of gcc 2016-06-26 15:49:59 -05:00
Joris Vink 8d92dd2af4 Allow building against libressl again. 2016-06-26 11:36:33 +02:00
Joris Vink 43a726c02d Use sys/types.h instead of sys/param.h for assets. 2016-06-23 16:18:36 +02:00
Joris Vink 89f81a8b5a Revert "Throw a handshake failure if we cannot find the given SNI hostname."
This reverts commit afd4182975.
2016-06-09 13:52:37 +02:00
Joris Vink afd4182975 Throw a handshake failure if we cannot find the given SNI hostname. 2016-06-08 16:46:28 +02:00
Joris Vink f62430d1fa Isolate ECDSA keys in keymgr as well. 2016-06-08 16:31:14 +02:00
Joris Vink ae31ec01ac Separate private keys from worker processes.
Kore will now isolate RSA private keys to a separate process (keymgr).

Worker processes that require RSA signing for TLS connections will
communicate with this keymgr process in order to do so.

This behaviour cannot be disabled and is always turned on.
2016-06-08 13:56:38 +02:00
Raphaël Monrouzeau 159de3960d Added new function kore_pgsql_v_query_params().
Same as kore_pgsql_query_params but takes a va_list as last parameter
(non-v version takes a variable list of parameters).

Lets people write easier to call wrappers around the query calls. I use
it in a wrapper that takes next states (error, current, continue) as
arguments in a handler with multiple async queries.
2016-06-07 13:12:31 +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 3b43df5536 remove useless cast. 2016-05-16 09:45:27 +02:00
Joris Vink 3c9d2d5948 missing va_end() in kore_pgsql_query_params().
from @fahlgren.
2016-05-16 09:25:37 +02:00
Joris Vink 5fe9512828 spacing. 2016-05-05 15:29:43 +02:00
Joris Vink be28c77c6d Merge pull request #118 from wtfuzz/master
cast difference between pointers as unsigned in kore_mem_find()
2016-05-05 15:28:59 +02:00
Elliot Schlegelmilch d37ecf9649 If we're iterating over rlimits, we should use rlim_t.
> error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'rlim_t' (aka 'long') [-Werror,-Wsign-compare]
2016-04-27 18:18:21 -07:00
Matt Thompson 43d3e0e746 double tab variable name 2016-04-01 17:55:43 +00:00
Matt Thompson 43a5703456 changed needle length type to size_t in kore_mem_find(). updated function prototype in kore.h. Use tab instead of space indentation 2016-04-01 17:54:10 +00:00
Matt Thompson 3ef8562bac use offset variable instead of pointer arithmetic 2016-03-31 12:55:23 -05:00
Matt Thompson 61eaaccd30 cast difference between pointers as unsigned in kore_mem_find() 2016-03-21 23:30:28 +00: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 4a69657dce Don't call close on an fd that is -1. 2016-02-25 22:04:24 +01:00
Joris Vink f7b0b7c7ac Sometimes content_length wasn't properly updated. 2016-02-22 14:08:21 +01:00
Joris Vink 0294212a3c Do not free results from http_request_header(). 2016-02-21 18:29:06 +01:00
Joris Vink 318647945f more consistency in fatal messages. 2016-02-13 15:45:33 +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 c83abd5afb If ele is 0 return 0 instead of fataling. 2016-02-13 15:27:02 +01:00
Joris Vink 1c8836b2e1 Add precaution against calling kore_split_string with ele == 0 2016-02-13 14:35:11 +01:00
Joris Vink 0c0393fc64 constify 2016-02-13 14:33:06 +01:00
Joris Vink c91fa9ab70 don't be dumb. 2016-02-13 14:13:59 +01:00
Joris Vink 9340d132db don't bother with empty things in kore_text_trim. 2016-02-13 14:02:10 +01:00
Daniel Fahlgren 04aec1c2dd Fix typo in log message 2016-02-12 08:57:58 +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 d4435bec8d Correct pretty bad typo. 2016-02-06 14:59:54 +01:00
Joris Vink 40f69924bb Move shared file reading code to kore_read_line() 2016-02-01 22:10:10 +01:00
Joris Vink 17e311dba9 constify kore_parse_config_file() parameter. 2016-02-01 21:59:31 +01:00
Joris Vink ac864a7419 Update output message to match others. 2016-02-01 21:54:09 +01:00
Joris Vink 3b1204d599 Make sure certkey and certfile are NULL after freeing.
From oneswig but I accidentally removed it during the merge.
2016-02-01 21:42:33 +01:00
Joris Vink d0e9441b60 remove debug. 2016-02-01 21:41:25 +01:00
Joris Vink c8484ee9ab Redo config parsing a bit.
No longer just call kore_string_split() on the line
but separate out the configuration directive and let
the appropriate callbacks parse things on their own.
2016-02-01 21:33:51 +01:00
Joris Vink 82a9e6ef59 Formatting and unbreaking NOHTTP builds. 2016-02-01 20:02:02 +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 7963a2deaa Added cleanup for the module handlers and validators 2016-02-01 12:13:22 +00:00
Stig Telfer 55d2451b90 Add cleanup of http_body_path pool 2016-02-01 10:10:04 +00:00
Stig Telfer bd975541dd Merge branch 'master' into oneswig 2016-02-01 10:04:59 +00:00
Joris Vink f2f5a3742f Let SIGTERM properly shutdown Kore as well. 2016-01-31 16:48:17 +01:00
Joris Vink cb489ce80f Define WAIT_ANY if not defined previously. 2016-01-31 16:47:00 +01:00
Joris Vink 3284265b74 Remove the -v stuff and always show cflags and ldflags. 2016-01-27 21:37:59 +01:00
Joris Vink b777ea65cd Long day .. actually add the new build flavor code. 2016-01-27 21:32:57 +01:00
Stig Telfer 2ac6e7d41d Merge branch 'master' into oneswig 2016-01-24 13:46:38 +00:00
Stig Telfer fd0ddca46c Add cleanup function for platform_event on BSD systems 2016-01-22 11:26:56 +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 a2216c84b0 Properly check if path was already set. 2016-01-18 11:43:10 +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 96641d3caa Merge branch 'master' of /opt/git/kore 2016-01-18 11:29:47 +01:00
Joris Vink 0dcd6087cf Add kore_buf_stringify().
NUL-terminates a kore_buf and returns a char pointer.
2016-01-18 11:26:55 +01:00
Joris Vink 3c40168e50 Deal with SSL_ERROR_SYSCALL properly. 2016-01-14 10:50:46 +01:00
Ansen Dong 490163de4a if read() return EINTER,retry 2016-01-14 03:02:53 -05:00
Joris Vink ed8110f109 Paranoia about overflows. 2016-01-09 13:51:50 +01:00
Carl Ekerot 53e8220ff6 Fix off-by-one error when reallocing buffer
There was an invalid boundary check when appending to a buffer. The
consequence of this was that the POST data buffer was deterministically
realloced.
2016-01-09 01:57:59 +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