1 /*
2 This file is part of libmicrohttpd
3 Copyright (C) 2013 Christian Grothoff
4
5 libmicrohttpd is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 libmicrohttpd is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with libmicrohttpd; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * @file test_empty_response.c
23 * @brief Testcase for libmicrohttpd HTTPS GET operations with empty reply
24 * @author Christian Grothoff
25 */
26 #include "platform.h"
27 #include "microhttpd.h"
28 #include <limits.h>
29 #include <sys/stat.h>
30 #include <curl/curl.h>
31 #ifdef MHD_HTTPS_REQUIRE_GRYPT
32 #include <gcrypt.h>
33 #endif /* MHD_HTTPS_REQUIRE_GRYPT */
34 #include "tls_test_common.h"
35
36 extern const char srv_key_pem[];
37 extern const char srv_self_signed_cert_pem[];
38
39 static int oneone;
40
41 static enum MHD_Result
ahc_echo(void * cls,struct MHD_Connection * connection,const char * url,const char * method,const char * version,const char * upload_data,size_t * upload_data_size,void ** unused)42 ahc_echo (void *cls,
43 struct MHD_Connection *connection,
44 const char *url,
45 const char *method,
46 const char *version,
47 const char *upload_data, size_t *upload_data_size,
48 void **unused)
49 {
50 struct MHD_Response *response;
51 enum MHD_Result ret;
52 (void) cls; (void) url; (void) method; (void) version; /* Unused. Silent compiler warning. */
53 (void) upload_data; (void) upload_data_size; (void) unused; /* Unused. Silent compiler warning. */
54
55 response = MHD_create_response_from_buffer (0, NULL,
56 MHD_RESPMEM_PERSISTENT);
57 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
58 MHD_destroy_response (response);
59 return ret;
60 }
61
62
63 static int
testInternalSelectGet()64 testInternalSelectGet ()
65 {
66 struct MHD_Daemon *d;
67 CURL *c;
68 char buf[2048];
69 struct CBC cbc;
70 CURLM *multi;
71 CURLMcode mret;
72 fd_set rs;
73 fd_set ws;
74 fd_set es;
75 int maxposixs; /* Max socket number unused on W32 */
76 int running;
77 struct CURLMsg *msg;
78 time_t start;
79 struct timeval tv;
80 int port;
81 char *aes256_sha = "AES256-SHA";
82
83 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
84 port = 0;
85 else
86 port = 3000;
87
88 multi = NULL;
89 cbc.buf = buf;
90 cbc.size = 2048;
91 cbc.pos = 0;
92 d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_TLS
93 | MHD_USE_INTERNAL_POLLING_THREAD,
94 port, NULL, NULL, &ahc_echo, "GET",
95 MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
96 MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
97 MHD_OPTION_END);
98 if (d == NULL)
99 return 256;
100
101 if (0 == port)
102 {
103 const union MHD_DaemonInfo *dinfo;
104 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
105 if ((NULL == dinfo) || (0 == dinfo->port) )
106 {
107 MHD_stop_daemon (d); return 32;
108 }
109 port = (int) dinfo->port;
110 }
111 if (curl_tls_is_nss ())
112 {
113 aes256_sha = "rsa_aes_256_sha";
114 }
115
116 c = curl_easy_init ();
117 curl_easy_setopt (c, CURLOPT_URL, "https://127.0.0.1/hello_world");
118 curl_easy_setopt (c, CURLOPT_PORT, (long) port);
119 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer);
120 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
121 /* TLS options */
122 curl_easy_setopt (c, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
123 curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, aes256_sha);
124 curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0L);
125 curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0L);
126 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
127 if (oneone)
128 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
129 else
130 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
131 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
132 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
133 /* NOTE: use of CONNECTTIMEOUT without also
134 setting NOSIGNAL results in really weird
135 crashes on my system! */
136 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
137
138
139 multi = curl_multi_init ();
140 if (multi == NULL)
141 {
142 curl_easy_cleanup (c);
143 MHD_stop_daemon (d);
144 return 512;
145 }
146 mret = curl_multi_add_handle (multi, c);
147 if (mret != CURLM_OK)
148 {
149 curl_multi_cleanup (multi);
150 curl_easy_cleanup (c);
151 MHD_stop_daemon (d);
152 return 1024;
153 }
154 start = time (NULL);
155 while ((time (NULL) - start < 5) && (multi != NULL))
156 {
157 maxposixs = -1;
158 FD_ZERO (&rs);
159 FD_ZERO (&ws);
160 FD_ZERO (&es);
161 mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
162 if (mret != CURLM_OK)
163 {
164 curl_multi_remove_handle (multi, c);
165 curl_multi_cleanup (multi);
166 curl_easy_cleanup (c);
167 MHD_stop_daemon (d);
168 return 2048;
169 }
170 tv.tv_sec = 0;
171 tv.tv_usec = 1000;
172 if (-1 != maxposixs)
173 {
174 if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
175 {
176 #ifdef MHD_POSIX_SOCKETS
177 if (EINTR != errno)
178 abort ();
179 #else
180 if ((WSAEINVAL != WSAGetLastError ()) || (0 != rs.fd_count) || (0 !=
181 ws.
182 fd_count)
183 || (0 != es.fd_count) )
184 abort ();
185 Sleep (1000);
186 #endif
187 }
188 }
189 else
190 (void) sleep (1);
191 curl_multi_perform (multi, &running);
192 if (0 == running)
193 {
194 int pending;
195 int curl_fine = 0;
196 while (NULL != (msg = curl_multi_info_read (multi, &pending)))
197 {
198 if (msg->msg == CURLMSG_DONE)
199 {
200 if (msg->data.result == CURLE_OK)
201 curl_fine = 1;
202 else
203 {
204 fprintf (stderr,
205 "%s failed at %s:%d: `%s'\n",
206 "curl_multi_perform",
207 __FILE__,
208 __LINE__, curl_easy_strerror (msg->data.result));
209 abort ();
210 }
211 }
212 }
213 if (! curl_fine)
214 {
215 fprintf (stderr, "libcurl haven't returned OK code\n");
216 abort ();
217 }
218 curl_multi_remove_handle (multi, c);
219 curl_multi_cleanup (multi);
220 curl_easy_cleanup (c);
221 c = NULL;
222 multi = NULL;
223 }
224 }
225 if (multi != NULL)
226 {
227 curl_multi_remove_handle (multi, c);
228 curl_easy_cleanup (c);
229 curl_multi_cleanup (multi);
230 }
231 MHD_stop_daemon (d);
232 if (cbc.pos != 0)
233 return 8192;
234 return 0;
235 }
236
237
238 int
main(int argc,char * const * argv)239 main (int argc, char *const *argv)
240 {
241 unsigned int errorCount = 0;
242 (void) argc; /* Unused. Silent compiler warning. */
243
244 if (! testsuite_curl_global_init ())
245 return 99;
246 if (NULL == curl_version_info (CURLVERSION_NOW)->ssl_version)
247 {
248 fprintf (stderr, "Curl does not support SSL. Cannot run the test.\n");
249 curl_global_cleanup ();
250 return 77;
251 }
252 if (0 != (errorCount = testInternalSelectGet ()))
253 fprintf (stderr, "Failed test: %s, error: %u.\n", argv[0], errorCount);
254 curl_global_cleanup ();
255 return errorCount != 0 ? 1 : 0;
256 }
257