diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 9 | ||||
-rw-r--r-- | client.c | 21 | ||||
-rw-r--r-- | icclient/client.h | 10 | ||||
-rw-r--r-- | icclient/request.h | 50 | ||||
-rw-r--r-- | login.c | 4 | ||||
-rw-r--r-- | login.h | 49 | ||||
-rw-r--r-- | main.c | 35 | ||||
-rw-r--r-- | request.c | 6 | ||||
-rw-r--r-- | request.h | 95 |
10 files changed, 219 insertions, 61 deletions
@@ -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 @@ -1,7 +1,6 @@ #include <stdbool.h> -#include <string.h> #include <stdlib.h> -#include <curl/curl.h> +#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 <android/log.h> -#else -#include <stdio.h> -#endif // ANDROID -#endif // DEBUG -#include <string.h> -#include <curl/curl.h> - -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 @@ -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 *); @@ -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 @@ -0,0 +1,35 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <icclient/client.h> + +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(); +} @@ -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 <android/log.h> +#endif +#include <string.h> +#include <stdarg.h> +#include <stdio.h> +#include <curl/curl.h> + +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 |