Skip to content

Commit

Permalink
Use static keyword for internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
babelouest committed Jul 29, 2017
1 parent 6c47c2b commit 430278f
Show file tree
Hide file tree
Showing 9 changed files with 570 additions and 595 deletions.
14 changes: 7 additions & 7 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LIBORCANIA_LOCATION=../lib/orcania/src
LIBYDER_LOCATION=../lib/yder/src
PREFIX=/usr/local
CC=gcc
CFLAGS=-c -fPIC -Wall -D_REENTRANT -I$(PREFIX)/include -I$(LIBORCANIA_LOCATION) -I$(LIBYDER_LOCATION) $(ADDITIONALFLAGS) $(JANSSONFLAG) $(CURLFLAG) $(WEBSOCKETFLAG)
CFLAGS=-c -pedantic -std=gnu99 -fPIC -Wall -D_REENTRANT -I$(PREFIX)/include -I$(LIBORCANIA_LOCATION) -I$(LIBYDER_LOCATION) $(ADDITIONALFLAGS) $(JANSSONFLAG) $(CURLFLAG) $(WEBSOCKETFLAG)
LIBS=-L$(PREFIX)/lib -L$(LIBORCANIA_LOCATION) -L$(LIBYDER_LOCATION) -lc -lmicrohttpd -lyder -lorcania -lpthread
OUTPUT=libulfius.so
VERSION=2.1
Expand All @@ -46,22 +46,22 @@ libulfius.so: ulfius.o u_map.o u_request.o u_response.o u_send_request.o u_webso
$(CC) -shared -Wl,-soname,$(OUTPUT) -o $(OUTPUT).$(VERSION) ulfius.o u_map.o u_request.o u_response.o u_send_request.o u_websocket.o $(LIBS) $(LJANSSON) $(LCURL) $(LWEBSOCKET)
ln -sf $(OUTPUT).$(VERSION) $(OUTPUT)

ulfius.o: ulfius.h ulfius.c
ulfius.o: ulfius.h u_private.h ulfius.c
$(CC) $(CFLAGS) ulfius.c

u_map.o: ulfius.h u_map.c
u_map.o: ulfius.h u_private.h u_map.c
$(CC) $(CFLAGS) u_map.c

u_request.o: ulfius.h u_request.c
u_request.o: ulfius.h u_private.h u_request.c
$(CC) $(CFLAGS) u_request.c

u_response.o: ulfius.h u_response.c
u_response.o: ulfius.h u_private.h u_response.c
$(CC) $(CFLAGS) u_response.c

u_websocket.o: ulfius.h u_websocket.c
u_websocket.o: ulfius.h u_private.h u_websocket.c
$(CC) $(CFLAGS) u_websocket.c

u_send_request.o: ulfius.h u_send_request.c
u_send_request.o: ulfius.h u_private.h u_send_request.c
$(CC) $(CFLAGS) u_send_request.c

clean:
Expand Down
1 change: 1 addition & 0 deletions src/u_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <stdio.h>
#include <string.h>

#include "u_private.h"
#include "ulfius.h"

/**
Expand Down
169 changes: 169 additions & 0 deletions src/u_private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/**
*
* Ulfius Framework
*
* REST framework library
*
* u_private.h: private structures and functions declarations
*
* Copyright 2015-2017 Nicolas Mora <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/

#ifndef __U_PRIVATE_H__
#define __U_PRIVATE_H__

#include "ulfius.h"

/**********************************
* Internal functions declarations
**********************************/

/**
* ulfius_endpoint_match
* return the endpoint array matching the url called with the proper http method
* the returned array always has its last value to NULL
* return NULL on memory error
*/
struct _u_endpoint ** ulfius_endpoint_match(const char * method, const char * url, struct _u_endpoint * endpoint_list);

/**
* ulfius_parse_url
* fills map with the keys/values defined in the url that are described in the endpoint format url
* return U_OK on success
*/
int ulfius_parse_url(const char * url, const struct _u_endpoint * endpoint, struct _u_map * map);

/**
* ulfius_set_response_header
* adds headers defined in the response_map_header to the response
* return the number of added headers, -1 on error
*/
int ulfius_set_response_header(struct MHD_Response * response, const struct _u_map * response_map_header);

/**
* ulfius_set_response_cookie
* adds cookies defined in the response_map_cookie
* return the number of added headers, -1 on error
*/
int ulfius_set_response_cookie(struct MHD_Response * mhd_response, const struct _u_response * response);

#ifndef U_DISABLE_WEBSOCKET

/**
* Websocket callback function for MHD
* Starts the websocket manager if set,
* then sets a listening message loop
* Complete the callback when the websocket is closed
* The websocket can be closed by the client, the manager, the program, or on network disconnect
*/
void ulfius_start_websocket_cb (void *cls,
struct MHD_Connection *connection,
void *con_cls,
const char *extra_in,
size_t extra_in_size,
MHD_socket sock,
struct MHD_UpgradeResponseHandle *urh);

/**
* Workaround to make sure a message, as long as it can be is complete sent
*/
void ulfius_websocket_send_all(MHD_socket sock, const uint8_t * data, size_t len);

/**
* Centralise socket reading in this function
* so if options or check must be done, it's done here instead of each read call
*/
size_t ulfius_websocket_recv_all(MHD_socket sock, uint8_t * data, size_t len);

/**
* Generates a handhshake answer from the key given in parameter
*/
int ulfius_generate_handshake_answer(const char * key, char * out_digest);

/**
* Return a match list between two list of items
* If match is NULL, then return source duplicate
* Returned value must be u_free'd after use
*/
char * ulfius_check_list_match(const char * source, const char * match);

/**
* Initialize a websocket message list
* Return U_OK on success
*/
int ulfius_init_websocket_message_list(struct _websocket_message_list * message_list);

/**
* Clear data of a websocket message list
*/
void ulfius_clear_websocket_message_list(struct _websocket_message_list * message_list);

/**
* Append a message in a message list
* Return U_OK on success
*/
int ulfius_push_websocket_message(struct _websocket_message_list * message_list, struct _websocket_message * message);

/**
* Clear data of a websocket
*/
int ulfius_clear_websocket(struct _websocket * websocket);

/**
* Clear data of a websocket_manager
*/
void ulfius_clear_websocket_manager(struct _websocket_manager * websocket_manager);

/**
* Close the websocket
*/
int ulfius_close_websocket(struct _websocket * websocket);

/**
* Read and parse a new message from the websocket
* Return the opcode of the new websocket, U_WEBSOCKET_OPCODE_NONE if no message arrived, or U_WEBSOCKET_OPCODE_ERROR on error
* Sets the new message in the message variable if available
*/
int ulfius_read_incoming_message(struct _websocket_manager * websocket_manager, struct _websocket_message ** message);

/**
* Run the websocket manager in a separated detached thread
*/
void * ulfius_thread_websocket_manager_run(void * args);

/**
* Send a message in the websocket without lock
* Return U_OK on success
*/
int ulfius_websocket_send_message_nolock(struct _websocket_manager * websocket_manager,
const uint8_t opcode,
const uint64_t data_len,
const char * data);

/**
* Add a websocket in the list of active websockets of the instance
*/
int ulfius_instance_add_websocket_active(struct _u_instance * instance, struct _websocket * websocket);

/**
* Remove a websocket from the list of active websockets of the instance
*/
int ulfius_instance_remove_websocket_active(struct _u_instance * instance, struct _websocket * websocket);

#endif // U_DISABLE_WEBSOCKET

#endif // __U_PRIVATE_H__
43 changes: 22 additions & 21 deletions src/u_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
#include <stdlib.h>
#include <string.h>

#include "u_private.h"
#include "ulfius.h"

/**
* Splits the url to an array of char *
*/
char ** ulfius_split_url(const char * prefix, const char * url) {
static char ** ulfius_split_url(const char * prefix, const char * url) {
char * saveptr = NULL, * cur_word = NULL, ** to_return = o_malloc(sizeof(char*)), * url_cpy = NULL, * url_cpy_addr = NULL;
int counter = 1;

Expand Down Expand Up @@ -90,7 +91,7 @@ char ** ulfius_split_url(const char * prefix, const char * url) {
* Compare two endoints by their priorities
* Used by qsort to compare endpoints
*/
int compare_endpoint_priorities(const void * a, const void * b) {
static int compare_endpoint_priorities(const void * a, const void * b) {
struct _u_endpoint * e1 = *(struct _u_endpoint **)a, * e2 = *(struct _u_endpoint **)b;

if (e1->priority < e2->priority) {
Expand All @@ -102,6 +103,25 @@ int compare_endpoint_priorities(const void * a, const void * b) {
}
}

/**
* ulfius_url_format_match
* return true if splitted_url matches splitted_url_format
* false otherwise
*/
static int ulfius_url_format_match(const char ** splitted_url, const char ** splitted_url_format) {
int i;

for (i=0; splitted_url_format[i] != NULL; i++) {
if (splitted_url_format[i][0] == '*' && splitted_url_format[i+1] == NULL) {
return 1;
}
if (splitted_url[i] == NULL || (splitted_url_format[i][0] != '@' && splitted_url_format[i][0] != ':' && 0 != o_strcmp(splitted_url_format[i], splitted_url[i]))) {
return 0;
}
}
return (splitted_url[i] == NULL && splitted_url_format[i] == NULL);
}

/**
* ulfius_endpoint_match
* return the endpoint array matching the url called with the proper http method
Expand Down Expand Up @@ -148,25 +168,6 @@ struct _u_endpoint ** ulfius_endpoint_match(const char * method, const char * ur
return endpoint_returned;
}

/**
* ulfius_url_format_match
* return true if splitted_url matches splitted_url_format
* false otherwise
*/
int ulfius_url_format_match(const char ** splitted_url, const char ** splitted_url_format) {
int i;

for (i=0; splitted_url_format[i] != NULL; i++) {
if (splitted_url_format[i][0] == '*' && splitted_url_format[i+1] == NULL) {
return 1;
}
if (splitted_url[i] == NULL || (splitted_url_format[i][0] != '@' && splitted_url_format[i][0] != ':' && 0 != o_strcmp(splitted_url_format[i], splitted_url[i]))) {
return 0;
}
}
return (splitted_url[i] == NULL && splitted_url_format[i] == NULL);
}

/**
* ulfius_parse_url
* fills map with the keys/values defined in the url that are described in the endpoint format url
Expand Down
Loading

0 comments on commit 430278f

Please sign in to comment.