File: client.c

package info (click to toggle)
civetweb 1.16%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,184 kB
  • sloc: ansic: 32,473; cpp: 1,374; sh: 480; javascript: 204; makefile: 120; php: 11; perl: 6; python: 3
file content (116 lines) | stat: -rw-r--r-- 2,807 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2018 the CivetWeb developers
 * MIT License
 */

/* Simple client demo. */
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "civetweb.h"


int
main(int argc, char *argv[])
{
	if (argc < 2) {
		fprintf(stderr, "Requires server name as argument\n");
		return EXIT_FAILURE;
	}

	/* Init libcivetweb. */
	mg_init_library(MG_FEATURES_TLS);

	if (mg_check_feature(MG_FEATURES_TLS) != MG_FEATURES_TLS) {
		fprintf(stderr, "TLS is not available\n");
		return EXIT_FAILURE;
	}

	/* Connect client */
	char errbuf[256] = {0};
	struct mg_client_options opt = {0};
	opt.host = argv[1];       /* Host name from command line */
	opt.port = 443;           /* Default HTTPS port */
	opt.client_cert = NULL;   /* Client certificate, if required */
	opt.server_cert = NULL;   /* Server certificate to verify */
	opt.host_name = opt.host; /* Host name for SNI */
	struct mg_connection *cli =
	    mg_connect_client_secure(&opt, errbuf, sizeof(errbuf));

	/* Check return value: */
	if (cli == NULL) {
		fprintf(stderr, "Cannot connect client: %s\n", errbuf);
		return EXIT_FAILURE;
	}

	printf("cli: %p\n", cli);

	mg_printf(cli, "GET / HTTP/1.1\r\n");
	mg_printf(cli, "Host: %s\r\n", opt.host);
	mg_printf(cli, "Connection: close\r\n\r\n");

	int ret = mg_get_response(cli, errbuf, sizeof(errbuf), 10000);
	if (ret < 0) {
		fprintf(stderr, "Download failed: %s\n", errbuf);
		mg_close_connection(cli);
		return EXIT_FAILURE;
	}

	const struct mg_response_info *ri = mg_get_response_info(cli);
	if (ri == NULL) {
		fprintf(stderr, "mg_response_info failed\n");
		mg_close_connection(cli);
		return EXIT_FAILURE;
	}

	printf("Status: %i %s\n", ri->status_code, ri->status_text);
	printf("HTTP Version: %s\n", ri->http_version);
	printf("Content-Length: %lli\n", ri->content_length);
	printf("Headers:\n");
	int is_chunked = 0;
	for (int i = 0; i < ri->num_headers; i++) {
		printf("  %s: %s\n",
		       ri->http_headers[i].name,
		       ri->http_headers[i].value);
		if (!strcasecmp(ri->http_headers[i].name, "Transfer-Encoding")
		    && !strcasecmp(ri->http_headers[i].value, "chunked")) {
			is_chunked = 1;
		}
	}

	long long cont = ri->content_length;
	if (cont > 0) {
		/* Read regular content */
		printf("Content:\n");
		while (cont > 0) {
			char buf[1024];
			int ret = mg_read(cli, buf, sizeof(buf));
			if (ret <= 0) {
				printf("read error\n");
				break;
			}
			cont -= ret;
			fwrite(buf, 1, ret, stdout);
		}
	} else {
		/* Read chunked content (or content without content length) */
		char buf[1024];
		for (;;) {
			int ret = mg_read(cli, buf, sizeof(buf));
			if (ret <= 0)
				break;
			fwrite(buf, 1, ret, stdout);
		}
	}

	mg_close_connection(cli);
	return EXIT_SUCCESS;
}