1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2013 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 /*
26 * This program is written to show how to use nghttp2 API in C and
27 * intentionally made simple.
28 */
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif /* HAVE_CONFIG_H */
32
33 #include <inttypes.h>
34 #include <stdlib.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif /* HAVE_UNISTD_H */
38 #ifdef HAVE_FCNTL_H
39 # include <fcntl.h>
40 #endif /* HAVE_FCNTL_H */
41 #include <sys/types.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 # include <sys/socket.h>
44 #endif /* HAVE_SYS_SOCKET_H */
45 #ifdef HAVE_NETDB_H
46 # include <netdb.h>
47 #endif /* HAVE_NETDB_H */
48 #ifdef HAVE_NETINET_IN_H
49 # include <netinet/in.h>
50 #endif /* HAVE_NETINET_IN_H */
51 #include <netinet/tcp.h>
52 #include <poll.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <assert.h>
56 #include <string.h>
57 #include <errno.h>
58
59 #define NGHTTP2_NO_SSIZE_T
60 #include <nghttp2/nghttp2.h>
61
62 #include <openssl/ssl.h>
63 #include <openssl/err.h>
64 #include <openssl/conf.h>
65
66 enum { IO_NONE, WANT_READ, WANT_WRITE };
67
68 #define MAKE_NV(NAME, VALUE) \
69 { \
70 (uint8_t *)NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, sizeof(VALUE) - 1, \
71 NGHTTP2_NV_FLAG_NONE \
72 }
73
74 #define MAKE_NV_CS(NAME, VALUE) \
75 { \
76 (uint8_t *)NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, strlen(VALUE), \
77 NGHTTP2_NV_FLAG_NONE \
78 }
79
80 struct Connection {
81 SSL *ssl;
82 nghttp2_session *session;
83 /* WANT_READ if SSL/TLS connection needs more input; or WANT_WRITE
84 if it needs more output; or IO_NONE. This is necessary because
85 SSL/TLS re-negotiation is possible at any time. nghttp2 API
86 offers similar functions like nghttp2_session_want_read() and
87 nghttp2_session_want_write() but they do not take into account
88 SSL/TSL connection. */
89 int want_io;
90 };
91
92 struct Request {
93 char *host;
94 /* In this program, path contains query component as well. */
95 char *path;
96 /* This is the concatenation of host and port with ":" in
97 between. */
98 char *hostport;
99 /* Stream ID for this request. */
100 int32_t stream_id;
101 uint16_t port;
102 };
103
104 struct URI {
105 const char *host;
106 /* In this program, path contains query component as well. */
107 const char *path;
108 size_t pathlen;
109 const char *hostport;
110 size_t hostlen;
111 size_t hostportlen;
112 uint16_t port;
113 };
114
115 /*
116 * Returns copy of string |s| with the length |len|. The returned
117 * string is NULL-terminated.
118 */
strcopy(const char * s,size_t len)119 static char *strcopy(const char *s, size_t len) {
120 char *dst;
121 dst = malloc(len + 1);
122 memcpy(dst, s, len);
123 dst[len] = '\0';
124 return dst;
125 }
126
127 /*
128 * Prints error message |msg| and exit.
129 */
130 NGHTTP2_NORETURN
die(const char * msg)131 static void die(const char *msg) {
132 fprintf(stderr, "FATAL: %s\n", msg);
133 exit(EXIT_FAILURE);
134 }
135
136 /*
137 * Prints error containing the function name |func| and message |msg|
138 * and exit.
139 */
140 NGHTTP2_NORETURN
dief(const char * func,const char * msg)141 static void dief(const char *func, const char *msg) {
142 fprintf(stderr, "FATAL: %s: %s\n", func, msg);
143 exit(EXIT_FAILURE);
144 }
145
146 /*
147 * Prints error containing the function name |func| and error code
148 * |error_code| and exit.
149 */
150 NGHTTP2_NORETURN
diec(const char * func,int error_code)151 static void diec(const char *func, int error_code) {
152 fprintf(stderr, "FATAL: %s: error_code=%d, msg=%s\n", func, error_code,
153 nghttp2_strerror(error_code));
154 exit(EXIT_FAILURE);
155 }
156
157 /*
158 * The implementation of nghttp2_send_callback2 type. Here we write
159 * |data| with size |length| to the network and return the number of
160 * bytes actually written. See the documentation of
161 * nghttp2_send_callback for the details.
162 */
send_callback(nghttp2_session * session,const uint8_t * data,size_t length,int flags,void * user_data)163 static nghttp2_ssize send_callback(nghttp2_session *session,
164 const uint8_t *data, size_t length,
165 int flags, void *user_data) {
166 struct Connection *connection;
167 int rv;
168 (void)session;
169 (void)flags;
170
171 connection = (struct Connection *)user_data;
172 connection->want_io = IO_NONE;
173 ERR_clear_error();
174 rv = SSL_write(connection->ssl, data, (int)length);
175 if (rv <= 0) {
176 int err = SSL_get_error(connection->ssl, rv);
177 if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ) {
178 connection->want_io =
179 (err == SSL_ERROR_WANT_READ ? WANT_READ : WANT_WRITE);
180 rv = NGHTTP2_ERR_WOULDBLOCK;
181 } else {
182 rv = NGHTTP2_ERR_CALLBACK_FAILURE;
183 }
184 }
185 return rv;
186 }
187
188 /*
189 * The implementation of nghttp2_recv_callback2 type. Here we read
190 * data from the network and write them in |buf|. The capacity of
191 * |buf| is |length| bytes. Returns the number of bytes stored in
192 * |buf|. See the documentation of nghttp2_recv_callback for the
193 * details.
194 */
recv_callback(nghttp2_session * session,uint8_t * buf,size_t length,int flags,void * user_data)195 static nghttp2_ssize recv_callback(nghttp2_session *session, uint8_t *buf,
196 size_t length, int flags, void *user_data) {
197 struct Connection *connection;
198 int rv;
199 (void)session;
200 (void)flags;
201
202 connection = (struct Connection *)user_data;
203 connection->want_io = IO_NONE;
204 ERR_clear_error();
205 rv = SSL_read(connection->ssl, buf, (int)length);
206 if (rv < 0) {
207 int err = SSL_get_error(connection->ssl, rv);
208 if (err == SSL_ERROR_WANT_WRITE || err == SSL_ERROR_WANT_READ) {
209 connection->want_io =
210 (err == SSL_ERROR_WANT_READ ? WANT_READ : WANT_WRITE);
211 rv = NGHTTP2_ERR_WOULDBLOCK;
212 } else {
213 rv = NGHTTP2_ERR_CALLBACK_FAILURE;
214 }
215 } else if (rv == 0) {
216 rv = NGHTTP2_ERR_EOF;
217 }
218 return rv;
219 }
220
on_frame_send_callback(nghttp2_session * session,const nghttp2_frame * frame,void * user_data)221 static int on_frame_send_callback(nghttp2_session *session,
222 const nghttp2_frame *frame, void *user_data) {
223 size_t i;
224 (void)user_data;
225
226 switch (frame->hd.type) {
227 case NGHTTP2_HEADERS:
228 if (nghttp2_session_get_stream_user_data(session, frame->hd.stream_id)) {
229 const nghttp2_nv *nva = frame->headers.nva;
230 printf("[INFO] C ----------------------------> S (HEADERS)\n");
231 for (i = 0; i < frame->headers.nvlen; ++i) {
232 fwrite(nva[i].name, 1, nva[i].namelen, stdout);
233 printf(": ");
234 fwrite(nva[i].value, 1, nva[i].valuelen, stdout);
235 printf("\n");
236 }
237 }
238 break;
239 case NGHTTP2_RST_STREAM:
240 printf("[INFO] C ----------------------------> S (RST_STREAM)\n");
241 break;
242 case NGHTTP2_GOAWAY:
243 printf("[INFO] C ----------------------------> S (GOAWAY)\n");
244 break;
245 }
246 return 0;
247 }
248
on_frame_recv_callback(nghttp2_session * session,const nghttp2_frame * frame,void * user_data)249 static int on_frame_recv_callback(nghttp2_session *session,
250 const nghttp2_frame *frame, void *user_data) {
251 size_t i;
252 (void)user_data;
253
254 switch (frame->hd.type) {
255 case NGHTTP2_HEADERS:
256 if (frame->headers.cat == NGHTTP2_HCAT_RESPONSE) {
257 const nghttp2_nv *nva = frame->headers.nva;
258 struct Request *req;
259 req = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
260 if (req) {
261 printf("[INFO] C <---------------------------- S (HEADERS)\n");
262 for (i = 0; i < frame->headers.nvlen; ++i) {
263 fwrite(nva[i].name, 1, nva[i].namelen, stdout);
264 printf(": ");
265 fwrite(nva[i].value, 1, nva[i].valuelen, stdout);
266 printf("\n");
267 }
268 }
269 }
270 break;
271 case NGHTTP2_RST_STREAM:
272 printf("[INFO] C <---------------------------- S (RST_STREAM)\n");
273 break;
274 case NGHTTP2_GOAWAY:
275 printf("[INFO] C <---------------------------- S (GOAWAY)\n");
276 break;
277 }
278 return 0;
279 }
280
281 /*
282 * The implementation of nghttp2_on_stream_close_callback type. We use
283 * this function to know the response is fully received. Since we just
284 * fetch 1 resource in this program, after reception of the response,
285 * we submit GOAWAY and close the session.
286 */
on_stream_close_callback(nghttp2_session * session,int32_t stream_id,uint32_t error_code,void * user_data)287 static int on_stream_close_callback(nghttp2_session *session, int32_t stream_id,
288 uint32_t error_code, void *user_data) {
289 struct Request *req;
290 (void)error_code;
291 (void)user_data;
292
293 req = nghttp2_session_get_stream_user_data(session, stream_id);
294 if (req) {
295 int rv;
296 rv = nghttp2_session_terminate_session(session, NGHTTP2_NO_ERROR);
297
298 if (rv != 0) {
299 diec("nghttp2_session_terminate_session", rv);
300 }
301 }
302 return 0;
303 }
304
305 /*
306 * The implementation of nghttp2_on_data_chunk_recv_callback type. We
307 * use this function to print the received response body.
308 */
on_data_chunk_recv_callback(nghttp2_session * session,uint8_t flags,int32_t stream_id,const uint8_t * data,size_t len,void * user_data)309 static int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
310 int32_t stream_id, const uint8_t *data,
311 size_t len, void *user_data) {
312 struct Request *req;
313 (void)flags;
314 (void)user_data;
315
316 req = nghttp2_session_get_stream_user_data(session, stream_id);
317 if (req) {
318 printf("[INFO] C <---------------------------- S (DATA chunk)\n"
319 "%lu bytes\n",
320 (unsigned long int)len);
321 fwrite(data, 1, len, stdout);
322 printf("\n");
323 }
324 return 0;
325 }
326
327 /*
328 * Setup callback functions. nghttp2 API offers many callback
329 * functions, but most of them are optional. The send_callback is
330 * always required. Since we use nghttp2_session_recv(), the
331 * recv_callback is also required.
332 */
setup_nghttp2_callbacks(nghttp2_session_callbacks * callbacks)333 static void setup_nghttp2_callbacks(nghttp2_session_callbacks *callbacks) {
334 nghttp2_session_callbacks_set_send_callback2(callbacks, send_callback);
335
336 nghttp2_session_callbacks_set_recv_callback2(callbacks, recv_callback);
337
338 nghttp2_session_callbacks_set_on_frame_send_callback(callbacks,
339 on_frame_send_callback);
340
341 nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
342 on_frame_recv_callback);
343
344 nghttp2_session_callbacks_set_on_stream_close_callback(
345 callbacks, on_stream_close_callback);
346
347 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
348 callbacks, on_data_chunk_recv_callback);
349 }
350
351 /*
352 * Setup SSL/TLS context.
353 */
init_ssl_ctx(SSL_CTX * ssl_ctx)354 static void init_ssl_ctx(SSL_CTX *ssl_ctx) {
355 /* Disable SSLv2 and enable all workarounds for buggy servers */
356 SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2);
357 SSL_CTX_set_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
358 SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
359
360 SSL_CTX_set_alpn_protos(ssl_ctx, (const unsigned char *)"\x02h2", 3);
361 }
362
ssl_handshake(SSL * ssl,int fd)363 static void ssl_handshake(SSL *ssl, int fd) {
364 int rv;
365 if (SSL_set_fd(ssl, fd) == 0) {
366 dief("SSL_set_fd", ERR_error_string(ERR_get_error(), NULL));
367 }
368 ERR_clear_error();
369 rv = SSL_connect(ssl);
370 if (rv <= 0) {
371 dief("SSL_connect", ERR_error_string(ERR_get_error(), NULL));
372 }
373 }
374
375 /*
376 * Connects to the host |host| and port |port|. This function returns
377 * the file descriptor of the client socket.
378 */
connect_to(const char * host,uint16_t port)379 static int connect_to(const char *host, uint16_t port) {
380 struct addrinfo hints;
381 int fd = -1;
382 int rv;
383 char service[NI_MAXSERV];
384 struct addrinfo *res, *rp;
385 snprintf(service, sizeof(service), "%u", port);
386 memset(&hints, 0, sizeof(struct addrinfo));
387 hints.ai_family = AF_UNSPEC;
388 hints.ai_socktype = SOCK_STREAM;
389 rv = getaddrinfo(host, service, &hints, &res);
390 if (rv != 0) {
391 dief("getaddrinfo", gai_strerror(rv));
392 }
393 for (rp = res; rp; rp = rp->ai_next) {
394 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
395 if (fd == -1) {
396 continue;
397 }
398 while ((rv = connect(fd, rp->ai_addr, rp->ai_addrlen)) == -1 &&
399 errno == EINTR)
400 ;
401 if (rv == 0) {
402 break;
403 }
404 close(fd);
405 fd = -1;
406 }
407 freeaddrinfo(res);
408 return fd;
409 }
410
make_non_block(int fd)411 static void make_non_block(int fd) {
412 int flags, rv;
413 while ((flags = fcntl(fd, F_GETFL, 0)) == -1 && errno == EINTR)
414 ;
415 if (flags == -1) {
416 dief("fcntl", strerror(errno));
417 }
418 while ((rv = fcntl(fd, F_SETFL, flags | O_NONBLOCK)) == -1 && errno == EINTR)
419 ;
420 if (rv == -1) {
421 dief("fcntl", strerror(errno));
422 }
423 }
424
set_tcp_nodelay(int fd)425 static void set_tcp_nodelay(int fd) {
426 int val = 1;
427 int rv;
428 rv = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, (socklen_t)sizeof(val));
429 if (rv == -1) {
430 dief("setsockopt", strerror(errno));
431 }
432 }
433
434 /*
435 * Update |pollfd| based on the state of |connection|.
436 */
ctl_poll(struct pollfd * pollfd,struct Connection * connection)437 static void ctl_poll(struct pollfd *pollfd, struct Connection *connection) {
438 pollfd->events = 0;
439 if (nghttp2_session_want_read(connection->session) ||
440 connection->want_io == WANT_READ) {
441 pollfd->events |= POLLIN;
442 }
443 if (nghttp2_session_want_write(connection->session) ||
444 connection->want_io == WANT_WRITE) {
445 pollfd->events |= POLLOUT;
446 }
447 }
448
449 /*
450 * Submits the request |req| to the connection |connection|. This
451 * function does not send packets; just append the request to the
452 * internal queue in |connection->session|.
453 */
submit_request(struct Connection * connection,struct Request * req)454 static void submit_request(struct Connection *connection, struct Request *req) {
455 int32_t stream_id;
456 /* Make sure that the last item is NULL */
457 const nghttp2_nv nva[] = {MAKE_NV(":method", "GET"),
458 MAKE_NV_CS(":path", req->path),
459 MAKE_NV(":scheme", "https"),
460 MAKE_NV_CS(":authority", req->hostport),
461 MAKE_NV("accept", "*/*"),
462 MAKE_NV("user-agent", "nghttp2/" NGHTTP2_VERSION)};
463
464 stream_id = nghttp2_submit_request2(connection->session, NULL, nva,
465 sizeof(nva) / sizeof(nva[0]), NULL, req);
466
467 if (stream_id < 0) {
468 diec("nghttp2_submit_request", stream_id);
469 }
470
471 req->stream_id = stream_id;
472 printf("[INFO] Stream ID = %d\n", stream_id);
473 }
474
475 /*
476 * Performs the network I/O.
477 */
exec_io(struct Connection * connection)478 static void exec_io(struct Connection *connection) {
479 int rv;
480 rv = nghttp2_session_recv(connection->session);
481 if (rv != 0) {
482 diec("nghttp2_session_recv", rv);
483 }
484 rv = nghttp2_session_send(connection->session);
485 if (rv != 0) {
486 diec("nghttp2_session_send", rv);
487 }
488 }
489
request_init(struct Request * req,const struct URI * uri)490 static void request_init(struct Request *req, const struct URI *uri) {
491 req->host = strcopy(uri->host, uri->hostlen);
492 req->port = uri->port;
493 req->path = strcopy(uri->path, uri->pathlen);
494 req->hostport = strcopy(uri->hostport, uri->hostportlen);
495 req->stream_id = -1;
496 }
497
request_free(struct Request * req)498 static void request_free(struct Request *req) {
499 free(req->host);
500 free(req->path);
501 free(req->hostport);
502 }
503
504 /*
505 * Fetches the resource denoted by |uri|.
506 */
fetch_uri(const struct URI * uri)507 static void fetch_uri(const struct URI *uri) {
508 nghttp2_session_callbacks *callbacks;
509 int fd;
510 SSL_CTX *ssl_ctx;
511 SSL *ssl;
512 struct Request req;
513 struct Connection connection;
514 int rv;
515 nfds_t npollfds = 1;
516 struct pollfd pollfds[1];
517
518 request_init(&req, uri);
519
520 /* Establish connection and setup SSL */
521 fd = connect_to(req.host, req.port);
522 if (fd == -1) {
523 die("Could not open file descriptor");
524 }
525 ssl_ctx = SSL_CTX_new(TLS_client_method());
526 if (ssl_ctx == NULL) {
527 dief("SSL_CTX_new", ERR_error_string(ERR_get_error(), NULL));
528 }
529 init_ssl_ctx(ssl_ctx);
530 ssl = SSL_new(ssl_ctx);
531 if (ssl == NULL) {
532 dief("SSL_new", ERR_error_string(ERR_get_error(), NULL));
533 }
534 /* To simplify the program, we perform SSL/TLS handshake in blocking
535 I/O. */
536 ssl_handshake(ssl, fd);
537
538 connection.ssl = ssl;
539 connection.want_io = IO_NONE;
540
541 /* Here make file descriptor non-block */
542 make_non_block(fd);
543 set_tcp_nodelay(fd);
544
545 printf("[INFO] SSL/TLS handshake completed\n");
546
547 rv = nghttp2_session_callbacks_new(&callbacks);
548
549 if (rv != 0) {
550 diec("nghttp2_session_callbacks_new", rv);
551 }
552
553 setup_nghttp2_callbacks(callbacks);
554
555 rv = nghttp2_session_client_new(&connection.session, callbacks, &connection);
556
557 nghttp2_session_callbacks_del(callbacks);
558
559 if (rv != 0) {
560 diec("nghttp2_session_client_new", rv);
561 }
562
563 rv = nghttp2_submit_settings(connection.session, NGHTTP2_FLAG_NONE, NULL, 0);
564
565 if (rv != 0) {
566 diec("nghttp2_submit_settings", rv);
567 }
568
569 /* Submit the HTTP request to the outbound queue. */
570 submit_request(&connection, &req);
571
572 pollfds[0].fd = fd;
573 ctl_poll(pollfds, &connection);
574
575 /* Event loop */
576 while (nghttp2_session_want_read(connection.session) ||
577 nghttp2_session_want_write(connection.session)) {
578 int nfds = poll(pollfds, npollfds, -1);
579 if (nfds == -1) {
580 dief("poll", strerror(errno));
581 }
582 if (pollfds[0].revents & (POLLIN | POLLOUT)) {
583 exec_io(&connection);
584 }
585 if ((pollfds[0].revents & POLLHUP) || (pollfds[0].revents & POLLERR)) {
586 die("Connection error");
587 }
588 ctl_poll(pollfds, &connection);
589 }
590
591 /* Resource cleanup */
592 nghttp2_session_del(connection.session);
593 SSL_shutdown(ssl);
594 SSL_free(ssl);
595 SSL_CTX_free(ssl_ctx);
596 shutdown(fd, SHUT_WR);
597 close(fd);
598 request_free(&req);
599 }
600
parse_uri(struct URI * res,const char * uri)601 static int parse_uri(struct URI *res, const char *uri) {
602 /* We only interested in https */
603 size_t len, i, offset;
604 int ipv6addr = 0;
605 memset(res, 0, sizeof(struct URI));
606 len = strlen(uri);
607 if (len < 9 || memcmp("https://", uri, 8) != 0) {
608 return -1;
609 }
610 offset = 8;
611 res->host = res->hostport = &uri[offset];
612 res->hostlen = 0;
613 if (uri[offset] == '[') {
614 /* IPv6 literal address */
615 ++offset;
616 ++res->host;
617 ipv6addr = 1;
618 for (i = offset; i < len; ++i) {
619 if (uri[i] == ']') {
620 res->hostlen = i - offset;
621 offset = i + 1;
622 break;
623 }
624 }
625 } else {
626 const char delims[] = ":/?#";
627 for (i = offset; i < len; ++i) {
628 if (strchr(delims, uri[i]) != NULL) {
629 break;
630 }
631 }
632 res->hostlen = i - offset;
633 offset = i;
634 }
635 if (res->hostlen == 0) {
636 return -1;
637 }
638 /* Assuming https */
639 res->port = 443;
640 if (offset < len) {
641 if (uri[offset] == ':') {
642 /* port */
643 const char delims[] = "/?#";
644 int port = 0;
645 ++offset;
646 for (i = offset; i < len; ++i) {
647 if (strchr(delims, uri[i]) != NULL) {
648 break;
649 }
650 if ('0' <= uri[i] && uri[i] <= '9') {
651 port *= 10;
652 port += uri[i] - '0';
653 if (port > 65535) {
654 return -1;
655 }
656 } else {
657 return -1;
658 }
659 }
660 if (port == 0) {
661 return -1;
662 }
663 offset = i;
664 res->port = (uint16_t)port;
665 }
666 }
667 res->hostportlen = (size_t)(uri + offset + ipv6addr - res->host);
668 for (i = offset; i < len; ++i) {
669 if (uri[i] == '#') {
670 break;
671 }
672 }
673 if (i - offset == 0) {
674 res->path = "/";
675 res->pathlen = 1;
676 } else {
677 res->path = &uri[offset];
678 res->pathlen = i - offset;
679 }
680 return 0;
681 }
682
main(int argc,char ** argv)683 int main(int argc, char **argv) {
684 struct URI uri;
685 struct sigaction act;
686 int rv;
687
688 if (argc < 2) {
689 die("Specify a https URI");
690 }
691
692 memset(&act, 0, sizeof(struct sigaction));
693 act.sa_handler = SIG_IGN;
694 sigaction(SIGPIPE, &act, 0);
695
696 rv = parse_uri(&uri, argv[1]);
697 if (rv != 0) {
698 die("parse_uri failed");
699 }
700 fetch_uri(&uri);
701 return EXIT_SUCCESS;
702 }
703