91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek libcurl tevent integration test tool
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek Copyright (C) Red Hat, 2016
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek This program is free software; you can redistribute it and/or modify
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek it under the terms of the GNU General Public License as published by
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek the Free Software Foundation; either version 3 of the License, or
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek (at your option) any later version.
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek This program is distributed in the hope that it will be useful,
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek GNU General Public License for more details.
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek You should have received a copy of the GNU General Public License
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozekstatic void request_done(struct tevent_req *req)
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina tool_ctx = tevent_req_callback_data(req, struct tool_ctx);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = tcurl_request_recv(tool_ctx, req, &outbuf, &http_code);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "HTTP request failed [%d]: %s\n",
b800a6d09244359959404aca81c6796a58cafbcbPavel Březinaparse_options(poptContext pc, struct tool_options *opts)
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unexpected option\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina "Content-type: application/octet-stream",
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina requests = talloc_zero_array(mem_ctx, struct tcurl_request *, MAXREQ + 1);
d1ed11fc50922aab2332758a9300f3fbf814f112Pavel Březina headers = opts->headers == NULL ? default_headers : opts->headers;
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Invalid method!\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina requests[i] = tcurl_http(requests, opts->method, opts->socket_path,
36e49a842e257ac9bde71728ee3bef4299b6e6e2Pavel Březina ret = tcurl_req_enable_rawoutput(requests[i]);
886e0f75e6f4c7877a23a3625f8a20c09109b09dPavel Březina ret = tcurl_req_verify_peer(requests[i], opts->capath, opts->cacert,
ae6b11229d9961e26922918183c7c1de7780b8d6Pavel Březina ret = tcurl_req_set_client_cert(requests[i], opts->clientcert,
c2ea75da72b426d98ba489039e220d417bfb4c2aPavel Březina if (opts->username != NULL && opts->password != NULL) {
c2ea75da72b426d98ba489039e220d417bfb4c2aPavel Březina ret = tcurl_req_http_basic_auth(requests[i], opts->username,
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina if (requests == NULL || requests[0] == NULL) {
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE, "Could not init tevent context\n");
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Could not init tcurl context\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina req = tcurl_request_send(tmp_ctx, ev, tcurl_ctx, requests[i], 5);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "Could not create tevent request\n");
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek tevent_req_set_callback(req, request_done, tool_ctx);
91b0592cdab22915dff27ceae6d8e49c608aea4aJakub Hrozek "The tool finished with some pending requests, fail!\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "debug", '\0', POPT_ARG_INT, &opts.debug, 0, "The debug level to run with", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "socket-path", 's', POPT_ARG_STRING, &opts.socket_path, 0, "The path to the HTTP server socket", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "get", 'g', POPT_ARG_NONE, NULL, 'g', "Perform a HTTP GET (default)", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "put", 'p', POPT_ARG_NONE, NULL, 'p', "Perform a HTTP PUT", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "post", 'o', POPT_ARG_NONE, NULL, 'o', "Perform a HTTP POST", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "del", 'd', POPT_ARG_NONE, NULL, 'd', "Perform a HTTP DELETE", NULL },
d1ed11fc50922aab2332758a9300f3fbf814f112Pavel Březina { "header", 'h', POPT_ARG_ARGV, &opts.headers, '\0', "Add HTTP header", NULL },
36e49a842e257ac9bde71728ee3bef4299b6e6e2Pavel Březina { "raw", 'r', POPT_ARG_NONE, &opts.raw, '\0', "Print raw protocol output", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina { "verbose", 'v', POPT_ARG_NONE, &opts.verbose, '\0', "Print response code and body", NULL },
886e0f75e6f4c7877a23a3625f8a20c09109b09dPavel Březina { "tls", '\0', POPT_ARG_NONE, &opts.tls, '\0', "Enable TLS", NULL },
886e0f75e6f4c7877a23a3625f8a20c09109b09dPavel Březina { "verify-peer", '\0', POPT_ARG_NONE, &opts.verify_peer, '\0', "Verify peer when TLS is enabled", NULL },
886e0f75e6f4c7877a23a3625f8a20c09109b09dPavel Březina { "verify-host", '\0', POPT_ARG_NONE, &opts.verify_host, '\0', "Verify host when TLS is enabled", NULL },
886e0f75e6f4c7877a23a3625f8a20c09109b09dPavel Březina { "capath", '\0', POPT_ARG_STRING, &opts.capath, '\0', "Path to CA directory where peer certificate is stored", NULL },
886e0f75e6f4c7877a23a3625f8a20c09109b09dPavel Březina { "cacert", '\0', POPT_ARG_STRING, &opts.cacert, '\0', "Path to CA certificate", NULL },
ae6b11229d9961e26922918183c7c1de7780b8d6Pavel Březina { "clientcert", '\0', POPT_ARG_STRING, &opts.clientcert, '\0', "Path to client's certificate", NULL },
ae6b11229d9961e26922918183c7c1de7780b8d6Pavel Březina { "clientkey", '\0', POPT_ARG_STRING, &opts.clientkey, '\0', "Path to client's private key", NULL },
c2ea75da72b426d98ba489039e220d417bfb4c2aPavel Březina /* BASIC AUTH */
c2ea75da72b426d98ba489039e220d417bfb4c2aPavel Březina { "username", '\0', POPT_ARG_STRING, &opts.username, '\0', "Username for basic authentication", NULL },
c2ea75da72b426d98ba489039e220d417bfb4c2aPavel Březina { "password", '\0', POPT_ARG_STRING, &opts.password, '\0', "Password for basic authentication", NULL },
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina pc = poptGetContext(NULL, argc, argv, long_options, 0);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina poptSetOtherOptionHelp(pc, "[URL HTTPDATA]*");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina tool_ctx = talloc_zero(NULL, struct tool_ctx);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Could not init tool context\n");
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "Unable to parse options [%d]: %s\n",
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina ret = prepare_requests(tool_ctx, pc, &opts, &requests, &tool_ctx->nreqs);
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "Unable to prepare requests [%d]: %s\n",
b800a6d09244359959404aca81c6796a58cafbcbPavel Březina DEBUG(SSSDBG_FATAL_FAILURE, "Unable to issue requests [%d]: %s\n",