Compare commits

..

2 commits

Author SHA1 Message Date
13ccc1b3bd Use stdint.h types instead of int, long, etc.
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 7s
2024-09-20 21:45:51 +09:00
5cc0c37d45 Update third_party submodule SimpleArchiver 2024-09-20 21:45:18 +09:00
13 changed files with 51 additions and 38 deletions

View file

@ -32,7 +32,7 @@ void print_usage(void) {
puts(" --enable-reload-config-on-change");
}
Args parse_args(int argc, char **argv) {
Args parse_args(int32_t argc, char **argv) {
--argc;
++argv;
@ -44,9 +44,9 @@ Args parse_args(int argc, char **argv) {
while (argc > 0) {
if ((strcmp(argv[0], "-p") == 0 || strcmp(argv[0], "--port") == 0)
&& argc > 1) {
int value = atoi(argv[1]);
int32_t value = atoi(argv[1]);
if (value >= 0 && value <= 0xFFFF) {
args.port = (unsigned short) value;
args.port = (uint16_t) value;
}
--argc;
++argv;

View file

@ -17,6 +17,9 @@
#ifndef SEODISPARATE_COM_C_SIMPLE_HTTP_ARG_PARSE_H_
#define SEODISPARATE_COM_C_SIMPLE_HTTP_ARG_PARSE_H_
// Standard library includes.
#include <stdint.h>
// Third party includes.
#include <SimpleArchiver/src/data_structures/linked_list.h>
@ -25,8 +28,8 @@ typedef struct Args {
// xxxx xxx1 - disable peer addr print.
// xxxx xx0x - disable listen on config file for reloading.
// xxxx xx1x - enable listen on config file for reloading.
unsigned short flags;
unsigned short port;
uint16_t flags;
uint16_t port;
// Does not need to be free'd, this should point to a string in argv.
const char *config_file;
// Needs to be free'd.

View file

@ -18,14 +18,14 @@
int is_big_endian(void) {
union {
int i;
int32_t i;
char c[4];
} bint = {0x01020304};
return bint.c[0] == 1 ? 1 : 0;
}
unsigned short u16_be_swap(unsigned short value) {
uint16_t u16_be_swap(uint16_t value) {
if (is_big_endian()) {
return value;
} else {

View file

@ -17,9 +17,12 @@
#ifndef SEODISPARATE_COM_C_SIMPLE_HTTP_BIG_ENDIAN_H_
#define SEODISPARATE_COM_C_SIMPLE_HTTP_BIG_ENDIAN_H_
// Standard library includes.
#include <stdint.h>
int is_big_endian(void);
unsigned short u16_be_swap(unsigned short value);
uint16_t u16_be_swap(uint16_t value);
#endif

View file

@ -237,7 +237,7 @@ typedef struct C_SIMPLE_HTTP_INTERNAL_RequiredIter {
int c_simple_http_required_iter_fn(void *data, void *ud) {
C_SIMPLE_HTTP_INTERNAL_RequiredIter *req_iter_struct = ud;
unsigned int data_str_length = (unsigned int)strlen(data) + 1;
uint32_t data_str_length = (uint32_t)strlen(data) + 1;
if (simple_archiver_hash_map_get(
req_iter_struct->hash_map,
data,
@ -267,7 +267,7 @@ typedef struct C_SIMPLE_HTTP_INTERNAL_RequiredCheck {
int c_simple_http_check_required_iter_fn(void *path_void_str, void *ud) {
C_SIMPLE_HTTP_INTERNAL_RequiredCheck *req = ud;
unsigned int path_void_str_len = (unsigned int)strlen(path_void_str) + 1;
uint32_t path_void_str_len = (uint32_t)strlen(path_void_str) + 1;
C_SIMPLE_HTTP_HashMapWrapper *wrapper =
simple_archiver_hash_map_get(
req->map_of_paths_and_their_vars,
@ -303,8 +303,8 @@ C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config(
fprintf(stderr, "ERROR: separating_key argument is NULL!\n");
return config;
}
const unsigned int separating_key_size =
(unsigned int)strlen(separating_key) + 1;
const uint32_t separating_key_size =
(uint32_t)strlen(separating_key) + 1;
config.hash_map = simple_archiver_hash_map_init();
@ -320,21 +320,21 @@ C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config(
}
unsigned char key_buf[C_SIMPLE_HTTP_CONFIG_BUF_SIZE];
unsigned char value_buf[C_SIMPLE_HTTP_CONFIG_BUF_SIZE];
unsigned int key_idx = 0;
unsigned int value_idx = 0;
uint32_t key_idx = 0;
uint32_t value_idx = 0;
__attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))
char *current_separating_key_value = NULL;
unsigned int current_separating_key_value_size = 0;
uint32_t current_separating_key_value_size = 0;
// xxx0 - reading key
// xxx1 - reading value
// 00xx - reading value is not quoted
// 01xx - reading value is single quoted
// 10xx - reading value is double quoted
unsigned int state = 0;
uint32_t state = 0;
unsigned char single_quote_count = 0;
unsigned char double_quote_count = 0;
int c;
int32_t c;
while (feof(f) == 0) {
c = fgetc(f);

View file

@ -55,7 +55,7 @@ const char *c_simple_http_response_code_error_to_response(
char *c_simple_http_request_response(
const char *request,
unsigned int size,
uint32_t size,
const C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t *out_size,
enum C_SIMPLE_HTTP_ResponseCode *out_response_code) {
@ -63,9 +63,9 @@ char *c_simple_http_request_response(
*out_size = 0;
}
// parse first line.
unsigned int idx = 0;
uint32_t idx = 0;
char request_type[REQUEST_TYPE_BUFFER_SIZE] = {0};
unsigned int request_type_idx = 0;
uint32_t request_type_idx = 0;
for (; idx < size
&& request[idx] != ' '
&& request[idx] != '\n'
@ -93,7 +93,7 @@ char *c_simple_http_request_response(
++idx) {}
// parse request path.
char request_path[REQUEST_PATH_BUFFER_SIZE] = {0};
unsigned int request_path_idx = 0;
uint32_t request_path_idx = 0;
for (; idx < size
&& request[idx] != ' '
&& request[idx] != '\n'
@ -121,7 +121,7 @@ char *c_simple_http_request_response(
++idx) {}
// parse request http protocol.
char request_proto[REQUEST_PROTO_BUFFER_SIZE] = {0};
unsigned int request_proto_idx = 0;
uint32_t request_proto_idx = 0;
for (; idx < size
&& request[idx] != ' '
&& request[idx] != '\n'
@ -222,7 +222,7 @@ SDArchiverHashMap *c_simple_http_request_to_headers_map(
// xxxx xx00 - Beginning of line.
// xxxx xx01 - Reached end of header key.
// xxxx xx10 - Non-header line.
unsigned int state = 0;
uint32_t state = 0;
size_t idx = 0;
size_t header_key_idx = 0;
__attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))

View file

@ -19,6 +19,7 @@
// Standard library includes.
#include <stddef.h>
#include <stdint.h>
// Third party includes.
#include <SimpleArchiver/src/data_structures/hash_map.h>
@ -44,7 +45,7 @@ const char *c_simple_http_response_code_error_to_response(
/// If the request is not valid, or 404, then the buffer will be NULL.
char *c_simple_http_request_response(
const char *request,
unsigned int size,
uint32_t size,
const C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t *out_size,
enum C_SIMPLE_HTTP_ResponseCode *out_response_code

View file

@ -19,6 +19,7 @@
// Standard library includes.
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
// Third party includes.
#include <SimpleArchiver/src/data_structures/linked_list.h>
@ -104,7 +105,7 @@ char *c_simple_http_path_to_generated(
fprintf(stderr, "ERROR: Path string is too large!\n");
return NULL;
}
unsigned int path_len = (unsigned int)path_len_size_t;
uint32_t path_len = (uint32_t)path_len_size_t;
C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map =
simple_archiver_hash_map_get(templates->hash_map, path, path_len);
if (!wrapped_hash_map) {
@ -164,7 +165,7 @@ char *c_simple_http_path_to_generated(
// xxxx xxx0 - Initial state, no delimeter reached.
// xxxx xxx1 - Three left-curly-brace delimeters reached.
unsigned int state = 0;
uint32_t state = 0;
for (; idx < html_buf_size; ++idx) {
if ((state & 1) == 0) {
@ -216,7 +217,7 @@ char *c_simple_http_path_to_generated(
simple_archiver_hash_map_get(
wrapped_hash_map->hash_map,
var,
(unsigned int)var_size + 1);
(uint32_t)var_size + 1);
if (value_c_str) {
if (c_simple_http_internal_ends_with_FILE(var) == 0) {
// Load from file specified by "value_c_str".

View file

@ -18,6 +18,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
// Linux/Unix includes.
#include <sys/socket.h>
@ -161,9 +162,9 @@ int main(int argc, char **argv) {
socklen_t socket_len;
// xxxx xxx1 - config needs to be reloaded.
unsigned int flags = 0;
uint32_t flags = 0;
size_t config_try_reload_ticks_count = 0;
unsigned int config_try_reload_attempts = 0;
uint32_t config_try_reload_attempts = 0;
while (C_SIMPLE_HTTP_KEEP_RUNNING) {
nanosleep(&sleep_time, NULL);
@ -307,7 +308,7 @@ int main(int argc, char **argv) {
// Received connection, handle it.
if ((args.flags & 1) == 0) {
printf("Peer connected: addr is ");
for (unsigned int idx = 0; idx < 16; ++idx) {
for (uint32_t idx = 0; idx < 16; ++idx) {
if (idx % 2 == 0 && idx > 0) {
printf(":");
}
@ -328,7 +329,7 @@ int main(int argc, char **argv) {
}
#ifndef NDEBUG
// DEBUG print received buf.
for (unsigned int idx = 0;
for (uint32_t idx = 0;
idx < C_SIMPLE_HTTP_RECV_BUF_SIZE && idx < read_ret;
++idx) {
if ((recv_buf[idx] >= 0x20 && recv_buf[idx] <= 0x7E)
@ -355,7 +356,7 @@ int main(int argc, char **argv) {
enum C_SIMPLE_HTTP_ResponseCode response_code;
const char *response = c_simple_http_request_response(
(const char*)recv_buf,
(unsigned int)read_ret,
(uint32_t)read_ret,
&parsed_config,
&response_size,
&response_code);
@ -368,7 +369,7 @@ int main(int argc, char **argv) {
size_t content_length_buf_size = 0;
memcpy(content_length_buf, "Content-Length: ", 16);
content_length_buf_size = 16;
int written = 0;
int32_t written = 0;
snprintf(
content_length_buf + content_length_buf_size,
127 - content_length_buf_size,

View file

@ -30,7 +30,7 @@
// Local includes.
#include "big_endian.h"
int create_tcp_socket(unsigned short port) {
int create_tcp_socket(uint16_t port) {
struct sockaddr_in6 ipv6_addr;
memset(&ipv6_addr, 0, sizeof(struct sockaddr_in6));
ipv6_addr.sin6_family = AF_INET6;

View file

@ -17,9 +17,12 @@
#ifndef SEODISPARATE_COM_C_SIMPLE_HTTP_TCP_SOCKET_H_
#define SEODISPARATE_COM_C_SIMPLE_HTTP_TCP_SOCKET_H_
// Standard library includes.
#include <stdint.h>
#define C_SIMPLE_HTTP_TCP_SOCKET_BACKLOG 64
int create_tcp_socket(unsigned short port);
int create_tcp_socket(uint16_t port);
void cleanup_tcp_socket(int *tcp_socket);

View file

@ -2,6 +2,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
// Local includes.
#include "config.h"
@ -12,8 +13,8 @@
#include <SimpleArchiver/src/helpers.h>
#include <SimpleArchiver/src/data_structures/hash_map.h>
static int checks_checked = 0;
static int checks_passed = 0;
static int32_t checks_checked = 0;
static int32_t checks_passed = 0;
#define RETURN() \
do { \

@ -1 +1 @@
Subproject commit 6b862ab022202523606bda2fe39236c71891b304
Subproject commit bb574d2a2c8732769329d4e760a28ead0728458f