/var/lib/pcp/testsuite/src/httpfetch.c is in pcp-testsuite 4.0.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | /*
* Copyright (c) 2016 Red Hat.
* Check the pmhttp.h / libpcp_web client APIs
*/
#include <pcp/pmapi.h>
#include <pcp/pmhttp.h>
int
main(int argc, char *argv[])
{
int c, code = 0;
int verbose = 0;
int version = 0;
int errflag = 0;
char *agent = NULL;
char *http_version = NULL;
char *agent_version = NULL;
struct timeval timeout = { 0 };
static const char *usage = "[-aAptV] url";
struct http_client *client;
pmSetProgname(argv[0]);
while ((c = getopt(argc, argv, "a:A:D:t:vV:?")) != EOF) {
switch (c) {
case 'a': /* user-agent string */
agent = optarg;
break;
case 'A': /* user-agent version */
agent_version = optarg;
break;
case 'D':
if ((c = pmSetDebug(optarg)) < 0) {
fprintf(stderr, "Unrecognized debug options - %s\n", optarg);
errflag++;
break;
}
break;
case 't': /* request timeout (sec) */
timeout.tv_sec = atoi(optarg);
break;
case 'v': /* increased verbosity */
verbose++;
break;
case 'V': /* HTTP protocol version */
http_version = optarg;
break;
case '?':
default:
errflag++;
break;
}
}
if (agent && !agent_version) {
fprintf(stderr, "User-Agent specified but not version (-A)\n");
errflag++;
}
if (!agent && agent_version) {
fprintf(stderr, "User-Agent version specified but not name (-a)\n");
errflag++;
}
if (http_version) {
if (strcmp(http_version, "1.0") == 0)
version = PV_HTTP_1_0;
if (strcmp(http_version, "1.1") == 0)
version = PV_HTTP_1_1;
else {
fprintf(stderr, "Unsupported HTTP protocol version\n");
errflag++;
}
}
if (errflag || optind >= argc) {
fprintf(stderr, "Usage: %s %s\n", pmGetProgname(), usage);
exit(1);
}
if ((client = pmhttpNewClient()) == NULL) {
perror("pmhttpNewClient");
exit(1);
}
if (agent && agent_version)
pmhttpClientSetUserAgent(client, agent, agent_version);
if (http_version)
pmhttpClientSetProtocol(client, version);
if (timeout.tv_sec)
pmhttpClientSetTimeout(client, &timeout);
while (optind < argc) {
char buf[BUFSIZ];
char type[64] = {0};
if (verbose)
printf("<-- GET %s -->\n", argv[optind]);
c = pmhttpClientFetch(client, argv[optind], buf, sizeof(buf),
type, sizeof(type));
if (c < 0) {
fprintf(stderr, "Failed to fetch %s [%d]\n", argv[optind], c);
code = 1;
} else if (c == 0) {
printf("Response with empty body\n");
} else {
if (verbose) {
printf("URL: %s\n", argv[optind]);
printf("Bytes: %d\n", c);
printf("Content-type: %s\n", type);
printf("Body:\n");
}
printf("%.*s", c, buf);
}
optind++;
}
pmhttpFreeClient(client);
exit(code);
}
|