aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/test_callback.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/test_callback.c')
-rw-r--r--src/testcurl/test_callback.c232
1 files changed, 118 insertions, 114 deletions
diff --git a/src/testcurl/test_callback.c b/src/testcurl/test_callback.c
index 5b1062fe..a0480932 100644
--- a/src/testcurl/test_callback.c
+++ b/src/testcurl/test_callback.c
@@ -35,56 +35,56 @@ struct callback_closure
35 35
36 36
37static ssize_t 37static ssize_t
38called_twice(void *cls, uint64_t pos, char *buf, size_t max) 38called_twice (void *cls, uint64_t pos, char *buf, size_t max)
39{ 39{
40 struct callback_closure *cls2 = cls; 40 struct callback_closure *cls2 = cls;
41 41
42 (void) pos; /* Unused. Silence compiler warning. */ 42 (void) pos; /* Unused. Silence compiler warning. */
43 (void) max; 43 (void) max;
44 if (cls2->called == 0) 44 if (cls2->called == 0)
45 { 45 {
46 memcpy(buf, "test", 5); 46 memcpy (buf, "test", 5);
47 cls2->called = 1; 47 cls2->called = 1;
48 return strlen(buf); 48 return strlen (buf);
49 } 49 }
50 if (cls2->called == 1) 50 if (cls2->called == 1)
51 { 51 {
52 cls2->called = 2; 52 cls2->called = 2;
53 return MHD_CONTENT_READER_END_OF_STREAM; 53 return MHD_CONTENT_READER_END_OF_STREAM;
54 } 54 }
55 fprintf(stderr, 55 fprintf (stderr,
56 "Handler called after returning END_OF_STREAM!\n"); 56 "Handler called after returning END_OF_STREAM!\n");
57 return MHD_CONTENT_READER_END_WITH_ERROR; 57 return MHD_CONTENT_READER_END_WITH_ERROR;
58} 58}
59 59
60 60
61static int 61static int
62callback(void *cls, 62callback (void *cls,
63 struct MHD_Connection *connection, 63 struct MHD_Connection *connection,
64 const char *url, 64 const char *url,
65 const char *method, 65 const char *method,
66 const char *version, 66 const char *version,
67 const char *upload_data, 67 const char *upload_data,
68 size_t *upload_data_size, 68 size_t *upload_data_size,
69 void **con_cls) 69 void **con_cls)
70{ 70{
71 struct callback_closure *cbc = calloc(1, sizeof(struct callback_closure)); 71 struct callback_closure *cbc = calloc (1, sizeof(struct callback_closure));
72 struct MHD_Response *r; 72 struct MHD_Response *r;
73 int ret; 73 int ret;
74 74
75 (void)cls; 75 (void) cls;
76 (void)url; /* Unused. Silent compiler warning. */ 76 (void) url; /* Unused. Silent compiler warning. */
77 (void)method; 77 (void) method;
78 (void)version; 78 (void) version;
79 (void)upload_data; /* Unused. Silent compiler warning. */ 79 (void) upload_data; /* Unused. Silent compiler warning. */
80 (void)upload_data_size; 80 (void) upload_data_size;
81 (void)con_cls; /* Unused. Silent compiler warning. */ 81 (void) con_cls; /* Unused. Silent compiler warning. */
82 82
83 if (NULL == cbc) 83 if (NULL == cbc)
84 return MHD_NO; 84 return MHD_NO;
85 r = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 1024, 85 r = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 1024,
86 &called_twice, cbc, 86 &called_twice, cbc,
87 &free); 87 &free);
88 if (NULL == r) 88 if (NULL == r)
89 { 89 {
90 free (cbc); 90 free (cbc);
@@ -104,13 +104,13 @@ discard_buffer (void *ptr,
104 size_t nmemb, 104 size_t nmemb,
105 void *ctx) 105 void *ctx)
106{ 106{
107 (void)ptr;(void)ctx; /* Unused. Silent compiler warning. */ 107 (void) ptr; (void) ctx; /* Unused. Silent compiler warning. */
108 return size * nmemb; 108 return size * nmemb;
109} 109}
110 110
111 111
112int 112int
113main(int argc, char **argv) 113main (int argc, char **argv)
114{ 114{
115 struct MHD_Daemon *d; 115 struct MHD_Daemon *d;
116 fd_set rs; 116 fd_set rs;
@@ -130,33 +130,35 @@ main(int argc, char **argv)
130 struct timeval tv; 130 struct timeval tv;
131 int extra; 131 int extra;
132 int port; 132 int port;
133 (void)argc; (void)argv; /* Unused. Silent compiler warning. */ 133 (void) argc; (void) argv; /* Unused. Silent compiler warning. */
134 134
135 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 135 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
136 port = 0; 136 port = 0;
137 else 137 else
138 port = 1140; 138 port = 1140;
139 139
140 d = MHD_start_daemon(0, 140 d = MHD_start_daemon (0,
141 port, 141 port,
142 NULL, 142 NULL,
143 NULL, 143 NULL,
144 &callback, 144 &callback,
145 NULL, 145 NULL,
146 MHD_OPTION_END); 146 MHD_OPTION_END);
147 if (d == NULL) 147 if (d == NULL)
148 return 32; 148 return 32;
149 if (0 == port) 149 if (0 == port)
150 {
151 const union MHD_DaemonInfo *dinfo;
152 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
153 if ((NULL == dinfo) ||(0 == dinfo->port) )
150 { 154 {
151 const union MHD_DaemonInfo *dinfo; 155 MHD_stop_daemon (d); return 48;
152 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
153 if (NULL == dinfo || 0 == dinfo->port)
154 { MHD_stop_daemon (d); return 48; }
155 port = (int)dinfo->port;
156 } 156 }
157 port = (int) dinfo->port;
158 }
157 c = curl_easy_init (); 159 c = curl_easy_init ();
158 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/"); 160 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/");
159 curl_easy_setopt (c, CURLOPT_PORT, (long)port); 161 curl_easy_setopt (c, CURLOPT_PORT, (long) port);
160 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &discard_buffer); 162 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &discard_buffer);
161 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 163 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
162 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 164 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
@@ -165,87 +167,89 @@ main(int argc, char **argv)
165 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 167 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
166 multi = curl_multi_init (); 168 multi = curl_multi_init ();
167 if (multi == NULL) 169 if (multi == NULL)
168 { 170 {
169 curl_easy_cleanup (c); 171 curl_easy_cleanup (c);
170 MHD_stop_daemon (d); 172 MHD_stop_daemon (d);
171 return 1; 173 return 1;
172 } 174 }
173 mret = curl_multi_add_handle (multi, c); 175 mret = curl_multi_add_handle (multi, c);
174 if (mret != CURLM_OK) 176 if (mret != CURLM_OK)
177 {
178 curl_multi_cleanup (multi);
179 curl_easy_cleanup (c);
180 MHD_stop_daemon (d);
181 return 2;
182 }
183 extra = 10;
184 while ( (c != NULL) || (--extra > 0) )
185 {
186 maxsock = MHD_INVALID_SOCKET;
187 maxposixs = -1;
188 FD_ZERO (&ws);
189 FD_ZERO (&rs);
190 FD_ZERO (&es);
191 curl_multi_perform (multi, &running);
192 if (NULL != multi)
193 {
194 mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
195 if (mret != CURLM_OK)
196 {
197 curl_multi_remove_handle (multi, c);
198 curl_multi_cleanup (multi);
199 curl_easy_cleanup (c);
200 MHD_stop_daemon (d);
201 return 3;
202 }
203 }
204 if (MHD_YES !=
205 MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
175 { 206 {
207 curl_multi_remove_handle (multi, c);
176 curl_multi_cleanup (multi); 208 curl_multi_cleanup (multi);
177 curl_easy_cleanup (c); 209 curl_easy_cleanup (c);
178 MHD_stop_daemon (d); 210 MHD_stop_daemon (d);
179 return 2; 211 return 4;
180 } 212 }
181 extra = 10; 213 tv.tv_sec = 0;
182 while ( (c != NULL) || (--extra > 0) ) 214 tv.tv_usec = 1000;
215 if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
216 {
217#ifdef MHD_POSIX_SOCKETS
218 if (EINTR != errno)
219 abort ();
220#else
221 if ((WSAEINVAL != WSAGetLastError ()) ||(0 != rs.fd_count) ||(0 !=
222 ws.fd_count)
223 ||(0 != es.fd_count) )
224 abort ();
225 Sleep (1000);
226#endif
227 }
228 if (NULL != multi)
183 { 229 {
184 maxsock = MHD_INVALID_SOCKET;
185 maxposixs = -1;
186 FD_ZERO(&ws);
187 FD_ZERO(&rs);
188 FD_ZERO(&es);
189 curl_multi_perform (multi, &running); 230 curl_multi_perform (multi, &running);
190 if (NULL != multi) 231 if (running == 0)
191 { 232 {
192 mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs); 233 msg = curl_multi_info_read (multi, &running);
193 if (mret != CURLM_OK) 234 if (msg == NULL)
194 { 235 break;
195 curl_multi_remove_handle (multi, c); 236 if (msg->msg == CURLMSG_DONE)
196 curl_multi_cleanup (multi); 237 {
197 curl_easy_cleanup (c); 238 if (msg->data.result != CURLE_OK)
198 MHD_stop_daemon (d); 239 printf ("%s failed at %s:%d: `%s'\n",
199 return 3; 240 "curl_multi_perform",
200 } 241 __FILE__,
201 } 242 __LINE__, curl_easy_strerror (msg->data.result));
202 if (MHD_YES !=
203 MHD_get_fdset(d, &rs, &ws, &es, &maxsock))
204 {
205 curl_multi_remove_handle (multi, c); 243 curl_multi_remove_handle (multi, c);
206 curl_multi_cleanup (multi); 244 curl_multi_cleanup (multi);
207 curl_easy_cleanup (c); 245 curl_easy_cleanup (c);
208 MHD_stop_daemon (d); 246 c = NULL;
209 return 4; 247 multi = NULL;
210 }
211 tv.tv_sec = 0;
212 tv.tv_usec = 1000;
213 if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
214 {
215#ifdef MHD_POSIX_SOCKETS
216 if (EINTR != errno)
217 abort ();
218#else
219 if (WSAEINVAL != WSAGetLastError() || 0 != rs.fd_count || 0 != ws.fd_count || 0 != es.fd_count)
220 abort ();
221 Sleep (1000);
222#endif
223 } 248 }
224 if (NULL != multi) 249 }
225 {
226 curl_multi_perform (multi, &running);
227 if (running == 0)
228 {
229 msg = curl_multi_info_read (multi, &running);
230 if (msg == NULL)
231 break;
232 if (msg->msg == CURLMSG_DONE)
233 {
234 if (msg->data.result != CURLE_OK)
235 printf ("%s failed at %s:%d: `%s'\n",
236 "curl_multi_perform",
237 __FILE__,
238 __LINE__, curl_easy_strerror (msg->data.result));
239 curl_multi_remove_handle (multi, c);
240 curl_multi_cleanup (multi);
241 curl_easy_cleanup (c);
242 c = NULL;
243 multi = NULL;
244 }
245 }
246 }
247 MHD_run(d);
248 } 250 }
249 MHD_stop_daemon(d); 251 MHD_run (d);
252 }
253 MHD_stop_daemon (d);
250 return 0; 254 return 0;
251} 255}