Commit Graph

303 Commits

Author SHA1 Message Date
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
Joris Vink 1dd7407a41 Close SPDY streams if len == 0 and SPDY_NO_CLOSE is not set. 2015-05-15 19:16:35 +02:00
Joris Vink cefeaf7992 HTTP layer improvements.
Add HTTP_REQUEST_NO_CONTENT_LENGTH which can be set by
a handler before calling http_response() to avoid Kore
from setting the content-length altogether.

If we are on a SPDY connection do not close the stream
if we do not pass data to http_response().
2015-05-15 19:12:18 +02:00
Joris Vink 09ed61187b Unfuck content-length changes.
We want a content-length unless its 204 or status < 200.
2015-05-13 09:16:06 +02:00
Joris Vink 7ce742f131 Merge pull request #48 from thorduri/content-length
Always write out content-length;
2015-05-13 09:02:58 +02:00
Thordur Bjornsson a62bce3e88 Always write out content-length;
Unless status is 204 or 1xx (informational).
2015-05-12 20:23:47 +02:00
Joris Vink 4f36b534aa Reset fsm_state back to 0 after we're done with it. 2015-05-08 21:19:20 +02:00
Joris Vink e5f6d37cd2 Make parameter validation less noisy. 2015-04-27 15:35:02 +02:00
Joris Vink dc6e6fb1b4 Kill NETBUF_CALL_CB_ALWAYS for incoming HTTP body
Do not let the NETBUF_CALL_CB_ALWAYS flag linger in
the netbuf we reset for the incoming HTTP body.
2015-04-21 11:30:59 +02:00
Joris Vink dfea3cf483 Place path and host in http_request into pools.
Also change the amount of times we end of doing unneccessairy strlen() calls.
2015-04-15 12:40:15 +02:00
Joris Vink 097a1166df Improve very heavy load handling.
Introduces two new configuration knobs:
	* socket_backlog (backlog for listen(2))
	* http_request_limit

The second one is the most interesting one.

Before, kore would iterate over all received HTTP requests
in its queue before returning out of http_process().

Under heavy load this queue can cause Kore to spend a considerable
amount of time iterating over said queue. With the http_request_limit,
kore will process at MOST http_request_limit requests before returning
back to the event loop.

This means responses to processed requests are sent out much quicker
and allows kore to handle any other incoming requests more gracefully.
2015-04-09 15:29:44 +02:00
Joris Vink af865abede Bump copyright to 2015 2015-04-07 13:08:26 +02:00
Joris Vink cf028f20b6 Rename kore_auth() to kore_auth_run(). 2015-04-02 13:45:42 +02:00
Joris Vink b234b7ed65 Add HTTP_REQUEST_RETAIN_EXTRA flag to http_request
Signals Kore to not free any pointer set in req->hdlr_extra.
Useful in certain scenarios where you have data per request
bound to something in memory but do not want to lose it when
the request is freed by Kore.

Set this flag before your handler returns.
2015-03-27 23:23:21 +01:00
Joris Vink ad5d662556 Use net_recv_reset() if more body bytes are coming.
Unbreaks clients that send the headers first and the
actual HTTP body after X amount of time.
2015-03-16 16:45:13 +01:00
Joris Vink 91c44af6f0 Always include includeSubDomains for HSTS. 2015-02-10 17:14:07 +01:00
Joris Vink 1507f88ec7 Respect keepalive when throwing HTTP errors.
Report from github issue #34
2015-02-06 10:59:15 +01:00
Joris Vink dbe301e58c Add a patch level to our version number. 2014-12-12 13:06:41 +01:00
Joris Vink f867882f43 Add websocket support to Kore.
Introduces a few new api functions:

- kore_websocket_handshake(struct http_request *):
	Performs the handshake on an HTTP request (coming from page handler)

- kore_websocket_send(struct connection *, u_int8_t, void *, size_t):
	Sends data to a websocket connection.

- kore_websocket_broadcast(struct connection *, u_int8_t, void *, size_t, int):
	Broadcast the given websocket op and data to all connected
	websocket clients on the worker. Note that as of right now
	the WEBSOCKET_BROADCAST_GLOBAL scope option does not work
	yet and messages broadcasted will be restricted to workers
	only.

- kore_worker_websocket_broadcast(struct connection *, void *, void *):
	Backend function used by kore_websocket_broadcast().
	Could prove useful for developers to have access to.

A simple example is given under examples/websocket.

Known issues:
	Kore does not support PING or CONT frames just yet.
2014-11-24 11:08:34 +01:00
Joris Vink 6de0f8568a Rework net, worker and some http internals.
- The net code no longer has a recv_queue, instead reuse same recv buffer.
- Introduce net_recv_reset() to reset the recv buffer when needed.
- Have the workers spread the load better between them by slightly
  delaying their next accept lock and giving them an accept treshold
  so they don't go ahead and keep accepting connections if they end
  up winning the race constantly between the workers.
- The kore_worker_acceptlock_release() is no longer available.

- Prepopulate the HTTP server response header that is added to each
  response in both normal HTTP and SPDY modes.
- The path and host members of http_request are now allocated on the heap.

These changes overall result better performance on a multicore machine,
especially the worker load changes shine through.
2014-10-22 21:16:49 +02:00
Joris Vink a603b77e24 Add PUT/DELETE/HEAD methods (finally).
This commit renames certain POST centric variable and configuration
naming to the correct HTTP body stuff.

API changes include http_postbody_text() and http_postbody_bytes() to
have become http_body_text() and http_body_bytes().

The developer is still responsible for validating the method their
page handler is called with. Hopefully this becomes a configuration
option soon enough.
2014-10-08 11:03:14 +02:00
Joris Vink c0c3e9fb05 Use s instead of req->stream. 2014-09-19 15:01:25 +02:00
Joris Vink f3e863a482 Properly end headers when doing status codes only. 2014-08-25 10:58:34 +02:00
Joris Vink 0542d70a71 Use a single header_buf in http_response_normal()
Avoids constantly allocating and freeing buffers all the time.
This can be done safely due to the fact workers are single threaded.
2014-08-22 14:38:48 +02:00
Joris Vink 7e74cd6e62 Introduce a wait queue for our pgsql code.
Instead of letting http_requests spin, if we cannot allocate
a connection for the request we will queue them up put them to sleep.

When a connection becomes available, we'll wake up a request that
was waiting for a connection and let it continue.

This completely avoids consuming massive amounts of cpu time
when dealing with thousands of requests waiting for a pgsql
worker to become ready.
2014-08-21 16:36:12 +02:00
Joris Vink d3149a2a71 We need at most 2 tokens here 2014-08-18 21:13:16 +02:00
Joris Vink 105804e325 Make http_status_text() return a const char 2014-08-18 10:37:13 +02:00
Joris Vink 873c68b515 Make http_status_text() available everywhere 2014-08-18 10:34:47 +02:00
Joris Vink 8565b47800 Attach tasks/pgsqls to http_requests once more.
This way if an http request is removed while tasks or
pgsqls are still active they are free'd out and cancelled
properly.
2014-08-14 22:05:34 +02:00
Joris Vink f93747828c Major pgsql rework.
Gone is the ugly KORE_PGSQL macro that hid an overly complex
state machine for the pgsql api.
Gone is the pgsql array that was attached to http_requests.
Gone are the callback hacks inside the pgsql api.

Instead, I strongly encourage people to use the new state machine
api Kore offers to properly deal with asynchronous queries.

The pgsql example in examples/pgsql has been updated to reflect
these changes.
2014-08-14 14:34:23 +02:00
Joris Vink 856d7b0cb2 Rename HTTP_STATE_OK to HTTP_STATE_CONTINUE 2014-08-14 11:20:04 +02:00
Joris Vink 210fb05a35 Add a hopefully easy to use and optional fsm for page handlers
In order to use this, define states for your page handler:

struct http_state mystates[] = {
	{ "PAGE_STATE_INIT",	page_init },
	{ "PAGE_STATE_RESULT",	page_result },
};

In your page handler you can then simply call http_state_run() with
your states and http_request. This will cause Kore to start calling
your state callbacks beginning at index 0.

State callbacks have the same prototype as page handlers:

int	func(struct http_request *);

However, unlike page handlers they MUST return one of the following:

- HTTP_STATE_OK:	All good, just continue the fsm.
- HTTP_STATE_ERROR:	Abort fsm and return KORE_RESULT_OK to Kore
			(This will cancel the http request).
- HTTP_STATE_RETRY:	Return KORE_RESULT_RETRY to Kore.
			(Kore will retry your page handler next event loop).
- HTTP_STATE_COMPLETE:	The fsm completed, break out cleanly.

Note that using this is completely optional and you can still
use the traditional way of writing page handlers.

The fsm is designed to get rid of the clutter that exists today
in Kore when dealing with non blocking tasks or pgsql calls.
2014-08-13 23:34:30 +02:00
Joris Vink 29afdd0b21 Make sure opt always contains what we want 2014-08-13 14:32:49 +02:00
Joris Vink 97c17f724b Add kore_snprintf() as a wrapper around snprintf(). 2014-08-11 10:45:10 +02:00
Joris Vink c665b7d926 Add a callback to http_response_stream().
This way we can get our code called whenever a stream is
completed. This cb handler does stand alone from an http_request
and is passed a netbuf data structure.
2014-08-10 18:46:44 +02:00
Joris Vink 10284d59b6 Another round of spdy/3.1 improvements.
* Always make sure we end the stream properly
* Check for SPDY_FLOW_WINDOW_MAX on window frame updates
* Kill SPDY_STREAM_BLOCKING, once flow control kicks in its per session
2014-08-10 18:18:27 +02:00
Joris Vink 5cca2f1f78 properly implement spdy/3.1 flow control 2014-08-08 14:18:15 +02:00
Joris Vink d2c65b4f62 Change http_response_stream() to only take a base + len 2014-08-07 14:31:45 +02:00
Joris Vink cda09b6065 Add http_response_stream() which can stream data from a buffer to the client. 2014-08-07 14:23:26 +02:00
Joris Vink 966ed3d20d Rename the two functions used to read/set headers.
Rename http_request_header_get() to http_request_header().
Rename http_response_header_add() to http_response_header().
2014-08-05 14:26:31 +02:00
Joris Vink 0413de6a8f fatal if the return value from a page handler isn't what we want. 2014-08-05 14:17:23 +02:00
Joris Vink b0700162c4 Make http_post_data_bytes() and http_post_data_text() available 2014-08-04 21:03:26 +02:00
Joris Vink 9b3ff7db53 Lowercase the HTTP headers sent in normal HTTP responses.
SPDY already had this, as required by the spec.
2014-08-04 20:12:31 +02:00
Joris Vink 31eac77cd2 sprinkle const char where appropriate 2014-08-04 09:48:41 +02:00
Joris Vink 0e2f478c75 Fix http_argument_get_*() integer functions. 2014-08-03 15:20:20 +02:00
Joris Vink 83f890e89e No longer preallocate worker_max_connections for the http pools.
Instead use 10% of worker_max_connections up to a maximum of 1000.
2014-07-30 13:20:27 +02:00
Joris Vink cb3322cd9f Make accesslogs optional 2014-07-30 09:11:21 +02:00
Joris Vink ea07ddef34 Properly send out early HTTP errors via the appropriate SPDY stream 2014-07-27 21:59:25 +02:00
Joris Vink 8c22f0f004 In http_request_free() remove the req from the owner connection. 2014-07-27 21:00:17 +02:00
Joris Vink d4cec3427e Properly convert 64bit parameters.
Introduces kore_strtonum64() for just this purpose as to
not taint kore_strtonum() too much.
2014-07-21 01:16:03 +02:00
Joris Vink ac345410dc Add support for fetching parameters as 64bit integers. 2014-07-20 00:43:32 +02:00
Joris Vink 19d146a09e Introduce http_request_sleep() and http_request_wakeup().
These 2 functions can be used to move an HTTP request
from/to the active http_requests list. Effectively
putting them to "sleep" or "waking them up".

Sprinkle this through the pgsql and task code.

If used correctly greatly reduces overhead for
managing sleeping tasks.
2014-07-04 16:51:19 +02:00
Joris Vink 7b6c03ca5b Task improvements.
Synchronize access to state/result properly so one
can access these from inside the task as well.

Introduce KORE_TASK_STATE_ABORT which will be set
when a task needs to be abort. You can use this
to create tasks that run in a loop until aborted.
2014-07-04 11:28:17 +02:00
Joris Vink 2c6b5e6b0f Normalize kore_*.h headers 2014-07-03 22:14:46 +02:00
Joris Vink 2bf43fe5f8 Unfuck boundary snprintf check, broke multipart forms 2014-07-03 20:10:55 +02:00
Joris Vink 723c99b65e Move kore_pgsql.h into includes/ directly.
Much like kore_tasks.h
2014-06-30 08:42:18 +02:00
Joris Vink b9bd2e9a14 Move tasks away from http_requests.
Instead if a task is used from inside a request
you MUST call kore_task_bind_request() on it.

This way we can move forward for tasks that
don't belong to page handlers.

Also, some bug fixes for removing http_requests
that are indeed linked to a currently running task.
2014-06-29 20:20:13 +02:00
Joris Vink 146a0189ab More work on the background task implementation.
Tasks are now assigned to available threads instead
of a global task list.

You can now pass messages between your page handler
and the created task using the kore_task_channel_*
functions.

Only one task per time can be assigned to a request
but I feel this is probably a bad design choice.

Preferably we'd want to be able to start tasks
regardless of being in a page handler or not,
this not only ads flexibility but seems like
a better choice overall as it opens a lot more
possibilities about how tasks can be used.
2014-06-29 14:15:40 +02:00
Joris Vink cf700b34f7 Add initial stab at asynchronous background tasks.
More to follow.
2014-06-28 16:17:18 +02:00
Joris Vink 0cb983adc0 Correct snprintf() usage 2014-04-23 16:29:58 +02:00
Joris Vink fd7f547013 Fix accesslog under SPDY connections 2014-04-23 13:18:37 +02:00
Joris Vink 4f31d53e76 Allow KORE_RESULT_RETRY from authentication blocks 2014-04-22 23:07:24 +02:00
Joris Vink d6d6f96ca0 Kore pgsql improvements.
Don't wait for a full event loop until we call the page handler
for a received pgsql result. This speeds up page loads using
KORE_PGSQL by quite a lot, especially on a non busy server.
2014-04-17 10:49:48 +02:00
Joris Vink 52a0764812 HTTP Post improvements for handlers.
Don't crash if we get a Content-length:0 on POST and our
handlers call http_populate_arguments().
2014-04-15 21:18:23 +02:00
Joris Vink 41aed45e4f Do not miscalculate end_headers. 2014-04-09 09:21:22 +02:00
Joris Vink 61b6f823c5 Improvements to pgsql contrib code.
Including but not limited to:
- Correctly use PQerrorMessage() in case we cleanup with PQfinish
- If we get a network error, cleanup the connection
- No longer call the page handler from inside kore_pgsql_handle()
  but instead just put it to sleep in case we don't need it.
  This does grow the http_requests list quite a bit with sleeping
  connections and can perhaps be improved later on.
- Allow us to on error return OK from a page handler from inside
  the completetion block for KORE_PGSQL().
- Count the cummulative time for a request to finish instead
  of the latest run time for the handler.
2014-03-31 00:57:00 +02:00
Joris Vink 2f044cc7eb Initial stab at entering postgresql contrib code.
Has support for full async pgsql queries. Most of the logic
is hidden behind a KORE_PGSQL() macro allowing you to insert
these pgsql calls in your page handlers without blocking the
kore worker while the query is going off.

There is place for improvement here, and perhaps KORE_PGSQL won't
stay as I feel this might overcomplicate things instead of making
them simpler as I thought it would.
2014-03-30 23:54:35 +02:00
Joris Vink cb17c0d610 When returning a 405 append an Allow header as per rfc 2014-02-27 22:24:28 +01:00
Joris Vink 634bb482d6 Pass the http_request responsible for calling the validator. 2014-02-01 17:47:58 +01:00
Joris Vink f0a9c1fe73 Major improvements to HTTP/1.1 behaviour, error reporting and handling.
This comes with changes to http_response() (no longer returns a result).
2014-01-29 22:48:51 +01:00
Joris Vink 9a8092bf41 Add authentication blocks for Kore.
Using authentication blocks one can define "authentication" mechanisms
in Kore for page handlers.

This can be used to require a session cookie (validated by your own validator)
for certain page handlers, and hopefully in the future provide a framework
for adding more authentication things (like HTTP Auth).

Right now only cookie checking is available.
2014-01-22 22:55:10 +01:00
Joris Vink 7e8371366f Make sure we properly close a SPDY stream if there's no data. 2014-01-14 22:22:26 +01:00
Joris Vink b64f674db2 Handle SPDY streams better when sending data. 2014-01-14 21:43:45 +01:00
Joris Vink 517de46790 Kore can now do query strings without lots of dynamic handler voodoo.
Any handler can receive query strings, however if you do not specify
parameters allowed in a param {} block Kore will discard them.
2014-01-13 20:21:20 +01:00
Joris Vink 84d2c7fa0d ULONG_MAX -> LONG_MAX 2013-12-16 18:00:33 +01:00
Joris Vink c7dcdbcd82 Rework the way validation and param extraction works.
- Parameter validation is now done only when http_process_*()
  is called and upon http_argument_add().
- You MUST have defined your params in a param block or they will
  be filtered out.
- http_argument_lookup() is dead, welcome http_argument_get() and
  its brothers and sisters:
	http_argument_get_string()
	http_argument_get_uint16()
	http_argument_get_int16()
	http_argument_get_uint32()
	http_argument_get_int32()

  They will automatically do bounds checking on integers for you
  and return proper integers or a NUL-terminated string.

- The http_argument_get* functions no longer create an additional
  copy of the string which you need to free. Easier going.
- http_multiple_args() is dead, byebye
- Make some stuff we don't want to share with the modules static.
2013-12-12 00:58:32 +01:00
Joris Vink 0f28b4a62d Enter versioning, starting at 1.0-current 2013-12-10 16:43:19 +01:00
Joris Vink 10aa4a28fe Multipart forms are now also validated via params 2013-12-05 20:28:07 +01:00
Joris Vink 269e5cb007 Strip out port from HTTP host request if its present. 2013-11-18 00:53:08 +01:00
Joris Vink 1e250c1473 Kore now supports GET parameters and automatic validation of GET/POST parameters.
Kore will automatically removes invalid parameters as a security measure.

See modules/examples/module.conf for an example of how this works.
2013-11-10 15:17:15 +01:00
Joris Vink f17242262b Improve on sending data to the client.
- Attempt to chain as much as we can in the send netbufs
  (keeps down the SSL_write calls, silly seeing it go out with 8 bytes)
- Change NETBUF_SEND_PAYLOAD_MAX to 4K
- Call SSL_write() with max NETBUF_SEND_PAYLOAD_MAX in size
- Go back to flushing the send buffers after each request
- No more need for a fixed pool for nb->buf, go back to the heap for now
- Disable Nagle, we're doing the chaining now anyway
2013-10-26 00:48:09 +02:00
Joris Vink 4247d3139c Chain our sending netbufs together. Gives us a lot less SSL_write() calls. 2013-10-25 14:22:29 +02:00
Joris Vink 4922171d96 Change net_send_queue() in preparation for improving send performance.
No longer takes callbacks, flags, or *out arguments.

Update rest of the code that called these callbacks whenever sending
was completed, instead call them right away now.
2013-10-25 11:10:03 +02:00
Joris Vink e10486fa10 Change urldecode back to 0 2013-10-24 08:56:17 +02:00
Joris Vink c30ff58b93 control characters have nothing to do inside a URL. 2013-10-18 12:43:20 +02:00
Joris Vink 5d027b7a21 Decode all url encoded components properly. 2013-10-18 12:32:00 +02:00
Joris Vink c64d3e7854 Add http_keepalive_time configuration parameter.
Allows you to configure maximum amount of seconds an HTTP connection
can stay open (does not affect SPDY connections). If set to 0 it will
disable keep-alive all together.

Add some inttypes fluff.
2013-10-15 11:10:45 +02:00
Joris Vink 8b47863cd4 Add http_hsts_enable (enabled by default with max-age=31536000) to Kore's configuration file.
If enabled Kore adds the HSTS header to every response.

- Additionally, fix some typos in the example configuration.
- Change default SSL cipher list again, no more RC4 and almost PFS for all browsers.
2013-10-15 10:44:56 +02:00
Joris Vink 88c3a3eb98 Add http_header_max and http_postbody_max configuration variables.
- http_header_max:
	Maximum size of HTTP headers (in non SPDY connections).

- http_postbody_max:
	Maximum size of an HTTP POST body (both in SPDY and HTTP mode).

Right now Kore will simply DC the client, ideally we want to send
a 413 (entity too large) to the client however.

See modules/examples/module.conf for more.
2013-09-22 20:05:24 +02:00
Joris Vink cffe4afb91 properly count amount of arguments parsed 2013-09-10 14:05:02 +02:00
Joris Vink 25e8f93331 Add support for multipart forms.
New API functions (docs need to be updated):
	- http_file_lookup()
	- http_file_add()
	- http_argument_add()
	- kore_strip_chars()
	- kore_mem_find()

- Add an example under the example module on how files can be read.
2013-09-10 11:02:59 +02:00
Joris Vink c9d4f70298 - Add SPDY RST control frame handler.
- Keep HTTP requests in connection, so we can delete them if the connection
  ends before the requests do (this way we don't leak them).
- When spdy_stream_close() is called, delete the attached http request.
  (This shouldn't hurt to do, so hopefully won't cause major fallout).
- When parsing HTTP, find the first occurence of end-of-headers so uploads
  with multipart/form-data can succeed properly.
- Add a test upload page to the example module.
2013-09-09 10:59:56 +02:00
Joris Vink 9fa9fd7402 remove useless casts 2013-08-26 08:52:56 +02:00
Joris Vink a8052c7ac4 Add reason phrases and http status constants (HTTP_STATUS_* see includes/http.h)
Based on work from mendor/ymv via github.
2013-08-19 09:11:31 +02:00
Joris Vink ef9d37e54b free hdlr_extra if its set 2013-08-14 16:09:09 +02:00
Joris Vink ecefdb18ab cb_extra -> hdlr_extra and add one to struct connection as well for persistent data across requests. 2013-08-14 15:56:44 +02:00
Joris Vink 34f6d088fa set cb_extra to NULL when req is allocated 2013-08-14 15:15:18 +02:00
Joris Vink bbb245654d Pass the base for strtoll() to kore_strtonum(), breakage ensues if we depend on the "auto" detection that happens when we pass 0 to strtoll() as base. 2013-08-13 16:13:43 +02:00
Joris Vink 3075162855 Add http_argument_urldecode() which takes a string and decodes any url encoding done to it.
Change kore_strtonum() to pass 0 to strtoll by default so we can use it to convert hex numbers (prepended with 0x) as well.
2013-08-13 14:18:47 +02:00
Joris Vink ef814a677d Add http_argument_multiple_lookup() and http_argument_multiple_free().
Prototypes:
	int	http_argument_multiple_lookup(struct http_req *req,
		    struct http_arg *args);
	void	http_argument_multiple_free(struct http_arg *args);

These functions can be used to lookup arguments in a single call.

args points to an array of struct http_arg elements. Each of them
have the argument name set and its value set to NULL.

The array must have its last element name field set to NULL.

Upon return http_argument_multiple_lookup() gives the caller the
number of arguments that were successfully found. It makes their values
available under the value field in the struct http_arg array passed.

Example:
	int			v;
	struct http_args	args[4];

	memset(args, 0, sizeof(args));
	args[0].name = "email";
	args[1].name = "password1";
	args[2].name = "password2";
	args[3].name = NULL;

	v = http_argument_multiple_lookup(req, args);
	if (v != 3) {
		kore_debug("argument %s was not present", args[v].name);
	} else {
		for (v = 0; args[v].name != NULL; v++)
			kore_debug("%s -> %s", args[v].name, args[v].value);
	}

	http_argument_multiple_free(args);
2013-08-07 14:41:16 +02:00
Joris Vink 2c1352b226 remove versioning numbers, i hate them. 2013-07-16 15:30:20 +02:00