summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2018-07-18 13:04:25 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2018-07-18 13:05:59 +0000
commitfb5ae495a8ee221ff12fe02d417752c8ade0372e (patch)
treee29d07d951bd5d8cf4a9da4154fdf8132c6fda5b
parentReleasing progress-linux version 2.3.5-1~dschinn1. (diff)
downloadulfius-fb5ae495a8ee221ff12fe02d417752c8ade0372e.zip
ulfius-fb5ae495a8ee221ff12fe02d417752c8ade0372e.tar.xz
Merging upstream version 2.3.6.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
-rw-r--r--API.md20
-rw-r--r--CHANGELOG.md6
-rw-r--r--CMakeLists.txt2
-rw-r--r--example_callbacks/oauth2_bearer/glewlwyd_resource.c20
-rw-r--r--example_callbacks/oauth2_bearer/glewlwyd_resource.h14
-rw-r--r--example_callbacks/static_file/static_file_callback.c52
-rw-r--r--example_callbacks/static_file/static_file_callback.h5
-rw-r--r--include/ulfius.h20
-rw-r--r--src/Makefile2
-rw-r--r--src/u_websocket.c2
10 files changed, 83 insertions, 60 deletions
diff --git a/API.md b/API.md
index 72a85ca..16461bd 100644
--- a/API.md
+++ b/API.md
@@ -569,6 +569,9 @@ if libjansson library is enabled, the following functions are available in Ulfiu
/**
* ulfius_get_json_body_request
* Get JSON structure from the request body if the request is valid
+ * In case of an error in getting or parsing JSON data in the request,
+ * the structure json_error_t * json_error will be filled with an error
+ * message if json_error is not NULL
*/
json_t * ulfius_get_json_body_request(const struct _u_request * request, json_error_t * json_error);
@@ -580,21 +583,26 @@ json_t * ulfius_get_json_body_request(const struct _u_request * request, json_er
int ulfius_set_json_body_request(struct _u_request * request, json_t * body);
/**
+ * ulfius_get_json_body_response
+ * Get JSON structure from the response body if the request is valid
+ * In case of an error in getting or parsing JSON data in the request,
+ * the structure json_error_t * json_error will be filled with an error
+ * message if json_error is not NULL
+ */
+json_t * ulfius_get_json_body_response(struct _u_response * response, json_error_t * json_error);
+
+/**
* ulfius_set_json_body_response
* Add a json_t body to a response
* return U_OK on success
*/
int ulfius_set_json_body_response(struct _u_response * response, const uint status, const json_t * body);
-
-/**
- * ulfius_get_json_body_response
- * Get JSON structure from the response body if the request is valid
- */
-json_t * ulfius_get_json_body_response(struct _u_response * response, json_error_t * json_error);
```
The `jansson` api documentation is available at the following address: [Jansson documentation](https://jansson.readthedocs.org/).
+Note: According to the [JSON RFC section 6](https://tools.ietf.org/html/rfc4627#section-6), the MIME media type for JSON text is `application/json`. Thus, if there is no HTTP header specifying JSON content-type, the functions `ulfius_get_json_body_request` and `ulfius_get_json_body_response` will return NULL.
+
#### Callback functions return value
The callback returned value can have the following values:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ea91fa2..847d3de 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
# Ulfius Changelog
+## 2.3.6
+
+- Fix websocket bug that did not close a websocket properly after wrongly closed connections
+- Add last example_callbacks versions
+- Improve documentation on ulfius_get_json_body_request and ulfius_get_json_body_response
+
## 2.3.5
- Fix websocket bug that kept some connections open after being unproperly closed by the client
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 77231fb..00acd67 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -28,7 +28,7 @@ set(PROJECT_BUGREPORT_PATH "https://github.com/babelouest/ulfius/issues")
set(LIBRARY_VERSION_MAJOR "2")
set(LIBRARY_VERSION_MINOR "3")
-set(LIBRARY_VERSION_PATCH "5")
+set(LIBRARY_VERSION_PATCH "6")
set(LIBRARY_VERSION "${LIBRARY_VERSION_MAJOR}.${LIBRARY_VERSION_MINOR}.${LIBRARY_VERSION_PATCH}")
set(LIBRARY_SOVERSION "${LIBRARY_VERSION_MAJOR}.${LIBRARY_VERSION_MINOR}")
set(YDER_VERSION_DOWNLOAD "1.3.3")
diff --git a/example_callbacks/oauth2_bearer/glewlwyd_resource.c b/example_callbacks/oauth2_bearer/glewlwyd_resource.c
index cb97242..5532ada 100644
--- a/example_callbacks/oauth2_bearer/glewlwyd_resource.c
+++ b/example_callbacks/oauth2_bearer/glewlwyd_resource.c
@@ -4,6 +4,8 @@
*
* Copyright 2016-2018 Nicolas Mora <mail@babelouest.org>
*
+ * Version 20180607
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* License as published by the Free Software Foundation;
@@ -71,7 +73,7 @@ int callback_check_glewlwyd_access_token (const struct _u_request * request, str
o_free(response_value);
} else {
res = U_CALLBACK_CONTINUE;
- response->shared_data = (void*)json_pack("{ssso}", "username", json_string_value(json_object_get(json_object_get(j_access_token, "grants"), "username")), "scope", json_copy(json_object_get(j_res_scope, "scope")));
+ response->shared_data = (void*)json_pack("{sssO}", "username", json_string_value(json_object_get(json_object_get(j_access_token, "grants"), "username")), "scope", json_object_get(j_res_scope, "scope"));
if (response->shared_data == NULL) {
res = U_CALLBACK_ERROR;
}
@@ -155,24 +157,14 @@ int access_token_check_validity(struct _glewlwyd_resource_config * config, json_
// Token is valid, check type and expiration date
time(&now);
expiration = json_integer_value(json_object_get(j_access_token, "iat")) + json_integer_value(json_object_get(j_access_token, "expires_in"));
- if (now < expiration &&
+ if (now < expiration &&
json_object_get(j_access_token, "type") != NULL &&
- json_is_string(json_object_get(j_access_token, "type"))) {
- if (config->accept_access_token &&
+ json_is_string(json_object_get(j_access_token, "type")) &&
0 == o_strcmp("access_token", json_string_value(json_object_get(j_access_token, "type"))) &&
json_object_get(j_access_token, "username") != NULL &&
json_is_string(json_object_get(j_access_token, "username")) &&
json_string_length(json_object_get(j_access_token, "username")) > 0) {
- res = G_OK;
- } else if (config->accept_client_token &&
- 0 == o_strcmp("client_token", json_string_value(json_object_get(j_access_token, "type"))) &&
- json_object_get(j_access_token, "client_id") != NULL &&
- json_is_string(json_object_get(j_access_token, "client_id")) &&
- json_string_length(json_object_get(j_access_token, "client_id")) > 0) {
- res = G_OK;
- } else {
- res = G_ERROR_INVALID_REQUEST;
- }
+ res = G_OK;
} else {
res = G_ERROR_INVALID_REQUEST;
}
diff --git a/example_callbacks/oauth2_bearer/glewlwyd_resource.h b/example_callbacks/oauth2_bearer/glewlwyd_resource.h
index 0a3b51a..3edf120 100644
--- a/example_callbacks/oauth2_bearer/glewlwyd_resource.h
+++ b/example_callbacks/oauth2_bearer/glewlwyd_resource.h
@@ -4,6 +4,8 @@
*
* Copyright 2016-2018 Nicolas Mora <mail@babelouest.org>
*
+ * Version 20180607
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* License as published by the Free Software Foundation;
@@ -37,13 +39,11 @@
#define BODY_URL_PARAMETER "access_token"
struct _glewlwyd_resource_config {
- int method;
- char * oauth_scope;
- char * jwt_decode_key;
- jwt_alg_t jwt_alg;
- char * realm;
- unsigned short accept_access_token;
- unsigned short accept_client_token;
+ int method;
+ char * oauth_scope;
+ char * jwt_decode_key;
+ jwt_alg_t jwt_alg;
+ char * realm;
};
int callback_check_glewlwyd_access_token (const struct _u_request * request, struct _u_response * response, void * user_data);
diff --git a/example_callbacks/static_file/static_file_callback.c b/example_callbacks/static_file/static_file_callback.c
index 5197be5..d5b8692 100644
--- a/example_callbacks/static_file/static_file_callback.c
+++ b/example_callbacks/static_file/static_file_callback.c
@@ -2,7 +2,9 @@
*
* Static file server Ulfius callback
*
- * Copyright 2017 Nicolas Mora <mail@babelouest.org>
+ * Copyright 2017-2018 Nicolas Mora <mail@babelouest.org>
+ *
+ * Version 20180607
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
@@ -42,20 +44,20 @@ const char * get_filename_ext(const char *path) {
* Streaming callback function to ease sending large files
*/
static ssize_t callback_static_file_stream(void * cls, uint64_t pos, char * buf, size_t max) {
- if (cls != NULL) {
- return fread (buf, 1, max, (FILE *)cls);
- } else {
- return U_STREAM_END;
- }
+ if (cls != NULL) {
+ return fread (buf, 1, max, (FILE *)cls);
+ } else {
+ return U_STREAM_END;
+ }
}
/**
* Cleanup FILE* structure when streaming is complete
*/
static void callback_static_file_stream_free(void * cls) {
- if (cls != NULL) {
- fclose((FILE *)cls);
- }
+ if (cls != NULL) {
+ fclose((FILE *)cls);
+ }
}
/**
@@ -67,13 +69,13 @@ int callback_static_file (const struct _u_request * request, struct _u_response
char * file_requested, * file_path, * url_dup_save;
const char * content_type;
- /*
- * Comment this if statement if you put static files url not in root, like /app
- */
- if (response->shared_data != NULL) {
- return U_CALLBACK_CONTINUE;
- }
-
+ /*
+ * Comment this if statement if you put static files url not in root, like /app
+ */
+ if (response->shared_data != NULL) {
+ return U_CALLBACK_CONTINUE;
+ }
+
if (user_data != NULL && ((struct _static_file_config *)user_data)->files_path != NULL) {
file_requested = o_strdup(request->http_url);
url_dup_save = file_requested;
@@ -107,21 +109,25 @@ int callback_static_file (const struct _u_request * request, struct _u_response
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
-
+
content_type = u_map_get_case(((struct _static_file_config *)user_data)->mime_types, get_filename_ext(file_requested));
if (content_type == NULL) {
content_type = u_map_get(((struct _static_file_config *)user_data)->mime_types, "*");
y_log_message(Y_LOG_LEVEL_WARNING, "Static File Server - Unknown mime type for extension %s", get_filename_ext(file_requested));
}
u_map_put(response->map_header, "Content-Type", content_type);
- u_map_put(response->map_header, "Cache-Control", "public, max-age=31536000");
-
- if (ulfius_set_stream_response(response, 200, callback_static_file_stream, callback_static_file_stream_free, length, STATIC_FILE_CHUNK, f) != U_OK) {
- y_log_message(Y_LOG_LEVEL_ERROR, "callback_static_file - Error ulfius_set_stream_response");
- }
+
+ if (ulfius_set_stream_response(response, 200, callback_static_file_stream, callback_static_file_stream_free, length, STATIC_FILE_CHUNK, f) != U_OK) {
+ y_log_message(Y_LOG_LEVEL_ERROR, "callback_static_file - Error ulfius_set_stream_response");
+ }
}
} else {
- ulfius_set_string_body_response(response, 404, "File not found");
+ if (((struct _static_file_config *)user_data)->redirect_on_404 == NULL) {
+ ulfius_set_string_body_response(response, 404, "File not found");
+ } else {
+ ulfius_add_header_to_response(response, "Location", ((struct _static_file_config *)user_data)->redirect_on_404);
+ response->status = 302;
+ }
}
o_free(file_path);
o_free(url_dup_save);
diff --git a/example_callbacks/static_file/static_file_callback.h b/example_callbacks/static_file/static_file_callback.h
index a57c72c..d63a88f 100644
--- a/example_callbacks/static_file/static_file_callback.h
+++ b/example_callbacks/static_file/static_file_callback.h
@@ -1,8 +1,12 @@
/**
+ *
+ * Version 20180607
+ *
* struct static_file_config must be initialized with proper values
* files_path: path (relative or absolute) to the DocumentRoot folder
* url_prefix: prefix used to access the callback function
* mime_types: a struct _u_map filled with all the mime-types needed for a static file server
+ * redirect_on_404: redirct uri on error 404, if NULL, send 404
*
* example of mime-types used in Hutch:
* {
@@ -61,6 +65,7 @@ struct _static_file_config {
char * files_path;
char * url_prefix;
struct _u_map * mime_types;
+ char * redirect_on_404;
};
int callback_static_file (const struct _u_request * request, struct _u_response * response, void * user_data);
diff --git a/include/ulfius.h b/include/ulfius.h
index 2babd3f..bd8316b 100644
--- a/include/ulfius.h
+++ b/include/ulfius.h
@@ -26,7 +26,7 @@
#ifndef __ULFIUS_H__
#define __ULFIUS_H__
-#define ULFIUS_VERSION 2.3.5
+#define ULFIUS_VERSION 2.3.6
/** External dependencies **/
@@ -666,6 +666,9 @@ struct _u_response * ulfius_duplicate_response(const struct _u_response * respon
/**
* ulfius_get_json_body_request
* Get JSON structure from the request body if the request is valid
+ * In case of an error in getting or parsing JSON data in the request,
+ * the structure json_error_t * json_error will be filled with an error
+ * message if json_error is not NULL
*/
json_t * ulfius_get_json_body_request(const struct _u_request * request, json_error_t * json_error);
@@ -677,17 +680,20 @@ json_t * ulfius_get_json_body_request(const struct _u_request * request, json_er
int ulfius_set_json_body_request(struct _u_request * request, json_t * j_body);
/**
+ * ulfius_get_json_body_response
+ * Get JSON structure from the response body if the request is valid
+ * In case of an error in getting or parsing JSON data in the request,
+ * the structure json_error_t * json_error will be filled with an error
+ * message if json_error is not NULL
+ */
+json_t * ulfius_get_json_body_response(struct _u_response * response, json_error_t * json_error);
+
+/**
* ulfius_set_json_body_response
* Add a json_t j_body to a response
* return U_OK on success
*/
int ulfius_set_json_body_response(struct _u_response * response, const unsigned int status, const json_t * j_body);
-
-/**
- * ulfius_get_json_body_response
- * Get JSON structure from the response body if the request is valid
- */
-json_t * ulfius_get_json_body_response(struct _u_response * response, json_error_t * json_error);
#endif
/************************************************************************
diff --git a/src/Makefile b/src/Makefile
index af57c95..34b841d 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -27,7 +27,7 @@ CC=gcc
CFLAGS+=-c -pedantic -std=gnu99 -fPIC -Wall -D_REENTRANT -I$(DESTDIR)/include -I$(ULFIUS_INCLUDE) -I$(LIBORCANIA_LOCATION) -I$(LIBYDER_LOCATION) $(ADDITIONALFLAGS) $(JANSSONFLAG) $(CURLFLAG) $(WEBSOCKETFLAG) $(CPPFLAGS)
LIBS=-L$(DESTDIR)/lib -L$(LIBORCANIA_LOCATION) -L$(LIBYDER_LOCATION) -lc -lmicrohttpd -lyder -lorcania -lpthread $(LDFLAGS)
OUTPUT=libulfius.so
-VERSION=2.3.5
+VERSION=2.3.6
ifndef JANSSONFLAG
LJANSSON=-ljansson
diff --git a/src/u_websocket.c b/src/u_websocket.c
index 9957c85..c4f98dc 100644
--- a/src/u_websocket.c
+++ b/src/u_websocket.c
@@ -715,7 +715,7 @@ char * ulfius_check_first_match(const char * source, const char * match, const c
*/
int ulfius_close_websocket(struct _websocket * websocket) {
if (websocket != NULL && websocket->websocket_manager != NULL) {
- if (websocket->websocket_onclose_callback != NULL && websocket->websocket_manager->connected) {
+ if (websocket->websocket_onclose_callback != NULL) {
// Call websocket_onclose_callback if set
websocket->websocket_onclose_callback(websocket->request, websocket->websocket_manager, websocket->websocket_onclose_user_data);
}