Commit Graph

23 Commits

Author SHA1 Message Date
Amnon Heiman
f06c12e031 http: fix crash due to set_routes() not managing lifetime correctly
When using the set_routes with the registry builder, the shared_ptr of
the registry builder should be captured.

In the original implementation the api_registery_builder captured this
parameter, but as the shared_ptr was not captured it was deleted,
causing the http to crash when running in debug.

In this implementation the method that use the registry creates a lambda
function and inside it calls the set_api_doc method, this allows to
catch the shared_ptr so it lives until the function complete.

It also replaces the c style cast to static_cast and add a FIXME note to
the routes implementation, that handlers should be deleted to prevent
memory leak.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-04-26 13:09:26 +03:00
Amnon Heiman
8a0538a218 http: fix query parameter parsing of last parameter
When serving a request with multiple query parameters the last parameter
was parsed incorectly.

This fix the issue and add a test to verify it

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-04-21 10:29:14 +03:00
Amnon Heiman
88e67cb379 http: fix a wrongly used string_view in transformers.cc 2015-04-08 11:21:03 +03:00
Amnon Heiman
c129b86589 Adding the api_docs to http
This is a migration of the api_docs from OSv. It replaces the static
api-doc.json with a dynamic generated reply, this allows to register API
in run time.

The api_registry_builder is a helper class that holds the file and api
path, simplifying registring both the api_doc handler and registering
additional API.

To use the api_doc, first generate a api_registry_builder.

The registry supply two functions, one for registring the api_doc
handler and one for registering an API.

Both function are passed as an argument for the set_routes method of the
http_server_control object.

To find the handler, the get_exact_match in the routes object was needed
to become public.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-04-05 11:39:48 +03:00
Amnon Heiman
f4c1f5d647 Adding the http server_control to simplify server handling
There are multiple tasks that need to be performed when setting and
running an http server, specifically the routes need to be set and the
server need to be start.

The server control wrap the server initiation functionality and adds an
iteration function for all routes.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-04-05 11:39:42 +03:00
Amnon Heiman
53604f1fae Adding file transformers to http
Files transformers are used to replace file content before retrieving
it. Specifically, it is used to return swagger definition files with the
host and protocol replaced.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-04-05 09:53:33 +03:00
Amnon Heiman
dca4b25e8b http: Allows function handler to return future
Function handler in general and json function in particular are the
easiest way to add logic to to handler.

While in some cases the method can return immediately, there are cases
when it is required to perform an async operation, to support those
cases, the function handler was modified to use future.

If it receives an old style function, it would wrap the results in a
make_ready_future. This you could still assign a function like:

new function_handler([](const_req req) {
        return "hello";
    });

It would no also support a function that return a future json so it is
no possible to assign logic like:
new function_handler([](std::unique_ptr<request> req) {
return make_ready_future<json::json_return_type>("json-future");
});

For the future case note that auto-boxing still works, although you now
need to use make_ready_future.

The json_path was also modified to accept the new kind of function, to
support the common case of route definition based on the code
generation.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-04-03 13:12:28 +03:00
Amnon Heiman
f2147e7490 Http adding the json path
Json path are used when parsing the swagger files. Each path represent
an operation in the swagger files.

They are used to simplified setting a handler or a function to a path.

For example: the code generation would define a json_path like:

path_description hello_world("/hello/world",GET,"hello_world", {},{});

Now to define the handler that would use hello_world, you can simply do:

hello_world.set(r,
            [](const_req req) {
               return "hello world";
});

When r is a reference to a route object.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-30 15:38:41 +03:00
Amnon Heiman
f6d253b713 request return http as the protocol name 2015-03-30 15:38:41 +03:00
Amnon Heiman
2ff0f41cfb http adding file handler
File handlers support reading local file and return it as the result
for the http request.

There are two kind of handler, a file handler will return a specific
file when called. A directory handler expect to find a path parameter
called 'path' and would concatinate this parameter to the direcoty path.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-30 15:38:41 +03:00
Amnon Heiman
6512d7f7fb http support query parameter and url encoding
Http url encode query parameters by adding a question mark after the uri
with pairs of key=value items.

All values in the url are url decoded.

This add the url encoding and query parameters support

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-30 15:38:41 +03:00
Amnon Heiman
be33b31ae2 Httpd Adding function handlers
Most of the time, implementing an http handler consist of a small amount
of logic.

Function handlers are a simplified way of adding such a logic, they
accept a lambda expression of various types and eliminate the need to
create a type for the handlers.

The prefered way of creating a handler is by using the
json_request_function, it would use auto-boxing to return a json object.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-30 15:38:41 +03:00
Amnon Heiman
950d921df3 Http server handlers to use future
The http server handlers can sometimes need to perform async operation,
specifically, when reading files from the disk.  To support async
behavior the handlers handle function will return a future, that will be
propagate back, when the future will be ready it will return the reply
to be sent.

When switching from direct function call to future, there is also a need
to switch from references to pointers.

Note that while the request will be discarded, the reply will be
propagate back via the future to the caller.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-30 15:38:41 +03:00
Amnon Heiman
c420b75e26 remove leading spaces from headers 2015-03-30 15:38:41 +03:00
Amnon Heiman
427a6165b3 using the routes and request from the http directory
This use the routes and the reqeuest found in the http directory and
move all files but main to the http directory

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-08 21:55:57 +02:00
Amnon Heiman
8fccdb77a0 Adding the routes object
The route object is the url dispatcher, it uses it matching rules to
find what handler should handle a request, perform the call and handle
exceptions that hanend during the handling.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-08 21:55:57 +02:00
Amnon Heiman
1866091b3d Adding basic json support
This is a migration of the basic json support taken from the
osv/httpserver.

The routes uses the json object to return errors in a json format.

This also adds json_exception which has a constructor from an exception.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-08 21:55:57 +02:00
Amnon Heiman
5aca499a7e Adding the matcher and match rules
The matcher and match rules are used to define and match a url rule. A
rule define a list of string and parameters.
For example, the rule /api/get/{id}/name
Will be mapped by the list of matcher:
str_match for /api/get
param_match for the {id}
and str_match for name

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-08 21:55:57 +02:00
Amnon Heiman
8577a8886e Adding the handler
The class handler_base is a base class for all handlers. All handlers
should inherit and implement the handle method.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-08 21:55:57 +02:00
Amnon Heiman
4bdbd2c80a Adding the http exception 2015-03-08 21:55:57 +02:00
Amnon Heiman
baedb5e775 Adding the request object
The request object was originally taken from the boost server example
and was modified in the OSv http implementation.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
2015-03-08 21:55:57 +02:00
Amnon Heiman
1ffd418ded Adding common for HTML operation support 2015-03-08 21:55:57 +02:00
Amnon Heiman
6cf25b8c5a add mime type support 2015-03-08 21:55:57 +02:00