Example of stream functions.
Showing
1 changed file
with
128 additions
and
0 deletions
examples/http.c
0 → 100644
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | |||
18 | /* This is an example program to illustrate the use of stream functions. | ||
19 | It connects to a remote HTTP server and prints the contents of its | ||
20 | index page */ | ||
21 | #include <stdlib.h> | ||
22 | #include <stdio.h> | ||
23 | #include <errno.h> | ||
24 | #include <string.h> | ||
25 | #include <unistd.h> | ||
26 | #include <sys/select.h> | ||
27 | |||
28 | #include <mailutils/mailutils.h> | ||
29 | |||
30 | const char *wbuf = "GET / HTTP/1.0\r\n\r\n"; | ||
31 | char rbuf[1024]; | ||
32 | |||
33 | int | ||
34 | main () | ||
35 | { | ||
36 | int ret, off = 0, fd; | ||
37 | stream_t stream; | ||
38 | size_t nb; | ||
39 | fd_set fds; | ||
40 | |||
41 | ret = tcp_stream_create (&stream, "www.gnu.org", 80, MU_STREAM_NONBLOCK); | ||
42 | if (ret != 0) | ||
43 | { | ||
44 | mu_error ( "tcp_stream_create: %s\n", mu_errstring (ret)); | ||
45 | exit (EXIT_FAILURE); | ||
46 | } | ||
47 | |||
48 | connect_again: | ||
49 | ret = stream_open (stream); | ||
50 | if (ret != 0) | ||
51 | { | ||
52 | if (ret == EAGAIN) | ||
53 | { | ||
54 | ret = stream_get_fd (stream, &fd); | ||
55 | if (ret != 0) | ||
56 | { | ||
57 | mu_error ( "stream_get_fd: %s\n", mu_errstring (ret)); | ||
58 | exit (EXIT_FAILURE); | ||
59 | } | ||
60 | FD_ZERO (&fds); | ||
61 | FD_SET (fd, &fds); | ||
62 | select (fd + 1, NULL, &fds, NULL, NULL); | ||
63 | goto connect_again; | ||
64 | } | ||
65 | mu_error ( "stream_open: %s\n", mu_errstring (ret)); | ||
66 | exit (EXIT_FAILURE); | ||
67 | } | ||
68 | |||
69 | ret = stream_get_fd (stream, &fd); | ||
70 | if (ret != 0) | ||
71 | { | ||
72 | mu_error ( "stream_get_fd: %s\n", mu_errstring (ret)); | ||
73 | exit (EXIT_FAILURE); | ||
74 | } | ||
75 | |||
76 | write_again: | ||
77 | ret = stream_write (stream, wbuf + off, strlen (wbuf), 0, &nb); | ||
78 | if (ret != 0) | ||
79 | { | ||
80 | if (ret == EAGAIN) | ||
81 | { | ||
82 | FD_ZERO (&fds); | ||
83 | FD_SET (fd, &fds); | ||
84 | select (fd + 1, NULL, &fds, NULL, NULL); | ||
85 | off += nb; | ||
86 | goto write_again; | ||
87 | } | ||
88 | mu_error ( "stream_write: %s\n", mu_errstring (ret)); | ||
89 | exit (EXIT_FAILURE); | ||
90 | } | ||
91 | |||
92 | if (nb != strlen (wbuf)) | ||
93 | { | ||
94 | mu_error ( "stream_write: %s\n", "nb != wbuf length"); | ||
95 | exit (EXIT_FAILURE); | ||
96 | } | ||
97 | |||
98 | do | ||
99 | { | ||
100 | ret = stream_read (stream, rbuf, sizeof (rbuf), 0, &nb); | ||
101 | if (ret != 0) | ||
102 | { | ||
103 | if (ret == EAGAIN) | ||
104 | { | ||
105 | FD_ZERO (&fds); | ||
106 | FD_SET (fd, &fds); | ||
107 | select (fd + 1, &fds, NULL, NULL, NULL); | ||
108 | } | ||
109 | else | ||
110 | { | ||
111 | mu_error ( "stream_read: %s\n", mu_errstring (ret)); | ||
112 | exit (EXIT_FAILURE); | ||
113 | } | ||
114 | } | ||
115 | write (2, rbuf, nb); | ||
116 | } | ||
117 | while (nb || ret == EAGAIN); | ||
118 | |||
119 | ret = stream_close (stream); | ||
120 | if (ret != 0) | ||
121 | { | ||
122 | mu_error ( "stream_close: %s\n", mu_errstring (ret)); | ||
123 | exit (EXIT_FAILURE); | ||
124 | } | ||
125 | |||
126 | stream_destroy (&stream, NULL); | ||
127 | exit (EXIT_SUCCESS); | ||
128 | } |
-
Please register or sign in to post a comment