Skip to content

Commit

Permalink
Merge branch 'dev' into feat/thread
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePedroo authored Feb 2, 2024
2 parents 61e9dc0 + 25aceab commit 7717c3d
Show file tree
Hide file tree
Showing 32 changed files with 436 additions and 218 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# But these
!AUTHORS
!.vscode
!core
!core/
!core/*.c
!core/*.h
!docs
!docs/*
!doxygen-awesome-css
Expand Down
3 changes: 1 addition & 2 deletions KNOWN_BUGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ for this have been made.
- There are several bugs in the CWS codebase that causes dependance on
little-endian CPUs. Despite making the obvious corrections (sus_imp0stor
rectified the issues), an unsolved bug (1/20/23) was spotted in the CWS
codebase that caused all opened websocket connections to close -- a fix
is under development and will be out soon.
codebase that caused all opened websocket connections to close



9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ The only dependency is `curl-7.56.1` or higher. If you are compiling libcurl fro

* Install **Cygwin**
* **Make sure that you installed libcurl, gcc, make, and git when you ran the Cygwin installer!**
* You will want to check the Windows tutorial [here](docs/WINDOWS.md)!
* Mingw64 and Msys2 are currently NOT supported. Please see [this](docs/MSYS2_MINGW64.md) for more information.
* You will want to check the Windows tutorial [here](docs/guides/compiling_on_windows.md)!
* Mingw64 and Msys2 are currently NOT supported. Please see [this](docs/guides/msys2_and_mingw64.md) for more information.
* Once installed, compile it normally like you would on UNIX/Linux/OS X/BSD.
* Note: you will likely need to include `-L/usr/local/lib -I/usr/local/include` on your `gcc` command, or in your `CFLAGS` variable in your Makefile for your bot.

Expand Down Expand Up @@ -141,7 +141,7 @@ The only dependency is `curl-7.56.1` or higher. If you are compiling libcurl fro
#### FreeBSD

```console
$ pkg install curl
# pkg install curl
```

#### OS X
Expand Down Expand Up @@ -304,6 +304,9 @@ This will install the headers and library files into $PREFIX. You can override t
# PREFIX=/opt/concord make install
```

### Cross-compiling Concord
To cross-compile Concord, see the manual [here](docs/guides/cross_compiling.md).

### Included dependencies

The following are `stable` and well documented dependencies that are packaged with Concord and can be included to your projects:
Expand Down
5 changes: 5 additions & 0 deletions core/attributes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#if defined(__MINGW32__) || (defined (__GNUC__) && __GNUC__ > 4 ? true : __GNUC_PATCHLEVEL__ >= 4) || defined(__USE_MINGW_ANSI_STDIO)
# define PRINTF_LIKE(a, b) __attribute__((format(gnu_printf, a, b)))
#else
# define PRINTF_LIKE(a, b)
#endif
3 changes: 2 additions & 1 deletion core/cog-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdio.h>
#include <stdint.h>
#include "attributes.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -97,7 +98,7 @@ size_t cog_strndup(const char src[], size_t len, char **p_dest);
* @param ... variadic arguments to be matched to `fmt` specifiers
* @return length of copied string on success, -1 on failure
*/
size_t cog_asprintf(char **strp, const char fmt[], ...);
size_t cog_asprintf(char **strp, const char fmt[], ...) PRINTF_LIKE(2, 3);

/**
* @brief Sleep for amount of milliseconds
Expand Down
1 change: 1 addition & 0 deletions core/curl-websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,7 @@ cws_new(const char *url, const char *websocket_protocols, const struct cws_callb
url = tmp;
}
curl_easy_setopt(easy, CURLOPT_URL, url);
curl_easy_setopt(easy, CURLOPT_PROXY, "");
free(tmp);

/*
Expand Down
12 changes: 9 additions & 3 deletions core/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
* @brief Concord error codes and meaning
* @{ */

/* XXX: As new values are added, discord_strerror() should be updated
* accordingly! */
typedef enum CCORDcode {
/** failure when creating request's payload */
CCORD_MALFORMED_PAYLOAD = -12,
/** couldn't enqueue worker thread (queue is full) */
CCORD_FULL_WORKER = -11,
/** couldn't perform action because resource is unavailable */
CCORD_UNAVAILABLE = -10,
/** couldn't perform action because of resource's ownership issues */
CCORD_OWNERSHIP = -9,
CCORD_RESOURCE_UNAVAILABLE = -10,
/* deprecated */
CCORD_UNAVAILABLE = CCORD_RESOURCE_UNAVAILABLE,
/** couldn't cleanup resource automatically due to being claimed */
CCORD_RESOURCE_OWNERSHIP = -9,
/* deprecated */
CCORD_OWNERSHIP = CCORD_RESOURCE_OWNERSHIP,
/** attempt to initialize globals more than once */
CCORD_GLOBAL_INIT = -8,
/** curl's multi handle internal error */
Expand Down
44 changes: 19 additions & 25 deletions core/jsmn-find.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ extern "C" {

#ifndef JSMN_H
#error "jsmn-find.h should be included after jsmn.h"
#endif

#else
/** @brief JSON token description */
struct jsmnftok {
/** start position in JSON data string */
Expand Down Expand Up @@ -163,13 +162,13 @@ JSMN_API long jsmnf_unescape(char buf[],
#include <string.h>

/* key */
#define CHASH_KEY_FIELD k
#define CHASH_KEY_FIELD k
/* value */
#define CHASH_VALUE_FIELD v
#define CHASH_VALUE_FIELD v
/* fields */
#define CHASH_BUCKETS_FIELD fields
/* members count */
#define CHASH_LENGTH_FIELD size
#define CHASH_LENGTH_FIELD size

#include "chash.h"

Expand Down Expand Up @@ -405,14 +404,13 @@ jsmnf_find_path(const struct jsmnf_pair *head,

#define RECALLOC_OR_ERROR(ptr, prev_size) \
do { \
const unsigned new_size = *prev_size * 2; \
const unsigned new_size = *(prev_size)*2; \
void *tmp = realloc((ptr), new_size * sizeof *(ptr)); \
if (!tmp) return JSMN_ERROR_NOMEM; \
\
*prev_size = new_size; \
(ptr) = tmp; \
memset((ptr) + *(prev_size), 0, \
(new_size - *(prev_size)) * sizeof *(ptr)); \
(ptr) = tmp; \
*(prev_size) = new_size; \
} while (0)

JSMN_API int
Expand All @@ -424,17 +422,14 @@ jsmn_parse_auto(struct jsmn_parser *parser,
{
int ret;

if (NULL == *p_tokens || !*num_tokens) {
if (NULL == *p_tokens || 0 == *num_tokens) {
*p_tokens = calloc(1, sizeof **p_tokens);
*num_tokens = 1;
}

while (1) {
ret = jsmn_parse(parser, js, length, *p_tokens, *num_tokens);
if (ret != JSMN_ERROR_NOMEM)
break;
else
RECALLOC_OR_ERROR(*p_tokens, num_tokens);
while (JSMN_ERROR_NOMEM
== (ret = jsmn_parse(parser, js, length, *p_tokens, *num_tokens)))
{
RECALLOC_OR_ERROR(*p_tokens, num_tokens);
}
return ret;
}
Expand All @@ -449,17 +444,15 @@ jsmnf_load_auto(struct jsmnf_loader *loader,
{
int ret;

if (NULL == *p_pairs || !*num_pairs) {
if (NULL == *p_pairs || 0 == *num_pairs) {
*p_pairs = calloc(1, sizeof **p_pairs);
*num_pairs = 1;
}

while (1) {
ret = jsmnf_load(loader, js, tokens, num_tokens, *p_pairs, *num_pairs);
if (ret != JSMN_ERROR_NOMEM)
break;
else
RECALLOC_OR_ERROR(*p_pairs, num_pairs);
while (JSMN_ERROR_NOMEM
== (ret = jsmnf_load(loader, js, tokens, num_tokens, *p_pairs,
*num_pairs)))
{
RECALLOC_OR_ERROR(*p_pairs, num_pairs);
}
return ret;
}
Expand Down Expand Up @@ -723,6 +716,7 @@ jsmnf_unescape(char buf[], size_t bufsize, const char src[], size_t len)
#undef BUF_PUSH

#endif /* JSMN_HEADER */
#endif /* JSMN_H */

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion core/logconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern "C" {

#include <stdint.h> /* uint64_t */

#include "attributes.h"
#include "log.h"

#define __ERR(fmt, ...) log_fatal(fmt "%s", __VA_ARGS__)
Expand Down Expand Up @@ -318,7 +319,7 @@ void logconf_http(struct logconf *conf,
struct logconf_szbuf header,
struct logconf_szbuf body,
char label_fmt[],
...);
...) PRINTF_LIKE(6, 7);

/**
* @brief If the log will be written to from multiple threads a lock function
Expand Down
4 changes: 3 additions & 1 deletion core/queriec.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define QUERIEC_ERROR_NOMEM -1
#define QUERIEC_OK 0

#include "attributes.h"

struct queriec {
int state;
size_t size;
Expand All @@ -16,7 +18,7 @@ void
queriec_init(struct queriec *queriec, size_t size);

int queriec_snprintf_add(struct queriec *queriec, char *query, const char key[], size_t keySize,
char buffer[], size_t bufferLen, const char *format, ...);
char buffer[], size_t bufferLen, const char *format, ...) PRINTF_LIKE(7, 8);

int
queriec_add(struct queriec *queriec, char *query, char key[], size_t keySize, char value[], size_t valueSize);
Expand Down
15 changes: 0 additions & 15 deletions core/sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,3 @@ void SHA1Final(
memset(context, '\0', sizeof(*context));
memset(&finalcount, '\0', sizeof(finalcount));
}

void SHA1(
char *hash_out,
const char *str,
int len)
{
SHA1_CTX ctx;
unsigned int ii;

SHA1Init(&ctx);
for (ii=0; ii<len; ii+=1)
SHA1Update(&ctx, (const unsigned char*)str + ii, 1);
SHA1Final((unsigned char *)hash_out, &ctx);
}

5 changes: 0 additions & 5 deletions core/sha1.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,4 @@ void SHA1Final(
SHA1_CTX * context
);

void SHA1(
char *hash_out,
const char *str,
int len);

#endif /* SHA1_H */
33 changes: 18 additions & 15 deletions core/user-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ ua_conn_add_header(struct ua_conn *conn,
const char value[])
{
size_t fieldlen = strlen(field);
struct curl_slist *node;
struct curl_slist *node, *prev;
char buf[4096];
size_t buflen;
char *ptr;
Expand All @@ -224,20 +224,26 @@ ua_conn_add_header(struct ua_conn *conn,
ASSERT_S(buflen < sizeof(buf), "Out of bounds write attempt");

/* check for match in existing fields */
for (node = conn->header; node != NULL; node = node->next) {
for (prev = NULL, node = conn->header; node != NULL;
prev = node, node = node->next)
{
if (!(ptr = strchr(node->data, ':')))
ERR("Missing ':' in header:\n\t%s", node->data);

if (fieldlen == (size_t)(ptr - node->data)
&& 0 == strncasecmp(node->data, field, fieldlen))
{
if (strlen(node->data) < buflen) {
/* FIXME: For some reason, cygwin builds will abort on this
* free() */
#ifndef __CYGWIN__
free(node->data);
#endif
node->data = strdup(buf);
if (prev) prev->next = node->next;
// XXX: since libcurl uses custom mallocs that offsets the data
// with some metadata, we rely on `curl_free()` to ensure
// proper behavior
curl_free(node->data);
curl_free(node);
// XXX: this process can be optimized by completely replacing
// libcurl's `curl_slist_xxx()` functions with our own
// (see above comment)
curl_slist_append(conn->header, buf);
}
else {
memcpy(node->data, buf, buflen + 1);
Expand Down Expand Up @@ -274,12 +280,9 @@ ua_conn_remove_header(struct ua_conn *conn, const char field[])
else
prev->next = node->next;

/* FIXME: For some reason, cygwin builds will abort on this
* free() */
#ifndef __CYGWIN__
free(node->data);
free(node);
#endif
curl_free(node->data);
curl_free(node);

return;
}
}
Expand Down Expand Up @@ -522,7 +525,7 @@ _ua_info_populate(struct ua_info *info, struct ua_conn *conn)
curl_easy_getinfo(conn->ehandle, CURLINFO_EFFECTIVE_URL, &resp_url);

logconf_http(&conn->ua->conf, &conn->info.loginfo, resp_url, logheader,
logbody, "HTTP_RCV_%s(%d)", http_code_print(info->httpcode),
logbody, "HTTP_RCV_%s(%ld)", http_code_print(info->httpcode),
info->httpcode);
}

Expand Down
Loading

0 comments on commit 7717c3d

Please sign in to comment.