From c95ebee5d8b815b7b2d1cf3d9737ea95087c4af3 Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Apr 23 2010 22:06:21 +0000 Subject: - finish the http client bits --- diff --git a/src/Makefile.am b/src/Makefile.am index eb78a77..b31137b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -123,6 +123,7 @@ pkglibexec_PROGRAMS += ipa-submit endif if WITH_CURL_AND_XML pkglibexec_PROGRAMS += dogtag-submit +noinst_PROGRAMS += submit-h endif ipa_submit_SOURCES = ipa.c submit-x.c submit-x.h \ submit-u.c submit-u.h submit-e.h util.c util.h log.c log.h @@ -133,3 +134,6 @@ certmaster_submit_LDADD = $(XMLRPC_LIBS) $(KRB5_LIBS) $(TALLOC_LIBS) dogtag_submit_SOURCES = dogtag.c submit-h.c submit-h.h \ submit-u.c submit-u.h submit-e.h util.c util.h log.c log.h dogtag_submit_LDADD = $(CURL_LIBS) $(XML_LIBS) $(TALLOC_LIBS) +submit_h_CFLAGS = $(AM_CFLAGS) -DCM_SUBMIT_H_MAIN +submit_h_SOURCES = submit-h.c submit-h.h log.c log.h +submit_h_LDADD = $(CURL_LIBS) $(XML_LIBS) $(TALLOC_LIBS) diff --git a/src/dogtag.c b/src/dogtag.c index d11ff8b..3e126c9 100644 --- a/src/dogtag.c +++ b/src/dogtag.c @@ -238,7 +238,7 @@ main(int argc, char **argv) } /* Get ready to submit the request. */ - ctx = cm_submit_h_init(method, uri, args); + ctx = cm_submit_h_init(tctx, method, uri, args); if (ctx == NULL) { fprintf(stderr, "Error setting up for contacting server.\n"); printf(_("Error setting up for contacting server.\n")); diff --git a/src/submit-h.c b/src/submit-h.c index 877dbb8..c6f2465 100644 --- a/src/submit-h.c +++ b/src/submit-h.c @@ -26,6 +26,8 @@ #include +#include + #include "log.h" #include "submit-e.h" #include "submit-h.h" @@ -33,27 +35,99 @@ struct cm_submit_h_context { int ret; char *method, *uri, *args, *result; + CURL *curl; }; struct cm_submit_h_context * -cm_submit_h_init(const char *method, const char *uri, const char *args) +cm_submit_h_init(void *parent, + const char *method, const char *uri, const char *args) +{ + struct cm_submit_h_context *ctx; + ctx = talloc_ptrtype(parent, ctx); + if (ctx != NULL) { + ctx->method = talloc_strdup(ctx, method); + ctx->uri = talloc_strdup(ctx, uri); + ctx->args = talloc_strdup(ctx, args); + ctx->curl = NULL; + ctx->ret = -1; + ctx->result = NULL; + } + return ctx; +} + +static uint +append_result(char *in, uint size, uint nmemb, struct cm_submit_h_context *ctx) { - return NULL; + uint n; + if (size < nmemb) { + n = nmemb; + nmemb = size; + size = n; + } + for (n = 0; n < nmemb; n++) { + if (ctx->result == NULL) { + ctx->result = talloc_strndup(ctx, in, size); + } else { + ctx->result = talloc_strndup_append_buffer(ctx->result, + in + + n * size, + size); + } + } + return n * size; } void cm_submit_h_run(struct cm_submit_h_context *ctx) { + char *uri; + if (ctx->curl != NULL) { + curl_easy_cleanup(ctx->curl); + } + ctx->curl = curl_easy_init(); + if (ctx->curl != NULL) { + if (strcasecmp(ctx->method, "GET") == 0) { + uri = talloc_asprintf(ctx, "%s?%s", + ctx->uri, ctx->args); + curl_easy_setopt(ctx->curl, CURLOPT_URL, uri); + } else { + curl_easy_setopt(ctx->curl, CURLOPT_URL, ctx->uri); + curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDS, + ctx->args); + } + talloc_free(ctx->result); + curl_easy_setopt(ctx->curl, CURLOPT_WRITEFUNCTION, + append_result); + curl_easy_setopt(ctx->curl, CURLOPT_WRITEDATA, ctx); + ctx->ret = curl_easy_perform(ctx->curl); + } } int cm_submit_h_result_code(struct cm_submit_h_context *ctx) { - return -1; + return ctx->ret; } const char * cm_submit_h_results(struct cm_submit_h_context *ctx) { - return NULL; + return ctx->result; +} + +#ifdef CM_SUBMIT_H_MAIN +int +main(int argc, char **argv) +{ + struct cm_submit_h_context *ctx; + if (argc < 3) { + printf("Usage: submit-h METHOD URI [ARGS]\n"); + return 1; + } + ctx = cm_submit_h_init(NULL, argv[1], argv[2], + (argc > 3) ? argv[3] : ""); + cm_submit_h_run(ctx); + printf("%s", cm_submit_h_results(ctx)); + return cm_submit_h_result_code(ctx); } +#endif diff --git a/src/submit-h.h b/src/submit-h.h index 296093e..b62fbaa 100644 --- a/src/submit-h.h +++ b/src/submit-h.h @@ -19,7 +19,8 @@ #define cmsubmith_h struct cm_submit_h_context; -struct cm_submit_h_context *cm_submit_h_init(const char *method, +struct cm_submit_h_context *cm_submit_h_init(void *parent, + const char *method, const char *uri, const char *args); void cm_submit_h_run(struct cm_submit_h_context *ctx);