From 1cbb45492da9a4c5351f3156b00286447a8b014a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=20=EA=A6=AB=EA=A6=B6=20=EA=A6=8F=EA=A7=80?= =?UTF-8?q?=EA=A6=A6=EA=A6=BF=20=EA=A6=A7=20=EA=A6=AE=20=EA=A6=91=20?= =?UTF-8?q?=EA=A6=A9=20=EA=A6=AD=EA=A7=80?= Date: Sun, 22 Sep 2019 16:19:13 +0800 Subject: Login and logout functions --- .gitignore | 1 + Makefile.am | 9 ++++-- client.c | 21 ++++++++++-- icclient/client.h | 10 ++++-- icclient/request.h | 50 ---------------------------- login.c | 4 +++ login.h | 49 ++++++++++++++++++++++++++++ main.c | 35 ++++++++++++++++++++ request.c | 6 ++-- request.h | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 219 insertions(+), 61 deletions(-) delete mode 100644 icclient/request.h create mode 100644 login.c create mode 100644 login.h create mode 100644 main.c create mode 100644 request.h diff --git a/.gitignore b/.gitignore index f3cf82c..5e78586 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ config.log config.status configure depcomp +icclienttest install-sh missing stamp-h1 diff --git a/Makefile.am b/Makefile.am index dd8ee02..54d9c15 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,8 +1,13 @@ noinst_LIBRARIES = libicclient.a libicclient_a_SOURCES = \ icclient/request.h \ - icclient/product.h \ + icclient/login.h \ icclient/client.h \ request.c \ - product.c \ + login.c \ client.c + +bin_PROGRAMS = icclienttest +icclienttest_SOURCES = main.c +icclienttest_LDADD = libicclient.a +icclienttest_LDFLAGS = -lcurl diff --git a/client.c b/client.c index 2213ebf..4e8c5df 100644 --- a/client.c +++ b/client.c @@ -1,7 +1,6 @@ #include -#include #include -#include +#include "login.h" #include "icclient/client.h" CURL *curl = NULL; @@ -17,13 +16,29 @@ bool icclient_init(const char *url) #ifdef DEBUG curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); #endif - server_url = malloc(strlen(url) + 1); + size_t length = strlen(url); + bool prepend = !(bool)(url[length - 1] == '/'); + server_url = malloc(length + (size_t)prepend + 1); strcpy(server_url, url); + if (prepend) + strcat(server_url, "/"); } return (bool)curl; } +void icclient_login(const char *username, const char *password + , const char *successpage, const char *nextpage + , const char *failpage) +{ + login(username, password, NULL, "Login", successpage, nextpage, failpage); +} + +void icclient_logout() +{ + request(NULL, NULL, NULL, "%s", "logout"); +} + void icclient_cleanup() { if (curl) { diff --git a/icclient/client.h b/icclient/client.h index 9273987..bea8b51 100644 --- a/icclient/client.h +++ b/icclient/client.h @@ -1,15 +1,19 @@ -#ifndef ICCLIENT_H -#define ICCLIENT_H +#ifndef ICCLIENT_CLIENT_H +#define ICCLIENT_CLIENT_H #ifdef __cplusplus extern "C" { #endif bool icclient_init(const char *url); + void icclient_login(const char *username, const char *password + , const char *successpage, const char *nextpage + , const char *failpage); + void icclient_logout(); void icclient_cleanup(); #ifdef __cplusplus } #endif -#endif // ICCLIENT_H +#endif // ICCLIENT_CLIENT_H diff --git a/icclient/request.h b/icclient/request.h deleted file mode 100644 index 192a154..0000000 --- a/icclient/request.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef ICCLIENT_REQUEST_H -#define ICCLIENT_REQUEST_H - -#ifdef DEBUG -#ifdef ANDROID -#include -#else -#include -#endif // ANDROID -#endif // DEBUG -#include -#include - -extern CURL *curl; -extern char *server_url; - -inline void request(const char *path - , size_t (*writefunction)(void *, size_t, size_t, void *) - , void *writedata, struct curl_httppost *post) -{ - char url[strlen(server_url) + strlen(path) + 1]; - sprintf(url, "%s%s", server_url, path); - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunction); - if (writedata) - curl_easy_setopt(curl, CURLOPT_WRITEDATA, writedata); - else - curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); - if (post) - curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); - else - curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); - -#ifdef DEBUG - CURLcode res = -#endif // DEBUG - curl_easy_perform(curl); -#ifdef DEBUG - if (res != CURLE_OK) { - const char *error = curl_easy_strerror(res); -#ifdef ANDROID - __android_log_print(ANDROID_LOG_ERROR, "libicclient", "%s: %s" - , __func__, error); -#else - fprintf(stderr, "%s: %s\n", __func__, error); -#endif // ANDROID - } -#endif // DEBUG -} -#endif // ICCLIENT_REQUEST_H diff --git a/login.c b/login.c new file mode 100644 index 0000000..7455e6f --- /dev/null +++ b/login.c @@ -0,0 +1,4 @@ +#include "login.h" + +extern inline void login(const char *, const char *, const char *, const char * + , const char *, const char *, const char *); diff --git a/login.h b/login.h new file mode 100644 index 0000000..a9dbe43 --- /dev/null +++ b/login.h @@ -0,0 +1,49 @@ +#ifndef ICCLIENT_LOGIN_H +#define ICCLIENT_LOGIN_H + +#include "request.h" + +inline void login(const char *username, const char *password, const char *verify + , const char *click, const char *successpage, const char *nextpage + , const char *failpage) +{ + struct curl_httppost *post, *last = NULL; + curl_formadd(&post, &last + , CURLFORM_COPYNAME, "mv_username" + , CURLFORM_PTRCONTENTS, username + , CURLFORM_END); + curl_formadd(&post, &last + , CURLFORM_COPYNAME, "mv_password" + , CURLFORM_PTRCONTENTS, password + , CURLFORM_END); + if (verify) + curl_formadd(&post, &last + , CURLFORM_COPYNAME, "mv_verify" + , CURLFORM_PTRCONTENTS, verify + , CURLFORM_END); + curl_formadd(&post, &last + , CURLFORM_COPYNAME, "mv_click" + , CURLFORM_PTRCONTENTS, click + , CURLFORM_END); + if (successpage) + curl_formadd(&post, &last + , CURLFORM_COPYNAME, "mv_successpage" + , CURLFORM_PTRCONTENTS, successpage + , CURLFORM_END); + if (nextpage) + curl_formadd(&post, &last + , CURLFORM_COPYNAME, "mv_nextpage" + , CURLFORM_PTRCONTENTS, nextpage + , CURLFORM_END); + if (failpage) + curl_formadd(&post, &last + , CURLFORM_COPYNAME, "mv_failpage" + , CURLFORM_PTRCONTENTS, failpage + , CURLFORM_END); + last = NULL; + request(NULL, NULL, post, "%s", "process"); + curl_formfree(post); + post = NULL; +} + +#endif // ICCLIENT_LOGIN_H diff --git a/main.c b/main.c new file mode 100644 index 0000000..e203a45 --- /dev/null +++ b/main.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +int main(void) +{ + char *url_line = NULL, *name_line = NULL, *pass_line = NULL; + printf("URL: "); + ssize_t url_nread = getline(&url_line, &(size_t){0}, stdin); + printf("Name: "); + ssize_t name_nread = getline(&name_line, &(size_t){0}, stdin); + printf("Pass: "); + ssize_t pass_nread = getline(&pass_line, &(size_t){0}, stdin); + + char *url = malloc(--url_nread + 1), *name = malloc(--name_nread + 1) + , *pass = malloc(--pass_nread + 1); + strncpy(url, url_line, url_nread); + free(url_line); + strncpy(name, name_line, name_nread); + free(name_line); + strncpy(pass, pass_line, pass_nread); + free(pass_line); + + icclient_init(url); + free(url); + + icclient_login(name, pass, "member/home", NULL, NULL); + free(name); + free(pass); + + icclient_logout(); + icclient_cleanup(); +} diff --git a/request.c b/request.c index 1aae970..652ec71 100644 --- a/request.c +++ b/request.c @@ -1,4 +1,4 @@ -#include "icclient/request.h" +#include "request.h" -extern inline void request(const char *, size_t (*)(void *, size_t, size_t, void *) - , void *, struct curl_httppost *); +extern inline void request(size_t (*writefunction)(void *, size_t, size_t, void *) + , void *writedata, struct curl_httppost *post, char *fmt, ...); diff --git a/request.h b/request.h new file mode 100644 index 0000000..1252374 --- /dev/null +++ b/request.h @@ -0,0 +1,95 @@ +#ifndef ICCLIENT_REQUEST_H +#define ICCLIENT_REQUEST_H + +#if defined(ANDROID) && defined(DEBUG) +#include +#endif +#include +#include +#include +#include + +extern CURL *curl; +extern char *server_url; + +inline void request(size_t (*writefunction)(void *, size_t, size_t, void *) + , void *writedata, struct curl_httppost *post, char *fmt, ...) +{ + va_list ap; + char *p, *sval; + unsigned int ival; + size_t length = strlen(server_url) + strlen(fmt); + + va_start(ap, fmt); + for (p = fmt; *p; p++) { + if (*p != '%') + continue; + switch(*++p) { + case 's': + sval = va_arg(ap, char *); + length += strlen(sval) - 2; + break; + case 'd': + ival = va_arg(ap, unsigned int); + do { + length++; + } while (ival / 10); + length -= 2; + break; + default: + break; + } + } + va_end(ap); + + char url[length + 1]; + strcpy(url, server_url); + + va_start(ap, fmt); + for (p = fmt; *p; p++) { + if (*p != '%') + continue; + switch(*++p) { + case 's': + sval = va_arg(ap, char *); + strcat(url, sval); + break; + case 'd': + ival = va_arg(ap, unsigned int); + sprintf(url, "%s%d", url, ival); + break; + default: + break; + } + } + va_end(ap); + + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunction); + if (writedata) + curl_easy_setopt(curl, CURLOPT_WRITEDATA, writedata); + else + curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); + if (post) + curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); + else + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); + +#ifdef DEBUG + CURLcode res = +#endif // DEBUG + curl_easy_perform(curl); +#ifdef DEBUG + if (res != CURLE_OK) { + const char *error = curl_easy_strerror(res); +#ifdef ANDROID + __android_log_print(ANDROID_LOG_ERROR, "libicclient", "%s: %s" + , __func__, error); +#else + fprintf(stderr, "%s: %s\n", __func__, error); +#endif // ANDROID + } +#endif // DEBUG +} + +#endif // ICCLIENT_REQUEST_H -- cgit v1.2.3