diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-09-04 14:47:20 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-09-04 14:47:20 +0000 |
commit | 1f2539eed6da8dcf7f4f1ede8ab14c278f9b0c5e (patch) | |
tree | 4482ab3dc3665a6486fb5a1f802ca81600d69d10 | |
parent | 2c94576ad0728bf94c31620e57a963c336c7c389 (diff) | |
download | libmicrohttpd-1f2539eed6da8dcf7f4f1ede8ab14c278f9b0c5e.tar.gz libmicrohttpd-1f2539eed6da8dcf7f4f1ede8ab14c278f9b0c5e.zip |
-preparations for testing external select
-rw-r--r-- | src/microhttpd/test_upgrade.c | 118 | ||||
-rw-r--r-- | src/microhttpd/test_upgrade_ssl.c | 51 |
2 files changed, 143 insertions, 26 deletions
diff --git a/src/microhttpd/test_upgrade.c b/src/microhttpd/test_upgrade.c index 0836f66e..c6005407 100644 --- a/src/microhttpd/test_upgrade.c +++ b/src/microhttpd/test_upgrade.c | |||
@@ -29,6 +29,7 @@ | |||
29 | #include <stdlib.h> | 29 | #include <stdlib.h> |
30 | #include <string.h> | 30 | #include <string.h> |
31 | #include <stdio.h> | 31 | #include <stdio.h> |
32 | #include <pthread.h> | ||
32 | 33 | ||
33 | #ifndef WINDOWS | 34 | #ifndef WINDOWS |
34 | #include <unistd.h> | 35 | #include <unistd.h> |
@@ -40,6 +41,41 @@ | |||
40 | #include "mhd_sockets.h" | 41 | #include "mhd_sockets.h" |
41 | 42 | ||
42 | 43 | ||
44 | /** | ||
45 | * Thread we use to run the interaction with the upgraded socket. | ||
46 | */ | ||
47 | static pthread_t pt; | ||
48 | |||
49 | /** | ||
50 | * Will be set to the upgraded socket. | ||
51 | */ | ||
52 | static MHD_socket usock; | ||
53 | |||
54 | /** | ||
55 | * Thread we use to run the interaction with the upgraded socket. | ||
56 | */ | ||
57 | static pthread_t pt_client; | ||
58 | |||
59 | |||
60 | /** | ||
61 | * Change itc FD options to be non-blocking. | ||
62 | * | ||
63 | * @param fd the FD to manipulate | ||
64 | * @return non-zero if succeeded, zero otherwise | ||
65 | */ | ||
66 | static void | ||
67 | make_blocking (MHD_socket fd) | ||
68 | { | ||
69 | int flags; | ||
70 | |||
71 | flags = fcntl (fd, F_GETFL); | ||
72 | if (-1 == flags) | ||
73 | return; | ||
74 | if ((flags & ~O_NONBLOCK) != flags) | ||
75 | fcntl (fd, F_SETFL, flags & ~O_NONBLOCK); | ||
76 | } | ||
77 | |||
78 | |||
43 | static void | 79 | static void |
44 | send_all (MHD_socket sock, | 80 | send_all (MHD_socket sock, |
45 | const char *text) | 81 | const char *text) |
@@ -47,6 +83,7 @@ send_all (MHD_socket sock, | |||
47 | size_t len = strlen (text); | 83 | size_t len = strlen (text); |
48 | ssize_t ret; | 84 | ssize_t ret; |
49 | 85 | ||
86 | make_blocking (sock); | ||
50 | for (size_t off = 0; off < len; off += ret) | 87 | for (size_t off = 0; off < len; off += ret) |
51 | { | 88 | { |
52 | ret = write (sock, | 89 | ret = write (sock, |
@@ -77,6 +114,7 @@ recv_hdr (MHD_socket sock) | |||
77 | char c; | 114 | char c; |
78 | ssize_t ret; | 115 | ssize_t ret; |
79 | 116 | ||
117 | make_blocking (sock); | ||
80 | next = '\r'; | 118 | next = '\r'; |
81 | i = 0; | 119 | i = 0; |
82 | while (i < 4) | 120 | while (i < 4) |
@@ -124,6 +162,7 @@ recv_all (MHD_socket sock, | |||
124 | char buf[len]; | 162 | char buf[len]; |
125 | ssize_t ret; | 163 | ssize_t ret; |
126 | 164 | ||
165 | make_blocking (sock); | ||
127 | for (size_t off = 0; off < len; off += ret) | 166 | for (size_t off = 0; off < len; off += ret) |
128 | { | 167 | { |
129 | ret = read (sock, | 168 | ret = read (sock, |
@@ -145,6 +184,54 @@ recv_all (MHD_socket sock, | |||
145 | 184 | ||
146 | 185 | ||
147 | /** | 186 | /** |
187 | * Main function for the thread that runs the interaction with | ||
188 | * the upgraded socket. | ||
189 | * | ||
190 | * @param cls the handle for the upgrade | ||
191 | */ | ||
192 | static void * | ||
193 | run_usock (void *cls) | ||
194 | { | ||
195 | struct MHD_UpgradeResponseHandle *urh = cls; | ||
196 | |||
197 | send_all (usock, | ||
198 | "Hello"); | ||
199 | recv_all (usock, | ||
200 | "World"); | ||
201 | send_all (usock, | ||
202 | "Finished"); | ||
203 | MHD_upgrade_action (urh, | ||
204 | MHD_UPGRADE_ACTION_CLOSE); | ||
205 | return NULL; | ||
206 | } | ||
207 | |||
208 | |||
209 | /** | ||
210 | * Main function for the thread that runs the client-side of the | ||
211 | * interaction with the upgraded socket. | ||
212 | * | ||
213 | * @param cls the client socket | ||
214 | */ | ||
215 | static void * | ||
216 | run_usock_client (void *cls) | ||
217 | { | ||
218 | MHD_socket *sock = cls; | ||
219 | |||
220 | send_all (*sock, | ||
221 | "GET / HTTP/1.1\r\nConnection: Upgrade\r\n\r\n"); | ||
222 | recv_hdr (*sock); | ||
223 | recv_all (*sock, | ||
224 | "Hello"); | ||
225 | send_all (*sock, | ||
226 | "World"); | ||
227 | recv_all (*sock, | ||
228 | "Finished"); | ||
229 | MHD_socket_close_ (*sock); | ||
230 | return NULL; | ||
231 | } | ||
232 | |||
233 | |||
234 | /** | ||
148 | * Function called after a protocol "upgrade" response was sent | 235 | * Function called after a protocol "upgrade" response was sent |
149 | * successfully and the socket should now be controlled by some | 236 | * successfully and the socket should now be controlled by some |
150 | * protocol other than HTTP. | 237 | * protocol other than HTTP. |
@@ -205,11 +292,13 @@ upgrade_cb (void *cls, | |||
205 | MHD_socket sock, | 292 | MHD_socket sock, |
206 | struct MHD_UpgradeResponseHandle *urh) | 293 | struct MHD_UpgradeResponseHandle *urh) |
207 | { | 294 | { |
208 | send_all (sock, "Hello"); | 295 | usock = sock; |
209 | recv_all (sock, "World"); | 296 | if (0 != extra_in_size) |
210 | send_all (sock, "Finished"); | 297 | abort (); |
211 | MHD_upgrade_action (urh, | 298 | pthread_create (&pt, |
212 | MHD_UPGRADE_ACTION_CLOSE); | 299 | NULL, |
300 | &run_usock, | ||
301 | urh); | ||
213 | } | 302 | } |
214 | 303 | ||
215 | 304 | ||
@@ -306,16 +395,15 @@ test_upgrade_internal (int flags, | |||
306 | (struct sockaddr *) &sa, | 395 | (struct sockaddr *) &sa, |
307 | sizeof (sa))) | 396 | sizeof (sa))) |
308 | abort (); | 397 | abort (); |
309 | send_all (sock, | 398 | pthread_create (&pt_client, |
310 | "GET / HTTP/1.1\r\nConnection: Upgrade\r\n\r\n"); | 399 | NULL, |
311 | recv_hdr (sock); | 400 | &run_usock_client, |
312 | recv_all (sock, | 401 | &sock); |
313 | "Hello"); | 402 | |
314 | send_all (sock, | 403 | pthread_join (pt_client, |
315 | "World"); | 404 | NULL); |
316 | recv_all (sock, | 405 | pthread_join (pt, |
317 | "Finished"); | 406 | NULL); |
318 | MHD_socket_close_ (sock); | ||
319 | MHD_stop_daemon (d); | 407 | MHD_stop_daemon (d); |
320 | return 0; | 408 | return 0; |
321 | } | 409 | } |
diff --git a/src/microhttpd/test_upgrade_ssl.c b/src/microhttpd/test_upgrade_ssl.c index f0e6d09e..26fd53fa 100644 --- a/src/microhttpd/test_upgrade_ssl.c +++ b/src/microhttpd/test_upgrade_ssl.c | |||
@@ -55,6 +55,11 @@ static pthread_t pt; | |||
55 | static MHD_socket usock; | 55 | static MHD_socket usock; |
56 | 56 | ||
57 | /** | 57 | /** |
58 | * Thread we use to run the interaction with the upgraded socket. | ||
59 | */ | ||
60 | static pthread_t pt_client; | ||
61 | |||
62 | /** | ||
58 | * Fork child that connects via OpenSSL to our @a port. Allows us to | 63 | * Fork child that connects via OpenSSL to our @a port. Allows us to |
59 | * talk to our port over a socket in @a sp without having to worry | 64 | * talk to our port over a socket in @a sp without having to worry |
60 | * about TLS. | 65 | * about TLS. |
@@ -257,6 +262,31 @@ run_usock (void *cls) | |||
257 | 262 | ||
258 | 263 | ||
259 | /** | 264 | /** |
265 | * Main function for the thread that runs the client-side of the | ||
266 | * interaction with the upgraded socket. | ||
267 | * | ||
268 | * @param cls the client socket | ||
269 | */ | ||
270 | static void * | ||
271 | run_usock_client (void *cls) | ||
272 | { | ||
273 | MHD_socket *sock = cls; | ||
274 | |||
275 | send_all (*sock, | ||
276 | "GET / HTTP/1.1\r\nConnection: Upgrade\r\n\r\n"); | ||
277 | recv_hdr (*sock); | ||
278 | recv_all (*sock, | ||
279 | "Hello"); | ||
280 | send_all (*sock, | ||
281 | "World"); | ||
282 | recv_all (*sock, | ||
283 | "Finished"); | ||
284 | MHD_socket_close_ (*sock); | ||
285 | return NULL; | ||
286 | } | ||
287 | |||
288 | |||
289 | /** | ||
260 | * Function called after a protocol "upgrade" response was sent | 290 | * Function called after a protocol "upgrade" response was sent |
261 | * successfully and the socket should now be controlled by some | 291 | * successfully and the socket should now be controlled by some |
262 | * protocol other than HTTP. | 292 | * protocol other than HTTP. |
@@ -418,19 +448,18 @@ test_upgrade_internal (int flags, | |||
418 | return 4; | 448 | return 4; |
419 | } | 449 | } |
420 | 450 | ||
421 | send_all (sock, | 451 | pthread_create (&pt_client, |
422 | "GET / HTTP/1.1\r\nConnection: Upgrade\r\n\r\n"); | 452 | NULL, |
423 | recv_hdr (sock); | 453 | &run_usock_client, |
424 | recv_all (sock, | 454 | &sock); |
425 | "Hello"); | 455 | |
426 | send_all (sock, | 456 | pthread_join (pt_client, |
427 | "World"); | 457 | NULL); |
428 | recv_all (sock, | ||
429 | "Finished"); | ||
430 | MHD_socket_close_ (sock); | ||
431 | pthread_join (pt, | 458 | pthread_join (pt, |
432 | NULL); | 459 | NULL); |
433 | waitpid (pid, NULL, 0); | 460 | waitpid (pid, |
461 | NULL, | ||
462 | 0); | ||
434 | MHD_stop_daemon (d); | 463 | MHD_stop_daemon (d); |
435 | return 0; | 464 | return 0; |
436 | } | 465 | } |