Skip to content

Commit

Permalink
upload_queue: use extensible arrays for params
Browse files Browse the repository at this point in the history
In a lot of cases we have "if (share) add this extra thing" and so
everything else gets duplicated.  Also, when we are going to start
updating apps, the param names are rather different.  Use the argv-style
arrays instead of varargs so that we can just dynamically create this
list as we go.

Signed-off-by: Bob Copeland <[email protected]>
  • Loading branch information
Bob Copeland committed Mar 25, 2016
1 parent 040e6a4 commit f70ddc7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 30 deletions.
71 changes: 47 additions & 24 deletions endpoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,18 @@ struct blob *lastpass_get_blob(const struct session *session, const unsigned cha

void lastpass_remove_account(enum blobsync sync, unsigned const char key[KDF_HASH_LEN], const struct session *session, const struct account *account, struct blob *blob)
{
++blob->version;
struct http_param_set params = {
.argv = NULL,
.n_alloced = 0
};

http_post_add_params(&params, "extjs", "1", "token", session->token, "delete", "1", "aid", account->id, NULL);

if (account->share)
upload_queue_enqueue(sync, key, session, "show_website.php", "extjs", "1", "token", session->token, "delete", "1", "aid", account->id, "sharedfolderid", account->share->id, NULL);
else
upload_queue_enqueue(sync, key, session, "show_website.php", "extjs", "1", "token", session->token, "delete", "1", "aid", account->id, NULL);
http_post_add_params(&params, "sharedfolderid", account->share->id, NULL);

++blob->version;
upload_queue_enqueue(sync, key, session, "show_website.php", &params);
}

static char *stringify_field(const struct field *field)
Expand Down Expand Up @@ -137,6 +144,11 @@ static char *stringify_fields(const struct list_head *field_head)

void lastpass_update_account(enum blobsync sync, unsigned const char key[KDF_HASH_LEN], const struct session *session, const struct account *account, struct blob *blob)
{
struct http_param_set params = {
.argv = NULL,
.n_alloced = 0
};

_cleanup_free_ char *url = NULL;
_cleanup_free_ char *fields = NULL;

Expand All @@ -145,22 +157,25 @@ void lastpass_update_account(enum blobsync sync, unsigned const char key[KDF_HAS

++blob->version;

if (account->share)
upload_queue_enqueue(sync, key, session, "show_website.php", "extjs", "1",
"token", session->token, "aid", account->id,
"name", account->name_encrypted, "grouping", account->group_encrypted,
"url", url, "username", account->username_encrypted,
"password", account->password_encrypted, /* "data", fields, Removing until server-side catches up. */
"pwprotect", account->pwprotect ? "on" : "off",
"extra", account->note_encrypted, "sharedfolderid", account->share->id, NULL);
else
upload_queue_enqueue(sync, key, session, "show_website.php", "extjs", "1",
"token", session->token, "aid", account->id,
"name", account->name_encrypted, "grouping", account->group_encrypted,
"url", url, "username", account->username_encrypted,
"password", account->password_encrypted, /* "data", fields, Removing until server-side catches up. */
"pwprotect", account->pwprotect ? "on" : "off",
"extra", account->note_encrypted, NULL);
http_post_add_params(&params,
"extjs", "1",
"token", session->token,
"aid", account->id,
"name", account->name_encrypted,
"grouping", account->group_encrypted,
"url", url,
"username", account->username_encrypted,
"password", account->password_encrypted,
"pwprotect", account->pwprotect ? "on" : "off",
"extra", account->note_encrypted,
NULL);

if (account->share) {
http_post_add_params(&params,
"sharedfolderid", account->share->id,
NULL);
}
upload_queue_enqueue(sync, key, session, "show_website.php", &params);
}

unsigned long long lastpass_get_blob_version(struct session *session, unsigned const char key[KDF_HASH_LEN])
Expand All @@ -179,12 +194,20 @@ unsigned long long lastpass_get_blob_version(struct session *session, unsigned c

void lastpass_log_access(enum blobsync sync, const struct session *session, unsigned const char key[KDF_HASH_LEN], const struct account *account)
{
struct http_param_set params = {
.argv = NULL,
.n_alloced = 0
};

if (!strcmp(account->id, "0"))
return;
if (!account->share)
upload_queue_enqueue(sync, key, session, "loglogin.php", "id", account->id, "method", "cli", NULL);
else
upload_queue_enqueue(sync, key, session, "loglogin.php", "id", account->id, "method", "cli", "sharedfolderid", account->share->id, NULL);

http_post_add_params(&params, "id", account->id, "method", "cli", NULL);

if (account->share)
http_post_add_params(&params, "sharedfolderid", account->share->id, NULL);

upload_queue_enqueue(sync, key, session, "loglogin.php", &params);
}


Expand Down
8 changes: 3 additions & 5 deletions upload-queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,23 +450,21 @@ bool upload_queue_is_running(void)
return process_is_same_executable(pid);
}

void upload_queue_enqueue(enum blobsync sync, unsigned const char key[KDF_HASH_LEN], const struct session *session, const char *page, ...)
void upload_queue_enqueue(enum blobsync sync, unsigned const char key[KDF_HASH_LEN], const struct session *session, const char *page, struct http_param_set *params)
{
_cleanup_free_ char *sum = xstrdup(page);
char *next = NULL;
char *escaped = NULL;
va_list params;
char *param;
char **argv = params->argv;

va_start(params, page);
while ((param = va_arg(params, char *))) {
while ((param = *argv++)) {
escaped = pinentry_escape(param);
xasprintf(&next, "%s\n%s", sum, escaped);
free(escaped);
free(sum);
sum = next;
}
va_end(params);

upload_queue_write_entry(sum, key);

Expand Down
3 changes: 2 additions & 1 deletion upload-queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#include "kdf.h"
#include "session.h"
#include "blob.h"
#include "http.h"
#include <stdbool.h>

void upload_queue_enqueue(enum blobsync sync, unsigned const char key[KDF_HASH_LEN], const struct session *session, const char *page, ...);
void upload_queue_enqueue(enum blobsync sync, unsigned const char key[KDF_HASH_LEN], const struct session *session, const char *page, struct http_param_set *params);
bool upload_queue_is_running(void);
void upload_queue_kill(void);
void upload_queue_ensure_running(unsigned const char key[KDF_HASH_LEN], const struct session *session);
Expand Down

0 comments on commit f70ddc7

Please sign in to comment.