aboutsummaryrefslogtreecommitdiff
path: root/src/examples/upgrade_example.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/upgrade_example.c')
-rw-r--r--src/examples/upgrade_example.c118
1 files changed, 60 insertions, 58 deletions
diff --git a/src/examples/upgrade_example.c b/src/examples/upgrade_example.c
index 35761c97..98675e09 100644
--- a/src/examples/upgrade_example.c
+++ b/src/examples/upgrade_example.c
@@ -32,7 +32,8 @@
32#include <microhttpd.h> 32#include <microhttpd.h>
33#include <pthread.h> 33#include <pthread.h>
34 34
35#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" 35#define PAGE \
36 "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
36 37
37 38
38/** 39/**
@@ -72,23 +73,23 @@ send_all (MHD_socket sock,
72 73
73 make_blocking (sock); 74 make_blocking (sock);
74 for (off = 0; off < len; off += ret) 75 for (off = 0; off < len; off += ret)
76 {
77 ret = send (sock,
78 &buf[off],
79 len - off,
80 0);
81 if (0 > ret)
75 { 82 {
76 ret = send (sock, 83 if (EAGAIN == errno)
77 &buf[off], 84 {
78 len - off, 85 ret = 0;
79 0); 86 continue;
80 if (0 > ret) 87 }
81 { 88 break;
82 if (EAGAIN == errno)
83 {
84 ret = 0;
85 continue;
86 }
87 break;
88 }
89 if (0 == ret)
90 break;
91 } 89 }
90 if (0 == ret)
91 break;
92 }
92} 93}
93 94
94 95
@@ -118,25 +119,25 @@ run_usock (void *cls)
118 make_blocking (md->sock); 119 make_blocking (md->sock);
119 /* start by sending extra data MHD may have already read, if any */ 120 /* start by sending extra data MHD may have already read, if any */
120 if (0 != md->extra_in_size) 121 if (0 != md->extra_in_size)
121 { 122 {
122 send_all (md->sock, 123 send_all (md->sock,
123 md->extra_in, 124 md->extra_in,
124 md->extra_in_size); 125 md->extra_in_size);
125 free (md->extra_in); 126 free (md->extra_in);
126 } 127 }
127 /* now echo in a loop */ 128 /* now echo in a loop */
128 while (1) 129 while (1)
129 { 130 {
130 got = recv (md->sock, 131 got = recv (md->sock,
131 buf,
132 sizeof (buf),
133 0);
134 if (0 >= got)
135 break;
136 send_all (md->sock,
137 buf, 132 buf,
138 got); 133 sizeof (buf),
139 } 134 0);
135 if (0 >= got)
136 break;
137 send_all (md->sock,
138 buf,
139 got);
140 }
140 free (md); 141 free (md);
141 MHD_upgrade_action (urh, 142 MHD_upgrade_action (urh,
142 MHD_UPGRADE_ACTION_CLOSE); 143 MHD_UPGRADE_ACTION_CLOSE);
@@ -202,23 +203,23 @@ uh_cb (void *cls,
202{ 203{
203 struct MyData *md; 204 struct MyData *md;
204 pthread_t pt; 205 pthread_t pt;
205 (void)cls; /* Unused. Silent compiler warning. */ 206 (void) cls; /* Unused. Silent compiler warning. */
206 (void)connection; /* Unused. Silent compiler warning. */ 207 (void) connection; /* Unused. Silent compiler warning. */
207 (void)con_cls; /* Unused. Silent compiler warning. */ 208 (void) con_cls; /* Unused. Silent compiler warning. */
208 209
209 md = malloc (sizeof (struct MyData)); 210 md = malloc (sizeof (struct MyData));
210 if (NULL == md) 211 if (NULL == md)
211 abort (); 212 abort ();
212 memset (md, 0, sizeof (struct MyData)); 213 memset (md, 0, sizeof (struct MyData));
213 if (0 != extra_in_size) 214 if (0 != extra_in_size)
214 { 215 {
215 md->extra_in = malloc (extra_in_size); 216 md->extra_in = malloc (extra_in_size);
216 if (NULL == md->extra_in) 217 if (NULL == md->extra_in)
217 abort (); 218 abort ();
218 memcpy (md->extra_in, 219 memcpy (md->extra_in,
219 extra_in, 220 extra_in,
220 extra_in_size); 221 extra_in_size);
221 } 222 }
222 md->extra_in_size = extra_in_size; 223 md->extra_in_size = extra_in_size;
223 md->sock = sock; 224 md->sock = sock;
224 md->urh = urh; 225 md->urh = urh;
@@ -252,20 +253,20 @@ ahc_echo (void *cls,
252 static int aptr; 253 static int aptr;
253 struct MHD_Response *response; 254 struct MHD_Response *response;
254 int ret; 255 int ret;
255 (void)cls; /* Unused. Silent compiler warning. */ 256 (void) cls; /* Unused. Silent compiler warning. */
256 (void)url; /* Unused. Silent compiler warning. */ 257 (void) url; /* Unused. Silent compiler warning. */
257 (void)version; /* Unused. Silent compiler warning. */ 258 (void) version; /* Unused. Silent compiler warning. */
258 (void)upload_data; /* Unused. Silent compiler warning. */ 259 (void) upload_data; /* Unused. Silent compiler warning. */
259 (void)upload_data_size; /* Unused. Silent compiler warning. */ 260 (void) upload_data_size; /* Unused. Silent compiler warning. */
260 261
261 if (0 != strcmp (method, "GET")) 262 if (0 != strcmp (method, "GET"))
262 return MHD_NO; /* unexpected method */ 263 return MHD_NO; /* unexpected method */
263 if (&aptr != *ptr) 264 if (&aptr != *ptr)
264 { 265 {
265 /* do never respond on first call */ 266 /* do never respond on first call */
266 *ptr = &aptr; 267 *ptr = &aptr;
267 return MHD_YES; 268 return MHD_YES;
268 } 269 }
269 *ptr = NULL; /* reset when done */ 270 *ptr = NULL; /* reset when done */
270 response = MHD_create_response_for_upgrade (&uh_cb, 271 response = MHD_create_response_for_upgrade (&uh_cb,
271 NULL); 272 NULL);
@@ -288,11 +289,12 @@ main (int argc,
288 struct MHD_Daemon *d; 289 struct MHD_Daemon *d;
289 290
290 if (argc != 2) 291 if (argc != 2)
291 { 292 {
292 printf ("%s PORT\n", argv[0]); 293 printf ("%s PORT\n", argv[0]);
293 return 1; 294 return 1;
294 } 295 }
295 d = MHD_start_daemon (MHD_ALLOW_UPGRADE | MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 296 d = MHD_start_daemon (MHD_ALLOW_UPGRADE | MHD_USE_AUTO
297 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
296 atoi (argv[1]), 298 atoi (argv[1]),
297 NULL, NULL, 299 NULL, NULL,
298 &ahc_echo, NULL, 300 &ahc_echo, NULL,