daemon_https_test_get.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. This file is part of libmicrohttpd
  3. (C) 2007 Christian Grothoff
  4. libmicrohttpd is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 2, or (at your
  7. option) any later version.
  8. libmicrohttpd is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with libmicrohttpd; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file daemon_HTTPS_test_get.c
  19. * @brief Testcase for libmicrohttpd GET operations
  20. * @author lv-426
  21. */
  22. #include "config.h"
  23. #include "plibc.h"
  24. #include "microhttpd.h"
  25. #include <errno.h>
  26. #include <curl/curl.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <time.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include <unistd.h>
  34. #define BUF_SIZE 1024
  35. #define MAX_URL_LEN 255
  36. #define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
  37. /* Test Certificate */
  38. const char cert_pem[] = "-----BEGIN CERTIFICATE-----\n"
  39. "MIIB5zCCAVKgAwIBAgIERiYdJzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
  40. "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTExWhcNMDgwNDE3MTMyOTExWjAZMRcw\n"
  41. "FQYDVQQDEw5HbnVUTFMgdGVzdCBDQTCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA\n"
  42. "vuyYeh1vfmslnuggeEKgZAVmQ5ltSdUY7H25WGSygKMUYZ0KT74v8C780qtcNt9T\n"
  43. "7EPH/N6RvB4BprdssgcQLsthR3XKA84jbjjxNCcaGs33lvOz8A1nf8p3hD+cKfRi\n"
  44. "kfYSW2JazLrtCC4yRCas/SPOUxu78of+3HiTfFm/oXUCAwEAAaNDMEEwDwYDVR0T\n"
  45. "AQH/BAUwAwEB/zAPBgNVHQ8BAf8EBQMDBwQAMB0GA1UdDgQWBBTpPBz7rZJu5gak\n"
  46. "Viyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAiaIRqGfp1jPpNeVhABK60SU0KIAy\n"
  47. "njuu7kHq5peUgYn8Jd9zNzExBOEp1VOipGsf6G66oQAhDFp2o8zkz7ZH71zR4HEW\n"
  48. "KoX6n5Emn6DvcEH/9pAhnGxNHJAoS7czTKv/JDZJhkqHxyrE1fuLsg5Qv25DTw7+\n"
  49. "PfqUpIhz5Bbm7J4=\n" "-----END CERTIFICATE-----\n";
  50. const char key_pem[] = "-----BEGIN RSA PRIVATE KEY-----\n"
  51. "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n"
  52. "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n"
  53. "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n"
  54. "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n"
  55. "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n"
  56. "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n"
  57. "7UtMjR0H3CSZJeQ8svMCxHLmOluG9H1UKk55ZBYfRTsXniqUkJBZ5wuV1L+pR9EK\n"
  58. "ca89a+1VAkEA3UmBelwEv2u9cAU1QjKjmwju1JgXbrjEohK+3B5y0ESEXPAwNQT9\n"
  59. "TrDM1m9AyxYTWLxX93dI5QwNFJtmbtjeBQJARSCWXhsoaDRG8QZrCSjBxfzTCqZD\n"
  60. "ZXtl807ymCipgJm60LiAt0JLr4LiucAsMZz6+j+quQbSakbFCACB8SLV1QJBAKZQ\n"
  61. "YKf+EPNtnmta/rRKKvySsi3GQZZN+Dt3q0r094XgeTsAqrqujVNfPhTMeP4qEVBX\n"
  62. "/iVX2cmMTSh3w3z8MaECQEp0XJWDVKOwcTW6Ajp9SowtmiZ3YDYo1LF9igb4iaLv\n"
  63. "sWZGfbnU3ryjvkb6YuFjgtzbZDZHWQCo8/cOtOBmPdk=\n"
  64. "-----END RSA PRIVATE KEY-----\n";
  65. struct CBC
  66. {
  67. char *buf;
  68. size_t pos;
  69. size_t size;
  70. };
  71. static size_t
  72. copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
  73. {
  74. struct CBC *cbc = ctx;
  75. if (cbc->pos + size * nmemb > cbc->size)
  76. return 0; /* overflow */
  77. memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
  78. cbc->pos += size * nmemb;
  79. return size * nmemb;
  80. }
  81. static int
  82. file_reader (void *cls, size_t pos, char *buf, int max)
  83. {
  84. FILE *file = cls;
  85. fseek (file, pos, SEEK_SET);
  86. return fread (buf, 1, max, file);
  87. }
  88. /* HTTP access handler call back */
  89. static int
  90. http_ahc (void *cls, struct MHD_Connection *connection,
  91. const char *url, const char *method, const char *upload_data,
  92. const char *version, unsigned int *upload_data_size, void **ptr)
  93. {
  94. static int aptr;
  95. static char full_url[MAX_URL_LEN];
  96. struct MHD_Response *response;
  97. int ret;
  98. FILE *file;
  99. struct stat buf;
  100. // TODO never respond on first call
  101. if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
  102. return MHD_NO; /* unexpected method */
  103. if (&aptr != *ptr)
  104. {
  105. /* do never respond on first call */
  106. *ptr = &aptr;
  107. return MHD_YES;
  108. }
  109. *ptr = NULL; /* reset when done */
  110. file = fopen (url, "r");
  111. if (file == NULL)
  112. {
  113. response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND),
  114. (void *) PAGE_NOT_FOUND,
  115. MHD_NO, MHD_NO);
  116. ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
  117. MHD_destroy_response (response);
  118. }
  119. else
  120. {
  121. stat (&url[1], &buf);
  122. response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
  123. &file_reader, file,
  124. (MHD_ContentReaderFreeCallback)
  125. & fclose);
  126. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  127. MHD_destroy_response (response);
  128. }
  129. return ret;
  130. }
  131. static int
  132. test_HTTPS_Get ()
  133. {
  134. struct MHD_Daemon *d;
  135. CURL *c;
  136. struct CBC cbc;
  137. CURLcode errornum;
  138. char *doc_path;
  139. char url[255];
  140. char **file_path;
  141. /* currently use hard coded certificate as test file - consider better alternatives */
  142. const char *test_file_name = "cert.pem";
  143. struct stat test_file_stat;
  144. int key_file, cert_file, test_file;
  145. /* used to memcmp local copy & deamon supplied copy */
  146. unsigned char *mem_test_file_local;
  147. /* setup test file path, url */
  148. doc_path = get_current_dir_name ();
  149. test_file = open (test_file_name, O_RDONLY);
  150. if (!test_file)
  151. {
  152. fprintf (stderr, "Error: failed to open `%s': %s\n",
  153. test_file_name,
  154. strerror(errno));
  155. return 1;
  156. }
  157. if (stat (test_file_name, &test_file_stat) == -1)
  158. {
  159. fprintf (stderr, "Error: failed to stat `%s': %s\n",
  160. test_file_name,
  161. strerror(errno));
  162. return 2;
  163. }
  164. mem_test_file_local = malloc (sizeof (char) * test_file_stat.st_size);
  165. if (read (test_file, mem_test_file_local, test_file_stat.st_size)
  166. != test_file_stat.st_size)
  167. {
  168. close (test_file);
  169. fprintf (stderr, "Error: failed to read test file\n",
  170. curl_easy_strerror (errornum));
  171. return 4;
  172. }
  173. close (test_file);
  174. if (NULL == (cbc.buf = malloc (sizeof (char) * test_file_stat.st_size)))
  175. return 8;
  176. cbc.size = test_file_stat.st_size;
  177. cbc.pos = 0;
  178. /* setup test */
  179. d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
  180. MHD_USE_DEBUG, 42433, NULL, NULL, &http_ahc, NULL,
  181. MHD_OPTION_HTTPS_MEM_KEY, key_pem,
  182. MHD_OPTION_HTTPS_MEM_CERT, cert_pem, MHD_OPTION_END);
  183. if (d == NULL)
  184. return 16;
  185. /* construct url - this might use doc_path */
  186. sprintf (url, "%s%s/%s", "https://localhost:42433", doc_path,
  187. test_file_name);
  188. fprintf (stderr, "URL: %s\n", url);
  189. c = curl_easy_init ();
  190. curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
  191. curl_easy_setopt (c, CURLOPT_URL, url);
  192. curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  193. curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
  194. curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
  195. curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
  196. curl_easy_setopt (c, CURLOPT_FILE, &cbc);
  197. /* TLS options */
  198. curl_easy_setopt (c, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
  199. curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, "AES256-SHA");
  200. /* currently skip any peer authentication */
  201. curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0L);
  202. curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0L);
  203. curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
  204. // NOTE: use of CONNECTTIMEOUT without also
  205. // setting NOSIGNAL results in really weird
  206. // crashes on my system!
  207. curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
  208. if (CURLE_OK != (errornum = curl_easy_perform (c)))
  209. {
  210. fprintf (stderr, "curl_easy_perform failed: `%s'\n",
  211. curl_easy_strerror (errornum));
  212. curl_easy_cleanup (c);
  213. MHD_stop_daemon (d);
  214. return 32;
  215. }
  216. curl_easy_cleanup (c);
  217. MHD_stop_daemon (d);
  218. if (memcmp (cbc.buf, mem_test_file_local, test_file_stat.st_size) != 0)
  219. {
  220. // TODO find proper error code
  221. return 64;
  222. }
  223. return 0;
  224. }
  225. int
  226. main (int argc, char *const *argv)
  227. {
  228. unsigned int errorCount = 0;
  229. if (0 != curl_global_init (CURL_GLOBAL_ALL))
  230. {
  231. fprintf (stderr, "Error (code: %u)\n", errorCount);
  232. return 2;
  233. }
  234. errorCount += test_HTTPS_Get ();
  235. if (errorCount != 0)
  236. fprintf (stderr, "Error (code: %u)\n", errorCount);
  237. curl_global_cleanup ();
  238. return errorCount != 0;
  239. }