Include http.c.texi
Showing
1 changed file
with
1 additions
and
113 deletions
... | @@ -114,118 +114,6 @@ Last action was @code{stream_close}. | ... | @@ -114,118 +114,6 @@ Last action was @code{stream_close}. |
114 | 114 | ||
115 | An example using @code{tcp_stream_create} to make a simple web client: | 115 | An example using @code{tcp_stream_create} to make a simple web client: |
116 | 116 | ||
117 | FIXME: this example won't build anymore. | ||
118 | |||
119 | @example | 117 | @example |
120 | #include <stdlib.h> | 118 | @include http.c.texi |
121 | #include <stdio.h> | ||
122 | #include <errno.h> | ||
123 | #include <string.h> | ||
124 | #include <unistd.h> | ||
125 | #include <sys/select.h> | ||
126 | |||
127 | #include <mailutils/io.h> | ||
128 | |||
129 | const char *wbuf = "GET / HTTP/1.0\r\n\r\n"; | ||
130 | char rbuf[1024]; | ||
131 | |||
132 | int | ||
133 | main(int argc, char **argv) | ||
134 | @{ | ||
135 | int ret, off = 0, fd; | ||
136 | stream_t stream; | ||
137 | size_t nb; | ||
138 | fd_set fds; | ||
139 | |||
140 | argc = argc, argv = argv; | ||
141 | |||
142 | ret = tcp_stream_create (&stream); | ||
143 | if (ret != 0) | ||
144 | @{ | ||
145 | fprintf (stderr, "tcp_stream_create: %s\n", | ||
146 | mailutils_error(ret)); | ||
147 | exit (EXIT_FAILURE); | ||
148 | @} | ||
149 | |||
150 | connect_again: | ||
151 | ret = stream_open (stream, "www.netscape.com", 80, | ||
152 | MU_STREAM_NONBLOCK); | ||
153 | if (ret != 0) | ||
154 | @{ | ||
155 | if (ret == MU_ERROR_EAGAIN) | ||
156 | @{ | ||
157 | ret = stream_get_fd(stream, &fd); | ||
158 | if (ret != 0) | ||
159 | @{ | ||
160 | fprintf (stderr, "stream_get_fd: %s\n", | ||
161 | mailutils_error(ret)); | ||
162 | exit (EXIT_FAILURE); | ||
163 | @} | ||
164 | FD_ZERO (&fds); | ||
165 | FD_SET (fd, &fds); | ||
166 | select (fd+1, NULL, &fds, NULL, NULL); | ||
167 | goto connect_again; | ||
168 | @} | ||
169 | fprintf (stderr, "stream_open: %s\n", mailutils_error (ret)); | ||
170 | exit (EXIT_FAILURE); | ||
171 | @} | ||
172 | |||
173 | ret = stream_get_fd (stream, &fd); | ||
174 | if (ret != 0) | ||
175 | @{ | ||
176 | fprintf(stderr, "stream_get_fd: %s\n", strerror(ret)); | ||
177 | exit (EXIT_FAILURE); | ||
178 | @} | ||
179 | |||
180 | write_again: | ||
181 | ret = stream_write (stream, wbuf + off, strlen (wbuf), 0, &nb); | ||
182 | if (ret != 0 ) | ||
183 | @{ | ||
184 | if (ret == EAGAIN) | ||
185 | @{ | ||
186 | FD_ZERO (&fds); | ||
187 | FD_SET (fd, &fds); | ||
188 | select (fd + 1, NULL, &fds, NULL, NULL); | ||
189 | off += nb; | ||
190 | goto write_again; | ||
191 | @} | ||
192 | fprintf (stderr, "stream_write: %s\n", strerror(ret)); | ||
193 | exit (EXIT_FAILURE) | ||
194 | @} | ||
195 | |||
196 | if (nb != strlen (wbuf)) | ||
197 | @{ | ||
198 | fprintf(stderr, "stream_write: %s\n", "nb != wbuf length"); | ||
199 | exit (EXIT_FAILURE); | ||
200 | @} | ||
201 | |||
202 | do | ||
203 | @{ | ||
204 | read_again: | ||
205 | ret = stream_read (stream, rbuf, sizeof (rbuf), 0, &nb); | ||
206 | if (ret != 0) | ||
207 | @{ | ||
208 | if (ret == EAGAIN) | ||
209 | @{ | ||
210 | FD_ZERO (&fds); | ||
211 | FD_SET (fd, &fds); | ||
212 | select (fd + 1, &fds, NULL, NULL, NULL); | ||
213 | goto read_again; | ||
214 | @} | ||
215 | fprintf (stderr, "stream_read: %s\n", strerror(ret)); | ||
216 | exit(EXIT_FAILURE); | ||
217 | @} | ||
218 | write (2, rbuf, nb); | ||
219 | @} while (nb); | ||
220 | |||
221 | ret = stream_close (stream); | ||
222 | if (ret!= 0) | ||
223 | @{ | ||
224 | fprintf (stderr, "stream_close: %s\n", strerror(ret)); | ||
225 | exit (EXIT_FAILURE); | ||
226 | @} | ||
227 | |||
228 | stream_destroy (&stream, NULL); | ||
229 | exit (EXIT_SUCCESS); | ||
230 | @} | ||
231 | @end example | 119 | @end example | ... | ... |
-
Please register or sign in to post a comment