aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/test_upgrade.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-09-04 14:47:20 +0000
committerChristian Grothoff <christian@grothoff.org>2016-09-04 14:47:20 +0000
commit1f2539eed6da8dcf7f4f1ede8ab14c278f9b0c5e (patch)
tree4482ab3dc3665a6486fb5a1f802ca81600d69d10 /src/microhttpd/test_upgrade.c
parent2c94576ad0728bf94c31620e57a963c336c7c389 (diff)
downloadlibmicrohttpd-1f2539eed6da8dcf7f4f1ede8ab14c278f9b0c5e.tar.gz
libmicrohttpd-1f2539eed6da8dcf7f4f1ede8ab14c278f9b0c5e.zip
-preparations for testing external select
Diffstat (limited to 'src/microhttpd/test_upgrade.c')
-rw-r--r--src/microhttpd/test_upgrade.c118
1 files changed, 103 insertions, 15 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 */
47static pthread_t pt;
48
49/**
50 * Will be set to the upgraded socket.
51 */
52static MHD_socket usock;
53
54/**
55 * Thread we use to run the interaction with the upgraded socket.
56 */
57static 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 */
66static void
67make_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
43static void 79static void
44send_all (MHD_socket sock, 80send_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 */
192static void *
193run_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 */
215static void *
216run_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}