xref: /libevent-2.1.12/sample/https-client.c (revision 42d7441a)
1 /*
2   This is an example of how to hook up evhttp with bufferevent_ssl
3 
4   It just GETs an https URL given on the command-line and prints the response
5   body to stdout.
6 
7   Actually, it also accepts plain http URLs to make it easy to compare http vs
8   https code paths.
9 
10   Loosely based on le-proxy.c.
11  */
12 
13 #include <stdio.h>
14 #include <assert.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <errno.h>
18 
19 #ifdef WIN32
20 #include <winsock2.h>
21 #include <ws2tcpip.h>
22 #else
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #endif
26 
27 #include <event2/bufferevent_ssl.h>
28 #include <event2/bufferevent.h>
29 #include <event2/buffer.h>
30 #include <event2/listener.h>
31 #include <event2/util.h>
32 #include <event2/http.h>
33 #include <event2/http_struct.h>
34 
35 #include <openssl/ssl.h>
36 #include <openssl/err.h>
37 #include <openssl/rand.h>
38 
39 static struct event_base *base;
40 
41 static void
42 http_request_done(struct evhttp_request *req, void *ctx)
43 {
44 	char buffer[256];
45 	int nread;
46 
47 	if (req == NULL) {
48 		fprintf(stderr, "some request failed - no idea which one though!\n");
49 		return;
50 	}
51 
52 	fprintf(stderr, "Response line: %d %s\n",
53 		req->response_code, req->response_code_line);
54 
55 	while ((nread = evbuffer_remove(req->input_buffer, buffer, sizeof(buffer)))
56 	       > 0) {
57 		/* These are just arbitrary chunks of 256 bytes.
58 		 * They are not lines, so we can't treat them as such. */
59 		fwrite(buffer, nread, 1, stdout);
60 	}
61 }
62 
63 static void
64 syntax(void)
65 {
66 	fputs("Syntax:\n", stderr);
67 	fputs("   https-client <https-url>\n", stderr);
68 	fputs("Example:\n", stderr);
69 	fputs("   https-client https://ip.appspot.com/\n", stderr);
70 
71 	exit(1);
72 }
73 
74 static void
75 die(const char *msg)
76 {
77 	fputs(msg, stderr);
78 	exit(1);
79 }
80 
81 int
82 main(int argc, char **argv)
83 {
84 	int r;
85 
86 	struct evhttp_uri *http_uri;
87 	const char *url, *scheme, *host, *path, *query;
88 	char uri[256];
89 	int port;
90 
91 	SSL_CTX *ssl_ctx;
92 	SSL *ssl;
93 	struct bufferevent *bev;
94 	struct evhttp_connection *evcon;
95 	struct evhttp_request *req;
96 
97 	if (argc != 2)
98 		syntax();
99 
100 	url = argv[1];
101 	http_uri = evhttp_uri_parse(url);
102 	if (http_uri == NULL) {
103 		die("malformed url");
104 	}
105 
106 	scheme = evhttp_uri_get_scheme(http_uri);
107 	if (scheme == NULL || (strcasecmp(scheme, "https") != 0 &&
108 	                       strcasecmp(scheme, "http") != 0)) {
109 		die("url must be http or https");
110 	}
111 
112 	host = evhttp_uri_get_host(http_uri);
113 	if (host == NULL) {
114 		die("url must have a host");
115 	}
116 
117 	port = evhttp_uri_get_port(http_uri);
118 	if (port == -1) {
119 		port = (strcasecmp(scheme, "http") == 0) ? 80 : 443;
120 	}
121 
122 	path = evhttp_uri_get_path(http_uri);
123 	if (path == NULL) {
124 		path = "/";
125 	}
126 
127 	query = evhttp_uri_get_query(http_uri);
128 	if (query == NULL) {
129 		snprintf(uri, sizeof(uri) - 1, "%s", path);
130 	} else {
131 		snprintf(uri, sizeof(uri) - 1, "%s?%s", path, query);
132 	}
133 	uri[sizeof(uri) - 1] = '\0';
134 
135 	// Initialize OpenSSL
136 	SSL_library_init();
137 	ERR_load_crypto_strings();
138 	SSL_load_error_strings();
139 	OpenSSL_add_all_algorithms();
140 	r = RAND_poll();
141 	if (r == 0) {
142 		fprintf(stderr, "RAND_poll() failed.\n");
143 		return 1;
144 	}
145 	ssl_ctx = SSL_CTX_new(SSLv23_method());
146 
147 	// Create event base
148 	base = event_base_new();
149 	if (!base) {
150 		perror("event_base_new()");
151 		return 1;
152 	}
153 
154 	// Create OpenSSL bufferevent and stack evhttp on top of it
155 	ssl = SSL_new(ssl_ctx);
156 	if (ssl == NULL) {
157 		fprintf(stderr, "SSL_new() failed\n");
158 		return 1;
159 	}
160 
161 	if (strcasecmp(scheme, "http") == 0) {
162 		bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
163 	} else {
164 		bev = bufferevent_openssl_socket_new(base, -1, ssl,
165 			BUFFEREVENT_SSL_CONNECTING,
166 			BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
167 	}
168 
169 	if (bev == NULL) {
170 		fprintf(stderr, "bufferevent_openssl_socket_new() failed\n");
171 		return 1;
172 	}
173 
174 	bufferevent_openssl_set_allow_dirty_shutdown(bev, 1);
175 
176 	// For simplicity, we let DNS resolution block. Everything else should be
177 	// asynchronous though.
178 	evcon = evhttp_connection_base_bufferevent_new(base, NULL, bev,
179 		host, port);
180 	if (evcon == NULL) {
181 		fprintf(stderr, "evhttp_connection_base_bufferevent_new() failed\n");
182 		return 1;
183 	}
184 
185 	// Fire off the request
186 	req = evhttp_request_new(http_request_done, NULL);
187 	if (req == NULL) {
188 		fprintf(stderr, "evhttp_request_new() failed\n");
189 		return 1;
190 	}
191 
192 	evhttp_add_header(req->output_headers, "Host", host);
193 	evhttp_add_header(req->output_headers, "Connection", "close");
194 
195 	r = evhttp_make_request(evcon, req, EVHTTP_REQ_GET, uri);
196 	if (r != 0) {
197 		fprintf(stderr, "evhttp_make_request() failed\n");
198 		return 1;
199 	}
200 
201 	event_base_dispatch(base);
202 
203 	evhttp_connection_free(evcon);
204 	event_base_free(base);
205 
206 	return 0;
207 }
208