summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2021-09-10 21:19:52 +0200
committerAnton Luka Šijanec <anton@sijanec.eu>2021-09-10 21:19:52 +0200
commitf10c726eb72bb4ced8fde4ff3f486ebd56024da8 (patch)
treed2f9d45beef6353c1faf742eeb3deeead9bf18a1
parentfuck me (diff)
downloaddiscord.c-f10c726eb72bb4ced8fde4ff3f486ebd56024da8.tar
discord.c-f10c726eb72bb4ced8fde4ff3f486ebd56024da8.tar.gz
discord.c-f10c726eb72bb4ced8fde4ff3f486ebd56024da8.tar.bz2
discord.c-f10c726eb72bb4ced8fde4ff3f486ebd56024da8.tar.lz
discord.c-f10c726eb72bb4ced8fde4ff3f486ebd56024da8.tar.xz
discord.c-f10c726eb72bb4ced8fde4ff3f486ebd56024da8.tar.zst
discord.c-f10c726eb72bb4ced8fde4ff3f486ebd56024da8.zip
-rw-r--r--src/api.c84
-rw-r--r--src/h.c29
-rw-r--r--src/lib.c2
-rw-r--r--src/main.c2
4 files changed, 65 insertions, 52 deletions
diff --git a/src/api.c b/src/api.c
index b3a756a..2a52f72 100644
--- a/src/api.c
+++ b/src/api.c
@@ -2,7 +2,7 @@
#define DC_USER_AGENT "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
#define DC_SERVER_ADDRESS "localhost"
#define DC_API_PREFIX "/api/v9/"
-#define DC_LWS_ABLE_TO_PARSE_HEADERS 1
+// #define DC_LWS_ABLE_TO_PARSE_HEADERS 1
/* libwebsockets information: libwebsockets works with event loops and discord.c primary targets debian for building (it must work on debian), but debian does not ship libwebsockets with glib or libevent event loop support, so loops are implemented in the classic poll() style. this reduces performance probably. if you use discord.c API with support for a platform specific event loop, you may rewrite LWS to do things differently. currently calls into API (dc_api_i and dc_api_o) will both do LWS stuff. */
void dc_api_stack (struct dc_api_io i) { /* stack output struct to be delivered via dc_api_o 2usr */
DC_MR(i.program->api_ios);
@@ -17,11 +17,16 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us,
lwsl_err("CLIENT_CONNECTION_ERROR: %s\n", in ? (char *) in : "(null)");
break;
case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: /* lws gives us len space after *in */
+ ;
unsigned char ** p = (unsigned char **) in, * end = (*p)+len;
for (int i = 0; i < DC_LWS_HEADERS_LENGTH; i++)
- if (pass.headers[i][0])
- if (lws_add_http_header_by_name(wsi, (const unsigned char *) dc_lws_headers[i], (const unsigned char *) pass.headers[i], strlen(pass.headers[i]), p, end))
+ if (pass->headers[i][0])
+ if (lws_add_http_header_by_name(wsi, (const unsigned char *) dc_lws_headers[i], (const unsigned char *) pass->headers[i], strlen(pass->headers[i]), p, end))
return -1;
+ char slen[32];
+ snprintf(slen, 32, "%u", pass->body_length);
+ if (lws_add_http_header_by_name(wsi, (const unsigned char *) "Content-Length:", (const unsigned char *) slen, strlen(slen), p, end))
+ return -1;
if (!lws_http_is_redirected_to_get(wsi)) {
lwsl_user("doing POST flow\n");
lws_client_http_body_pending(wsi, 1);
@@ -30,57 +35,60 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us,
lwsl_user("doing GET flow, got redirected - this is a bug, report!\n");
break;
case LWS_CALLBACK_CLIENT_HTTP_WRITEABLE: /* see minimal post example on how to send */
- if (lws_http_is_redirected_to_get(wsi)) /* multipart with files, for example */
+ if (lws_http_is_redirected_to_get(wsi)) {
+ fprintf(stderr, "http is redirected to get\n");
break; /* for setting avatar pictures or uploading files. Yecch */
- lwsl_user("LWS_CALLBACK_CLIENT_HTTP_WRITABLE");
- if (!(pass.api_io.status & DC_BODY_SENT)) {
- if (lws_write(wsi, (uint8_t *) pass.body, pass.body_length, LWS_WRITE_HTTP) != (int) pass.body_length) /* TODO: fix to allow sending large bodies */
- return 1; /* we send body in one whole chunk OR FAIL! */
- pass.api_io.status |= DC_BODY_SENT;
- lws_callback_on_writable(wsi);
- } else {
- if (lws_write(wsi, NULL, 0, LWS_WRITE_HTTP_FINAL) == -1 /* ret snt by */)
- return 1; /* _FINAL is necessary to support H2, srv need know */
- lws_client_http_body_pending(wsi, 0);
}
- if (pass.api_io.status & DC_MUST_FREE)
- free(body);
+ fprintf(stderr, "LWS_CALLBACK_CLIENT_HTTP_WRITABLE, %d\n", pass->api_io.status);
+ if (lws_write(wsi, (unsigned char *) pass->body, pass->body_length, LWS_WRITE_HTTP_FINAL) != (int) pass->body_length) { /* TODO: fix to allow sending large bodies */
+ fprintf(stderr, "lws_write can't write that much!\n");
+ return 1;
+ }
+ lws_client_http_body_pending(wsi, 0);
+ if (pass->api_io.status & DC_MUST_FREE) {
+ free(pass->body);
+ pass->body = NULL;
+ }
break;
case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
- pass.status = lws_http_client_http_response(wsi); /* 200 OK, 404 Not Found, ...*/
- fprintf(stderr, "Connected with server response: %d\n", pass.status);
+ pass->status = lws_http_client_http_response(wsi); /* 200 OK, 404 Not Found,...*/
+ fprintf(stderr, "Connected with server response: %d\n", pass->status);
#ifdef DC_LWS_ABLE_TO_PARSE_HEADERS
/* how to query custom headers (http 1.x only at the moment) */
for (int i = 0; i < DC_LWS_HEADERS_LENGTH; i++) {
- pass.headers[i][0] = '\0'; /* to clear any headers we requested with */
+ pass->headers[i][0] = '\0'; /* to clear any headers we requested with */
int n = lws_hdr_custom_length(wsi, dc_lws_headers[i], strlen(dc_lws_headers[i]));
if (n < -1)
lwsl_notice("No header %s.\n", dc_lws_headers[i]);
else
- if (lws_hdr_custom_copy(wsi, pass.headers[i], sizeof(DC_MAX_HEADER_LENGTH), dc_lws_headers[i], strlen(dc_lws_headers[i])) < 0)
+ if (lws_hdr_custom_copy(wsi, pass->headers[i], DC_LWS_MAX_HEADER_LENGTH, dc_lws_headers[i], strlen(dc_lws_headers[i])) < 0)
lwsl_notice("Header %s too long.\n", dc_lws_headers[i]);
else
- lwsl_notice("%s %s\n", dc_lws_headers[i], pass.headers[i]);
+ lwsl_notice("%s %s\n", dc_lws_headers[i], pass->headers[i]);
}
#endif
break;
case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ: /* chunked body, without headers */
- lwsl_user("RECEIVE_CLIENT_HTTP_READ: read %d\n", (int) len);
+ fprintf(stderr, "RECEIVE_CLIENT_HTTP_READ: read %d\n", len);
lwsl_hexdump_notice(in, len); /* for debugging purposes, kek */
pass->body_length = len;
pass->body = in;
- ((char *)in+1)[len] = '\0'; /* NULL terminating note0 src/h.c */
+ pass->body[len] = '\0'; /* NULL terminating note0 src/h.c */
if (pass->api_io.pass != pass)
- fprintf(stderr, "[!!!] REPORT THIS BUG: pass->api_io.pass != pass");
- dc_api_i(pass.api_io);
+ fprintf(stderr, "[!!!] REPORT THIS BUG: pass->api_io.pass != pass\n");
+ pass->api_io.status |= DC_FROM_LWS;
+ dc_api_i(pass->api_io);
+ pass->api_io.status &= ~DC_FROM_LWS;
return 0; /* don't pass to lws_callback_http_dummy */
case LWS_CALLBACK_RECEIVE_CLIENT_HTTP: /* uninterpreted http content */
- ; /* we add 1 to buffer size for NULL terminanting, note0 src/h.c */
+ fprintf(stderr, "LCRCH\n"); /* +1 to bufsize for NULL term, note0 src/h.c */
char buffer[DC_LWS_BUF + LWS_PRE + 1 /* lws needs this many bytes b4 the ptr */];
char * ptr = buffer + LWS_PRE; /* see, BEFORE the pointer - lower addr */
int len2 /* DC_LWS_BUF again */ = sizeof(buffer) - LWS_PRE;
- if (lws_http_client_read(wsi, &ptr, &len2) < 0)
+ if (lws_http_client_read(wsi, &ptr, &len2) < 0) {
+ fprintf(stderr, "lws_http_client_read -1\n");
return -1;
+ }
return 0; /* don't pass to lws_callback_http_dummy */
case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
lwsl_user("LWS_CALLBACK_COMPLETED_CLIENT_HTTP\n");
@@ -89,7 +97,10 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us,
lwsl_user("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n");
break;
case LWS_CALLBACK_WSI_DESTROY: /* if I understand the docs correctly, this is allways */
+ fprintf(stderr, "pass before freed is %p\n", (void *) pass);
free(pass); /* called at the final moment when user pointer and wsi is still */
+ pass = NULL;
+ break;
default: /* accessible, so we can now free the struct that was passed in as a heap ptr */
break;
}
@@ -98,8 +109,8 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us,
void dc_api_i (struct dc_api_io i) { /* this function does not call attached functions, only output does that */
if (!i.program)
return;
- if (i.program->lws_context)
- lws_service(i.program->lws_context, 0);
+ if (i.program->lws_context && !(i.status & DC_FROM_LWS))
+ lws_service(i.program->lws_context, 0); /* DO NOT CALL THIS FROM _cb! */
switch (i.type) {
case DC_API_MESSAGE:
break;
@@ -121,14 +132,15 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun
info.host = info.address;
info.origin = info.address;
info.method = "POST";
- struct dc_lws_pass pass; /* for passing data to/from lws callback function */
- memset(&pass, 0, sizeof(pass));
+ struct dc_lws_pass * pass = calloc(1, sizeof(struct dc_lws_pass)); /* cb frees */
+ fprintf(stderr, "allocated pass at %p\n", (void *) pass);
i.status |= DC_MUST_FREE;
- pass.body_length = smprintf(&pass.body, DC_LOGIN_FORMAT, i.client->email, i.client->password);
+ pass->body_length = smprintf(&pass->body, DC_LOGIN_FORMAT, i.client->email, i.client->password);
+ fprintf(stderr, "length: %u string: %s\n", pass->body_length, pass->body);
i.type = DC_API_LOGIN_CB;
- memcpy(&pass.api_io, &i, sizeof(i))
- info.userdata = malloc(sizeof(pass));
- memcpy(info.userdata, &pass, sizeof(pass));
+ memcpy(&pass->api_io, &i, sizeof(i));
+ pass->api_io.pass = pass;
+ info.userdata = pass;
#ifdef DC_LWS_ABLE_TO_PARSE_HEADERS
info.alpn = "http/1.1";
#endif
@@ -167,7 +179,7 @@ struct dc_api_io dc_api_o (struct dc_api_io i /* for ->program */) {
};
/* dc_api_stack pop: */
if (i.program->api_ios_length) {
- o = *(i.program->api_ios[--i.program->api_ios_length]);
+ memcpy(&o, (i.program->api_ios[--i.program->api_ios_length]), sizeof(o));
dc_api_io_free(i.program->api_ios[i.program->api_ios_length]);
}
for (size_t j = 0; j < i.program->attached_functions_length; j++) { /* call attached functions */
diff --git a/src/h.c b/src/h.c
index 4512cd9..07852a3 100644
--- a/src/h.c
+++ b/src/h.c
@@ -17,18 +17,18 @@
/* it's strongly recommended to calloc structs during initialization. */
enum dc_status { /* theese are flags and should be and-checked */
DC_UNSET = 0, /* default value when enum is calloced */
- DC_INCOMPLETE = 1 << 1, /* struct SHALL NOT be used by the ui, it is yet to be filled by api */
- DC_OK = 1 << 2, /* success status, api usually sets this after completion/filling of the strct */
- DC_BAD_LOGIN = 1 << 3, /* login failed */
- DC_VERIFICATION_NEEDED = 1 << 4, /* login: check email, click link/reg: tough luck ur IP flagd */
- DC_CAPTCHA_NEEDED = 1 << 5, /* must solve captcha, tough luck, not impl, use browser login */
- DC_BAD_USERNAME = 1 << 6, /* provided username can't be registered */
- DC_BAD_EMAIL = 1 << 7, /* provided email can't be registered */
- DC_NOT_FOUND = 1 << 8, /* when querying for roles/users/... received a 404 */
- DC_CONTINUE = 1 << 9, /* attached handlers return this to continue processing this output, N/I */
- DC_BREAK = 1 << 10, /* attached handlers return this to stop processing this output */
- DC_BODY_SENT = 1 << 11, /* LWS already sent body, it's now time to send _FINAL for H2 */
- DC_MUST_FREE = 1 << 12 /* cb pass: body must be freed when request is done with user_data */
+ DC_INCOMPLETE = 1 << 0, /* struct SHALL NOT be used by the ui, it is yet to be filled by api */
+ DC_OK = 1 << 1, /* success status, api usually sets this after completion/filling of the strct */
+ DC_BAD_LOGIN = 1 << 2, /* login failed */
+ DC_VERIFICATION_NEEDED = 1 << 3, /* login: check email, click link/reg: tough luck ur IP flagd */
+ DC_CAPTCHA_NEEDED = 1 << 4, /* must solve captcha, tough luck, not impl, use browser login */
+ DC_BAD_USERNAME = 1 << 5, /* provided username can't be registered */
+ DC_BAD_EMAIL = 1 << 6, /* provided email can't be registered */
+ DC_NOT_FOUND = 1 << 7, /* when querying for roles/users/... received a 404 */
+ DC_CONTINUE = 1 << 8, /* attached handlers return this to continue processing this output, N/I */
+ DC_BREAK = 1 << 9, /* attached handlers return this to stop processing this output */
+ DC_FROM_LWS = 1 << 10, /* LWS cb is the caller, so do not attempt to do lws_service (loop) */
+ DC_MUST_FREE = 1 << 11 /* cb pass: body must be freed when request is done with user_data */
};
enum dc_permissions { /* other permissions exist, but are not implemented/understood */
DC_ALL_PERMISSIONS = 1 << 3, /* this is incredibly retarded, why is this SEPARATE?!? - admins */
@@ -96,11 +96,12 @@ void dc_api_io_free (struct dc_api_io * s) {
free(s);
return;
}
+void dc_api_i (struct dc_api_io);
enum dc_lws_headers {
DC_LWS_AUTHORIZATION,
DC_LWS_HEADERS_LENGTH
-}
-char dc_lws_headers[][] = {
+};
+char * dc_lws_headers[] = {
"Authorization:",
};
struct dc_lws_pass { /* struct that is allocated for in dc_lws_cb unique per connection in void * us */
diff --git a/src/lib.c b/src/lib.c
index 8563873..9916264 100644
--- a/src/lib.c
+++ b/src/lib.c
@@ -4,7 +4,7 @@ int smprintf(char ** str, const char * format, ...) { /* allocates automaticalls
va_copy(aq, ap);
int len = vsnprintf(NULL, 0, format, ap);
*str = malloc(len+1);
- if (len != vsprintf(str, format, ap))
+ if (len != vsprintf(*str, format, ap))
fprintf(stderr, "[BUG] !!! len1 != len2\n");
va_end(ap);
va_end(aq);
diff --git a/src/main.c b/src/main.c
index 1adf9d5..cd35da6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -8,7 +8,7 @@
int main (int argc, char * argv[]) {
struct dc_program * p = dc_program_init();
struct dc_client * client = dc_client_init();
- lws_set_log_level(0xFF /* all message types */, NULL /* do not change output location - cerr */);
+ lws_set_log_level(0xFF /* all message types */, NULL /* not change output location - cerr */);
client->email = strdup(argv[1]);
client->password = strdup(argv[2]);
struct dc_api_io i = {